Skip to content
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

feat(page): add color-gamut support to Page.emulateMediaFeatures #6857

Merged
merged 3 commits into from Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/api.md
Expand Up @@ -1411,7 +1411,7 @@ List of all available devices is available in the source code: [src/common/Devic

#### page.emulateMediaFeatures(features)
- `features` <?[Array]<[Object]>> Given an array of media feature objects, emulates CSS media features on the page. Each media feature object must have the following properties:
- `name` <[string]> The CSS media feature name. Supported names are `'prefers-colors-scheme'` and `'prefers-reduced-motion'`.
- `name` <[string]> The CSS media feature name. Supported names are `'prefers-colors-scheme'`, `'prefers-reduced-motion'`, and `'color-gamut'`.
- `value` <[string]> The value for the given CSS media feature.
- returns: <[Promise]>

Expand Down Expand Up @@ -1440,6 +1440,16 @@ await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches
// → true
await page.evaluate(() => matchMedia('(prefers-reduced-motion: no-preference)').matches);
// → false

await page.emulateMediaFeatures([
{ name: 'color-gamut', value: 'p3' },
]);
await page.evaluate(() => matchMedia('(color-gamut: srgb)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamut: p3)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamut: rec2020)').matches);
// → false
```

#### page.emulateMediaType(type)
Expand Down
4 changes: 3 additions & 1 deletion src/common/Page.ts
Expand Up @@ -1420,7 +1420,9 @@ export class Page extends EventEmitter {
features.every((mediaFeature) => {
const name = mediaFeature.name;
assert(
/^prefers-(?:color-scheme|reduced-motion)$/.test(name),
/^(?:prefers-(?:color-scheme|reduced-motion)|color-gamut)$/.test(
name
),
'Unsupported media feature: ' + name
);
return true;
Expand Down
32 changes: 32 additions & 0 deletions test/emulation.spec.ts
Expand Up @@ -242,6 +242,38 @@ describe('Emulation', () => {
() => matchMedia('(prefers-color-scheme: dark)').matches
)
).toBe(false);
await page.emulateMediaFeatures([{ name: 'color-gamut', value: 'srgb' }]);
expect(
await page.evaluate(() => matchMedia('(color-gamut: p3)').matches)
).toBe(false);
expect(
await page.evaluate(() => matchMedia('(color-gamut: srgb)').matches)
).toBe(true);
expect(
await page.evaluate(() => matchMedia('(color-gamut: rec2020)').matches)
).toBe(false);
await page.emulateMediaFeatures([{ name: 'color-gamut', value: 'p3' }]);
expect(
await page.evaluate(() => matchMedia('(color-gamut: p3)').matches)
).toBe(true);
expect(
await page.evaluate(() => matchMedia('(color-gamut: srgb)').matches)
).toBe(true);
expect(
await page.evaluate(() => matchMedia('(color-gamut: rec2020)').matches)
).toBe(false);
await page.emulateMediaFeatures([
{ name: 'color-gamut', value: 'rec2020' },
]);
expect(
await page.evaluate(() => matchMedia('(color-gamut: p3)').matches)
).toBe(true);
expect(
await page.evaluate(() => matchMedia('(color-gamut: srgb)').matches)
).toBe(true);
expect(
await page.evaluate(() => matchMedia('(color-gamut: rec2020)').matches)
).toBe(true);
});
it('should throw in case of bad argument', async () => {
const { page } = getTestState();
Expand Down