summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/emulation
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /remote/cdp/test/browser/emulation
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'remote/cdp/test/browser/emulation')
-rw-r--r--remote/cdp/test/browser/emulation/browser.toml25
-rw-r--r--remote/cdp/test/browser/emulation/browser_setDeviceMetricsOverride.js403
-rw-r--r--remote/cdp/test/browser/emulation/browser_setTouchEmulationEnabled.js78
-rw-r--r--remote/cdp/test/browser/emulation/browser_setUserAgentOverride.js140
-rw-r--r--remote/cdp/test/browser/emulation/head.js9
5 files changed, 655 insertions, 0 deletions
diff --git a/remote/cdp/test/browser/emulation/browser.toml b/remote/cdp/test/browser/emulation/browser.toml
new file mode 100644
index 0000000000..e5051abace
--- /dev/null
+++ b/remote/cdp/test/browser/emulation/browser.toml
@@ -0,0 +1,25 @@
+[DEFAULT]
+tags = "cdp"
+subsuite = "remote"
+args = [
+ "--remote-debugging-port",
+ "--remote-allow-origins=null",
+]
+prefs = [ # Bug 1600054: Make CDP Fission compatible
+ "fission.bfcacheInParent=false",
+ "fission.webContentIsolationStrategy=0",
+]
+skip-if = [
+ "display == 'wayland'" # Bug 1861933: Timestamp unreliable due to worker setup
+]
+support-files = [
+ "!/remote/cdp/test/browser/chrome-remote-interface.js",
+ "!/remote/cdp/test/browser/head.js",
+ "head.js",
+]
+
+["browser_setDeviceMetricsOverride.js"]
+
+["browser_setTouchEmulationEnabled.js"]
+
+["browser_setUserAgentOverride.js"]
diff --git a/remote/cdp/test/browser/emulation/browser_setDeviceMetricsOverride.js b/remote/cdp/test/browser/emulation/browser_setDeviceMetricsOverride.js
new file mode 100644
index 0000000000..eb8c314161
--- /dev/null
+++ b/remote/cdp/test/browser/emulation/browser_setDeviceMetricsOverride.js
@@ -0,0 +1,403 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const DOC_SMALL = toDataURL("<div>Hello world");
+const DOC_LARGE = toDataURL("<div style='margin: 150vh 0 0 150vw'>Hello world");
+
+const MAX_WINDOW_SIZE = 10000000;
+
+function getContentDPR() {
+ info(`Retrieve device pixel ratio in content`);
+ return SpecialPowers.spawn(
+ gBrowser.selectedBrowser,
+ [],
+ _ => content.browsingContext.overrideDPPX || content.devicePixelRatio
+ );
+}
+
+add_task(async function dimensionsSmallerThanWindow({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: Math.floor(layoutViewport.clientWidth / 2),
+ height: Math.floor(layoutViewport.clientHeight / 3),
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ overrideSettings.width,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ overrideSettings.height,
+ "Expected layout viewport height set"
+ );
+ is(
+ updatedLayoutMetrics.contentSize.width,
+ overrideSettings.width,
+ "Expected content size width set"
+ );
+ is(
+ updatedLayoutMetrics.contentSize.height,
+ overrideSettings.height,
+ "Expected content size height set"
+ );
+});
+
+add_task(async function dimensionsLargerThanWindow({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_LARGE);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: layoutViewport.clientWidth * 2,
+ height: layoutViewport.clientHeight * 2,
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_LARGE);
+
+ const scrollbarSize = await getScrollbarSize();
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ overrideSettings.width - scrollbarSize.width,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ overrideSettings.height - scrollbarSize.height,
+ "Expected layout viewport height set"
+ );
+});
+
+add_task(async function noSizeChangeForSameWidthAndHeight({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: layoutViewport.clientWidth,
+ height: layoutViewport.clientHeight,
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Expected layout viewport height set"
+ );
+});
+
+add_task(async function noWidthChangeWithZeroWidth({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: 0,
+ height: Math.floor(layoutViewport.clientHeight / 3),
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ overrideSettings.height,
+ "Expected layout viewport height set"
+ );
+});
+
+add_task(async function noHeightChangeWithZeroHeight({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: Math.floor(layoutViewport.clientWidth / 2),
+ height: 0,
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ overrideSettings.width,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Expected layout viewport height set"
+ );
+});
+
+add_task(async function nosizeChangeWithZeroWidthAndHeight({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+
+ const overrideSettings = {
+ width: 0,
+ height: 0,
+ deviceScaleFactor: 1.0,
+ };
+
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Expected layout viewport width set"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Expected layout viewport height set"
+ );
+});
+
+add_task(async function failsWithNegativeWidth({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio = await getContentDPR();
+
+ const overrideSettings = {
+ width: -1,
+ height: Math.floor(layoutViewport.clientHeight / 3),
+ deviceScaleFactor: 1.0,
+ };
+
+ await Assert.rejects(
+ Emulation.setDeviceMetricsOverride(overrideSettings),
+ err =>
+ err.message.includes(
+ "Width and height values must be positive, not greater than 10000000"
+ ),
+ "Negative width raised error"
+ );
+
+ await loadURL(DOC_SMALL);
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Visible layout width hasn't been changed"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Visible layout height hasn't been changed"
+ );
+ is(await getContentDPR(), ratio, "Device pixel ratio hasn't been changed");
+});
+
+add_task(async function failsWithTooLargeWidth({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio = await getContentDPR();
+
+ const overrideSettings = {
+ width: MAX_WINDOW_SIZE + 1,
+ height: Math.floor(layoutViewport.clientHeight / 3),
+ deviceScaleFactor: 1.0,
+ };
+
+ await Assert.rejects(
+ Emulation.setDeviceMetricsOverride(overrideSettings),
+ err =>
+ err.message.includes(
+ "Width and height values must be positive, not greater than 10000000"
+ ),
+ "Too large width raised error"
+ );
+
+ await loadURL(DOC_SMALL);
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Visible layout width hasn't been changed"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Visible layout height hasn't been changed"
+ );
+ is(await getContentDPR(), ratio, "Device pixel ratio hasn't been changed");
+});
+
+add_task(async function failsWithNegativeHeight({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio = await getContentDPR();
+
+ const overrideSettings = {
+ width: Math.floor(layoutViewport.clientWidth / 2),
+ height: -1,
+ deviceScaleFactor: 1.0,
+ };
+
+ await Assert.rejects(
+ Emulation.setDeviceMetricsOverride(overrideSettings),
+ err =>
+ err.message.includes(
+ "Width and height values must be positive, not greater than 10000000"
+ ),
+ "Negative height raised error"
+ );
+ await loadURL(DOC_SMALL);
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Visible layout width hasn't been changed"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Visible layout height hasn't been changed"
+ );
+ is(await getContentDPR(), ratio, "Device pixel ratio hasn't been changed");
+});
+
+add_task(async function failsWithTooLargeHeight({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio = await getContentDPR();
+
+ const overrideSettings = {
+ width: Math.floor(layoutViewport.clientWidth / 2),
+ height: MAX_WINDOW_SIZE + 1,
+ deviceScaleFactor: 1.0,
+ };
+ await Assert.rejects(
+ Emulation.setDeviceMetricsOverride(overrideSettings),
+ err =>
+ err.message.includes(
+ "Width and height values must be positive, not greater than 10000000"
+ ),
+ "Too large height raised error"
+ );
+
+ await loadURL(DOC_SMALL);
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Visible layout width hasn't been changed"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Visible layout height hasn't been changed"
+ );
+ is(await getContentDPR(), ratio, "Device pixel ratio hasn't been changed");
+});
+
+add_task(async function setDevicePixelRatio({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio_orig = await getContentDPR();
+
+ const overrideSettings = {
+ width: layoutViewport.clientWidth,
+ height: layoutViewport.clientHeight,
+ deviceScaleFactor: ratio_orig * 2,
+ };
+
+ // Set a custom pixel ratio
+ await Emulation.setDeviceMetricsOverride(overrideSettings);
+ await loadURL(DOC_SMALL);
+
+ is(await getContentDPR(), ratio_orig * 2, "Expected device pixel ratio set");
+});
+
+add_task(async function failsWithNegativeRatio({ client }) {
+ const { Emulation, Page } = client;
+
+ await loadURL(DOC_SMALL);
+ const { layoutViewport } = await Page.getLayoutMetrics();
+ const ratio = await getContentDPR();
+
+ const overrideSettings = {
+ width: Math.floor(layoutViewport.clientHeight / 2),
+ height: Math.floor(layoutViewport.clientHeight / 3),
+ deviceScaleFactor: -1,
+ };
+
+ await Assert.rejects(
+ Emulation.setDeviceMetricsOverride(overrideSettings),
+ err => err.message.includes("deviceScaleFactor: must be positive"),
+ "Negative device scale factor raised error"
+ );
+
+ await loadURL(DOC_SMALL);
+ const updatedLayoutMetrics = await Page.getLayoutMetrics();
+
+ is(
+ updatedLayoutMetrics.layoutViewport.clientWidth,
+ layoutViewport.clientWidth,
+ "Visible layout width hasn't been changed"
+ );
+ is(
+ updatedLayoutMetrics.layoutViewport.clientHeight,
+ layoutViewport.clientHeight,
+ "Visible layout height hasn't been changed"
+ );
+ is(await getContentDPR(), ratio, "Device pixel ratio hasn't been changed");
+});
diff --git a/remote/cdp/test/browser/emulation/browser_setTouchEmulationEnabled.js b/remote/cdp/test/browser/emulation/browser_setTouchEmulationEnabled.js
new file mode 100644
index 0000000000..1d4efacff1
--- /dev/null
+++ b/remote/cdp/test/browser/emulation/browser_setTouchEmulationEnabled.js
@@ -0,0 +1,78 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const DOC_TOUCH = toDataURL("<div>Hello world");
+
+add_task(async function invalidEnabledType({ client }) {
+ const { Emulation } = client;
+
+ for (const enabled of [null, "", 1, [], {}]) {
+ await Assert.rejects(
+ Emulation.setTouchEmulationEnabled({ enabled }),
+ /enabled: boolean value expected/,
+ `Fails with invalid type: ${enabled}`
+ );
+ }
+});
+
+add_task(async function enableAndDisable({ client }) {
+ const { Emulation, Runtime } = client;
+ const url = toDataURL("<p>foo");
+
+ await enableRuntime(client);
+
+ let contextCreated = Runtime.executionContextCreated();
+ await loadURL(url);
+ let result = await contextCreated;
+ await assertTouchEnabled(client, result.context, false);
+
+ await Emulation.setTouchEmulationEnabled({ enabled: true });
+ contextCreated = Runtime.executionContextCreated();
+ await loadURL(url);
+ result = await contextCreated;
+ await assertTouchEnabled(client, result.context, true);
+
+ await Emulation.setTouchEmulationEnabled({ enabled: false });
+ contextCreated = Runtime.executionContextCreated();
+ await loadURL(url);
+ result = await contextCreated;
+ await assertTouchEnabled(client, result.context, false);
+});
+
+add_task(async function receiveTouchEventsWhenEnabled({ client }) {
+ const { Emulation, Runtime } = client;
+
+ await enableRuntime(client);
+
+ await Emulation.setTouchEmulationEnabled({ enabled: true });
+ const contextCreated = Runtime.executionContextCreated();
+ await loadURL(DOC_TOUCH);
+ const { context } = await contextCreated;
+
+ await assertTouchEnabled(client, context, true);
+
+ const { result } = await evaluate(client, context.id, () => {
+ return new Promise(resolve => {
+ window.ontouchstart = () => {
+ resolve(true);
+ };
+ window.dispatchEvent(new Event("touchstart"));
+ resolve(false);
+ });
+ });
+ is(result.value, true, "Received touch event");
+});
+
+async function assertTouchEnabled(client, context, expectedStatus) {
+ const { result } = await evaluate(client, context.id, () => {
+ return "ontouchstart" in window;
+ });
+
+ if (expectedStatus) {
+ ok(result.value, "Touch emulation enabled");
+ } else {
+ ok(!result.value, "Touch emulation disabled");
+ }
+}
diff --git a/remote/cdp/test/browser/emulation/browser_setUserAgentOverride.js b/remote/cdp/test/browser/emulation/browser_setUserAgentOverride.js
new file mode 100644
index 0000000000..a0f292435d
--- /dev/null
+++ b/remote/cdp/test/browser/emulation/browser_setUserAgentOverride.js
@@ -0,0 +1,140 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const DOC = toDataURL(`<script>document.write(navigator.userAgent);</script>`);
+
+add_task(async function invalidPlatform({ client }) {
+ const { Emulation } = client;
+ const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";
+
+ for (const platform of [null, true, 1, [], {}]) {
+ await Assert.rejects(
+ Emulation.setUserAgentOverride({ userAgent, platform }),
+ /platform: string value expected/,
+ `Fails with invalid type: ${platform}`
+ );
+ }
+});
+
+add_task(async function setAndResetUserAgent({ client }) {
+ const { Emulation } = client;
+ const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
+
+ await loadURL(DOC);
+ const originalUserAgent = await getNavigatorProperty("userAgent");
+
+ isnot(originalUserAgent, userAgent, "Custom user agent hasn't been set");
+
+ await Emulation.setUserAgentOverride({ userAgent });
+ await loadURL(DOC);
+ is(
+ await getNavigatorProperty("userAgent"),
+ userAgent,
+ "Custom user agent has been set"
+ );
+
+ await Emulation.setUserAgentOverride({ userAgent: "" });
+ await loadURL(DOC);
+ is(
+ await getNavigatorProperty("userAgent"),
+ originalUserAgent,
+ "Custom user agent has been reset"
+ );
+});
+
+add_task(async function invalidUserAgent({ client }) {
+ const { Emulation } = client;
+ const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";
+
+ await loadURL(DOC);
+ isnot(
+ await getNavigatorProperty("userAgent"),
+ userAgent,
+ "Custom user agent hasn't been set"
+ );
+
+ await Assert.rejects(
+ Emulation.setUserAgentOverride({ userAgent }),
+ err => err.message.includes("Invalid characters found in userAgent"),
+ "Invalid user agent format raised error"
+ );
+});
+
+add_task(async function setAndResetPlatform({ client }) {
+ const { Emulation } = client;
+ const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
+ const platform = "foobar";
+
+ await loadURL(DOC);
+ const originalUserAgent = await getNavigatorProperty("userAgent");
+ const originalPlatform = await getNavigatorProperty("platform");
+
+ isnot(userAgent, originalUserAgent, "Custom user agent hasn't been set");
+ isnot(platform, originalPlatform, "Custom platform hasn't been set");
+
+ await Emulation.setUserAgentOverride({ userAgent, platform });
+ await loadURL(DOC);
+ is(
+ await getNavigatorProperty("userAgent"),
+ userAgent,
+ "Custom user agent has been set"
+ );
+ is(
+ await getNavigatorProperty("platform"),
+ platform,
+ "Custom platform has been set"
+ );
+
+ await Emulation.setUserAgentOverride({ userAgent: "", platform: "" });
+ await loadURL(DOC);
+ is(
+ await getNavigatorProperty("userAgent"),
+ originalUserAgent,
+ "Custom user agent has been reset"
+ );
+ is(
+ await getNavigatorProperty("platform"),
+ originalPlatform,
+ "Custom platform has been reset"
+ );
+});
+
+add_task(async function notSetForNewContext({ client }) {
+ const { Emulation, Target } = client;
+ const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0";
+ const platform = "foobar";
+
+ await Emulation.setUserAgentOverride({ userAgent, platform });
+ await loadURL(DOC);
+ is(
+ await getNavigatorProperty("userAgent"),
+ userAgent,
+ "Custom user agent has been set"
+ );
+ is(
+ await getNavigatorProperty("platform"),
+ platform,
+ "Custom platform has been set"
+ );
+
+ await openTab(Target, { activate: true });
+
+ isnot(
+ await getNavigatorProperty("userAgent"),
+ userAgent,
+ "Custom user agent has not been set"
+ );
+ isnot(
+ await getNavigatorProperty("platform"),
+ platform,
+ "Custom platform has not been set"
+ );
+});
+
+async function getNavigatorProperty(prop) {
+ return SpecialPowers.spawn(gBrowser.selectedBrowser, [prop], _prop => {
+ return content.navigator[_prop];
+ });
+}
diff --git a/remote/cdp/test/browser/emulation/head.js b/remote/cdp/test/browser/emulation/head.js
new file mode 100644
index 0000000000..1a1c90fbf6
--- /dev/null
+++ b/remote/cdp/test/browser/emulation/head.js
@@ -0,0 +1,9 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+Services.scriptloader.loadSubScript(
+ "chrome://mochitests/content/browser/remote/cdp/test/browser/head.js",
+ this
+);