-
-
Notifications
You must be signed in to change notification settings - Fork 19
Refit a background image when its bloom-canvas is sized after page load #8312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hatton
wants to merge
3
commits into
master
Choose a base branch
from
canvas-background-refit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
255 changes: 255 additions & 0 deletions
255
...oomBrowserUI/bookEdit/js/canvasElementManager/CanvasElementBackgroundImageManager.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| // Comical wants paper.js and a real <canvas>, which jsdom doesn't give us. Nothing in the | ||
| // code under test needs it to do anything. | ||
| vi.mock("comicaljs", () => ({ | ||
| Bubble: class {}, | ||
| Comical: { | ||
| setSelectorForBubblesWhichTailMidpointMayOverlap: () => {}, | ||
| activateElement: () => {}, | ||
| update: () => {}, | ||
| }, | ||
| })); | ||
|
|
||
| // jsdom gives every element a zero bounding rectangle, so the real getExactClientSize | ||
| // could only ever report the zero-area case. We control the reported size instead, so | ||
| // the test can also show what happens when the bloom-canvas does have a size. | ||
| const reportedSize = { width: 0, height: 0 }; | ||
| vi.mock("../../../utils/elementUtils", async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal<typeof import("../../../utils/elementUtils")>(); | ||
| return { | ||
| ...actual, | ||
| getExactClientSize: () => ({ | ||
| width: reportedSize.width, | ||
| height: reportedSize.height, | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| // These imports deliberately come after the vi.mock calls above, so that the module graph | ||
| // they pull in gets the stubbed modules. | ||
| import { | ||
| adjustBackgroundImageSize, | ||
| getBackgroundCanvasElement, | ||
| } from "./CanvasElementBackgroundImageManager"; | ||
| import type { BackgroundImageManagerState } from "./CanvasElementBackgroundImageManager"; | ||
|
|
||
| // The inline styles a background image already has when something asks for a refit. | ||
| const initialCanvasElementStyle = { | ||
| width: "300px", | ||
| height: "200px", | ||
| left: "10px", | ||
| top: "20px", | ||
| }; | ||
|
|
||
| function setUpBackgroundImage(): { | ||
| bloomCanvas: HTMLElement; | ||
| bgCanvasElement: HTMLElement; | ||
| img: HTMLImageElement; | ||
| } { | ||
| document.body.innerHTML = ` | ||
| <div class="bloom-page"> | ||
| <div class="bloom-canvas"> | ||
| <div class="bloom-canvas-element bloom-backgroundImage"> | ||
| <div class="bloom-imageContainer"> | ||
| <img src="rabbit.png" class="bloom-imageLoadError" /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div>`; | ||
| const bloomCanvas = document.querySelector(".bloom-canvas") as HTMLElement; | ||
| const bgCanvasElement = document.querySelector( | ||
| ".bloom-backgroundImage", | ||
| ) as HTMLElement; | ||
| bgCanvasElement.style.width = initialCanvasElementStyle.width; | ||
| bgCanvasElement.style.height = initialCanvasElementStyle.height; | ||
| bgCanvasElement.style.left = initialCanvasElementStyle.left; | ||
| bgCanvasElement.style.top = initialCanvasElementStyle.top; | ||
| return { | ||
| bloomCanvas, | ||
| bgCanvasElement, | ||
| img: bgCanvasElement.getElementsByTagName("img")[0], | ||
| }; | ||
| } | ||
|
|
||
| function makeState(): BackgroundImageManagerState { | ||
| return { bgImageLoadListeners: new WeakMap() }; | ||
| } | ||
|
|
||
| function refit( | ||
| bloomCanvas: HTMLElement, | ||
| bgCanvasElement: HTMLElement, | ||
| ): Promise<void> { | ||
| return adjustBackgroundImageSize( | ||
| makeState(), | ||
| bloomCanvas, | ||
| bgCanvasElement, | ||
| false, | ||
| () => undefined, // nothing is the active element, so no controls get rendered | ||
| () => {}, | ||
| ); | ||
| } | ||
|
|
||
| describe("adjustBackgroundImageSize", () => { | ||
| beforeEach(() => { | ||
| reportedSize.width = 0; | ||
| reportedSize.height = 0; | ||
| }); | ||
|
|
||
| test("leaves the background image alone when the bloom-canvas has no area", async () => { | ||
| const { bloomCanvas, bgCanvasElement, img } = setUpBackgroundImage(); | ||
| // Sanity check: the styles we expect to survive the call are there to start with. | ||
| expect(bgCanvasElement.style.width).toBe( | ||
| initialCanvasElementStyle.width, | ||
| ); | ||
| expect(bgCanvasElement.style.left).toBe(initialCanvasElementStyle.left); | ||
| expect(img.style.width).toBe(""); | ||
|
|
||
| await refit(bloomCanvas, bgCanvasElement); | ||
|
|
||
| expect(bgCanvasElement.style.width).toBe( | ||
| initialCanvasElementStyle.width, | ||
| ); | ||
| expect(bgCanvasElement.style.height).toBe( | ||
| initialCanvasElementStyle.height, | ||
| ); | ||
| expect(bgCanvasElement.style.left).toBe(initialCanvasElementStyle.left); | ||
| expect(bgCanvasElement.style.top).toBe(initialCanvasElementStyle.top); | ||
| // and it did not start cropping the image either | ||
| expect(img.style.width).toBe(""); | ||
| }); | ||
|
|
||
| test("leaves the background image alone when the bloom-canvas has width but no height", async () => { | ||
| const { bloomCanvas, bgCanvasElement } = setUpBackgroundImage(); | ||
| reportedSize.width = 400; | ||
| reportedSize.height = 0; | ||
|
|
||
| await refit(bloomCanvas, bgCanvasElement); | ||
|
|
||
| expect(bgCanvasElement.style.width).toBe( | ||
| initialCanvasElementStyle.width, | ||
| ); | ||
| expect(bgCanvasElement.style.height).toBe( | ||
| initialCanvasElementStyle.height, | ||
| ); | ||
| }); | ||
|
|
||
| // A hidden bloom-canvas that has a border reports a negative size, because | ||
| // getExactClientSize subtracts the border from a zero bounding rectangle. | ||
| test("leaves the background image alone when the bloom-canvas reports a negative size", async () => { | ||
| const { bloomCanvas, bgCanvasElement } = setUpBackgroundImage(); | ||
| reportedSize.width = -2; | ||
| reportedSize.height = -2; | ||
|
|
||
| await refit(bloomCanvas, bgCanvasElement); | ||
|
|
||
| expect(bgCanvasElement.style.width).toBe( | ||
| initialCanvasElementStyle.width, | ||
| ); | ||
| expect(bgCanvasElement.style.height).toBe( | ||
| initialCanvasElementStyle.height, | ||
| ); | ||
| }); | ||
|
|
||
| // This is the guard against the tests above passing for the wrong reason: given a | ||
| // bloom-canvas that does have a size, the same call really does resize the background | ||
| // image. (The image here has failed to load, which is the one case the code can size | ||
| // synchronously, since it then fills the container to show the error message.) | ||
| test("fits the background image to a bloom-canvas that has a size", async () => { | ||
| const { bloomCanvas, bgCanvasElement } = setUpBackgroundImage(); | ||
| reportedSize.width = 400; | ||
| reportedSize.height = 500; | ||
|
|
||
| await refit(bloomCanvas, bgCanvasElement); | ||
|
|
||
| expect(bgCanvasElement.style.width).toBe("400px"); | ||
| expect(bgCanvasElement.style.height).toBe("500px"); | ||
| expect(bgCanvasElement.style.left).toBe("0px"); | ||
| expect(bgCanvasElement.style.top).toBe("0px"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getBackgroundCanvasElement", () => { | ||
| test("finds the background image that is a direct child of the bloom-canvas", () => { | ||
| const { bloomCanvas, bgCanvasElement } = setUpBackgroundImage(); | ||
| expect(getBackgroundCanvasElement(bloomCanvas)).toBe(bgCanvasElement); | ||
| }); | ||
|
|
||
| test("returns undefined when the bloom-canvas has no background image", () => { | ||
| document.body.innerHTML = ` | ||
| <div class="bloom-canvas"> | ||
| <div class="bloom-canvas-element"><div class="bloom-imageContainer"><img src="a.png"/></div></div> | ||
| </div>`; | ||
| const bloomCanvas = document.querySelector( | ||
| ".bloom-canvas", | ||
| ) as HTMLElement; | ||
| expect(getBackgroundCanvasElement(bloomCanvas)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("does not take a nested bloom-canvas's background image for the outer one", () => { | ||
| document.body.innerHTML = ` | ||
| <div class="bloom-canvas" id="outer"> | ||
| <div class="bloom-canvas-element"> | ||
| <div class="bloom-canvas" id="inner"> | ||
| <div class="bloom-canvas-element bloom-backgroundImage" id="innerBg"> | ||
| <div class="bloom-imageContainer"><img src="inner.png"/></div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div>`; | ||
| const outer = document.getElementById("outer") as HTMLElement; | ||
| const inner = document.getElementById("inner") as HTMLElement; | ||
| const innerBg = document.getElementById("innerBg") as HTMLElement; | ||
| // Sanity check: a plain descendant search would have found the inner one. | ||
| expect(outer.getElementsByClassName("bloom-backgroundImage")[0]).toBe( | ||
| innerBg, | ||
| ); | ||
|
|
||
| expect(getBackgroundCanvasElement(outer)).toBeUndefined(); | ||
| expect(getBackgroundCanvasElement(inner)).toBe(innerBg); | ||
| }); | ||
|
|
||
| test("finds each bloom-canvas's own background image when both have one", () => { | ||
| document.body.innerHTML = ` | ||
| <div class="bloom-canvas" id="outer"> | ||
| <div class="bloom-canvas-element bloom-backgroundImage" id="outerBg"> | ||
| <div class="bloom-imageContainer"><img src="outer.png"/></div> | ||
| </div> | ||
| <div class="bloom-canvas-element"> | ||
| <div class="bloom-canvas" id="inner"> | ||
| <div class="bloom-canvas-element bloom-backgroundImage" id="innerBg"> | ||
| <div class="bloom-imageContainer"><img src="inner.png"/></div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div>`; | ||
| const outer = document.getElementById("outer") as HTMLElement; | ||
| const inner = document.getElementById("inner") as HTMLElement; | ||
| expect(getBackgroundCanvasElement(outer)).toBe( | ||
| document.getElementById("outerBg"), | ||
| ); | ||
| expect(getBackgroundCanvasElement(inner)).toBe( | ||
| document.getElementById("innerBg"), | ||
| ); | ||
| }); | ||
|
|
||
| // The Image Description tool wraps the background image in a bloom-describedImage | ||
| // while it is active, so the background image is not a direct child then. | ||
| test("still finds the background image when the Image Description tool has wrapped it", () => { | ||
| document.body.innerHTML = ` | ||
| <div class="bloom-canvas"> | ||
| <div class="bloom-describedImage"> | ||
| <div class="bloom-canvas-element bloom-backgroundImage" id="bg"> | ||
| <div class="bloom-imageContainer"><img src="a.png"/></div> | ||
| </div> | ||
| </div> | ||
| </div>`; | ||
| const bloomCanvas = document.querySelector( | ||
| ".bloom-canvas", | ||
| ) as HTMLElement; | ||
| expect(getBackgroundCanvasElement(bloomCanvas)).toBe( | ||
| document.getElementById("bg"), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.