summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_profiler_dialog.js
blob: fd546bd854e74c5c0554f8735a40795f5dae322f (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/devtools/client/performance-new/test/browser/helpers.js",
  this
);

const BackgroundJSM = ChromeUtils.importESModule(
  "resource://devtools/client/performance-new/shared/background.sys.mjs"
);

registerCleanupFunction(() => {
  BackgroundJSM.revertRecordingSettings();
});

const RUNTIME_ID = "1337id";
const DEVICE_NAME = "Fancy Phone";
const RUNTIME_NAME = "Lorem ipsum";

/**
 * Test opening and closing the profiler dialog.
 */
add_task(async function test_opening_profiler_dialog() {
  const { mocks } = await connectToLocalFirefox();
  const { document, tab, window } = await openAboutDebugging();

  mocks.emitUSBUpdate();
  await connectToRuntime(DEVICE_NAME, document);
  await selectRuntime(DEVICE_NAME, RUNTIME_NAME, document);

  info("Open the profiler dialog");
  await openProfilerDialogWithRealClient(document);
  assertDialogVisible(document);

  info("Click on the close button and wait until the dialog disappears");
  const closeDialogButton = document.querySelector(".qa-profiler-dialog-close");
  closeDialogButton.click();
  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
  assertDialogHidden(document);

  info("Open the profiler dialog again");
  await openProfilerDialogWithRealClient(document);
  assertDialogVisible(document);

  info("Click on the mask element and wait until the dialog disappears");
  const mask = document.querySelector(".qa-profiler-dialog-mask");
  EventUtils.synthesizeMouse(mask, 5, 5, {}, window);
  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
  assertDialogHidden(document);

  info("Open the profiler dialog again");
  await openProfilerDialogWithRealClient(document);
  assertDialogVisible(document);

  info("Navigate to this-firefox and wait until the dialog disappears");
  document.location.hash = "#/runtime/this-firefox";
  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
  assertDialogHidden(document);

  info("Select the remote runtime again, check the dialog is still hidden");
  await selectRuntime(DEVICE_NAME, RUNTIME_NAME, document);
  assertDialogHidden(document);

  await disconnectFromLocalFirefox({ mocks, doc: document });
  await removeTab(tab);
});

add_task(async function test_set_profiler_settings() {
  const { mocks } = await connectToLocalFirefox();
  const { document, tab } = await openAboutDebugging();

  mocks.emitUSBUpdate();
  await connectToRuntime(DEVICE_NAME, document);
  await selectRuntime(DEVICE_NAME, RUNTIME_NAME, document);

  info("Open the profiler dialog");
  await openProfilerDialogWithRealClient(document);
  assertDialogVisible(document);

  const profilerSettingsDocument = await openProfilerSettings(document);
  const radioButtonForCustomPreset = await getNearestInputFromText(
    profilerSettingsDocument,
    "Custom"
  );
  ok(
    radioButtonForCustomPreset.checked,
    "The radio button for the preset 'custom' is checked."
  );

  info("Change the preset to Graphics.");
  const radioButtonForGraphicsPreset = await getNearestInputFromText(
    profilerSettingsDocument,
    "Graphics"
  );
  radioButtonForGraphicsPreset.click();

  const profilerDocument = await saveSettingsAndGoBack(document);
  const perfPresetsSelect = await getNearestInputFromText(
    profilerDocument,
    "Settings"
  );
  is(
    perfPresetsSelect.value,
    "graphics",
    "The preset has been changed in the devtools panel UI as well."
  );

  await disconnectFromLocalFirefox({ mocks, doc: document });
  await removeTab(tab);
});

async function connectToLocalFirefox() {
  // This is a client to the current Firefox.
  const clientWrapper = await createLocalClientWrapper();

  // enable USB devices mocks
  const mocks = new Mocks();
  const usbClient = mocks.createUSBRuntime(RUNTIME_ID, {
    deviceName: DEVICE_NAME,
    name: RUNTIME_NAME,
    clientWrapper,
  });

  return { mocks, usbClient };
}

async function disconnectFromLocalFirefox({ doc, mocks }) {
  info("Remove USB runtime");
  mocks.removeUSBRuntime(RUNTIME_ID);
  mocks.emitUSBUpdate();
  await waitUntilUsbDeviceIsUnplugged(DEVICE_NAME, doc);
}

function assertDialogVisible(doc) {
  ok(doc.querySelector(".qa-profiler-dialog"), "Dialog is displayed");
  ok(doc.querySelector(".qa-profiler-dialog-mask"), "Dialog mask is displayed");
}

function assertDialogHidden(doc) {
  ok(!doc.querySelector(".qa-profiler-dialog"), "Dialog is removed");
  ok(!doc.querySelector(".qa-profiler-dialog-mask"), "Dialog mask is removed");
}

/**
 * Retrieve the iframe containing the profiler UIs.
 * Be careful as it's completely replaced when switching UIs.
 */
function getProfilerIframe(doc) {
  return doc.querySelector(".profiler-dialog__frame");
}

/**
 * This waits for the full render of the UI inside the profiler iframe, and
 * returns the content document object.
 */
async function waitForProfilerUiRendering(doc, selector) {
  // The iframe is replaced completely, so we need to retrieve a new reference
  // each time.
  const profilerIframe = getProfilerIframe(doc);
  // Wait for the settings to render.
  await TestUtils.waitForCondition(
    () =>
      profilerIframe.contentDocument &&
      profilerIframe.contentDocument.querySelector(selector)
  );

  return profilerIframe.contentDocument;
}

/**
 * Open the performance profiler dialog with a real client.
 */
async function openProfilerDialogWithRealClient(doc) {
  info("Click on the Profile Runtime button");
  const profileButton = doc.querySelector(".qa-profile-runtime-button");
  profileButton.click();

  info("Wait for the rendering of the profiler UI");
  const contentDocument = await waitForProfilerUiRendering(
    doc,
    ".perf-presets"
  );
  await getActiveButtonFromText(contentDocument, "Start recording");
  info("The profiler UI is rendered!");
  return contentDocument;
}

/**
 * Open the performance profiler settings. This assumes the profiler dialog is
 * already open by the previous function openProfilerDialog.
 */
async function openProfilerSettings(doc) {
  const profilerDocument = getProfilerIframe(doc).contentDocument;

  // Select the custom preset.
  const perfPresetsSelect = await getNearestInputFromText(
    profilerDocument,
    "Settings"
  );
  setReactFriendlyInputValue(perfPresetsSelect, "custom");

  // Click on "Edit Settings".
  const editSettingsLink = await getElementFromDocumentByText(
    profilerDocument,
    "Edit Settings"
  );
  editSettingsLink.click();

  info("Wait for the rendering of the profiler settings UI");
  const contentDocument = await waitForProfilerUiRendering(
    doc,
    ".perf-aboutprofiling-remote"
  );
  info("The profiler settings UI is rendered!");
  return contentDocument;
}

async function saveSettingsAndGoBack(doc) {
  const profilerDocument = getProfilerIframe(doc).contentDocument;

  const saveSettingsAndGoBackButton = await getActiveButtonFromText(
    profilerDocument,
    "Save settings"
  );
  saveSettingsAndGoBackButton.click();

  info("Wait for the rendering of the profiler UI");
  const contentDocument = await waitForProfilerUiRendering(
    doc,
    ".perf-presets"
  );
  await getActiveButtonFromText(contentDocument, "Start recording");
  info("The profiler UI is rendered!");
  return contentDocument;
}