Bug 1497917 - Add USB runtime mock and sidebar test for USB runtimes;r=ladybenko,daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 09 Nov 2018 09:34:25 +0000
changeset 445396 88ef91cd3bd6f0ad01653654b53b4a94c66949b5
parent 445395 d67038338e2f736261b745a3f7e0417a0cd54635
child 445397 0620a6320abfcb9177f601c40af8d89a0dc89b0d
push id35015
push userdluca@mozilla.com
push dateFri, 09 Nov 2018 17:45:20 +0000
treeherdermozilla-central@2f1158e5e0ce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersladybenko, daisuke
bugs1497917
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1497917 - Add USB runtime mock and sidebar test for USB runtimes;r=ladybenko,daisuke I want to go through a first round of review before moving forward. If we agree on the basic approach in this patch two things: - I want to complexify things by adding a test that connects to a runtime and shows the runtime page (to exercise more than this very simple use case) - I will probably separate the loader modification in a separate bug, will open a RFC to discuss it and will use a workaround for now in our code so that we are not blocked Overall I don't want to start adding too many features to this mocking solution, I would rather wait until the tests help us understand what fits best our needs. Differential Revision: https://phabricator.services.mozilla.com/D9801
devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
devtools/client/aboutdebugging-new/test/browser/browser.ini
devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_sidebar_usb_runtime.js
devtools/client/aboutdebugging-new/test/browser/mocks/head-usb-runtimes-mock.js
--- a/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
+++ b/devtools/client/aboutdebugging-new/src/modules/usb-runtimes.js
@@ -37,8 +37,10 @@ function removeUSBRuntimesObserver(liste
   adbScanner.off("runtime-list-updated", listener);
 }
 exports.removeUSBRuntimesObserver = removeUSBRuntimesObserver;
 
 function refreshUSBRuntimes() {
   return adbScanner.scan();
 }
 exports.refreshUSBRuntimes = refreshUSBRuntimes;
+
+require("./test-helper").enableMocks(module, "modules/usb-runtimes");
--- a/devtools/client/aboutdebugging-new/test/browser/browser.ini
+++ b/devtools/client/aboutdebugging-new/test/browser/browser.ini
@@ -1,25 +1,27 @@
 [DEFAULT]
 tags = devtools
 subsuite = devtools
 support-files =
   debug-target-pane_collapsibilities_head.js
   head-addons-script.js
   head.js
