browser/base/content/test/general/browser_save_link-perwindowpb.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_save_link-perwindowpb.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,207 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +var MockFilePicker = SpecialPowers.MockFilePicker;
     1.8 +MockFilePicker.init(window);
     1.9 +
    1.10 +// Trigger a save of a link in public mode, then trigger an identical save
    1.11 +// in private mode and ensure that the second request is differentiated from
    1.12 +// the first by checking that cookies set by the first response are not sent
    1.13 +// during the second request.
    1.14 +function triggerSave(aWindow, aCallback) {
    1.15 +  info("started triggerSave");
    1.16 +  var fileName;
    1.17 +  let testBrowser = aWindow.gBrowser.selectedBrowser;
    1.18 +  // This page sets a cookie if and only if a cookie does not exist yet
    1.19 +  let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517-2.html";
    1.20 +  testBrowser.loadURI(testURI);
    1.21 +  testBrowser.addEventListener("pageshow", function pageShown(event) {
    1.22 +    info("got pageshow with " + event.target.location);
    1.23 +    if (event.target.location != testURI) {
    1.24 +      info("try again!");
    1.25 +      testBrowser.loadURI(testURI);
    1.26 +      return;
    1.27 +    }
    1.28 +    info("found our page!");
    1.29 +    testBrowser.removeEventListener("pageshow", pageShown, false);
    1.30 +
    1.31 +    waitForFocus(function () {
    1.32 +      info("register to handle popupshown");
    1.33 +      aWindow.document.addEventListener("popupshown", contextMenuOpened, false);
    1.34 +
    1.35 +      var link = testBrowser.contentDocument.getElementById("fff");
    1.36 +      info("link: " + link);
    1.37 +      EventUtils.synthesizeMouseAtCenter(link,
    1.38 +                                         { type: "contextmenu", button: 2 },
    1.39 +                                         testBrowser.contentWindow);
    1.40 +      info("right clicked!");
    1.41 +    }, testBrowser.contentWindow);
    1.42 +  }, false);
    1.43 +
    1.44 +  function contextMenuOpened(event) {
    1.45 +    info("contextMenuOpened");
    1.46 +    aWindow.document.removeEventListener("popupshown", contextMenuOpened);
    1.47 +
    1.48 +    // Create the folder the link will be saved into.
    1.49 +    var destDir = createTemporarySaveDirectory();
    1.50 +    var destFile = destDir.clone();
    1.51 +
    1.52 +    MockFilePicker.displayDirectory = destDir;
    1.53 +    MockFilePicker.showCallback = function(fp) {
    1.54 +      info("showCallback");
    1.55 +      fileName = fp.defaultString;
    1.56 +      info("fileName: " + fileName);
    1.57 +      destFile.append (fileName);
    1.58 +      MockFilePicker.returnFiles = [destFile];
    1.59 +      MockFilePicker.filterIndex = 1; // kSaveAsType_URL
    1.60 +      info("done showCallback");
    1.61 +    };
    1.62 +
    1.63 +    mockTransferCallback = function(downloadSuccess) {
    1.64 +      info("mockTransferCallback");
    1.65 +      onTransferComplete(aWindow, downloadSuccess, destDir);
    1.66 +      destDir.remove(true);
    1.67 +      ok(!destDir.exists(), "Destination dir should be removed");
    1.68 +      ok(!destFile.exists(), "Destination file should be removed");
    1.69 +      mockTransferCallback = null;
    1.70 +      info("done mockTransferCallback");
    1.71 +    }
    1.72 +
    1.73 +    // Select "Save Link As" option from context menu
    1.74 +    var saveLinkCommand = aWindow.document.getElementById("context-savelink");
    1.75 +    info("saveLinkCommand: " + saveLinkCommand);
    1.76 +    saveLinkCommand.doCommand();
    1.77 +
    1.78 +    event.target.hidePopup();
    1.79 +    info("popup hidden");
    1.80 +  }
    1.81 +
    1.82 +  function onTransferComplete(aWindow, downloadSuccess, destDir) {
    1.83 +    ok(downloadSuccess, "Link should have been downloaded successfully");
    1.84 +    aWindow.close();
    1.85 +
    1.86 +    executeSoon(function() aCallback());
    1.87 +  }
    1.88 +}
    1.89 +
    1.90 +function test() {
    1.91 +  info("Start the test");
    1.92 +  waitForExplicitFinish();
    1.93 +
    1.94 +  var gNumSet = 0;
    1.95 +  function testOnWindow(options, callback) {
    1.96 +    info("testOnWindow(" + options + ")");
    1.97 +    var win = OpenBrowserWindow(options);
    1.98 +    info("got " + win);
    1.99 +    whenDelayedStartupFinished(win, () => callback(win));
   1.100 +  }
   1.101 +
   1.102 +  function whenDelayedStartupFinished(aWindow, aCallback) {
   1.103 +    info("whenDelayedStartupFinished");
   1.104 +    Services.obs.addObserver(function observer(aSubject, aTopic) {
   1.105 +      info("whenDelayedStartupFinished, got topic: " + aTopic + ", got subject: " + aSubject + ", waiting for " + aWindow);
   1.106 +      if (aWindow == aSubject) {
   1.107 +        Services.obs.removeObserver(observer, aTopic);
   1.108 +        executeSoon(aCallback);
   1.109 +        info("whenDelayedStartupFinished found our window");
   1.110 +      }
   1.111 +    }, "browser-delayed-startup-finished", false);
   1.112 +  }
   1.113 +
   1.114 +  mockTransferRegisterer.register();
   1.115 +
   1.116 +  registerCleanupFunction(function () {
   1.117 +    info("Running the cleanup code");
   1.118 +    mockTransferRegisterer.unregister();
   1.119 +    MockFilePicker.cleanup();
   1.120 +    Services.obs.removeObserver(observer, "http-on-modify-request");
   1.121 +    Services.obs.removeObserver(observer, "http-on-examine-response");
   1.122 +    info("Finished running the cleanup code");
   1.123 +  });
   1.124 + 
   1.125 +  function observer(subject, topic, state) {
   1.126 +    info("observer called with " + topic);
   1.127 +    if (topic == "http-on-modify-request") {
   1.128 +      onModifyRequest(subject);
   1.129 +    } else if (topic == "http-on-examine-response") {
   1.130 +      onExamineResponse(subject);
   1.131 +    }
   1.132 +  }
   1.133 +
   1.134 +  function onExamineResponse(subject) {
   1.135 +    let channel = subject.QueryInterface(Ci.nsIHttpChannel);
   1.136 +    info("onExamineResponse with " + channel.URI.spec);
   1.137 +    if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") {
   1.138 +      info("returning");
   1.139 +      return;
   1.140 +    }
   1.141 +    try {
   1.142 +      let cookies = channel.getResponseHeader("set-cookie");
   1.143 +      // From browser/base/content/test/general/bug792715.sjs, we receive a Set-Cookie
   1.144 +      // header with foopy=1 when there are no cookies for that domain.
   1.145 +      is(cookies, "foopy=1", "Cookie should be foopy=1");
   1.146 +      gNumSet += 1;
   1.147 +      info("gNumSet = " + gNumSet);
   1.148 +    } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) {
   1.149 +      info("onExamineResponse caught NOTAVAIL" + ex);
   1.150 +    } catch (ex) {
   1.151 +      info("ionExamineResponse caught " + ex);
   1.152 +    }
   1.153 +  }
   1.154 +
   1.155 +  function onModifyRequest(subject) {
   1.156 +    let channel = subject.QueryInterface(Ci.nsIHttpChannel);
   1.157 +    info("onModifyRequest with " + channel.URI.spec);
   1.158 +    if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") {
   1.159 +      return;
   1.160 +    }
   1.161 +    try {
   1.162 +      let cookies = channel.getRequestHeader("cookie");
   1.163 +      info("cookies: " + cookies);
   1.164 +      // From browser/base/content/test/general/bug792715.sjs, we should never send a
   1.165 +      // cookie because we are making only 2 requests: one in public mode, and
   1.166 +      // one in private mode.
   1.167 +      throw "We should never send a cookie in this test";
   1.168 +    } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) {
   1.169 +      info("onModifyRequest caught NOTAVAIL" + ex);
   1.170 +    } catch (ex) {
   1.171 +      info("ionModifyRequest caught " + ex);
   1.172 +    }
   1.173 +  }
   1.174 +
   1.175 +  Services.obs.addObserver(observer, "http-on-modify-request", false);
   1.176 +  Services.obs.addObserver(observer, "http-on-examine-response", false);
   1.177 +
   1.178 +  testOnWindow(undefined, function(win) {
   1.179 +    // The first save from a regular window sets a cookie.
   1.180 +    triggerSave(win, function() {
   1.181 +      is(gNumSet, 1, "1 cookie should be set");
   1.182 +
   1.183 +      // The second save from a private window also sets a cookie.
   1.184 +      testOnWindow({private: true}, function(win) {
   1.185 +        triggerSave(win, function() {
   1.186 +          is(gNumSet, 2, "2 cookies should be set");
   1.187 +          finish();
   1.188 +        });
   1.189 +      });
   1.190 +    });
   1.191 +  });
   1.192 +}
   1.193 +
   1.194 +Cc["@mozilla.org/moz/jssubscript-loader;1"]
   1.195 +  .getService(Ci.mozIJSSubScriptLoader)
   1.196 +  .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
   1.197 +                 this);
   1.198 +
   1.199 +function createTemporarySaveDirectory() {
   1.200 +  var saveDir = Cc["@mozilla.org/file/directory_service;1"]
   1.201 +                  .getService(Ci.nsIProperties)
   1.202 +                  .get("TmpD", Ci.nsIFile);
   1.203 +  saveDir.append("testsavedir");
   1.204 +  if (!saveDir.exists()) {
   1.205 +    info("create testsavedir!");
   1.206 +    saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
   1.207 +  }
   1.208 +  info("return from createTempSaveDir: " + saveDir.path);
   1.209 +  return saveDir;
   1.210 +}

mercurial