tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_aboutdebugging_profiler_dialog.js (6969B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 Services.scriptloader.loadSubScript(
      7  "chrome://mochitests/content/browser/devtools/client/performance-new/test/browser/helpers.js",
      8  this
      9 );
     10 
     11 registerCleanupFunction(() => {
     12  const { revertRecordingSettings } = ChromeUtils.importESModule(
     13    "resource://devtools/shared/performance-new/prefs-presets.sys.mjs"
     14  );
     15  revertRecordingSettings();
     16 });
     17 
     18 const RUNTIME_ID = "1337id";
     19 const DEVICE_NAME = "Fancy Phone";
     20 const RUNTIME_NAME = "Lorem ipsum";
     21 
     22 /**
     23 * Test opening and closing the profiler dialog.
     24 */
     25 add_task(async function test_opening_profiler_dialog() {
     26  const { disconnect, mocks } = await connectToLocalFirefox({
     27    runtimeId: RUNTIME_ID,
     28    runtimeName: RUNTIME_NAME,
     29    deviceName: DEVICE_NAME,
     30  });
     31  const { document, tab, window } = await openAboutDebugging();
     32 
     33  mocks.emitUSBUpdate();
     34  await connectToRuntime(DEVICE_NAME, document);
     35  await waitForRuntimePage(RUNTIME_NAME, document);
     36 
     37  info("Open the profiler dialog");
     38  await openProfilerDialogWithRealClient(document);
     39  assertDialogVisible(document);
     40 
     41  info("Click on the close button and wait until the dialog disappears");
     42  const closeDialogButton = document.querySelector(".qa-profiler-dialog-close");
     43  closeDialogButton.click();
     44  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
     45  assertDialogHidden(document);
     46 
     47  info("Open the profiler dialog again");
     48  await openProfilerDialogWithRealClient(document);
     49  assertDialogVisible(document);
     50 
     51  info("Click on the mask element and wait until the dialog disappears");
     52  const mask = document.querySelector(".qa-profiler-dialog-mask");
     53  EventUtils.synthesizeMouse(mask, 5, 5, {}, window);
     54  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
     55  assertDialogHidden(document);
     56 
     57  info("Open the profiler dialog again");
     58  await openProfilerDialogWithRealClient(document);
     59  assertDialogVisible(document);
     60 
     61  info("Navigate to this-firefox and wait until the dialog disappears");
     62  document.location.hash = "#/runtime/this-firefox";
     63  await waitUntil(() => !document.querySelector(".qa-profiler-dialog"));
     64  assertDialogHidden(document);
     65 
     66  info("Select the remote runtime again, check the dialog is still hidden");
     67  await selectRuntime(DEVICE_NAME, RUNTIME_NAME, document);
     68  assertDialogHidden(document);
     69 
     70  await disconnect(document);
     71  await removeTab(tab);
     72 });
     73 
     74 add_task(async function test_set_profiler_settings() {
     75  const { disconnect, mocks } = await connectToLocalFirefox({
     76    runtimeId: RUNTIME_ID,
     77    runtimeName: RUNTIME_NAME,
     78    deviceName: DEVICE_NAME,
     79  });
     80  const { document, tab } = await openAboutDebugging();
     81 
     82  mocks.emitUSBUpdate();
     83  await connectToRuntime(DEVICE_NAME, document);
     84  await waitForRuntimePage(RUNTIME_NAME, document);
     85 
     86  info("Open the profiler dialog");
     87  await openProfilerDialogWithRealClient(document);
     88  assertDialogVisible(document);
     89 
     90  const profilerSettingsDocument = await openProfilerSettings(document);
     91  const radioButtonForCustomPreset = await getNearestInputFromText(
     92    profilerSettingsDocument,
     93    "Custom"
     94  );
     95  ok(
     96    radioButtonForCustomPreset.checked,
     97    "The radio button for the preset 'custom' is checked."
     98  );
     99 
    100  info("Change the preset to Graphics.");
    101  const radioButtonForGraphicsPreset = await getNearestInputFromText(
    102    profilerSettingsDocument,
    103    "Graphics"
    104  );
    105  radioButtonForGraphicsPreset.click();
    106 
    107  const profilerDocument = await saveSettingsAndGoBack(document);
    108  const perfPresetsSelect = await getNearestInputFromText(
    109    profilerDocument,
    110    "Settings"
    111  );
    112  is(
    113    perfPresetsSelect.value,
    114    "graphics",
    115    "The preset has been changed in the devtools panel UI as well."
    116  );
    117 
    118  await disconnect(document);
    119  await removeTab(tab);
    120 });
    121 
    122 function assertDialogVisible(doc) {
    123  ok(doc.querySelector(".qa-profiler-dialog"), "Dialog is displayed");
    124  ok(doc.querySelector(".qa-profiler-dialog-mask"), "Dialog mask is displayed");
    125 }
    126 
    127 function assertDialogHidden(doc) {
    128  ok(!doc.querySelector(".qa-profiler-dialog"), "Dialog is removed");
    129  ok(!doc.querySelector(".qa-profiler-dialog-mask"), "Dialog mask is removed");
    130 }
    131 
    132 /**
    133 * Retrieve the iframe containing the profiler UIs.
    134 * Be careful as it's completely replaced when switching UIs.
    135 */
    136 function getProfilerIframe(doc) {
    137  return doc.querySelector(".profiler-dialog__frame");
    138 }
    139 
    140 /**
    141 * This waits for the full render of the UI inside the profiler iframe, and
    142 * returns the content document object.
    143 */
    144 async function waitForProfilerUiRendering(doc, selector) {
    145  // The iframe is replaced completely, so we need to retrieve a new reference
    146  // each time.
    147  const profilerIframe = getProfilerIframe(doc);
    148  // Wait for the settings to render.
    149  await TestUtils.waitForCondition(
    150    () =>
    151      profilerIframe.contentDocument &&
    152      profilerIframe.contentDocument.querySelector(selector)
    153  );
    154 
    155  return profilerIframe.contentDocument;
    156 }
    157 
    158 /**
    159 * Open the performance profiler dialog with a real client.
    160 */
    161 async function openProfilerDialogWithRealClient(doc) {
    162  info("Click on the Profile Runtime button");
    163  const profileButton = doc.querySelector(".qa-profile-runtime-button");
    164  profileButton.click();
    165 
    166  info("Wait for the rendering of the profiler UI");
    167  const contentDocument = await waitForProfilerUiRendering(
    168    doc,
    169    ".perf-presets"
    170  );
    171  await getActiveButtonFromText(contentDocument, "Start recording");
    172  info("The profiler UI is rendered!");
    173  return contentDocument;
    174 }
    175 
    176 /**
    177 * Open the performance profiler settings. This assumes the profiler dialog is
    178 * already open by the previous function openProfilerDialog.
    179 */
    180 async function openProfilerSettings(doc) {
    181  const profilerDocument = getProfilerIframe(doc).contentDocument;
    182 
    183  // Select the custom preset.
    184  const perfPresetsSelect = await getNearestInputFromText(
    185    profilerDocument,
    186    "Settings"
    187  );
    188  setReactFriendlyInputValue(perfPresetsSelect, "custom");
    189 
    190  // Click on "Edit Settings".
    191  const editSettingsLink = await getElementFromDocumentByText(
    192    profilerDocument,
    193    "Edit Settings"
    194  );
    195  editSettingsLink.click();
    196 
    197  info("Wait for the rendering of the profiler settings UI");
    198  const contentDocument = await waitForProfilerUiRendering(
    199    doc,
    200    ".perf-aboutprofiling-remote"
    201  );
    202  info("The profiler settings UI is rendered!");
    203  return contentDocument;
    204 }
    205 
    206 async function saveSettingsAndGoBack(doc) {
    207  const profilerDocument = getProfilerIframe(doc).contentDocument;
    208 
    209  const saveSettingsAndGoBackButton = await getActiveButtonFromText(
    210    profilerDocument,
    211    "Save settings"
    212  );
    213  saveSettingsAndGoBackButton.click();
    214 
    215  info("Wait for the rendering of the profiler UI");
    216  const contentDocument = await waitForProfilerUiRendering(
    217    doc,
    218    ".perf-presets"
    219  );
    220  await getActiveButtonFromText(contentDocument, "Start recording");
    221  info("The profiler UI is rendered!");
    222  return contentDocument;
    223 }