browser/components/privatebrowsing/test/browser/browser_privatebrowsing_DownloadLastDirWithCPS.js

Thu, 15 Jan 2015 21:13:52 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:13:52 +0100
branch
TOR_BUG_9701
changeset 12
7540298fafa1
permissions
-rw-r--r--

Remove forgotten relic of ABI crash risk averse overloaded method change.

michael@0 1 /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 let gTests;
michael@0 7 function test() {
michael@0 8 waitForExplicitFinish();
michael@0 9 requestLongerTimeout(2);
michael@0 10 gTests = runTest();
michael@0 11 gTests.next();
michael@0 12 }
michael@0 13
michael@0 14 /*
michael@0 15 * ================
michael@0 16 * Helper functions
michael@0 17 * ================
michael@0 18 */
michael@0 19
michael@0 20 function moveAlong(aResult) {
michael@0 21 try {
michael@0 22 gTests.send(aResult);
michael@0 23 } catch (x if x instanceof StopIteration) {
michael@0 24 finish();
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 function createWindow(aOptions) {
michael@0 29 whenNewWindowLoaded(aOptions, function(win) {
michael@0 30 moveAlong(win);
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 function getFile(downloadLastDir, aURI) {
michael@0 35 downloadLastDir.getFileAsync(aURI, function(result) {
michael@0 36 moveAlong(result);
michael@0 37 });
michael@0 38 }
michael@0 39
michael@0 40 function setFile(downloadLastDir, aURI, aValue) {
michael@0 41 downloadLastDir.setFile(aURI, aValue);
michael@0 42 executeSoon(moveAlong);
michael@0 43 }
michael@0 44
michael@0 45 function clearHistoryAndWait() {
michael@0 46 clearHistory();
michael@0 47 executeSoon(function() executeSoon(moveAlong));
michael@0 48 }
michael@0 49
michael@0 50 /*
michael@0 51 * ===================
michael@0 52 * Function with tests
michael@0 53 * ===================
michael@0 54 */
michael@0 55
michael@0 56 function runTest() {
michael@0 57 let FileUtils =
michael@0 58 Cu.import("resource://gre/modules/FileUtils.jsm", {}).FileUtils;
michael@0 59 let DownloadLastDir =
michael@0 60 Cu.import("resource://gre/modules/DownloadLastDir.jsm", {}).DownloadLastDir;
michael@0 61
michael@0 62 let tmpDir = FileUtils.getDir("TmpD", [], true);
michael@0 63 let dir1 = newDirectory();
michael@0 64 let dir2 = newDirectory();
michael@0 65 let dir3 = newDirectory();
michael@0 66
michael@0 67 let uri1 = Services.io.newURI("http://test1.com/", null, null);
michael@0 68 let uri2 = Services.io.newURI("http://test2.com/", null, null);
michael@0 69 let uri3 = Services.io.newURI("http://test3.com/", null, null);
michael@0 70 let uri4 = Services.io.newURI("http://test4.com/", null, null);
michael@0 71
michael@0 72 // cleanup functions registration
michael@0 73 registerCleanupFunction(function () {
michael@0 74 Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
michael@0 75 Services.prefs.clearUserPref("browser.download.lastDir");
michael@0 76 [dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
michael@0 77 win.close();
michael@0 78 pbWin.close();
michael@0 79 });
michael@0 80
michael@0 81 function checkDownloadLastDir(gDownloadLastDir, aLastDir) {
michael@0 82 is(gDownloadLastDir.file.path, aLastDir.path,
michael@0 83 "gDownloadLastDir should point to the expected last directory");
michael@0 84 getFile(gDownloadLastDir, uri1);
michael@0 85 }
michael@0 86
michael@0 87 function checkDownloadLastDirNull(gDownloadLastDir) {
michael@0 88 is(gDownloadLastDir.file, null, "gDownloadLastDir should be null");
michael@0 89 getFile(gDownloadLastDir, uri1);
michael@0 90 }
michael@0 91
michael@0 92 /*
michael@0 93 * ================================
michael@0 94 * Create a regular and a PB window
michael@0 95 * ================================
michael@0 96 */
michael@0 97
michael@0 98 let win = yield createWindow({private: false});
michael@0 99 let pbWin = yield createWindow({private: true});
michael@0 100
michael@0 101 let downloadLastDir = new DownloadLastDir(win);
michael@0 102 let pbDownloadLastDir = new DownloadLastDir(pbWin);
michael@0 103
michael@0 104 /*
michael@0 105 * ==================
michael@0 106 * Beginning of tests
michael@0 107 * ==================
michael@0 108 */
michael@0 109
michael@0 110 is(typeof downloadLastDir, "object",
michael@0 111 "downloadLastDir should be a valid object");
michael@0 112 is(downloadLastDir.file, null,
michael@0 113 "LastDir pref should be null to start with");
michael@0 114
michael@0 115 // set up last dir
michael@0 116 yield setFile(downloadLastDir, null, tmpDir);
michael@0 117 is(downloadLastDir.file.path, tmpDir.path,
michael@0 118 "LastDir should point to the tmpDir");
michael@0 119 isnot(downloadLastDir.file, tmpDir,
michael@0 120 "downloadLastDir.file should not be pointing to tmpDir");
michael@0 121
michael@0 122 // set uri1 to dir1, all should now return dir1
michael@0 123 // also check that a new object is returned
michael@0 124 yield setFile(downloadLastDir, uri1, dir1);
michael@0 125 is(downloadLastDir.file.path, dir1.path,
michael@0 126 "downloadLastDir should return dir1");
michael@0 127 isnot(downloadLastDir.file, dir1,
michael@0 128 "downloadLastDir.file should not return dir1");
michael@0 129 is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
michael@0 130 "uri1 should return dir1"); // set in CPS
michael@0 131 isnot((yield getFile(downloadLastDir, uri1)), dir1,
michael@0 132 "getFile on uri1 should not return dir1");
michael@0 133 is((yield getFile(downloadLastDir, uri2)).path, dir1.path,
michael@0 134 "uri2 should return dir1"); // fallback
michael@0 135 isnot((yield getFile(downloadLastDir, uri2)), dir1,
michael@0 136 "getFile on uri2 should not return dir1");
michael@0 137 is((yield getFile(downloadLastDir, uri3)).path, dir1.path,
michael@0 138 "uri3 should return dir1"); // fallback
michael@0 139 isnot((yield getFile(downloadLastDir, uri3)), dir1,
michael@0 140 "getFile on uri3 should not return dir1");
michael@0 141 is((yield getFile(downloadLastDir, uri4)).path, dir1.path,
michael@0 142 "uri4 should return dir1"); // fallback
michael@0 143 isnot((yield getFile(downloadLastDir, uri4)), dir1,
michael@0 144 "getFile on uri4 should not return dir1");
michael@0 145
michael@0 146 // set uri2 to dir2, all except uri1 should now return dir2
michael@0 147 yield setFile(downloadLastDir, uri2, dir2);
michael@0 148 is(downloadLastDir.file.path, dir2.path,
michael@0 149 "downloadLastDir should point to dir2");
michael@0 150 is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
michael@0 151 "uri1 should return dir1"); // set in CPS
michael@0 152 is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
michael@0 153 "uri2 should return dir2"); // set in CPS
michael@0 154 is((yield getFile(downloadLastDir, uri3)).path, dir2.path,
michael@0 155 "uri3 should return dir2"); // fallback
michael@0 156 is((yield getFile(downloadLastDir, uri4)).path, dir2.path,
michael@0 157 "uri4 should return dir2"); // fallback
michael@0 158
michael@0 159 // set uri3 to dir3, all except uri1 and uri2 should now return dir3
michael@0 160 yield setFile(downloadLastDir, uri3, dir3);
michael@0 161 is(downloadLastDir.file.path, dir3.path,
michael@0 162 "downloadLastDir should point to dir3");
michael@0 163 is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
michael@0 164 "uri1 should return dir1"); // set in CPS
michael@0 165 is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
michael@0 166 "uri2 should return dir2"); // set in CPS
michael@0 167 is((yield getFile(downloadLastDir, uri3)).path, dir3.path,
michael@0 168 "uri3 should return dir3"); // set in CPS
michael@0 169 is((yield getFile(downloadLastDir, uri4)).path, dir3.path,
michael@0 170 "uri4 should return dir4"); // fallback
michael@0 171
michael@0 172 // set uri1 to dir2, all except uri3 should now return dir2
michael@0 173 yield setFile(downloadLastDir, uri1, dir2);
michael@0 174 is(downloadLastDir.file.path, dir2.path,
michael@0 175 "downloadLastDir should point to dir2");
michael@0 176 is((yield getFile(downloadLastDir, uri1)).path, dir2.path,
michael@0 177 "uri1 should return dir2"); // set in CPS
michael@0 178 is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
michael@0 179 "uri2 should return dir2"); // set in CPS
michael@0 180 is((yield getFile(downloadLastDir, uri3)).path, dir3.path,
michael@0 181 "uri3 should return dir3"); // set in CPS
michael@0 182 is((yield getFile(downloadLastDir, uri4)).path, dir2.path,
michael@0 183 "uri4 should return dir2"); // fallback
michael@0 184
michael@0 185 yield clearHistoryAndWait();
michael@0 186
michael@0 187 // check clearHistory removes all data
michael@0 188 is(downloadLastDir.file, null, "clearHistory removes all data");
michael@0 189 //is(Services.contentPrefs.hasPref(uri1, "browser.download.lastDir", null),
michael@0 190 // false, "LastDir preference should be absent");
michael@0 191 is((yield getFile(downloadLastDir, uri1)), null, "uri1 should point to null");
michael@0 192 is((yield getFile(downloadLastDir, uri2)), null, "uri2 should point to null");
michael@0 193 is((yield getFile(downloadLastDir, uri3)), null, "uri3 should point to null");
michael@0 194 is((yield getFile(downloadLastDir, uri4)), null, "uri4 should point to null");
michael@0 195
michael@0 196 yield setFile(downloadLastDir, null, tmpDir);
michael@0 197
michael@0 198 // check data set outside PB mode is remembered
michael@0 199 is((yield checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
michael@0 200 is((yield checkDownloadLastDir(downloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
michael@0 201 yield clearHistoryAndWait();
michael@0 202
michael@0 203 yield setFile(downloadLastDir, uri1, dir1);
michael@0 204
michael@0 205 // check data set using CPS outside PB mode is remembered
michael@0 206 is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
michael@0 207 is((yield checkDownloadLastDir(downloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
michael@0 208 yield clearHistoryAndWait();
michael@0 209
michael@0 210 // check data set inside PB mode is forgotten
michael@0 211 yield setFile(pbDownloadLastDir, null, tmpDir);
michael@0 212
michael@0 213 is((yield checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
michael@0 214 is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
michael@0 215
michael@0 216 yield clearHistoryAndWait();
michael@0 217
michael@0 218 // check data set using CPS inside PB mode is forgotten
michael@0 219 yield setFile(pbDownloadLastDir, uri1, dir1);
michael@0 220
michael@0 221 is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
michael@0 222 is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
michael@0 223
michael@0 224 // check data set outside PB mode but changed inside is remembered correctly
michael@0 225 yield setFile(downloadLastDir, uri1, dir1);
michael@0 226 yield setFile(pbDownloadLastDir, uri1, dir2);
michael@0 227 is((yield checkDownloadLastDir(pbDownloadLastDir, dir2)).path, dir2.path, "uri1 should return the expected last directory");
michael@0 228 is((yield checkDownloadLastDir(downloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
michael@0 229
michael@0 230 /*
michael@0 231 * ====================
michael@0 232 * Create new PB window
michael@0 233 * ====================
michael@0 234 */
michael@0 235
michael@0 236 // check that the last dir store got cleared in a new PB window
michael@0 237 pbWin.close();
michael@0 238 // And give it time to close
michael@0 239 executeSoon(moveAlong);
michael@0 240 yield;
michael@0 241 let pbWin = yield createWindow({private: true});
michael@0 242 let pbDownloadLastDir = new DownloadLastDir(pbWin);
michael@0 243
michael@0 244 is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
michael@0 245
michael@0 246 yield clearHistoryAndWait();
michael@0 247
michael@0 248 // check clearHistory inside PB mode clears data outside PB mode
michael@0 249 yield setFile(pbDownloadLastDir, uri1, dir2);
michael@0 250
michael@0 251 yield clearHistoryAndWait();
michael@0 252
michael@0 253 is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
michael@0 254 is((yield checkDownloadLastDirNull(pbDownloadLastDir)), null, "uri1 should return the expected last directory");
michael@0 255
michael@0 256 // check that disabling CPS works
michael@0 257 Services.prefs.setBoolPref("browser.download.lastDir.savePerSite", false);
michael@0 258
michael@0 259 yield setFile(downloadLastDir, uri1, dir1);
michael@0 260 is(downloadLastDir.file.path, dir1.path, "LastDir should be set to dir1");
michael@0 261 is((yield getFile(downloadLastDir, uri1)).path, dir1.path, "uri1 should return dir1");
michael@0 262 is((yield getFile(downloadLastDir, uri2)).path, dir1.path, "uri2 should return dir1");
michael@0 263 is((yield getFile(downloadLastDir, uri3)).path, dir1.path, "uri3 should return dir1");
michael@0 264 is((yield getFile(downloadLastDir, uri4)).path, dir1.path, "uri4 should return dir1");
michael@0 265
michael@0 266 downloadLastDir.setFile(uri2, dir2);
michael@0 267 is(downloadLastDir.file.path, dir2.path, "LastDir should be set to dir2");
michael@0 268 is((yield getFile(downloadLastDir, uri1)).path, dir2.path, "uri1 should return dir2");
michael@0 269 is((yield getFile(downloadLastDir, uri2)).path, dir2.path, "uri2 should return dir2");
michael@0 270 is((yield getFile(downloadLastDir, uri3)).path, dir2.path, "uri3 should return dir2");
michael@0 271 is((yield getFile(downloadLastDir, uri4)).path, dir2.path, "uri4 should return dir2");
michael@0 272
michael@0 273 Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
michael@0 274
michael@0 275 // check that passing null to setFile clears the stored value
michael@0 276 yield setFile(downloadLastDir, uri3, dir3);
michael@0 277 is((yield getFile(downloadLastDir, uri3)).path, dir3.path, "LastDir should be set to dir3");
michael@0 278 yield setFile(downloadLastDir, uri3, null);
michael@0 279 is((yield getFile(downloadLastDir, uri3)), null, "uri3 should return null");
michael@0 280
michael@0 281 yield clearHistoryAndWait();
michael@0 282 }

mercurial