Bug 1398229 - Save-link-as feature should use the loading principal - part 1 - test, r=ckerschb
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 16 Nov 2017 12:26:13 +0100
changeset 392252 314c6024ea272b3d53214e94ae703a707ffcc624
parent 392251 96fdf13f21180ba7e77b2ed95cc93b9e22cc721b
child 392253 e7d2101e1aa91d7e171aa9687acb8807a72d5fad
push id32916
push useraciure@mozilla.com
push dateFri, 17 Nov 2017 09:59:52 +0000
treeherdermozilla-central@a77c628829b3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckerschb
bugs1398229
milestone59.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 1398229 - Save-link-as feature should use the loading principal - part 1 - test, r=ckerschb
browser/components/contextualidentity/test/browser/browser.ini
browser/components/contextualidentity/test/browser/browser_saveLink.js
browser/components/contextualidentity/test/browser/saveLink.sjs
--- a/browser/components/contextualidentity/test/browser/browser.ini
+++ b/browser/components/contextualidentity/test/browser/browser.ini
@@ -25,8 +25,12 @@ tags = openwindow
 tags = openwindow
 [browser_serviceworkers.js]
 [browser_broadcastchannel.js]
 [browser_blobUrl.js]
 [browser_middleClick.js]
 [browser_imageCache.js]
 [browser_count_and_remove.js]
 [browser_relatedTab.js]
