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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 var MockFilePicker = SpecialPowers.MockFilePicker;
michael@0 5 MockFilePicker.init(window);
michael@0 6
michael@0 7 // Trigger a save of a link in public mode, then trigger an identical save
michael@0 8 // in private mode and ensure that the second request is differentiated from
michael@0 9 // the first by checking that cookies set by the first response are not sent
michael@0 10 // during the second request.
michael@0 11 function triggerSave(aWindow, aCallback) {
michael@0 12 info("started triggerSave");
michael@0 13 var fileName;
michael@0 14 let testBrowser = aWindow.gBrowser.selectedBrowser;
michael@0 15 // This page sets a cookie if and only if a cookie does not exist yet
michael@0 16 let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517-2.html";
michael@0 17 testBrowser.loadURI(testURI);
michael@0 18 testBrowser.addEventListener("pageshow", function pageShown(event) {
michael@0 19 info("got pageshow with " + event.target.location);
michael@0 20 if (event.target.location != testURI) {
michael@0 21 info("try again!");
michael@0 22 testBrowser.loadURI(testURI);
michael@0 23 return;
michael@0 24 }
michael@0 25 info("found our page!");
michael@0 26 testBrowser.removeEventListener("pageshow", pageShown, false);
michael@0 27
michael@0 28 waitForFocus(function () {
michael@0 29 info("register to handle popupshown");
michael@0 30 aWindow.document.addEventListener("popupshown", contextMenuOpened, false);
michael@0 31
michael@0 32 var link = testBrowser.contentDocument.getElementById("fff");
michael@0 33 info("link: " + link);
michael@0 34 EventUtils.synthesizeMouseAtCenter(link,
michael@0 35 { type: "contextmenu", button: 2 },
michael@0 36 testBrowser.contentWindow);
michael@0 37 info("right clicked!");
michael@0 38 }, testBrowser.contentWindow);
michael@0 39 }, false);
michael@0 40
michael@0 41 function contextMenuOpened(event) {
michael@0 42 info("contextMenuOpened");
michael@0 43 aWindow.document.removeEventListener("popupshown", contextMenuOpened);
michael@0 44
michael@0 45 // Create the folder the link will be saved into.
michael@0 46 var destDir = createTemporarySaveDirectory();
michael@0 47 var destFile = destDir.clone();
michael@0 48
michael@0 49 MockFilePicker.displayDirectory = destDir;
michael@0 50 MockFilePicker.showCallback = function(fp) {
michael@0 51 info("showCallback");
michael@0 52 fileName = fp.defaultString;
michael@0 53 info("fileName: " + fileName);
michael@0 54 destFile.append (fileName);
michael@0 55 MockFilePicker.returnFiles = [destFile];
michael@0 56 MockFilePicker.filterIndex = 1; // kSaveAsType_URL
michael@0 57 info("done showCallback");
michael@0 58 };
michael@0 59
michael@0 60 mockTransferCallback = function(downloadSuccess) {
michael@0 61 info("mockTransferCallback");
michael@0 62 onTransferComplete(aWindow, downloadSuccess, destDir);
michael@0 63 destDir.remove(true);
michael@0 64 ok(!destDir.exists(), "Destination dir should be removed");
michael@0 65 ok(!destFile.exists(), "Destination file should be removed");
michael@0 66 mockTransferCallback = null;
michael@0 67 info("done mockTransferCallback");
michael@0 68 }
michael@0 69
michael@0 70 // Select "Save Link As" option from context menu
michael@0 71 var saveLinkCommand = aWindow.document.getElementById("context-savelink");
michael@0 72 info("saveLinkCommand: " + saveLinkCommand);
michael@0 73 saveLinkCommand.doCommand();
michael@0 74
michael@0 75 event.target.hidePopup();
michael@0 76 info("popup hidden");
michael@0 77 }
michael@0 78
michael@0 79 function onTransferComplete(aWindow, downloadSuccess, destDir) {
michael@0 80 ok(downloadSuccess, "Link should have been downloaded successfully");
michael@0 81 aWindow.close();
michael@0 82
michael@0 83 executeSoon(function() aCallback());
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 function test() {
michael@0 88 info("Start the test");
michael@0 89 waitForExplicitFinish();
michael@0 90
michael@0 91 var gNumSet = 0;
michael@0 92 function testOnWindow(options, callback) {
michael@0 93 info("testOnWindow(" + options + ")");
michael@0 94 var win = OpenBrowserWindow(options);
michael@0 95 info("got " + win);
michael@0 96 whenDelayedStartupFinished(win, () => callback(win));
michael@0 97 }
michael@0 98
michael@0 99 function whenDelayedStartupFinished(aWindow, aCallback) {
michael@0 100 info("whenDelayedStartupFinished");
michael@0 101 Services.obs.addObserver(function observer(aSubject, aTopic) {
michael@0 102 info("whenDelayedStartupFinished, got topic: " + aTopic + ", got subject: " + aSubject + ", waiting for " + aWindow);
michael@0 103 if (aWindow == aSubject) {
michael@0 104 Services.obs.removeObserver(observer, aTopic);
michael@0 105 executeSoon(aCallback);
michael@0 106 info("whenDelayedStartupFinished found our window");
michael@0 107 }
michael@0 108 }, "browser-delayed-startup-finished", false);
michael@0 109 }
michael@0 110
michael@0 111 mockTransferRegisterer.register();
michael@0 112
michael@0 113 registerCleanupFunction(function () {
michael@0 114 info("Running the cleanup code");
michael@0 115 mockTransferRegisterer.unregister();
michael@0 116 MockFilePicker.cleanup();
michael@0 117 Services.obs.removeObserver(observer, "http-on-modify-request");
michael@0 118 Services.obs.removeObserver(observer, "http-on-examine-response");
michael@0 119 info("Finished running the cleanup code");
michael@0 120 });
michael@0 121
michael@0 122 function observer(subject, topic, state) {
michael@0 123 info("observer called with " + topic);
michael@0 124 if (topic == "http-on-modify-request") {
michael@0 125 onModifyRequest(subject);
michael@0 126 } else if (topic == "http-on-examine-response") {
michael@0 127 onExamineResponse(subject);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 function onExamineResponse(subject) {
michael@0 132 let channel = subject.QueryInterface(Ci.nsIHttpChannel);
michael@0 133 info("onExamineResponse with " + channel.URI.spec);
michael@0 134 if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") {
michael@0 135 info("returning");
michael@0 136 return;
michael@0 137 }
michael@0 138 try {
michael@0 139 let cookies = channel.getResponseHeader("set-cookie");
michael@0 140 // From browser/base/content/test/general/bug792715.sjs, we receive a Set-Cookie
michael@0 141 // header with foopy=1 when there are no cookies for that domain.
michael@0 142 is(cookies, "foopy=1", "Cookie should be foopy=1");
michael@0 143 gNumSet += 1;
michael@0 144 info("gNumSet = " + gNumSet);
michael@0 145 } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) {
michael@0 146 info("onExamineResponse caught NOTAVAIL" + ex);
michael@0 147 } catch (ex) {
michael@0 148 info("ionExamineResponse caught " + ex);
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 function onModifyRequest(subject) {
michael@0 153 let channel = subject.QueryInterface(Ci.nsIHttpChannel);
michael@0 154 info("onModifyRequest with " + channel.URI.spec);
michael@0 155 if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") {
michael@0 156 return;
michael@0 157 }
michael@0 158 try {
michael@0 159 let cookies = channel.getRequestHeader("cookie");
michael@0 160 info("cookies: " + cookies);
michael@0 161 // From browser/base/content/test/general/bug792715.sjs, we should never send a
michael@0 162 // cookie because we are making only 2 requests: one in public mode, and
michael@0 163 // one in private mode.
michael@0 164 throw "We should never send a cookie in this test";
michael@0 165 } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) {
michael@0 166 info("onModifyRequest caught NOTAVAIL" + ex);
michael@0 167 } catch (ex) {
michael@0 168 info("ionModifyRequest caught " + ex);
michael@0 169 }
michael@0 170 }
michael@0 171
michael@0 172 Services.obs.addObserver(observer, "http-on-modify-request", false);
michael@0 173 Services.obs.addObserver(observer, "http-on-examine-response", false);
michael@0 174
michael@0 175 testOnWindow(undefined, function(win) {
michael@0 176 // The first save from a regular window sets a cookie.
michael@0 177 triggerSave(win, function() {
michael@0 178 is(gNumSet, 1, "1 cookie should be set");
michael@0 179
michael@0 180 // The second save from a private window also sets a cookie.
michael@0 181 testOnWindow({private: true}, function(win) {
michael@0 182 triggerSave(win, function() {
michael@0 183 is(gNumSet, 2, "2 cookies should be set");
michael@0 184 finish();
michael@0 185 });
michael@0 186 });
michael@0 187 });
michael@0 188 });
michael@0 189 }
michael@0 190
michael@0 191 Cc["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 192 .getService(Ci.mozIJSSubScriptLoader)
michael@0 193 .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
michael@0 194 this);
michael@0 195
michael@0 196 function createTemporarySaveDirectory() {
michael@0 197 var saveDir = Cc["@mozilla.org/file/directory_service;1"]
michael@0 198 .getService(Ci.nsIProperties)
michael@0 199 .get("TmpD", Ci.nsIFile);
michael@0 200 saveDir.append("testsavedir");
michael@0 201 if (!saveDir.exists()) {
michael@0 202 info("create testsavedir!");
michael@0 203 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
michael@0 204 }
michael@0 205 info("return from createTempSaveDir: " + saveDir.path);
michael@0 206 return saveDir;
michael@0 207 }

mercurial