Bug 1226120 - add test for unknownContentType.xul and file types with a default handler but always ask set, r?mconley draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Thu, 17 Dec 2015 17:28:15 +0000
changeset 316026 4cc12c96c194284df030839c72aa4b666db8d385
parent 316015 0dd30d9b7c9a86a0033c9529f2b540e9ffcba7ee
child 512115 40d1df59830f0b54c7935f166b9b11833b45b071
push id8498
push usergijskruitbosch@gmail.com
push dateThu, 17 Dec 2015 17:35:51 +0000
reviewersmconley
bugs1226120
milestone46.0a1
Bug 1226120 - add test for unknownContentType.xul and file types with a default handler but always ask set, r?mconley
uriloader/exthandler/tests/mochitest/browser.ini
uriloader/exthandler/tests/mochitest/browser_download_always_ask_preferred_app.js
uriloader/exthandler/tests/moz.build
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/browser.ini
@@ -0,0 +1,3 @@
+[DEFAULT]
+
+[browser_download_always_ask_preferred_app.js]
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/browser_download_always_ask_preferred_app.js
@@ -0,0 +1,122 @@
+add_task(function*() {
+  // Get a helper app dialog instance:
+  let helperAppDialog = Cc["@mozilla.org/helperapplauncherdialog;1"].
+                        createInstance(Ci.nsIHelperAppLauncherDialog);
+  // Mock the mime info:
+  let mockedMIME = {
+    _launched: 0,
+
+    // nsIHandlerInfo
+    type: "text/magic-automated-test",
+    description: "My magic test mime type",
+    defaultDescription: "Use the default app, luke!",
+    hasDefaultHandler: true,
+    possibleApplicationHandlers: {
+      appendElement() {},
+      removeElementAt() {},
+      insertElementAt() {},
+      replaceElementAt() {},
+      clear() {},
+      queryElementAt() {},
+      indexOf() { return -1 },
+      enumerate() { return null },
+      length: 0,
+      QueryInterface: XPCOMUtils.generateQI([Ci.nsIMutableArray, Ci.nsIArray])
+    },
+    preferredApplicationHandler: null,
+    launchWithURI() {
+      this._launched++;
+    },
+    alwaysAskBeforeHandling: true,
+    preferredAction: 2, // useHelperApp
+
+    // nsIMIMEInfo
+    getFileExtensions() { throw Cr.NS_ERROR_NOT_IMPLEMENTED; },
+    setFileExtensions() {},
+    extensionExists(ext) { return ext == this.primaryExtension; },
+    appendExtension() {},
+    primaryExtension: "something",
+    MIMEType: "text/magic-automated-test",
+
+    equals() { return false },
+
+    possibleLocalHandlers: {
+      appendElement() {},
+      removeElementAt() {},
+      insertElementAt() {},
+      replaceElementAt() {},
+      clear() {},
+      queryElementAt() {},
+      indexOf() { return -1 },
+      enumerate() { return null },
+      length: 0,
+      QueryInterface: XPCOMUtils.generateQI([Ci.nsIArray])
+    },
+
+    launchWithFile() {
+      this._launched++;
+    },
+
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsIMIMEInfo, Ci.nsIHandlerInfo])
+  };
+  // Mock the launcher:
+  let launcher = {
+    _saveCount: 0,
+    _cancelCount: 0,
+    _launched: 0,
+
+    MIMEInfo: mockedMIME,
+    source: Services.io.newURI("http://www.mozilla.org/", null, null),
+    suggestedFileName: "test_always_ask_preferred_app.something",
+    targetFileIsExecutable: false,
+    saveToDisk() {
+      this._saveCount++;
+    },
+    cancel() {
+      this._cancelCount++;
+    },
+    launchWithApplication() {
+      this._launched++;
+    },
+    setWebProgressListener() {
+    },
+    saveDestinationAvailable() {
+    },
+    contentLength: 42,
+    targetFile: null, // never read
+    // PRTime is microseconds since epoch, Date.now() returns milliseconds:
+    timeDownloadStarted: Date.now() * 1000,
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable, Ci.nsIHelperAppLauncher])
+  };
+
+  let helperAppDialogShownPromise = BrowserTestUtils.domWindowOpened();
+  try {
+  helperAppDialog.show(launcher, window, "foopy");
+  } catch (ex) {
+    Cu.reportError(ex);
+  }
+  let dlg = yield helperAppDialogShownPromise;
+  yield new Promise(resolve => {
+    let onLoad = ev => {
+      dlg.removeEventListener("load", onLoad);
+      resolve(ev);
+    };
+    dlg.addEventListener("load", onLoad, false);
+  });
+  is(dlg.location.href, "chrome://mozapps/content/downloads/unknownContentType.xul",
+     "Got correct dialog");
+  let doc = dlg.document;
+  let location = doc.getElementById("source");
+  let expectedValue = launcher.source.prePath;
+  if (location.value != expectedValue) {
+    info("Waiting for dialog to be populated.");
+    yield BrowserTestUtils.waitForAttribute("value", location, expectedValue);
+  }
+  is(doc.getElementById("mode").selectedItem.id, "open", "Should be opening the file.");
+  ok(!dlg.document.getElementById("openHandler").selectedItem.hidden,
+     "Should not have selected a hidden item.");
+  let helperAppDialogHiddenPromise = BrowserTestUtils.windowClosed(dlg);
+  doc.documentElement.cancelDialog();
+  yield helperAppDialogHiddenPromise;
+});
+
--- a/uriloader/exthandler/tests/moz.build
+++ b/uriloader/exthandler/tests/moz.build
@@ -3,16 +3,18 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 MOCHITEST_MANIFESTS += ['mochitest/mochitest.ini']
 
 XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
 
+BROWSER_CHROME_MANIFESTS += ['mochitest/browser.ini']
+
 # The encoding test is already implemented in the Downloads API by a set of
 # test cases with the string "content_encoding" in their names.
 if not CONFIG['MOZ_JSDOWNLOADS']:
     XPCSHELL_TESTS_MANIFESTS += ['unit_ipc/xpcshell.ini']
 
 GeckoSimplePrograms([
     'WriteArgument',
 ], linkage=None)