api: make clear the use of null in page.emulateMedia (#3078)

We can pass null to disable emulating particular feature.
This change updates the docs and rpc protocol accordingly.
This commit is contained in:
Dmitry Gozman 2020-07-21 18:56:41 -07:00 committed by GitHub
parent f751ab1791
commit ced0bc2d1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 14 deletions

View File

@ -1138,8 +1138,8 @@ await page.dispatchEvent('#source', 'dragstart', { dataTransfer });
#### page.emulateMedia(options) #### page.emulateMedia(options)
- `options` <[Object]> - `options` <[Object]>
- `media` <"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation. - `media` <?"screen"|"print"> Changes the CSS media type of the page. The only allowed values are `'screen'`, `'print'` and `null`. Passing `null` disables CSS media emulation. Omitting `media` or passing `undefined` does not change the emulated value.
- `colorScheme` <"dark"|"light"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. - `colorScheme` <?"dark"|"light"|"no-preference"> Emulates `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'`. Passing `null` disables color scheme emulation. Omitting `colorScheme` or passing `undefined` does not change the emulated value.
- returns: <[Promise]> - returns: <[Promise]>
```js ```js

View File

@ -293,7 +293,8 @@ export class FFPage implements PageDelegate {
async updateEmulateMedia(): Promise<void> { async updateEmulateMedia(): Promise<void> {
const colorScheme = this._page._state.colorScheme || this._browserContext._options.colorScheme || 'light'; const colorScheme = this._page._state.colorScheme || this._browserContext._options.colorScheme || 'light';
await this._session.send('Page.setEmulatedMedia', { await this._session.send('Page.setEmulatedMedia', {
type: this._page._state.mediaType === null ? undefined : this._page._state.mediaType, // Empty string means reset.
type: this._page._state.mediaType === null ? '' : this._page._state.mediaType,
colorScheme colorScheme
}); });
} }

View File

@ -385,9 +385,11 @@ export class Page extends EventEmitter {
return waitPromise; return waitPromise;
} }
async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) { async emulateMedia(options: { media?: types.MediaType | null, colorScheme?: types.ColorScheme | null }) {
assert(!options.media || types.mediaTypes.has(options.media), 'media: expected one of (screen|print)'); if (options.media !== undefined)
assert(!options.colorScheme || types.colorSchemes.has(options.colorScheme), 'colorScheme: expected one of (dark|light|no-preference)'); assert(options.media === null || types.mediaTypes.has(options.media), 'media: expected one of (screen|print)');
if (options.colorScheme !== undefined)
assert(options.colorScheme === null || types.colorSchemes.has(options.colorScheme), 'colorScheme: expected one of (dark|light|no-preference)');
if (options.media !== undefined) if (options.media !== undefined)
this._state.mediaType = options.media; this._state.mediaType = options.media;
if (options.colorScheme !== undefined) if (options.colorScheme !== undefined)

View File

@ -618,8 +618,8 @@ export type PageCloseParams = {
}; };
export type PageCloseResult = void; export type PageCloseResult = void;
export type PageEmulateMediaParams = { export type PageEmulateMediaParams = {
media?: 'screen' | 'print', media?: 'screen' | 'print' | 'reset',
colorScheme?: 'dark' | 'light' | 'no-preference', colorScheme?: 'dark' | 'light' | 'no-preference' | 'reset',
}; };
export type PageEmulateMediaResult = void; export type PageEmulateMediaResult = void;
export type PageExposeBindingParams = { export type PageExposeBindingParams = {

View File

@ -357,9 +357,12 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
}); });
} }
async emulateMedia(options: { media?: types.MediaType, colorScheme?: types.ColorScheme }) { async emulateMedia(options: { media?: types.MediaType | null, colorScheme?: types.ColorScheme | null }) {
return this._wrapApiCall('page.emulateMedia', async () => { return this._wrapApiCall('page.emulateMedia', async () => {
await this._channel.emulateMedia(options); await this._channel.emulateMedia({
media: options.media === null ? 'reset' : options.media,
colorScheme: options.colorScheme === null ? 'reset' : options.colorScheme,
});
}); });
} }

View File

@ -527,10 +527,14 @@ interface Page
media?: enum media?: enum
screen screen
print print
# Reset emulated value to the system default.
reset
colorScheme?: enum colorScheme?: enum
dark dark
light light
no-preference no-preference
# Reset emulated value to the system default.
reset
command exposeBinding command exposeBinding
parameters parameters

View File

@ -20,7 +20,7 @@ import { Frame } from '../../frames';
import { Request } from '../../network'; import { Request } from '../../network';
import { Page, Worker } from '../../page'; import { Page, Worker } from '../../page';
import * as types from '../../types'; import * as types from '../../types';
import { BindingCallChannel, BindingCallInitializer, ElementHandleChannel, PageChannel, PageInitializer, ResponseChannel, WorkerInitializer, WorkerChannel, JSHandleChannel, Binary, SerializedArgument, PagePdfParams, SerializedError, PageAccessibilitySnapshotResult, SerializedValue } from '../channels'; import { BindingCallChannel, BindingCallInitializer, ElementHandleChannel, PageChannel, PageInitializer, ResponseChannel, WorkerInitializer, WorkerChannel, JSHandleChannel, Binary, SerializedArgument, PagePdfParams, SerializedError, PageAccessibilitySnapshotResult, SerializedValue, PageEmulateMediaParams } from '../channels';
import { Dispatcher, DispatcherScope, lookupDispatcher, lookupNullableDispatcher } from './dispatcher'; import { Dispatcher, DispatcherScope, lookupDispatcher, lookupNullableDispatcher } from './dispatcher';
import { parseError, serializeError, headersArrayToObject, axNodeToProtocol } from '../serializers'; import { parseError, serializeError, headersArrayToObject, axNodeToProtocol } from '../serializers';
import { ConsoleMessageDispatcher } from './consoleMessageDispatcher'; import { ConsoleMessageDispatcher } from './consoleMessageDispatcher';
@ -106,8 +106,11 @@ export class PageDispatcher extends Dispatcher<Page, PageInitializer> implements
return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goForward(params)) }; return { response: lookupNullableDispatcher<ResponseDispatcher>(await this._page.goForward(params)) };
} }
async emulateMedia(params: { media?: 'screen' | 'print', colorScheme?: 'dark' | 'light' | 'no-preference' }): Promise<void> { async emulateMedia(params: PageEmulateMediaParams): Promise<void> {
await this._page.emulateMedia(params); await this._page.emulateMedia({
media: params.media === 'reset' ? null : params.media,
colorScheme: params.colorScheme === 'reset' ? null : params.colorScheme,
});
} }
async setViewportSize(params: { viewportSize: types.Size }): Promise<void> { async setViewportSize(params: { viewportSize: types.Size }): Promise<void> {

View File

@ -241,7 +241,7 @@ describe('Page.emulateMedia type', function() {
await page.emulateMedia({}); await page.emulateMedia({});
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false); expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(false);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true); expect(await page.evaluate(() => matchMedia('print').matches)).toBe(true);
await page.emulateMedia({ media: '' }); await page.emulateMedia({ media: null });
expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true); expect(await page.evaluate(() => matchMedia('screen').matches)).toBe(true);
expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false); expect(await page.evaluate(() => matchMedia('print').matches)).toBe(false);
}); });