summaryrefslogtreecommitdiffstats
path: root/devtools/client/webide/test/test_simulators.html
blob: 204881512dcef7eb8214c9fe5daa1d9642624486 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<!DOCTYPE html>

<html>

  <head>
    <meta charset="utf8">
    <title></title>

    <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    <script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js"></script>
    <script type="application/javascript;version=1.8" src="head.js"></script>
    <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  </head>

  <body>

    <script type="application/javascript;version=1.8">
      window.onload = function() {
        SimpleTest.waitForExplicitFinish();

        const asyncStorage = require("devtools/shared/async-storage");
        const EventEmitter = require("devtools/shared/event-emitter");
        const { GetAvailableAddons } = require("devtools/client/webide/modules/addons");
        const { getDevices } = require("devtools/client/shared/devices");
        const { Simulator, Simulators } = require("devtools/client/webide/modules/simulators");
        const { AddonSimulatorProcess,
                OldAddonSimulatorProcess,
                CustomSimulatorProcess } = require("devtools/client/webide/modules/simulator-process");

        function addonStatus(addon, status) {
          if (addon.status == status) {
            return promise.resolve();
          }
          let deferred = promise.defer();
          addon.on("update", function onUpdate() {
            if (addon.status == status) {
              addon.off("update", onUpdate);
              nextTick().then(() => deferred.resolve());
            }
          });
          return deferred.promise;
        }

        function waitForUpdate(length) {
          info(`Wait for update with length ${length}`);
          let deferred = promise.defer();
          let handler = (_, data) => {
            if (data.length != length) {
              return;
            }
            info(`Got update with length ${length}`);
            Simulators.off("updated", handler);
            deferred.resolve();
          };
          Simulators.on("updated", handler);
          return deferred.promise;
        }

        Task.spawn(function* () {
          let win = yield openWebIDE(false);

          yield Simulators._load();

          let docRuntime = getRuntimeDocument(win);
          let find = win.document.querySelector.bind(docRuntime);
          let findAll = win.document.querySelectorAll.bind(docRuntime);

          let simulatorList = find("#runtime-panel-simulator");
          let simulatorPanel = win.document.querySelector("#deck-panel-simulator");

          // Hack SimulatorProcesses to spy on simulation parameters.

          let runPromise;
          function fakeRun() {
            runPromise.resolve({
              path: this.b2gBinary.path,
              args: this.args
            });
            // Don't actually try to connect to the fake simulator.
            throw new Error("Aborting on purpose before connection.");
          }

          AddonSimulatorProcess.prototype.run = fakeRun;
          OldAddonSimulatorProcess.prototype.run = fakeRun;
          CustomSimulatorProcess.prototype.run = fakeRun;

          function runSimulator(i) {
            runPromise = promise.defer();
            findAll(".runtime-panel-item-simulator")[i].click();
            return runPromise.promise;
          }

          // Install fake "Firefox OS 1.0" simulator addon.

          let addons = yield GetAvailableAddons();

          let sim10 = addons.simulators.filter(a => a.version == "1.0")[0];

          sim10.install();

          let updated = waitForUpdate(1);
          yield addonStatus(sim10, "installed");
          yield updated;
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          is(findAll(".runtime-panel-item-simulator").length, 1, "One simulator in runtime panel");

          // Install fake "Firefox OS 2.0" simulator addon.

          let sim20 = addons.simulators.filter(a => a.version == "2.0")[0];

          sim20.install();

          updated = waitForUpdate(2);
          yield addonStatus(sim20, "installed");
          yield updated;
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          is(findAll(".runtime-panel-item-simulator").length, 2, "Two simulators in runtime panel");

          // Dry run a simulator to verify that its parameters look right.

          let params = yield runSimulator(0);

          ok(params.path.includes(sim10.addonID) && params.path.includes("b2g-bin"), "Simulator binary path looks right");

          let pid = params.args.indexOf("-profile");
          ok(pid > -1, "Simulator process arguments have --profile");

          let profilePath = params.args[pid + 1];
          ok(profilePath.includes(sim10.addonID) && profilePath.includes("profile"), "Simulator profile path looks right");

          ok(params.args.indexOf("-dbgport") > -1 || params.args.indexOf("-start-debugger-server") > -1, "Simulator process arguments have a debugger port");

          ok(params.args.indexOf("-no-remote") > -1, "Simulator process arguments have --no-remote");

          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          // Configure the fake 1.0 simulator.

          simulatorList.querySelectorAll(".configure-button")[0].click();
          is(win.document.querySelector("#deck").selectedPanel, simulatorPanel, "Simulator deck panel is selected");

          yield lazyIframeIsLoaded(simulatorPanel);

          let doc = simulatorPanel.contentWindow.document;
          let form = doc.querySelector("#simulator-editor");

          let formReady = new Promise((resolve, reject) => {
            form.addEventListener("change", () => {
              resolve();
            });
          });

          let change = doc.createEvent("HTMLEvents");
          change.initEvent("change", true, true);

          function set(input, value) {
            input.value = value;
            input.dispatchEvent(change);
            return nextTick();
          }

          let MockFilePicker = SpecialPowers.MockFilePicker;
          MockFilePicker.init(simulatorPanel.contentWindow);

          yield formReady;

          // Test `name`.

          is(form.name.value, find(".runtime-panel-item-simulator").textContent, "Original simulator name");

          let customName = "CustomFox ";
          yield set(form.name, customName + "1.0");

          is(find(".runtime-panel-item-simulator").textContent, form.name.value, "Updated simulator name");

          // Test `version`.

          is(form.version.value, sim10.addonID, "Original simulator version");
          ok(!form.version.classList.contains("custom"), "Version selector is not customized");

          yield set(form.version, sim20.addonID);

          ok(!form.version.classList.contains("custom"), "Version selector is not customized after addon change");
          is(form.name.value, customName + "2.0", "Simulator name was updated to new version");

          // Pick custom binary, but act like the user aborted the file picker.

          MockFilePicker.returnFiles = [];
          yield set(form.version, "pick");

          is(form.version.value, sim20.addonID, "Version selector reverted to last valid choice after customization abort");
          ok(!form.version.classList.contains("custom"), "Version selector is not customized after customization abort");

          // Pick custom binary, and actually follow through. (success, verify value = "custom" and textContent = custom path)

          MockFilePicker.useAnyFile();
          yield set(form.version, "pick");

          let fakeBinary = MockFilePicker.returnFiles[0];

          ok(form.version.value == "custom", "Version selector was set to a new custom binary");
          ok(form.version.classList.contains("custom"), "Version selector is now customized");
          is(form.version.selectedOptions[0].textContent, fakeBinary.path, "Custom option textContent is correct");

          yield set(form.version, sim10.addonID);

          ok(form.version.classList.contains("custom"), "Version selector remains customized after change back to addon");
          is(form.name.value, customName + "1.0", "Simulator name was updated to new version");

          yield set(form.version, "custom");

          ok(form.version.value == "custom", "Version selector is back to custom");

          // Test `profile`.

          is(form.profile.value, "default", "Default simulator profile");
          ok(!form.profile.classList.contains("custom"), "Profile selector is not customized");

          MockFilePicker.returnFiles = [];
          yield set(form.profile, "pick");

          is(form.profile.value, "default", "Profile selector reverted to last valid choice after customization abort");
          ok(!form.profile.classList.contains("custom"), "Profile selector is not customized after customization abort");

          let fakeProfile = FileUtils.getDir("TmpD", []);

          MockFilePicker.returnFiles = [ fakeProfile ];
          yield set(form.profile, "pick");

          ok(form.profile.value == "custom", "Profile selector was set to a new custom directory");
          ok(form.profile.classList.contains("custom"), "Profile selector is now customized");
          is(form.profile.selectedOptions[0].textContent, fakeProfile.path, "Custom option textContent is correct");

          yield set(form.profile, "default");

          is(form.profile.value, "default", "Profile selector back to default");
          ok(form.profile.classList.contains("custom"), "Profile selector remains customized after change back to default");

          yield set(form.profile, "custom");

          is(form.profile.value, "custom", "Profile selector back to custom");

          params = yield runSimulator(0);

          is(params.path, fakeBinary.path, "Simulator process uses custom binary path");

          pid = params.args.indexOf("-profile");
          is(params.args[pid + 1], fakeProfile.path, "Simulator process uses custom profile directory");

          yield set(form.version, sim10.addonID);

          is(form.name.value, customName + "1.0", "Simulator restored to 1.0");

          params = yield runSimulator(0);

          pid = params.args.indexOf("-profile");
          is(params.args[pid + 1], fakeProfile.path, "Simulator process still uses custom profile directory");

          yield set(form.version, "custom");

          // Test `device`.

          let defaults = Simulator.prototype._defaults;

          for (let param in defaults.phone) {
            is(form[param].value, String(defaults.phone[param]), "Default phone value for device " + param);
          }

          let width = 5000, height = 4000;
          yield set(form.width, width);
          yield set(form.height, height);

          is(form.device.value, "custom", "Device selector is custom");

          params = yield runSimulator(0);

          let sid = params.args.indexOf("-screen");
          ok(sid > -1, "Simulator process arguments have --screen");
          ok(params.args[sid + 1].includes(width + "x" + height), "Simulator screen resolution looks right");

          yield set(form.version, sim10.addonID);

          // Configure the fake 2.0 simulator.

          simulatorList.querySelectorAll(".configure-button")[1].click();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          // Test `name`.

          is(form.name.value, findAll(".runtime-panel-item-simulator")[1].textContent, "Original simulator name");

          yield set(form.name, customName + "2.0");

          is(findAll(".runtime-panel-item-simulator")[1].textContent, form.name.value, "Updated simulator name");

          yield set(form.version, sim10.addonID);

          ok(form.name.value !== customName + "1.0", "Conflicting simulator name was deduplicated");

          is(form.name.value, findAll(".runtime-panel-item-simulator")[1].textContent, "Deduplicated simulator name stayed consistent");

          yield set(form.version, sim20.addonID);

          is(form.name.value, customName + "2.0", "Name deduplication was undone when possible");

          // Test `device`.

          for (let param in defaults.phone) {
            is(form[param].value, String(defaults.phone[param]), "Default phone value for device " + param);
          }

          let devices = yield getDevices();
          devices = devices[devices.TYPES[0]];
          let device = devices[devices.length - 1];

          yield set(form.device, device.name);

          is(form.device.value, device.name, "Device selector was changed");
          is(form.width.value, String(device.width), "New device width is correct");
          is(form.height.value, String(device.height), "New device height is correct");

          params = yield runSimulator(1);

          sid = params.args.indexOf("-screen");
          ok(params.args[sid + 1].includes(device.width + "x" + device.height), "Simulator screen resolution looks right");

          // Test Simulator Menu.
          is(doc.querySelector("#tv_simulator_menu").style.visibility, "hidden", "OpenTVDummyDirectory Button is not hidden");

          // Restore default simulator options.

          doc.querySelector("#reset").click();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          for (let param in defaults.phone) {
            is(form[param].value, String(defaults.phone[param]), "Default phone value for device " + param);
          }

          // Install and configure the fake "Firefox OS 3.0 TV" simulator addon.

          let sim30tv = addons.simulators.filter(a => a.version == "3.0_tv")[0];

          sim30tv.install();

          updated = waitForUpdate(3);
          yield addonStatus(sim30tv, "installed");
          yield updated;
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          is(findAll(".runtime-panel-item-simulator").length, 3, "Three simulators in runtime panel");

          simulatorList.querySelectorAll(".configure-button")[2].click();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          for (let param in defaults.television) {
            is(form[param].value, String(defaults.television[param]), "Default TV value for device " + param);
          }

          // Test Simulator Menu
          is(doc.querySelector("#tv_simulator_menu").style.visibility, "visible", "OpenTVDummyDirectory Button is not visible");

          // Force reload the list of simulators.

          Simulators._loadingPromise = null;
          Simulators._simulators = [];
          yield Simulators._load();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          is(findAll(".runtime-panel-item-simulator").length, 3, "Three simulators saved and reloaded " + Simulators._simulators.map(s => s.name).join(','));

          // Uninstall the 3.0 TV and 2.0 addons, and watch their Simulator objects disappear.

          sim30tv.uninstall();

          yield addonStatus(sim30tv, "uninstalled");

          is(findAll(".runtime-panel-item-simulator").length, 2, "Two simulators left in runtime panel");

          sim20.uninstall();

          yield addonStatus(sim20, "uninstalled");

          is(findAll(".runtime-panel-item-simulator").length, 1, "One simulator left in runtime panel");

          // Remove 1.0 simulator.

          simulatorList.querySelectorAll(".configure-button")[0].click();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          doc.querySelector("#remove").click();
          // Wait for next tick to ensure UI elements are updated
          yield nextTick();

          is(findAll(".runtime-panel-item-simulator").length, 0, "Last simulator was removed");

          yield asyncStorage.removeItem("simulators");

          sim10.uninstall();

          MockFilePicker.cleanup();

          doc.querySelector("#close").click();

          ok(!win.document.querySelector("#deck").selectedPanel, "No panel selected");

          yield closeWebIDE(win);

          SimpleTest.finish();

        });
      }

    </script>
  </body>
</html>