+[browser_saveLink.js]
+support-files =
+  saveLink.sjs
+  !/toolkit/content/tests/browser/common/mockTransfer.js
new file mode 100644
--- /dev/null
+++ b/browser/components/contextualidentity/test/browser/browser_saveLink.js
@@ -0,0 +1,111 @@
+"use strict";
+
+const BASE_ORIGIN = "https://example.com";
+const URI = BASE_ORIGIN +
+  "/browser/browser/components/contextualidentity/test/browser/saveLink.sjs";
+
+let MockFilePicker = SpecialPowers.MockFilePicker;
+MockFilePicker.init(window);
+
+add_task(async function setup() {
+  info("Setting the prefs.");
+
+  // make sure userContext is enabled.
+  await SpecialPowers.pushPrefEnv({"set": [
+    ["privacy.userContext.enabled", true]
+  ]});
+});
+
+add_task(async function test() {
+  info("Let's open a new window");
+  let win = await BrowserTestUtils.openNewBrowserWindow();
+
+  info("Opening tab with UCI: 1");
+  let tab = BrowserTestUtils.addTab(win.gBrowser, URI + "?UCI=1", {userContextId: 1});
+
+  // select tab and make sure its browser is focused
+  win.gBrowser.selectedTab = tab;
+  tab.ownerGlobal.focus();
+
+  info("Waiting to load content");
+  let browser = gBrowser.getBrowserForTab(tab);
+  await BrowserTestUtils.browserLoaded(browser);
+
+  let popupShownPromise = BrowserTestUtils.waitForEvent(win.document, "popupshown");
+
+  await BrowserTestUtils.synthesizeMouseAtCenter("#fff", {type: "contextmenu", button: 2},
+                                                 win.gBrowser.selectedBrowser);
+  info("Right clicked!");
+
+  await popupShownPromise;
+  info("Context menu opened");
+
+  info("Let's create a temporary dir");
+  let tempDir = createTemporarySaveDirectory();
+  let destFile;
+
+  MockFilePicker.displayDirectory = tempDir;
+  MockFilePicker.showCallback = fp => {
+    info("MockFilePicker showCallback");
+
+    let fileName = fp.defaultString;
+    destFile = tempDir.clone();
+    destFile.append(fileName);
+
+    MockFilePicker.setFiles([destFile]);
+    MockFilePicker.filterIndex = 1; // kSaveAsType_URL
+
+    info("MockFilePicker showCallback done");
+  };
+
+  let transferCompletePromise = new Promise((resolve) => {
+    function onTransferComplete(downloadSuccess) {
+      ok(downloadSuccess, "File should have been downloaded successfully");
+      resolve();
+    }
+
+    mockTransferCallback = onTransferComplete;
+    mockTransferRegisterer.register();
+  });
+
+  registerCleanupFunction(function() {
+    mockTransferRegisterer.unregister();
+    MockFilePicker.cleanup();
+    tempDir.remove(true);
+  });
+
+  // Select "Save Link As" option from context menu
+  let saveLinkCommand = win.document.getElementById("context-savelink");
+  info("saveLinkCommand: " + saveLinkCommand);
+  saveLinkCommand.doCommand();
+
+  let contextMenu = win.document.getElementById("contentAreaContextMenu");
+  let popupHiddenPromise = BrowserTestUtils.waitForEvent(contextMenu, "popuphidden");
+  contextMenu.hidePopup();
+  await popupHiddenPromise;
+  info("popup hidden");
+
+  await transferCompletePromise;
+
+  // Let's query the SJS to know if the download happened with the correct cookie.
+  let response = await fetch(URI + "?result=1");
+  let text = await response.text();
+  is(text, "Result:UCI=1", "Correct cookie used: -" + text + "-");
+
+  info("Closing the window");
+  await BrowserTestUtils.closeWindow(win);
+});
+
+/* import-globals-from ../../../../../toolkit/content/tests/browser/common/mockTransfer.js */
+Services.scriptloader.loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", this);
+
+function createTemporarySaveDirectory() {
+  let saveDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
+  saveDir.append("testsavedir");
+  if (!saveDir.exists()) {
+    info("create testsavedir!");
+    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
+  }
+  info("return from createTempSaveDir: " + saveDir.path);
+  return saveDir;
+}
new file mode 100644
--- /dev/null
+++ b/browser/components/contextualidentity/test/browser/saveLink.sjs
@@ -0,0 +1,45 @@
+const HTTP_ORIGIN = "http://example.com";
+const HTTPS_ORIGIN = "https://example.com";
+const URI_PATH = "/browser/browser/components/contextualidentity/test/browser/saveLink.sjs";
+
+Components.utils.importGlobalProperties(["URLSearchParams"]);
+
+function handleRequest(aRequest, aResponse) {
+  var params = new URLSearchParams(aRequest.queryString);
+
+  // This is the first request, where we set the cookie.
+  if (params.has("UCI")) {
+    aResponse.setStatusLine(aRequest.httpVersion, 200);
+    aResponse.setHeader("Set-Cookie", "UCI=" + params.get("UCI"));
+    aResponse.write("<html><body><a href='" + HTTPS_ORIGIN + URI_PATH + "?redirect=1' id='fff'>this is a link</a></body></html>");
+    return;
+  }
+
+  // Second request. This is the save-as content, but we make a redirect to see
+  // if we are able to follow it.
+  if (params.has("redirect")) {
+    aResponse.setStatusLine(aRequest.httpVersion, 302, "Found");
+    aResponse.setHeader("Location", HTTP_ORIGIN + URI_PATH + "?download=1", false);
+    aResponse.write("Redirect!");
+    return;
+  }
+
+  // This is the 3rd request where we offer the content to be saved.
+  if (params.has("download")) {
+    setState("downloadUCI", aRequest.getHeader("Cookie"));
+    aResponse.setStatusLine(aRequest.httpVersion, 200);
+    aResponse.write("All Good!");
+    return;
+  }
+
+  // This is the last request to check that the download happened with the correct cookie
+  if (params.has("result")) {
+    aResponse.setStatusLine(aRequest.httpVersion, 200);
+    aResponse.write("Result:" + getState("downloadUCI"));
+    return;
+  }
+
+  // We should not be here!
+  aResponse.setStatusLine(aRequest.httpVersion, 500);
+  aResponse.write("ERROR!!!");
+}