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