summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/test/browser/browser_device_state_restore.js
blob: f8778795c2e628bddf80883194d503b466e86764 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that the previous selected device is restored when reopening RDM.

const TEST_URL = "data:text/html;charset=utf-8,";
const DEFAULT_DPPX = window.devicePixelRatio;

/* eslint-disable max-len */
const TEST_DEVICE = {
  name: "iPhone 6/7/8",
  width: 375,
  height: 667,
  pixelRatio: 2,
  userAgent:
    "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",
  touch: true,
  firefoxOS: false,
  os: "iOS",
  featured: true,
};
/* eslint-enable max-len */

// Add the device to the list
const {
  updatePreferredDevices,
} = require("resource://devtools/client/responsive/actions/devices.js");
updatePreferredDevices({
  added: [TEST_DEVICE.name],
  removed: [],
});

const Types = require("resource://devtools/client/responsive/types.js");

addRDMTask(
  TEST_URL,
  async function ({ ui }) {
    const { store } = ui.toolWindow;

    reloadOnUAChange(true);

    // Wait until the viewport has been added and the device list has been loaded
    await waitUntilState(
      store,
      state =>
        state.viewports.length == 1 &&
        state.devices.listState == Types.loadableState.LOADED
    );

    info("Checking the default RDM state.");
    testViewportDeviceMenuLabel(ui, "Responsive");
    testViewportDimensions(ui, 320, 480);
    await testUserAgent(ui, DEFAULT_UA);
    await testDevicePixelRatio(ui, DEFAULT_DPPX);
    await testTouchEventsOverride(ui, false);

    info("Select a device");
    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
    await selectDevice(ui, TEST_DEVICE.name);
    await waitForReload();
    await waitForViewportResizeTo(ui, TEST_DEVICE.width, TEST_DEVICE.height);

    info("Checking the RDM device state.");
    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
    await testUserAgent(ui, TEST_DEVICE.userAgent);
    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
    await testTouchEventsOverride(ui, TEST_DEVICE.touch);

    reloadOnUAChange(false);
  },
  { waitForDeviceList: true }
);

addRDMTaskWithPreAndPost(
  TEST_URL,
  function rdmPreTask({ browser }) {
    reloadOnUAChange(true);
  },
  async function ({ ui }) {
    // Note: This code might be racy. Call watchForDevToolsReload as early as
    // possible to catch the reload that will happen on RDM startup.
    // We cannot easily call watchForDevToolsReload in the preTask because it
    // needs RDM to be already started. Otherwise it will not find any devtools
    // UI to wait for.
    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());

    const { store } = ui.toolWindow;

    info(
      "Reopening RDM and checking that the previous device state is restored."
    );

    // Wait until the viewport has been added and the device list has been loaded
    await waitUntilState(
      store,
      state =>
        state.viewports.length == 1 &&
        state.viewports[0].device === TEST_DEVICE.name &&
        state.devices.listState == Types.loadableState.LOADED
    );
    await waitForViewportResizeTo(ui, TEST_DEVICE.width, TEST_DEVICE.height);
    await waitForReload();

    info("Checking the restored RDM state.");
    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
    testViewportDimensions(ui, TEST_DEVICE.width, TEST_DEVICE.height);
    await testUserAgent(ui, TEST_DEVICE.userAgent);
    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
    await testTouchEventsOverride(ui, TEST_DEVICE.touch);

    info("Rotating the viewport.");
    rotateViewport(ui);

    reloadOnUAChange(false);
  },
  function rdmPostTask({ browser }) {},
  { waitForDeviceList: true }
);

addRDMTask(
  TEST_URL,
  async function ({ ui }) {
    const { store } = ui.toolWindow;

    reloadOnUAChange(true);

    info(
      "Reopening RDM and checking that the previous device state is restored."
    );

    // Wait until the viewport has been added and the device list has been loaded
    await waitUntilState(
      store,
      state =>
        state.viewports.length == 1 &&
        state.viewports[0].device === TEST_DEVICE.name &&
        state.devices.listState == Types.loadableState.LOADED
    );
    await waitForViewportResizeTo(ui, TEST_DEVICE.height, TEST_DEVICE.width);
    const waitForReload = await watchForDevToolsReload(ui.getViewportBrowser());
    await waitForReload();

    info("Checking the restored RDM state.");
    testViewportDeviceMenuLabel(ui, TEST_DEVICE.name);
    testViewportDimensions(ui, TEST_DEVICE.height, TEST_DEVICE.width);
    await testUserAgent(ui, TEST_DEVICE.userAgent);
    await testDevicePixelRatio(ui, TEST_DEVICE.pixelRatio);
    await testTouchEventsOverride(ui, TEST_DEVICE.touch);

    reloadOnUAChange(false);
  },
  { waitForDeviceList: true }
);