tor-browser

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

browser_user_agent_input.js (4045B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TEST_URL = "data:text/html;charset=utf-8,";
      7 const NEW_USER_AGENT = "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0";
      8 
      9 const testDevice = {
     10  name: "Fake Phone RDM Test",
     11  width: 320,
     12  height: 570,
     13  pixelRatio: 5.5,
     14  userAgent: "Mozilla/5.0 (Mobile; rv:39.0) Gecko/39.0 Firefox/39.0",
     15  touch: true,
     16  firefoxOS: true,
     17  os: "custom",
     18  featured: true,
     19 };
     20 
     21 // Add the new device to the list
     22 addDeviceForTest(testDevice);
     23 
     24 addRDMTask(TEST_URL, async function ({ ui }) {
     25  reloadOnUAChange(true);
     26 
     27  info("Check the default state of the user agent input");
     28  await testUserAgent(ui, DEFAULT_UA);
     29 
     30  info(`Change the user agent input to ${NEW_USER_AGENT} and press Escape`);
     31  await changeUserAgentInput(ui, NEW_USER_AGENT, "VK_ESCAPE");
     32  await testUserAgent(ui, DEFAULT_UA);
     33 
     34  info(`Change the user agent input to ${NEW_USER_AGENT}`);
     35  await changeUserAgentInput(ui, NEW_USER_AGENT);
     36  await testUserAgent(ui, NEW_USER_AGENT);
     37 
     38  info("Reset the user agent input back to the default UA");
     39  await changeUserAgentInput(ui, "", "VK_TAB");
     40  await testUserAgent(ui, DEFAULT_UA);
     41 
     42  info("Test selecting Fenix user agent");
     43  const firefoxVersion = AppConstants.MOZ_APP_VERSION.replace(/[ab]\d+/, "");
     44  await changeUserAgentFromSelector(
     45    ui,
     46    "Firefox for Android",
     47    `Mozilla/5.0 (Android 15; Mobile; rv:${firefoxVersion}) Gecko/${firefoxVersion} Firefox/${firefoxVersion}`
     48  );
     49 
     50  info(
     51    "Verify that device user agent isn't shown before the device is selected"
     52  );
     53  const { toolWindow } = ui;
     54  const { document } = toolWindow;
     55  const userAgentSelector = document.querySelector("#user-agent-selector");
     56  await testMenuItems(toolWindow, userAgentSelector, items => {
     57    const menuItem = findMenuItem(items, testDevice.name);
     58    ok(
     59      !menuItem,
     60      "Before selecting the device, it isn't shown in the user agent dropdown"
     61    );
     62  });
     63 
     64  info("Test selecting the selected device user agent");
     65  const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
     66  await selectDevice(ui, testDevice.name);
     67  await waitForReload();
     68  // We don't expect a reload here because the user agent is already set to the
     69  // device's user agent when the device is selected. Selecting the user agent
     70  // of the device won't trigger a reload here because the user agent isn't
     71  // changed.
     72  await changeUserAgentFromSelector(
     73    ui,
     74    testDevice.name,
     75    testDevice.userAgent,
     76    false
     77  );
     78 
     79  await testMenuItems(toolWindow, userAgentSelector, items => {
     80    const menuItem = findMenuItem(items, testDevice.name);
     81    ok(menuItem, "Found the item for the device's user agent");
     82    is(menuItem.getAttribute("aria-checked"), "true", "The item is checked");
     83  });
     84 
     85  info("Test selecting the default user agent");
     86  await changeUserAgentFromSelector(ui, "Firefox Desktop", "");
     87 
     88  info(
     89    "Test changing to a custom agent string, no user agent should be selected in the dropdrown menu"
     90  );
     91  await changeUserAgentInput(ui, "Custom user agent");
     92  await testMenuItems(toolWindow, userAgentSelector, items => {
     93    for (const menuItem of items) {
     94      is(
     95        menuItem.getAttribute("aria-checked"),
     96        null,
     97        "None of the user agent items are checked"
     98      );
     99    }
    100  });
    101 
    102  reloadOnUAChange(false);
    103 });
    104 
    105 async function changeUserAgentFromSelector(
    106  ui,
    107  browserName,
    108  expectedUserAgent,
    109  expectReload = true
    110 ) {
    111  const { document } = ui.toolWindow;
    112  const browser = ui.getViewportBrowser();
    113 
    114  const changed = once(ui, "user-agent-changed");
    115  let waitForDevToolsReload;
    116  if (expectReload) {
    117    waitForDevToolsReload = await watchForDevToolsReload(browser);
    118  } else {
    119    waitForDevToolsReload = async () => {};
    120  }
    121  await selectMenuItem(ui, "#user-agent-selector", browserName);
    122  await changed;
    123  await waitForDevToolsReload();
    124 
    125  const userAgentInput = document.getElementById("user-agent-input");
    126  is(userAgentInput.value, expectedUserAgent);
    127 }