Skip to content

Commit 76063fb

Browse files
JiuqingSongCopilot
andauthored
Fix applyChange tests to pass at all screen scales (#3391)
generateDataURL sizes the output canvas by window.devicePixelRatio, but the expected newSrc images were captured at 100% scale, so the IHDR dimension assertions failed on hosts at other scales. Pin devicePixelRatio to 1 for these tests so canvas output is deterministic regardless of screen scale. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8475e5f commit 76063fb

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

packages/roosterjs-content-model-plugins/test/imageEdit/utils/applyChangeTest.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,20 @@ describe('applyChange', () => {
5757
let img: HTMLImageElement;
5858
let editor: IEditor;
5959
let triggerEvent: jasmine.Spy;
60+
let originalDevicePixelRatio: PropertyDescriptor | undefined;
6061

6162
beforeEach(async () => {
63+
// generateDataURL sizes the output canvas as targetWidth/Height * window.devicePixelRatio
64+
// ("Adjust the canvas size and scaling for high display resolution"). The expected newSrc
65+
// values below were captured at 100% screen scale (devicePixelRatio === 1), so on a host
66+
// running at any other scale the generated PNG would be larger and the dimension assertions
67+
// would fail. Pin devicePixelRatio to 1 so these tests are deterministic at any screen scale.
68+
originalDevicePixelRatio = Object.getOwnPropertyDescriptor(window, 'devicePixelRatio');
69+
Object.defineProperty(window, 'devicePixelRatio', {
70+
configurable: true,
71+
get: () => 1,
72+
});
73+
6274
// Create a fresh model image per test. It is otherwise shared module-level state, and
6375
// applyChange persists/clears editingInfo on it, so a stale rotation/crop from a prior
6476
// test would make checkEditInfoState misclassify the next one (flaky across spec order).
@@ -80,6 +92,12 @@ describe('applyChange', () => {
8092

8193
afterEach(() => {
8294
img?.parentNode?.removeChild(img);
95+
96+
if (originalDevicePixelRatio) {
97+
Object.defineProperty(window, 'devicePixelRatio', originalDevicePixelRatio);
98+
} else {
99+
delete (window as any).devicePixelRatio;
100+
}
83101
});
84102

85103
function runTest(input: ContentModelDocument, callback: () => boolean) {

0 commit comments

Comments
 (0)