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