tor-browser

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

browser_device_state_restore.js (4863B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the previous selected device is restored when reopening RDM.
      7 
      8 const TEST_URL = "data:text/html;charset=utf-8,";
      9 const DEFAULT_DPPX = window.devicePixelRatio;
     10 
     11 /* eslint-disable max-len */
     12 const TEST_DEVICE = {
     13  name: "iPhone 6/7/8",
     14  width: 375,
     15  height: 667,
     16  pixelRatio: 2,
     17  userAgent:
     18    "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
     19  touch: true,
     20  firefoxOS: false,
     21  os: "iOS",
     22  featured: true,
     23 };
     24 /* eslint-enable max-len */
     25 
     26 // Add the device to the list
     27 const {
     28  updatePreferredDevices,
     29 } = require("resource://devtools/client/responsive/actions/devices.js");
     30 updatePreferredDevices({
     31  added: [TEST_DEVICE.name],
     32  removed: [],
     33 });
     34 
     35 const Types = require("resource://devtools/client/responsive/types.js");
     36 
     37 addRDMTask(
     38  TEST_URL,
     39  async function ({ ui }) {
     40    const { store } = ui.toolWindow;
     41 
     42    reloadOnUAChange(true);
     43 
     44    // Wait until the viewport has been added and the device list has been loaded
     45    await waitUntilState(
     46      store,
     47      state =>
     48        state.viewports.length == 1 &&
     49        state.devices.listState == Types.loadableState.LOADED
     50    );
     51 
     52    info("Checking the default RDM state.");
     53    testViewportDeviceMenuLabel(ui, "Responsive");
     54    testViewportDimensions(ui, 320, 480);
     55    await testUserAgent(ui, DEFAULT_UA);
     56    await testDevicePixelRatio(ui, DEFAULT_DPPX);
     57    testTouchEventsOverride(ui, false);
     58 
     59    info("Select a device");
     60    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
     61    await selectDevice(ui, TEST_DEVICE.name);
     62    await waitForReload();
     63    await waitForViewportResizeTo(ui, TEST_DEVICE.width, TEST_DEVICE.height);
     64 
     65    info("Checking the RDM device state.");
     66    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
     67    await testUserAgent(ui, TEST_DEVICE.userAgent);
     68    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
     69    testTouchEventsOverride(ui, TEST_DEVICE.touch);
     70 
     71    reloadOnUAChange(false);
     72  },
     73  { waitForDeviceList: true }
     74 );
     75 
     76 addRDMTaskWithPreAndPost(
     77  TEST_URL,
     78  function rdmPreTask() {
     79    reloadOnUAChange(true);
     80  },
     81  async function ({ ui }) {
     82    // Note: This code might be racy. Call watchForDevToolsReload as early as
     83    // possible to catch the reload that will happen on RDM startup.
     84    // We cannot easily call watchForDevToolsReload in the preTask because it
     85    // needs RDM to be already started. Otherwise it will not find any devtools
     86    // UI to wait for.
     87    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
     88 
     89    const { store } = ui.toolWindow;
     90 
     91    info(
     92      "Reopening RDM and checking that the previous device state is restored."
     93    );
     94 
     95    // Wait until the viewport has been added and the device list has been loaded
     96    await waitUntilState(
     97      store,
     98      state =>
     99        state.viewports.length == 1 &&
    100        state.viewports[0].device === TEST_DEVICE.name &&
    101        state.devices.listState == Types.loadableState.LOADED
    102    );
    103    await waitForViewportResizeTo(ui, TEST_DEVICE.width, TEST_DEVICE.height);
    104    await waitForReload();
    105 
    106    info("Checking the restored RDM state.");
    107    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
    108    testViewportDimensions(ui, TEST_DEVICE.width, TEST_DEVICE.height);
    109    await testUserAgent(ui, TEST_DEVICE.userAgent);
    110    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
    111    testTouchEventsOverride(ui, TEST_DEVICE.touch);
    112 
    113    info("Rotating the viewport.");
    114    rotateViewport(ui);
    115 
    116    reloadOnUAChange(false);
    117  },
    118  function rdmPostTask() {},
    119  { waitForDeviceList: true }
    120 );
    121 
    122 addRDMTask(
    123  TEST_URL,
    124  async function ({ ui }) {
    125    const { store } = ui.toolWindow;
    126 
    127    reloadOnUAChange(true);
    128 
    129    info(
    130      "Reopening RDM and checking that the previous device state is restored."
    131    );
    132 
    133    // Wait until the viewport has been added and the device list has been loaded
    134    await waitUntilState(
    135      store,
    136      state =>
    137        state.viewports.length == 1 &&
    138        state.viewports[0].device === TEST_DEVICE.name &&
    139        state.devices.listState == Types.loadableState.LOADED
    140    );
    141    await waitForViewportResizeTo(ui, TEST_DEVICE.height, TEST_DEVICE.width);
    142    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
    143    await waitForReload();
    144 
    145    info("Checking the restored RDM state.");
    146    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
    147    testViewportDimensions(ui, TEST_DEVICE.height, TEST_DEVICE.width);
    148    await testUserAgent(ui, TEST_DEVICE.userAgent);
    149    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
    150    testTouchEventsOverride(ui, TEST_DEVICE.touch);
    151 
    152    reloadOnUAChange(false);
    153  },
    154  { waitForDeviceList: true }
    155 );