test: add tests for device pixel ratio (#11690)

This commit is contained in:
Alex Rudenko 2024-01-16 08:55:25 +01:00 committed by GitHub
parent 67feac95c9
commit 8b71db7fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 0 deletions

6
test/assets/picture.html Normal file
View File

@ -0,0 +1,6 @@
<!DOCTYPE html>
<img
srcset="logo-1x.png, logo-2x.png 2x, logo-3x.png 3x"
src="logo-1x.png"
height="320"
width="320" />

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<style>
p {
color: transparent;
}
@media (resolution: 1dppx) {
p {
font-size: 1px;
}
}
@media (resolution: 2dppx) {
p {
font-size: 2px;
}
}
@media (resolution: 3dppx) {
p {
font-size: 3px;
}
}
</style>
<p>Test</p>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

View File

@ -133,6 +133,58 @@ describe('Emulation', () => {
})
).toBe('portrait-primary');
});
it('should update media queries when resoltion changes', async () => {
const {page, server} = await getTestState();
async function getFontSize() {
return await page.evaluate(() => {
return parseInt(
window.getComputedStyle(document.querySelector('p')!).fontSize,
10
);
});
}
for (const dpr of [1, 2, 3]) {
await page.setViewport({
width: 800,
height: 600,
deviceScaleFactor: dpr,
});
await page.goto(server.PREFIX + '/resolution.html');
await expect(getFontSize()).resolves.toEqual(dpr);
const screenshot = await page.screenshot({
fullPage: false,
});
expect(screenshot).toBeGolden(`device-pixel-ratio${dpr}.png`);
}
});
it('should load correct pictures when emulation dpr', async () => {
const {page, server} = await getTestState();
async function getCurrentSrc() {
return await page.evaluate(() => {
return document.querySelector('img')!.currentSrc;
});
}
for (const dpr of [1, 2, 3]) {
await page.setViewport({
width: 800,
height: 600,
deviceScaleFactor: dpr,
});
await page.goto(server.PREFIX + '/picture.html');
await expect(getCurrentSrc()).resolves.toMatch(
new RegExp(`logo-${dpr}x.png`)
);
}
});
});
describe('Page.emulate', function () {