Skip to content

Commit

Permalink
feat(page): add color-gamut support to Page.emulateMediaFeatures
Browse files Browse the repository at this point in the history
The change updates the validation function to allow color-gamut media
features and updates the documentation.

Issues: #6761
  • Loading branch information
OrKoN committed Feb 10, 2021
1 parent 6a0eb78 commit f13f615
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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-gamu: srgb)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamu: p3)').matches);
// → true
await page.evaluate(() => matchMedia('(color-gamu: rec2020)').matches);
// → false
```

#### page.emulateMediaType(type)
Expand Down
2 changes: 1 addition & 1 deletion src/common/Page.ts
Expand Up @@ -1387,7 +1387,7 @@ 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
34 changes: 34 additions & 0 deletions test/emulation.spec.ts
Expand Up @@ -242,6 +242,40 @@ 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

0 comments on commit f13f615

Please sign in to comment.