devtools/client/webide/test/test_simulators.html
author Wes Kocher <wkocher@mozilla.com>
Thu, 02 Mar 2017 14:57:21 -0800
changeset 392283 63c4e77cfe90e90d35d85e1a4821015823c4ecee
parent 392275 429ff39f3d28b195fb5ee1467cd8126bab3d89b8
child 392446 e769531b24333372cf6e28c6c495abb069136060
permissions -rw-r--r--
Backed out 2 changesets (bug 1334975, bug 1335539) for merge conflicts a=backout Backed out changeset 429ff39f3d28 (bug 1335539) Backed out changeset eea959a93ce4 (bug 1334975) MozReview-Commit-ID: GlvA0B0vHRT

<!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" src="head.js"></script>
    <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  </head>

  <body>

    <script type="application/javascript">
      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>