michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: var MockFilePicker = SpecialPowers.MockFilePicker; michael@0: MockFilePicker.init(window); michael@0: michael@0: // Trigger a save of a link in public mode, then trigger an identical save michael@0: // in private mode and ensure that the second request is differentiated from michael@0: // the first by checking that cookies set by the first response are not sent michael@0: // during the second request. michael@0: function triggerSave(aWindow, aCallback) { michael@0: info("started triggerSave"); michael@0: var fileName; michael@0: let testBrowser = aWindow.gBrowser.selectedBrowser; michael@0: // This page sets a cookie if and only if a cookie does not exist yet michael@0: let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517-2.html"; michael@0: testBrowser.loadURI(testURI); michael@0: testBrowser.addEventListener("pageshow", function pageShown(event) { michael@0: info("got pageshow with " + event.target.location); michael@0: if (event.target.location != testURI) { michael@0: info("try again!"); michael@0: testBrowser.loadURI(testURI); michael@0: return; michael@0: } michael@0: info("found our page!"); michael@0: testBrowser.removeEventListener("pageshow", pageShown, false); michael@0: michael@0: waitForFocus(function () { michael@0: info("register to handle popupshown"); michael@0: aWindow.document.addEventListener("popupshown", contextMenuOpened, false); michael@0: michael@0: var link = testBrowser.contentDocument.getElementById("fff"); michael@0: info("link: " + link); michael@0: EventUtils.synthesizeMouseAtCenter(link, michael@0: { type: "contextmenu", button: 2 }, michael@0: testBrowser.contentWindow); michael@0: info("right clicked!"); michael@0: }, testBrowser.contentWindow); michael@0: }, false); michael@0: michael@0: function contextMenuOpened(event) { michael@0: info("contextMenuOpened"); michael@0: aWindow.document.removeEventListener("popupshown", contextMenuOpened); michael@0: michael@0: // Create the folder the link will be saved into. michael@0: var destDir = createTemporarySaveDirectory(); michael@0: var destFile = destDir.clone(); michael@0: michael@0: MockFilePicker.displayDirectory = destDir; michael@0: MockFilePicker.showCallback = function(fp) { michael@0: info("showCallback"); michael@0: fileName = fp.defaultString; michael@0: info("fileName: " + fileName); michael@0: destFile.append (fileName); michael@0: MockFilePicker.returnFiles = [destFile]; michael@0: MockFilePicker.filterIndex = 1; // kSaveAsType_URL michael@0: info("done showCallback"); michael@0: }; michael@0: michael@0: mockTransferCallback = function(downloadSuccess) { michael@0: info("mockTransferCallback"); michael@0: onTransferComplete(aWindow, downloadSuccess, destDir); michael@0: destDir.remove(true); michael@0: ok(!destDir.exists(), "Destination dir should be removed"); michael@0: ok(!destFile.exists(), "Destination file should be removed"); michael@0: mockTransferCallback = null; michael@0: info("done mockTransferCallback"); michael@0: } michael@0: michael@0: // Select "Save Link As" option from context menu michael@0: var saveLinkCommand = aWindow.document.getElementById("context-savelink"); michael@0: info("saveLinkCommand: " + saveLinkCommand); michael@0: saveLinkCommand.doCommand(); michael@0: michael@0: event.target.hidePopup(); michael@0: info("popup hidden"); michael@0: } michael@0: michael@0: function onTransferComplete(aWindow, downloadSuccess, destDir) { michael@0: ok(downloadSuccess, "Link should have been downloaded successfully"); michael@0: aWindow.close(); michael@0: michael@0: executeSoon(function() aCallback()); michael@0: } michael@0: } michael@0: michael@0: function test() { michael@0: info("Start the test"); michael@0: waitForExplicitFinish(); michael@0: michael@0: var gNumSet = 0; michael@0: function testOnWindow(options, callback) { michael@0: info("testOnWindow(" + options + ")"); michael@0: var win = OpenBrowserWindow(options); michael@0: info("got " + win); michael@0: whenDelayedStartupFinished(win, () => callback(win)); michael@0: } michael@0: michael@0: function whenDelayedStartupFinished(aWindow, aCallback) { michael@0: info("whenDelayedStartupFinished"); michael@0: Services.obs.addObserver(function observer(aSubject, aTopic) { michael@0: info("whenDelayedStartupFinished, got topic: " + aTopic + ", got subject: " + aSubject + ", waiting for " + aWindow); michael@0: if (aWindow == aSubject) { michael@0: Services.obs.removeObserver(observer, aTopic); michael@0: executeSoon(aCallback); michael@0: info("whenDelayedStartupFinished found our window"); michael@0: } michael@0: }, "browser-delayed-startup-finished", false); michael@0: } michael@0: michael@0: mockTransferRegisterer.register(); michael@0: michael@0: registerCleanupFunction(function () { michael@0: info("Running the cleanup code"); michael@0: mockTransferRegisterer.unregister(); michael@0: MockFilePicker.cleanup(); michael@0: Services.obs.removeObserver(observer, "http-on-modify-request"); michael@0: Services.obs.removeObserver(observer, "http-on-examine-response"); michael@0: info("Finished running the cleanup code"); michael@0: }); michael@0: michael@0: function observer(subject, topic, state) { michael@0: info("observer called with " + topic); michael@0: if (topic == "http-on-modify-request") { michael@0: onModifyRequest(subject); michael@0: } else if (topic == "http-on-examine-response") { michael@0: onExamineResponse(subject); michael@0: } michael@0: } michael@0: michael@0: function onExamineResponse(subject) { michael@0: let channel = subject.QueryInterface(Ci.nsIHttpChannel); michael@0: info("onExamineResponse with " + channel.URI.spec); michael@0: if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") { michael@0: info("returning"); michael@0: return; michael@0: } michael@0: try { michael@0: let cookies = channel.getResponseHeader("set-cookie"); michael@0: // From browser/base/content/test/general/bug792715.sjs, we receive a Set-Cookie michael@0: // header with foopy=1 when there are no cookies for that domain. michael@0: is(cookies, "foopy=1", "Cookie should be foopy=1"); michael@0: gNumSet += 1; michael@0: info("gNumSet = " + gNumSet); michael@0: } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) { michael@0: info("onExamineResponse caught NOTAVAIL" + ex); michael@0: } catch (ex) { michael@0: info("ionExamineResponse caught " + ex); michael@0: } michael@0: } michael@0: michael@0: function onModifyRequest(subject) { michael@0: let channel = subject.QueryInterface(Ci.nsIHttpChannel); michael@0: info("onModifyRequest with " + channel.URI.spec); michael@0: if (channel.URI.spec != "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.sjs") { michael@0: return; michael@0: } michael@0: try { michael@0: let cookies = channel.getRequestHeader("cookie"); michael@0: info("cookies: " + cookies); michael@0: // From browser/base/content/test/general/bug792715.sjs, we should never send a michael@0: // cookie because we are making only 2 requests: one in public mode, and michael@0: // one in private mode. michael@0: throw "We should never send a cookie in this test"; michael@0: } catch (ex if ex.result == Cr.NS_ERROR_NOT_AVAILABLE) { michael@0: info("onModifyRequest caught NOTAVAIL" + ex); michael@0: } catch (ex) { michael@0: info("ionModifyRequest caught " + ex); michael@0: } michael@0: } michael@0: michael@0: Services.obs.addObserver(observer, "http-on-modify-request", false); michael@0: Services.obs.addObserver(observer, "http-on-examine-response", false); michael@0: michael@0: testOnWindow(undefined, function(win) { michael@0: // The first save from a regular window sets a cookie. michael@0: triggerSave(win, function() { michael@0: is(gNumSet, 1, "1 cookie should be set"); michael@0: michael@0: // The second save from a private window also sets a cookie. michael@0: testOnWindow({private: true}, function(win) { michael@0: triggerSave(win, function() { michael@0: is(gNumSet, 2, "2 cookies should be set"); michael@0: finish(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader) michael@0: .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", michael@0: this); michael@0: michael@0: function createTemporarySaveDirectory() { michael@0: var saveDir = Cc["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties) michael@0: .get("TmpD", Ci.nsIFile); michael@0: saveDir.append("testsavedir"); michael@0: if (!saveDir.exists()) { michael@0: info("create testsavedir!"); michael@0: saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); michael@0: } michael@0: info("return from createTempSaveDir: " + saveDir.path); michael@0: return saveDir; michael@0: }