Skip to content

Commit

Permalink
feat: implement device pixel ratio changes (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored Oct 12, 2023
1 parent 19fe3cc commit 49f6dee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/bidiMapper/domains/context/BrowsingContextImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,11 @@ export class BrowsingContextImpl {
};
}

async setViewport(viewport: BrowsingContext.Viewport | null) {
if (viewport === null) {
async setViewport(
viewport: BrowsingContext.Viewport | null,
devicePixelRatio?: number | null
) {
if (viewport === null && devicePixelRatio === null) {
await this.#cdpTarget.cdpClient.sendCommand(
'Emulation.clearDeviceMetricsOverride'
);
Expand All @@ -688,9 +691,9 @@ export class BrowsingContextImpl {
await this.#cdpTarget.cdpClient.sendCommand(
'Emulation.setDeviceMetricsOverride',
{
width: viewport.width,
height: viewport.height,
deviceScaleFactor: 0,
width: viewport ? viewport.width : 0,
height: viewport ? viewport.height : 0,
deviceScaleFactor: devicePixelRatio ? devicePixelRatio : 0,
mobile: false,
dontSetVisibleSize: true,
}
Expand Down
2 changes: 1 addition & 1 deletion src/bidiMapper/domains/context/BrowsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class BrowsingContextProcessor {
'Emulating viewport is only supported on the top-level context'
);
}
await context.setViewport(params.viewport);
await context.setViewport(params.viewport, params.devicePixelRatio);
return {};
}

Expand Down
1 change: 1 addition & 0 deletions src/protocol-parser/webdriver-bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ export namespace BrowsingContext {
z.object({
context: BrowsingContext.BrowsingContextSchema,
viewport: z.union([BrowsingContext.ViewportSchema, z.null()]),
devicePixelRatio: z.union([z.number().gt(0), z.null()]).optional(),
})
);
}
Expand Down
1 change: 1 addition & 0 deletions src/protocol/webdriver-bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ export namespace BrowsingContext {
export type SetViewportParameters = {
context: BrowsingContext.BrowsingContext;
viewport: BrowsingContext.Viewport | null;
devicePixelRatio?: number | null;
};
}
export namespace BrowsingContext {
Expand Down
39 changes: 37 additions & 2 deletions tests/browsing_context/test_set_viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ async def test_set_viewport(websocket, context_id, html):
"viewport": {
"width": 300,
"height": 300,
}
},
"devicePixelRatio": None
}
})

Expand All @@ -55,6 +56,39 @@ async def test_set_viewport(websocket, context_id, html):
}]] == result["result"]["value"]


@pytest.mark.asyncio
async def test_set_viewport_dpr(websocket, context_id, html):
await goto_url(websocket, context_id, html())

await execute_command(
websocket, {
"method": "browsingContext.setViewport",
"params": {
"context": context_id,
"viewport": None,
"devicePixelRatio": 4,
}
})

result = await execute_command(
websocket, {
"method": "script.evaluate",
"params": {
"expression": "({'devicePixelRatio': window.devicePixelRatio})",
"target": {
"context": context_id
},
"resultOwnership": "none",
"awaitPromise": True
}
})

assert [["devicePixelRatio", {
"type": "number",
"value": 4
}]] == result["result"]["value"]


@pytest.mark.asyncio
@pytest.mark.parametrize("width,height", [
(300, 10000001),
Expand Down Expand Up @@ -85,6 +119,7 @@ async def test_set_viewport_unsupported(websocket, context_id, html, width,
"viewport": {
"width": width,
"height": height,
}
},
"devicePixelRatio": None
}
})

0 comments on commit 49f6dee

Please sign in to comment.