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

"use strict";

const RUNTIME_ID = "test-runtime-id";
const RUNTIME_NAME = "test runtime name";
const RUNTIME_DEVICE_NAME = "test device name";
const RUNTIME_SHORT_NAME = "test short name";

const CONNECTION_TIMING_OUT_DELAY = 1000;
const CONNECTION_CANCEL_DELAY = 2000;

// Test following connection state tests.
// * Connect button label and state will change during connecting.
// * Show error message if connection failed.
// * Show warninng if connection has been taken time.
add_task(async function () {
  await setupPreferences();

  const mocks = new Mocks();

  const { document, tab } = await openAboutDebugging();

  mocks.createUSBRuntime(RUNTIME_ID, {
    name: RUNTIME_NAME,
    deviceName: RUNTIME_DEVICE_NAME,
    shortName: RUNTIME_SHORT_NAME,
  });
  mocks.emitUSBUpdate();

  info("Wait until the USB sidebar item appears");
  await waitUntil(() => findSidebarItemByText(RUNTIME_DEVICE_NAME, document));
  const usbRuntimeSidebarItem = findSidebarItemByText(
    RUNTIME_DEVICE_NAME,
    document
  );
  const connectButton =
    usbRuntimeSidebarItem.querySelector(".qa-connect-button");

  info("Simulate to happen connection error");
  mocks.runtimeClientFactoryMock.createClientForRuntime = async () => {
    throw new Error("Dummy connection error");
  };

  info(
    "Check whether the error message displayed after clicking connect button"
  );
  connectButton.click();
  await waitUntil(() => document.querySelector(".qa-connection-error"));
  ok(true, "Error message displays when connection failed");

  info("Simulate to wait for the connection prompt on remote runtime");
  let resumeConnection;
  const resumeConnectionPromise = new Promise(r => {
    resumeConnection = r;
  });
  mocks.runtimeClientFactoryMock.createClientForRuntime = async runtime => {
    await resumeConnectionPromise;
    return mocks._clients[runtime.type][runtime.id];
  };

  info("Click on the connect button and wait until it disappears");
  connectButton.click();
  info("Check whether a warning of connection not responding displays");
  await waitUntil(() =>
    document.querySelector(".qa-connection-not-responding")
  );
  ok(
    document.querySelector(".qa-connection-not-responding"),
    "A warning of connection not responding displays"
  );
  ok(connectButton.disabled, "Connect button is disabled");
  ok(
    connectButton.textContent.startsWith("Connecting"),
    "Label of the connect button changes"
  );
  ok(
    !document.querySelector(".qa-connection-error"),
    "Error message disappears"
  );

  info(
    "Unblock the connection and check the message and connect button disappear"
  );
  resumeConnection();
  await waitUntil(
    () => !usbRuntimeSidebarItem.querySelector(".qa-connect-button")
  );
  ok(!document.querySelector(".qa-connection-error"), "Error disappears");
  ok(
    !document.querySelector(".qa-connection-not-responding"),
    "Warning disappears"
  );

  info("Remove a USB runtime");
  mocks.removeUSBRuntime(RUNTIME_ID);
  mocks.emitUSBUpdate();
  await waitUntilUsbDeviceIsUnplugged(RUNTIME_DEVICE_NAME, document);

  await removeTab(tab);
});

// Test whether the status of all will be reverted after a certain period of time during
// waiting connection.
add_task(async function () {
  await setupPreferences();

  const mocks = new Mocks();

  const { document, tab } = await openAboutDebugging();

  mocks.createUSBRuntime(RUNTIME_ID, {
    name: RUNTIME_NAME,
    deviceName: RUNTIME_DEVICE_NAME,
    shortName: RUNTIME_SHORT_NAME,
  });
  mocks.emitUSBUpdate();

  info("Wait until the USB sidebar item appears");
  await waitUntil(() => findSidebarItemByText(RUNTIME_DEVICE_NAME, document));
  const usbRuntimeSidebarItem = findSidebarItemByText(
    RUNTIME_DEVICE_NAME,
    document
  );
  const connectButton =
    usbRuntimeSidebarItem.querySelector(".qa-connect-button");

  let resumeConnection;
  const resumeConnectionPromise = new Promise(r => {
    resumeConnection = r;
  });
  mocks.runtimeClientFactoryMock.createClientForRuntime = async runtime => {
    await resumeConnectionPromise;
    return mocks._clients[runtime.type][runtime.id];
  };

  info("Click on the connect button and wait until it disappears");
  connectButton.click();
  await waitUntil(() =>
    document.querySelector(".qa-connection-not-responding")
  );
  info("Check whether the all status will be reverted");
  await waitUntil(
    () => !document.querySelector(".qa-connection-not-responding")
  );
  ok(
    document.querySelector(".qa-connection-timeout"),
    "Connection timeout message displays"
  );
  ok(!connectButton.disabled, "Connect button is enabled");
  is(
    connectButton.textContent,
    "Connect",
    "Label of the connect button reverted"
  );
  ok(
    !document.querySelector(".qa-connection-error"),
    "Error message disappears"
  );

  info("Check whether the timeout message disappears");
  resumeConnection();
  await waitUntil(() => !document.querySelector(".qa-connection-timeout"));

  info("Remove a USB runtime");
  mocks.removeUSBRuntime(RUNTIME_ID);
  mocks.emitUSBUpdate();

  info("Wait until the USB sidebar item disappears");
  await waitUntilUsbDeviceIsUnplugged(RUNTIME_DEVICE_NAME, document);

  await removeTab(tab);
});

async function setupPreferences() {
  if (SpecialPowers.isDebugBuild) {
    // On debug builds, reducing the timings might lead to skip the "warning"
    // state and will block the test execution.
    // Do not change the timings in debug builds.
    return;
  }

  await pushPref(
    "devtools.aboutdebugging.test-connection-timing-out-delay",
    CONNECTION_TIMING_OUT_DELAY
  );
  await pushPref(
    "devtools.aboutdebugging.test-connection-cancel-delay",
    CONNECTION_CANCEL_DELAY
  );
}