+  mocks/*
   resources/test-adb-extension/*
   resources/test-temporary-extension/*
   !/devtools/client/shared/test/shared-head.js
   !/devtools/client/shared/test/shared-redux-head.js
   !/devtools/client/shared/test/telemetry-test-helpers.js
 
 [browser_aboutdebugging_connect_networklocations.js]
 [browser_aboutdebugging_connect_toggle_usb_devices.js]
 skip-if = (os == 'linux' && bits == 32) # ADB start() fails on linux 32, see Bug 1499638
 [browser_aboutdebugging_connection_prompt_setting.js]
 [browser_aboutdebugging_debug-target-pane_collapsibilities_interaction.js]
 [browser_aboutdebugging_debug-target-pane_collapsibilities_preference.js]
 [browser_aboutdebugging_debug-target-pane_empty.js]
 [browser_aboutdebugging_navigate.js]
 [browser_aboutdebugging_sidebar_network_runtimes.js]
+[browser_aboutdebugging_sidebar_usb_runtime.js]
 [browser_aboutdebugging_sidebar_usb_status.js]
 skip-if = (os == 'linux' && bits == 32) # ADB start() fails on linux 32, see Bug 1499638
 [browser_aboutdebugging_thisfirefox.js]
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging-new/test/browser/browser_aboutdebugging_sidebar_usb_runtime.js
@@ -0,0 +1,50 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* import-globals-from mocks/head-usb-runtimes-mock.js */
+
+"use strict";
+
+// Load USB Runtimes mock module
+Services.scriptloader.loadSubScript(
+  CHROME_URL_ROOT + "mocks/head-usb-runtimes-mock.js", this);
+
+// Test that USB runtimes appear and disappear from the sidebar.
+add_task(async function() {
+  const usbRuntimesMock = createUsbRuntimesMock();
+  const observerMock = addObserverMock(usbRuntimesMock);
+  enableUsbRuntimesMock(usbRuntimesMock);
+
+  // Disable our mock when the test ends.
+  registerCleanupFunction(() => {
+    disableUsbRuntimesMock();
+  });
+
+  const { document, tab } = await openAboutDebugging();
+
+  usbRuntimesMock.getUSBRuntimes = function() {
+    return [{
+      id: "test_device_id",
+      _socketPath: "test/path",
+      deviceName: "test device name",
+      shortName: "testshort",
+    }];
+  };
+  observerMock.emit("runtime-list-updated");
+
+  info("Wait until the USB sidebar item appears");
+  await waitUntil(() => findSidebarItemByText("test device name", document));
+  const usbRuntimeSidebarItem = findSidebarItemByText("test device name", document);
+  ok(usbRuntimeSidebarItem.textContent.includes("testshort"),
+    "The short name of the usb runtime is visible");
+
+  usbRuntimesMock.getUSBRuntimes = function() {
+    return [];
+  };
+  observerMock.emit("runtime-list-updated");
+
+  info("Wait until the USB sidebar item disappears");
+  await waitUntil(() => !findSidebarItemByText("test device name", document));
+
+  await removeTab(tab);
+});
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging-new/test/browser/mocks/head-usb-runtimes-mock.js
@@ -0,0 +1,86 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+/* import-globals-from ../../../../shared/test/shared-head.js */
+
+"use strict";
+
+/**
+ * Setup the loader to return the provided mock object instead of the regular
+ * usb-runtimes module.
+ *
+ * @param {Object}
+ *        mock should implement the following methods:
+ *        - addUSBRuntimesObserver(listener)
+ *        - disableUSBRuntimes()
+ *        - enableUSBRuntimes()
+ *        - getUSBRuntimes()
+ *        - removeUSBRuntimesObserver(listener)
+ */
+function enableUsbRuntimesMock(mock) {
+  const { setMockedModule } = require("devtools/client/aboutdebugging-new/src/modules/test-helper");
+  setMockedModule(mock, "modules/usb-runtimes");
+}
+
+/**
+ * Update the loader to clear the mock entry for the usb-runtimes module.
+ */
+function disableUsbRuntimesMock() {
+  const { removeMockedModule } = require("devtools/client/aboutdebugging-new/src/modules/test-helper");
+  removeMockedModule("modules/usb-runtimes");
+}
+
+/**
+ * Creates a simple mock version for usb-runtimes, implementing all the expected methods
+ * with empty placeholders.
+ */
+function createUsbRuntimesMock() {
+  const usbRuntimesMock = {};
+  usbRuntimesMock.addUSBRuntimesObserver = function(listener) {
+    console.log("MOCKED METHOD addUSBRuntimesObserver");
+  };
+
+  usbRuntimesMock.disableUSBRuntimes = function() {
+    console.log("MOCKED METHOD disableUSBRuntimes");
+  };
+
+  usbRuntimesMock.enableUSBRuntimes = function() {
+    console.log("MOCKED METHOD enableUSBRuntimes");
+  };
+
+  usbRuntimesMock.getUSBRuntimes = function() {
+    console.log("MOCKED METHOD getUSBRuntimes");
+  };
+
+  usbRuntimesMock.removeUSBRuntimesObserver = function(listener) {
+    console.log("MOCKED METHOD removeUSBRuntimesObserver");
+  };
+
+  return usbRuntimesMock;
+}
+
+/**
+ * The usb-runtimes module allows to observer runtime updates. To simulate this behaviour
+ * the easiest is to use an EventEmitter-decorated object that can accept listeners and
+ * can emit events from the test.
+ *
+ * This method will update the addUSBRuntimesObserver method of the provided
+ * usbRuntimesMock in order to add listeners to a mockObserver, and returns said observer
+ * so that the test can emit "runtime-list-updated" when needed.
+ */
+function addObserverMock(usbRuntimesMock) {
+  const EventEmitter = require("devtools/shared/event-emitter");
+
+  const observerMock = {};
+  EventEmitter.decorate(observerMock);
+  usbRuntimesMock.addUSBRuntimesObserver = function(listener) {
+    console.log("MOCKED METHOD addUSBRuntimesObserver with mock scanner");
+    observerMock.on("runtime-list-updated", listener);
+  };
+
+  // NOTE FOR REVIEW: Instead of emitting "runtime-list-updated" events in the test,
+  // this mock could have a emitObservedEvent method, that would just emit the correct
+  // event. This way if the event name changes, everything remains contained in this
+  // method.
+
+  return observerMock;
+}