michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let {LoadContextInfo} = Cu.import("resource://gre/modules/LoadContextInfo.jsm", null); michael@0: michael@0: function test() { michael@0: // initialization michael@0: waitForExplicitFinish(); michael@0: let windowsToClose = []; michael@0: let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.html"; michael@0: let fileName; michael@0: let MockFilePicker = SpecialPowers.MockFilePicker; michael@0: let cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"] michael@0: .getService(Ci.nsICacheStorageService); michael@0: michael@0: function checkDiskCacheFor(filename, goon) { michael@0: Visitor.prototype = { michael@0: onCacheStorageInfo: function(num, consumption) michael@0: { michael@0: info("disk storage contains " + num + " entries"); michael@0: }, michael@0: onCacheEntryInfo: function(entry) michael@0: { michael@0: info(entry.key); michael@0: is(entry.key.contains(filename), false, "web content present in disk cache"); michael@0: }, michael@0: onCacheEntryVisitCompleted: function() michael@0: { michael@0: goon(); michael@0: } michael@0: }; michael@0: function Visitor() {} michael@0: michael@0: var storage = cache.diskCacheStorage(LoadContextInfo.default, false); michael@0: storage.asyncVisitStorage(new Visitor(), true /* Do walk entries */); michael@0: } michael@0: michael@0: function onTransferComplete(downloadSuccess) { michael@0: ok(downloadSuccess, "Image file should have been downloaded successfully"); michael@0: michael@0: // Give the request a chance to finish and create a cache entry michael@0: executeSoon(function() { michael@0: checkDiskCacheFor(fileName, finish); michael@0: mockTransferCallback = null; michael@0: }); michael@0: } 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: saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); michael@0: return saveDir; michael@0: } michael@0: michael@0: function doTest(aIsPrivateMode, aWindow, aCallback) { michael@0: function contextMenuOpened(event) { michael@0: cache.clear(); michael@0: michael@0: aWindow.document.removeEventListener("popupshown", contextMenuOpened); michael@0: michael@0: // Create the folder the image 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: fileName = fp.defaultString; michael@0: destFile.append (fileName); michael@0: MockFilePicker.returnFiles = [destFile]; michael@0: MockFilePicker.filterIndex = 1; // kSaveAsType_URL michael@0: }; michael@0: michael@0: mockTransferCallback = onTransferComplete; michael@0: mockTransferRegisterer.register(); michael@0: michael@0: registerCleanupFunction(function () { michael@0: mockTransferRegisterer.unregister(); michael@0: MockFilePicker.cleanup(); michael@0: destDir.remove(true); michael@0: }); michael@0: michael@0: // Select "Save Image As" option from context menu michael@0: var saveVideoCommand = aWindow.document.getElementById("context-saveimage"); michael@0: saveVideoCommand.doCommand(); michael@0: michael@0: event.target.hidePopup(); michael@0: } michael@0: michael@0: aWindow.gBrowser.addEventListener("pageshow", function pageShown(event) { michael@0: // If data: -url PAC file isn't loaded soon enough, we may get about:privatebrowsing loaded michael@0: if (event.target.location == "about:blank" || michael@0: event.target.location == "about:privatebrowsing") { michael@0: aWindow.gBrowser.selectedBrowser.loadURI(testURI); michael@0: return; michael@0: } michael@0: aWindow.gBrowser.removeEventListener("pageshow", pageShown); michael@0: michael@0: waitForFocus(function () { michael@0: aWindow.document.addEventListener("popupshown", contextMenuOpened, false); michael@0: var img = aWindow.gBrowser.selectedBrowser.contentDocument.getElementById("img"); michael@0: EventUtils.synthesizeMouseAtCenter(img, michael@0: { type: "contextmenu", button: 2 }, michael@0: aWindow.gBrowser.contentWindow); michael@0: }, aWindow.gBrowser.selectedBrowser.contentWindow); michael@0: }); michael@0: } michael@0: michael@0: function testOnWindow(aOptions, aCallback) { michael@0: whenNewWindowLoaded(aOptions, function(aWin) { michael@0: windowsToClose.push(aWin); michael@0: // execute should only be called when need, like when you are opening michael@0: // web pages on the test. If calling executeSoon() is not necesary, then michael@0: // call whenNewWindowLoaded() instead of testOnWindow() on your test. michael@0: executeSoon(function() aCallback(aWin)); michael@0: }); michael@0: }; michael@0: michael@0: // this function is called after calling finish() on the test. michael@0: registerCleanupFunction(function() { michael@0: windowsToClose.forEach(function(aWin) { michael@0: aWin.close(); michael@0: }); michael@0: }); michael@0: michael@0: MockFilePicker.init(window); michael@0: // then test when on private mode michael@0: testOnWindow({private: true}, function(aWin) { michael@0: doTest(true, aWin, finish); 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);