Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | let {LoadContextInfo} = Cu.import("resource://gre/modules/LoadContextInfo.jsm", null); |
michael@0 | 6 | |
michael@0 | 7 | function test() { |
michael@0 | 8 | // initialization |
michael@0 | 9 | waitForExplicitFinish(); |
michael@0 | 10 | let windowsToClose = []; |
michael@0 | 11 | let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.html"; |
michael@0 | 12 | let fileName; |
michael@0 | 13 | let MockFilePicker = SpecialPowers.MockFilePicker; |
michael@0 | 14 | let cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"] |
michael@0 | 15 | .getService(Ci.nsICacheStorageService); |
michael@0 | 16 | |
michael@0 | 17 | function checkDiskCacheFor(filename, goon) { |
michael@0 | 18 | Visitor.prototype = { |
michael@0 | 19 | onCacheStorageInfo: function(num, consumption) |
michael@0 | 20 | { |
michael@0 | 21 | info("disk storage contains " + num + " entries"); |
michael@0 | 22 | }, |
michael@0 | 23 | onCacheEntryInfo: function(entry) |
michael@0 | 24 | { |
michael@0 | 25 | info(entry.key); |
michael@0 | 26 | is(entry.key.contains(filename), false, "web content present in disk cache"); |
michael@0 | 27 | }, |
michael@0 | 28 | onCacheEntryVisitCompleted: function() |
michael@0 | 29 | { |
michael@0 | 30 | goon(); |
michael@0 | 31 | } |
michael@0 | 32 | }; |
michael@0 | 33 | function Visitor() {} |
michael@0 | 34 | |
michael@0 | 35 | var storage = cache.diskCacheStorage(LoadContextInfo.default, false); |
michael@0 | 36 | storage.asyncVisitStorage(new Visitor(), true /* Do walk entries */); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | function onTransferComplete(downloadSuccess) { |
michael@0 | 40 | ok(downloadSuccess, "Image file should have been downloaded successfully"); |
michael@0 | 41 | |
michael@0 | 42 | // Give the request a chance to finish and create a cache entry |
michael@0 | 43 | executeSoon(function() { |
michael@0 | 44 | checkDiskCacheFor(fileName, finish); |
michael@0 | 45 | mockTransferCallback = null; |
michael@0 | 46 | }); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function createTemporarySaveDirectory() { |
michael@0 | 50 | var saveDir = Cc["@mozilla.org/file/directory_service;1"] |
michael@0 | 51 | .getService(Ci.nsIProperties) |
michael@0 | 52 | .get("TmpD", Ci.nsIFile); |
michael@0 | 53 | saveDir.append("testsavedir"); |
michael@0 | 54 | if (!saveDir.exists()) |
michael@0 | 55 | saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); |
michael@0 | 56 | return saveDir; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | function doTest(aIsPrivateMode, aWindow, aCallback) { |
michael@0 | 60 | function contextMenuOpened(event) { |
michael@0 | 61 | cache.clear(); |
michael@0 | 62 | |
michael@0 | 63 | aWindow.document.removeEventListener("popupshown", contextMenuOpened); |
michael@0 | 64 | |
michael@0 | 65 | // Create the folder the image will be saved into. |
michael@0 | 66 | var destDir = createTemporarySaveDirectory(); |
michael@0 | 67 | var destFile = destDir.clone(); |
michael@0 | 68 | |
michael@0 | 69 | MockFilePicker.displayDirectory = destDir; |
michael@0 | 70 | MockFilePicker.showCallback = function(fp) { |
michael@0 | 71 | fileName = fp.defaultString; |
michael@0 | 72 | destFile.append (fileName); |
michael@0 | 73 | MockFilePicker.returnFiles = [destFile]; |
michael@0 | 74 | MockFilePicker.filterIndex = 1; // kSaveAsType_URL |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | mockTransferCallback = onTransferComplete; |
michael@0 | 78 | mockTransferRegisterer.register(); |
michael@0 | 79 | |
michael@0 | 80 | registerCleanupFunction(function () { |
michael@0 | 81 | mockTransferRegisterer.unregister(); |
michael@0 | 82 | MockFilePicker.cleanup(); |
michael@0 | 83 | destDir.remove(true); |
michael@0 | 84 | }); |
michael@0 | 85 | |
michael@0 | 86 | // Select "Save Image As" option from context menu |
michael@0 | 87 | var saveVideoCommand = aWindow.document.getElementById("context-saveimage"); |
michael@0 | 88 | saveVideoCommand.doCommand(); |
michael@0 | 89 | |
michael@0 | 90 | event.target.hidePopup(); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | aWindow.gBrowser.addEventListener("pageshow", function pageShown(event) { |
michael@0 | 94 | // If data: -url PAC file isn't loaded soon enough, we may get about:privatebrowsing loaded |
michael@0 | 95 | if (event.target.location == "about:blank" || |
michael@0 | 96 | event.target.location == "about:privatebrowsing") { |
michael@0 | 97 | aWindow.gBrowser.selectedBrowser.loadURI(testURI); |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | aWindow.gBrowser.removeEventListener("pageshow", pageShown); |
michael@0 | 101 | |
michael@0 | 102 | waitForFocus(function () { |
michael@0 | 103 | aWindow.document.addEventListener("popupshown", contextMenuOpened, false); |
michael@0 | 104 | var img = aWindow.gBrowser.selectedBrowser.contentDocument.getElementById("img"); |
michael@0 | 105 | EventUtils.synthesizeMouseAtCenter(img, |
michael@0 | 106 | { type: "contextmenu", button: 2 }, |
michael@0 | 107 | aWindow.gBrowser.contentWindow); |
michael@0 | 108 | }, aWindow.gBrowser.selectedBrowser.contentWindow); |
michael@0 | 109 | }); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | function testOnWindow(aOptions, aCallback) { |
michael@0 | 113 | whenNewWindowLoaded(aOptions, function(aWin) { |
michael@0 | 114 | windowsToClose.push(aWin); |
michael@0 | 115 | // execute should only be called when need, like when you are opening |
michael@0 | 116 | // web pages on the test. If calling executeSoon() is not necesary, then |
michael@0 | 117 | // call whenNewWindowLoaded() instead of testOnWindow() on your test. |
michael@0 | 118 | executeSoon(function() aCallback(aWin)); |
michael@0 | 119 | }); |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | // this function is called after calling finish() on the test. |
michael@0 | 123 | registerCleanupFunction(function() { |
michael@0 | 124 | windowsToClose.forEach(function(aWin) { |
michael@0 | 125 | aWin.close(); |
michael@0 | 126 | }); |
michael@0 | 127 | }); |
michael@0 | 128 | |
michael@0 | 129 | MockFilePicker.init(window); |
michael@0 | 130 | // then test when on private mode |
michael@0 | 131 | testOnWindow({private: true}, function(aWin) { |
michael@0 | 132 | doTest(true, aWin, finish); |
michael@0 | 133 | }); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | Cc["@mozilla.org/moz/jssubscript-loader;1"] |
michael@0 | 137 | .getService(Ci.mozIJSSubScriptLoader) |
michael@0 | 138 | .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", |
michael@0 | 139 | this); |