summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target-configuration/tests/browser_target_configuration_command_dppx.js
blob: 744ac2c403c5d12bc25c16e0a8e1b59433fff2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test device pixel ratio override.
const TEST_DOCUMENT = "target_configuration_test_doc.sjs";
const TEST_URI = URL_ROOT_COM_SSL + TEST_DOCUMENT;

add_task(async function () {
  const tab = await addTab(TEST_URI);

  info("Create commands for the tab");
  const commands = await CommandsFactory.forTab(tab);

  const targetConfigurationCommand = commands.targetConfigurationCommand;
  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();

  const originalDpr = await getTopLevelDocumentDevicePixelRatio();

  info("Update configuration to change device pixel ratio");
  const CUSTOM_DPR = 5.5;

  await targetConfigurationCommand.updateConfiguration({
    overrideDPPX: CUSTOM_DPR,
  });

  is(
    await getTopLevelDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The ratio is properly set on the top level document after updating the configuration"
  );
  is(
    await getIframeDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The ratio is properly set on the iframe after updating the configuration"
  );

  info("Reload the page");
  await BrowserTestUtils.reloadTab(tab, /* includeSubFrames */ true);

  is(
    await getTopLevelDocumentDevicePixelRatioAtStartup(),
    CUSTOM_DPR,
    "The custom ratio was set in the content page when it loaded after reloading"
  );
  is(
    await getTopLevelDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The custom ratio is set in the content page after reloading"
  );
  is(
    await getIframeDocumentDevicePixelRatioAtStartup(),
    CUSTOM_DPR,
    "The custom ratio was set in the remote iframe when it loaded after reloading"
  );
  is(
    await getIframeDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The custom ratio is set in the remote iframe after reloading"
  );

  const previousBrowsingContextId = gBrowser.selectedBrowser.browsingContext.id;
  info(
    "Check that navigating to a page that forces the creation of a new browsing context keep the simulation enabled"
  );

  const onPageLoaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    /* includeSubFrames */ true
  );
  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    URL_ROOT_ORG_SSL + TEST_DOCUMENT + "?crossOriginIsolated=true"
  );
  await onPageLoaded;

  isnot(
    gBrowser.selectedBrowser.browsingContext.id,
    previousBrowsingContextId,
    "A new browsing context was created"
  );

  is(
    await getTopLevelDocumentDevicePixelRatioAtStartup(),
    CUSTOM_DPR,
    "The custom ratio was set in the content page when it loaded after navigating to a new browsing context"
  );
  is(
    await getTopLevelDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The custom ratio is set in the content page after navigating to a new browsing context"
  );
  is(
    await getIframeDocumentDevicePixelRatioAtStartup(),
    CUSTOM_DPR,
    "The custom ratio was set in the remote iframe when it loaded after navigating to a new browsing context"
  );
  is(
    await getIframeDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The custom ratio is set in the remote iframe after navigating to a new browsing context"
  );

  info(
    "Create another commands instance and check that destroying it won't reset the ratio"
  );
  const otherCommands = await CommandsFactory.forTab(tab);
  const otherTargetConfigurationCommand =
    otherCommands.targetConfigurationCommand;
  const otherTargetCommand = otherCommands.targetCommand;
  await otherTargetCommand.startListening();

  // Let's update the configuration with this commands instance to make sure we hit the TargetConfigurationActor
  await otherTargetConfigurationCommand.updateConfiguration({
    colorSchemeSimulation: "dark",
  });

  otherTargetCommand.destroy();
  await otherCommands.destroy();

  is(
    await getTopLevelDocumentDevicePixelRatio(),
    CUSTOM_DPR,
    "The custom ratio is still set on the page after destroying another commands instance"
  );

  info(
    "Check that destroying the commands we overrode the ratio in will reset the page ratio"
  );
  targetCommand.destroy();
  await commands.destroy();

  is(
    await getTopLevelDocumentDevicePixelRatio(),
    originalDpr,
    "The ratio was reset in the content page after destroying the commands"
  );
  is(
    await getIframeDocumentDevicePixelRatio(),
    originalDpr,
    "The ratio was reset in the remote iframe after destroying the commands"
  );
});

function getDevicePixelRatio(browserOrBrowsingContext) {
  return SpecialPowers.spawn(
    browserOrBrowsingContext,
    [],
    () => content.browsingContext.top.overrideDPPX || content.devicePixelRatio
  );
}

function getDevicePixelRatioAtStartup(browserOrBrowsingContext) {
  return SpecialPowers.spawn(
    browserOrBrowsingContext,
    [],
    () => content.wrappedJSObject.initialDevicePixelRatio
  );
}

function getTopLevelDocumentDevicePixelRatio() {
  return getDevicePixelRatio(gBrowser.selectedBrowser);
}

function getTopLevelDocumentDevicePixelRatioAtStartup() {
  return getDevicePixelRatioAtStartup(gBrowser.selectedBrowser);
}

function getIframeBrowsingContext() {
  return SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    () => content.document.querySelector("iframe").browsingContext
  );
}

async function getIframeDocumentDevicePixelRatio() {
  const iframeBC = await getIframeBrowsingContext();
  return getDevicePixelRatio(iframeBC);
}

async function getIframeDocumentDevicePixelRatioAtStartup() {
  const iframeBC = await getIframeBrowsingContext();
  return getDevicePixelRatioAtStartup(iframeBC);
}