Bug 854299. Part 3. Change tests accordingly to support operations that are now async. r=me

This commit is contained in:
Felipe Gomes 2013-04-01 03:09:01 -03:00
parent bbbc13ab19
commit 508023e187
3 changed files with 239 additions and 216 deletions

View File

@ -8,17 +8,51 @@ function test() {
waitForExplicitFinish();
requestLongerTimeout(2);
gTests = runTest();
moveAlong();
gTests.next();
}
function moveAlong() {
/*
* ================
* Helper functions
* ================
*/
function moveAlong(aResult) {
try {
gTests.next();
gTests.send(aResult);
} catch (x if x instanceof StopIteration) {
finish();
}
}
function createWindow(aOptions) {
whenNewWindowLoaded(aOptions, function(win) {
moveAlong(win);
});
}
function getFile(downloadLastDir, aURI) {
downloadLastDir.getFileAsync(aURI, function(result) {
moveAlong(result);
});
}
function setFile(downloadLastDir, aURI, aValue) {
downloadLastDir.setFile(aURI, aValue);
executeSoon(moveAlong);
}
function clearHistoryAndWait() {
clearHistory();
executeSoon(function() executeSoon(moveAlong));
}
/*
* ===================
* Function with tests
* ===================
*/
function runTest() {
let FileUtils =
Cu.import("resource://gre/modules/FileUtils.jsm", {}).FileUtils;
@ -40,219 +74,206 @@ function runTest() {
Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
Services.prefs.clearUserPref("browser.download.lastDir");
[dir1, dir2, dir3].forEach(function(dir) dir.remove(true));
win.close();
pbWin.close();
});
function testOnWindow(aPrivate, aCallback) {
whenNewWindowLoaded({private: aPrivate}, function(win) {
let gDownloadLastDir = new DownloadLastDir(win);
aCallback(win, gDownloadLastDir);
gDownloadLastDir.cleanupPrivateFile();
win.close();
executeSoon(moveAlong);
});
}
function checkInit(aWin, gDownloadLastDir) {
is(typeof gDownloadLastDir, "object",
"gDownloadLastDir should be a valid object");
is(gDownloadLastDir.file, null,
"LastDir pref should be null to start with");
// set up last dir
gDownloadLastDir.setFile(null, tmpDir);
is(gDownloadLastDir.file.path, tmpDir.path,
"LastDir should point to the tmpDir");
isnot(gDownloadLastDir.file, tmpDir,
"gDownloadLastDir.file should not be pointing to tmpDir");
// set uri1 to dir1, all should now return dir1
// also check that a new object is returned
gDownloadLastDir.setFile(uri1, dir1);
is(gDownloadLastDir.file.path, dir1.path,
"gDownloadLastDir should return dir1");
isnot(gDownloadLastDir.file, dir1,
"gDownloadLastDir.file should not return dir1");
is(gDownloadLastDir.getFile(uri1).path, dir1.path,
"uri1 should return dir1"); // set in CPS
isnot(gDownloadLastDir.getFile(uri1), dir1,
"getFile on uri1 should not return dir1");
is(gDownloadLastDir.getFile(uri2).path, dir1.path,
"uri2 should return dir1"); // fallback
isnot(gDownloadLastDir.getFile(uri2), dir1,
"getFile on uri2 should not return dir1");
is(gDownloadLastDir.getFile(uri3).path, dir1.path,
"uri3 should return dir1"); // fallback
isnot(gDownloadLastDir.getFile(uri3), dir1,
"getFile on uri3 should not return dir1");
is(gDownloadLastDir.getFile(uri4).path, dir1.path,
"uri4 should return dir1"); // fallback
isnot(gDownloadLastDir.getFile(uri4), dir1,
"getFile on uri4 should not return dir1");
// set uri2 to dir2, all except uri1 should now return dir2
gDownloadLastDir.setFile(uri2, dir2);
is(gDownloadLastDir.file.path, dir2.path,
"gDownloadLastDir should point to dir2");
is(gDownloadLastDir.getFile(uri1).path, dir1.path,
"uri1 should return dir1"); // set in CPS
is(gDownloadLastDir.getFile(uri2).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is(gDownloadLastDir.getFile(uri3).path, dir2.path,
"uri3 should return dir2"); // fallback
is(gDownloadLastDir.getFile(uri4).path, dir2.path,
"uri4 should return dir2"); // fallback
// set uri3 to dir3, all except uri1 and uri2 should now return dir3
gDownloadLastDir.setFile(uri3, dir3);
is(gDownloadLastDir.file.path, dir3.path,
"gDownloadLastDir should point to dir3");
is(gDownloadLastDir.getFile(uri1).path, dir1.path,
"uri1 should return dir1"); // set in CPS
is(gDownloadLastDir.getFile(uri2).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is(gDownloadLastDir.getFile(uri3).path, dir3.path,
"uri3 should return dir3"); // set in CPS
is(gDownloadLastDir.getFile(uri4).path, dir3.path,
"uri4 should return dir4"); // fallback
// set uri1 to dir2, all except uri3 should now return dir2
gDownloadLastDir.setFile(uri1, dir2);
is(gDownloadLastDir.file.path, dir2.path,
"gDownloadLastDir should point to dir2");
is(gDownloadLastDir.getFile(uri1).path, dir2.path,
"uri1 should return dir2"); // set in CPS
is(gDownloadLastDir.getFile(uri2).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is(gDownloadLastDir.getFile(uri3).path, dir3.path,
"uri3 should return dir3"); // set in CPS
is(gDownloadLastDir.getFile(uri4).path, dir2.path,
"uri4 should return dir2"); // fallback
}
function checkDownloadLastDir(aWin, gDownloadLastDir, aLastDir) {
function checkDownloadLastDir(gDownloadLastDir, aLastDir) {
is(gDownloadLastDir.file.path, aLastDir.path,
"gDownloadLastDir should point to the expected last directory");
is(gDownloadLastDir.getFile(uri1).path, aLastDir.path,
"uri1 should return the expected last directory");
getFile(gDownloadLastDir, uri1);
}
function checkDownloadLastDirNull(aWin, gDownloadLastDir) {
function checkDownloadLastDirNull(gDownloadLastDir) {
is(gDownloadLastDir.file, null, "gDownloadLastDir should be null");
is(gDownloadLastDir.getFile(uri1), null, "uri1 should return null");
getFile(gDownloadLastDir, uri1);
}
function checkSetFile(gDownloadLastDir, aDir1, aDir2, aDir3) {
// check that disabling CPS works
Services.prefs.setBoolPref("browser.download.lastDir.savePerSite", false);
/*
* ================================
* Create a regular and a PB window
* ================================
*/
gDownloadLastDir.setFile(uri1, aDir1);
is(gDownloadLastDir.file.path, aDir1.path, "LastDir should be set to dir1");
is(gDownloadLastDir.getFile(uri1).path, aDir1.path, "uri1 should return dir1");
is(gDownloadLastDir.getFile(uri2).path, aDir1.path, "uri2 should return dir1");
is(gDownloadLastDir.getFile(uri3).path, aDir1.path, "uri3 should return dir1");
is(gDownloadLastDir.getFile(uri4).path, aDir1.path, "uri4 should return dir1");
let win = yield createWindow({private: false});
let pbWin = yield createWindow({private: true});
gDownloadLastDir.setFile(uri2, aDir2);
is(gDownloadLastDir.file.path, aDir2.path, "LastDir should be set to dir2");
is(gDownloadLastDir.getFile(uri1).path, aDir2.path, "uri1 should return dir2");
is(gDownloadLastDir.getFile(uri2).path, aDir2.path, "uri2 should return dir2");
is(gDownloadLastDir.getFile(uri3).path, aDir2.path, "uri3 should return dir2");
is(gDownloadLastDir.getFile(uri4).path, aDir2.path, "uri4 should return dir2");
let downloadLastDir = new DownloadLastDir(win);
let pbDownloadLastDir = new DownloadLastDir(pbWin);
Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
/*
* ==================
* Beginning of tests
* ==================
*/
// check that passing null to setFile clears the stored value
gDownloadLastDir.setFile(uri3, aDir3);
is(gDownloadLastDir.getFile(uri3).path, aDir3.path, "LastDir should be set to dir3");
gDownloadLastDir.setFile(uri3, null);
is(gDownloadLastDir.getFile(uri3), null, "uri3 should return null");
}
is(typeof downloadLastDir, "object",
"downloadLastDir should be a valid object");
is(downloadLastDir.file, null,
"LastDir pref should be null to start with");
yield testOnWindow(false, function(win, downloadDir) {
checkInit(win, downloadDir);
clearHistory();
});
// set up last dir
yield setFile(downloadLastDir, null, tmpDir);
is(downloadLastDir.file.path, tmpDir.path,
"LastDir should point to the tmpDir");
isnot(downloadLastDir.file, tmpDir,
"downloadLastDir.file should not be pointing to tmpDir");
yield testOnWindow(false, function(win, downloadDir) {
// check clearHistory removes all data
is(downloadDir.file, null, "clearHistory removes all data");
is(Services.contentPrefs.hasPref(uri1, "browser.download.lastDir", null),
false, "LastDir preference should be absent");
is(downloadDir.getFile(uri1), null, "uri1 should point to null");
is(downloadDir.getFile(uri2), null, "uri2 should point to null");
is(downloadDir.getFile(uri3), null, "uri3 should point to null");
is(downloadDir.getFile(uri4), null, "uri4 should point to null");
// set uri1 to dir1, all should now return dir1
// also check that a new object is returned
yield setFile(downloadLastDir, uri1, dir1);
is(downloadLastDir.file.path, dir1.path,
"downloadLastDir should return dir1");
isnot(downloadLastDir.file, dir1,
"downloadLastDir.file should not return dir1");
is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
"uri1 should return dir1"); // set in CPS
isnot((yield getFile(downloadLastDir, uri1)), dir1,
"getFile on uri1 should not return dir1");
is((yield getFile(downloadLastDir, uri2)).path, dir1.path,
"uri2 should return dir1"); // fallback
isnot((yield getFile(downloadLastDir, uri2)), dir1,
"getFile on uri2 should not return dir1");
is((yield getFile(downloadLastDir, uri3)).path, dir1.path,
"uri3 should return dir1"); // fallback
isnot((yield getFile(downloadLastDir, uri3)), dir1,
"getFile on uri3 should not return dir1");
is((yield getFile(downloadLastDir, uri4)).path, dir1.path,
"uri4 should return dir1"); // fallback
isnot((yield getFile(downloadLastDir, uri4)), dir1,
"getFile on uri4 should not return dir1");
downloadDir.setFile(null, tmpDir);
});
// set uri2 to dir2, all except uri1 should now return dir2
yield setFile(downloadLastDir, uri2, dir2);
is(downloadLastDir.file.path, dir2.path,
"downloadLastDir should point to dir2");
is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
"uri1 should return dir1"); // set in CPS
is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is((yield getFile(downloadLastDir, uri3)).path, dir2.path,
"uri3 should return dir2"); // fallback
is((yield getFile(downloadLastDir, uri4)).path, dir2.path,
"uri4 should return dir2"); // fallback
// set uri3 to dir3, all except uri1 and uri2 should now return dir3
yield setFile(downloadLastDir, uri3, dir3);
is(downloadLastDir.file.path, dir3.path,
"downloadLastDir should point to dir3");
is((yield getFile(downloadLastDir, uri1)).path, dir1.path,
"uri1 should return dir1"); // set in CPS
is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is((yield getFile(downloadLastDir, uri3)).path, dir3.path,
"uri3 should return dir3"); // set in CPS
is((yield getFile(downloadLastDir, uri4)).path, dir3.path,
"uri4 should return dir4"); // fallback
// set uri1 to dir2, all except uri3 should now return dir2
yield setFile(downloadLastDir, uri1, dir2);
is(downloadLastDir.file.path, dir2.path,
"downloadLastDir should point to dir2");
is((yield getFile(downloadLastDir, uri1)).path, dir2.path,
"uri1 should return dir2"); // set in CPS
is((yield getFile(downloadLastDir, uri2)).path, dir2.path,
"uri2 should return dir2"); // set in CPS
is((yield getFile(downloadLastDir, uri3)).path, dir3.path,
"uri3 should return dir3"); // set in CPS
is((yield getFile(downloadLastDir, uri4)).path, dir2.path,
"uri4 should return dir2"); // fallback
yield clearHistoryAndWait();
// check clearHistory removes all data
is(downloadLastDir.file, null, "clearHistory removes all data");
//is(Services.contentPrefs.hasPref(uri1, "browser.download.lastDir", null),
// false, "LastDir preference should be absent");
is((yield getFile(downloadLastDir, uri1)), null, "uri1 should point to null");
is((yield getFile(downloadLastDir, uri2)), null, "uri2 should point to null");
is((yield getFile(downloadLastDir, uri3)), null, "uri3 should point to null");
is((yield getFile(downloadLastDir, uri4)), null, "uri4 should point to null");
yield setFile(downloadLastDir, null, tmpDir);
// check data set outside PB mode is remembered
yield testOnWindow(true, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, tmpDir);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, tmpDir);
clearHistory();
downloadDir.setFile(uri1, dir1);
});
is((yield checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
is((yield checkDownloadLastDir(downloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
yield clearHistoryAndWait();
yield setFile(downloadLastDir, uri1, dir1);
// check data set using CPS outside PB mode is remembered
yield testOnWindow(true, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, dir1);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, dir1);
clearHistory();
});
is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
is((yield checkDownloadLastDir(downloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
yield clearHistoryAndWait();
// check data set inside PB mode is forgotten
yield testOnWindow(true, function(win, downloadDir) {
downloadDir.setFile(null, tmpDir);
checkDownloadLastDir(win, downloadDir, tmpDir);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDirNull(win, downloadDir);
clearHistory();
});
yield setFile(pbDownloadLastDir, null, tmpDir);
is((yield checkDownloadLastDir(pbDownloadLastDir, tmpDir)).path, tmpDir.path, "uri1 should return the expected last directory");
is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
yield clearHistoryAndWait();
// check data set using CPS inside PB mode is forgotten
yield testOnWindow(true, function(win, downloadDir) {
downloadDir.setFile(uri1, dir1);
checkDownloadLastDir(win, downloadDir, dir1);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDirNull(win, downloadDir);
clearHistory();
downloadDir.setFile(uri1, dir1);
});
yield setFile(pbDownloadLastDir, uri1, dir1);
is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
// check data set outside PB mode but changed inside is remembered correctly
yield testOnWindow(true, function(win, downloadDir) {
downloadDir.setFile(uri1, dir2);
checkDownloadLastDir(win, downloadDir, dir2);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, dir1);
});
yield setFile(downloadLastDir, uri1, dir1);
yield setFile(pbDownloadLastDir, uri1, dir2);
is((yield checkDownloadLastDir(pbDownloadLastDir, dir2)).path, dir2.path, "uri1 should return the expected last directory");
is((yield checkDownloadLastDir(downloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
// check that the last dir store got cleared
yield testOnWindow(true, function(win, downloadDir) {
checkDownloadLastDir(win, downloadDir, dir1);
});
yield testOnWindow(false, function(win, downloadDir) {
clearHistory();
});
/*
* ====================
* Create new PB window
* ====================
*/
// check that the last dir store got cleared in a new PB window
pbWin.close();
let pbWin = yield createWindow({private: true});
let pbDownloadLastDir = new DownloadLastDir(pbWin);
is((yield checkDownloadLastDir(pbDownloadLastDir, dir1)).path, dir1.path, "uri1 should return the expected last directory");
yield clearHistoryAndWait();
// check clearHistory inside PB mode clears data outside PB mode
yield testOnWindow(true, function(win, downloadDir) {
downloadDir.setFile(uri1, dir2);
clearHistory();
});
yield testOnWindow(true, function(win, downloadDir) {
checkDownloadLastDirNull(win, downloadDir);
});
yield testOnWindow(false, function(win, downloadDir) {
checkDownloadLastDirNull(win, downloadDir);
checkSetFile(downloadDir, dir1, dir2, dir3);
});
yield setFile(pbDownloadLastDir, uri1, dir2);
yield clearHistoryAndWait();
is((yield checkDownloadLastDirNull(downloadLastDir)), null, "uri1 should return the expected last directory");
is((yield checkDownloadLastDirNull(pbDownloadLastDir)), null, "uri1 should return the expected last directory");
// check that disabling CPS works
Services.prefs.setBoolPref("browser.download.lastDir.savePerSite", false);
yield setFile(downloadLastDir, uri1, dir1);
is(downloadLastDir.file.path, dir1.path, "LastDir should be set to dir1");
is((yield getFile(downloadLastDir, uri1)).path, dir1.path, "uri1 should return dir1");
is((yield getFile(downloadLastDir, uri2)).path, dir1.path, "uri2 should return dir1");
is((yield getFile(downloadLastDir, uri3)).path, dir1.path, "uri3 should return dir1");
is((yield getFile(downloadLastDir, uri4)).path, dir1.path, "uri4 should return dir1");
downloadLastDir.setFile(uri2, dir2);
is(downloadLastDir.file.path, dir2.path, "LastDir should be set to dir2");
is((yield getFile(downloadLastDir, uri1)).path, dir2.path, "uri1 should return dir2");
is((yield getFile(downloadLastDir, uri2)).path, dir2.path, "uri2 should return dir2");
is((yield getFile(downloadLastDir, uri3)).path, dir2.path, "uri3 should return dir2");
is((yield getFile(downloadLastDir, uri4)).path, dir2.path, "uri4 should return dir2");
Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
// check that passing null to setFile clears the stored value
yield setFile(downloadLastDir, uri3, dir3);
is((yield getFile(downloadLastDir, uri3)).path, dir3.path, "LastDir should be set to dir3");
yield setFile(downloadLastDir, uri3, null);
is((yield getFile(downloadLastDir, uri3)), null, "uri3 should return null");
yield clearHistoryAndWait();
}

View File

@ -43,7 +43,6 @@ function test() {
let gDownloadLastDir = new DownloadLastDir(win);
aCallback(win, gDownloadLastDir);
gDownloadLastDir.cleanupPrivateFile();
win.close();
});
}
@ -60,21 +59,25 @@ function test() {
MockFilePicker.returnFiles = [aFile];
MockFilePicker.displayDirectory = null;
let file =
launcherDialog.promptForSaveToFile(launcher, context, null, null, null);
ok(!!file, "promptForSaveToFile correctly returned a file");
// File picker should start with expected display dir.
is(MockFilePicker.displayDirectory.path, aDisplayDir.path,
"File picker should start with browser.download.lastDir");
// browser.download.lastDir should be modified on not private windows
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, aLastDir.path,
"LastDir should be the expected last dir");
// gDownloadLastDir should be usable outside of private windows
is(gDownloadLastDir.file.path, aGlobalLastDir.path,
"gDownloadLastDir should be the expected global last dir");
launcher.saveDestinationAvailable = function (file) {
ok(!!file, "promptForSaveToFile correctly returned a file");
aCallback();
// File picker should start with expected display dir.
is(MockFilePicker.displayDirectory.path, aDisplayDir.path,
"File picker should start with browser.download.lastDir");
// browser.download.lastDir should be modified on not private windows
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, aLastDir.path,
"LastDir should be the expected last dir");
// gDownloadLastDir should be usable outside of private windows
is(gDownloadLastDir.file.path, aGlobalLastDir.path,
"gDownloadLastDir should be the expected global last dir");
aWin.close();
aCallback();
};
launcherDialog.promptForSaveToFileAsync(launcher, context, null, null, null);
}
testOnWindow(false, function(win, downloadDir) {

View File

@ -66,20 +66,19 @@ function test() {
MockFilePicker.returnFiles = [aFile];
MockFilePicker.displayDirectory = null;
ok(aWin.getTargetFile(params),
"Show the file picker dialog with given params");
aWin.getTargetFile(params, function() {
// File picker should start with expected display dir.
is(MockFilePicker.displayDirectory.path, aDisplayDir.path,
"File picker should start with browser.download.lastDir");
// browser.download.lastDir should be modified on not private windows
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, aLastDir.path,
"LastDir should be the expected last dir");
// gDownloadLastDir should be usable outside of private windows
is(gDownloadLastDir.file.path, aGlobalLastDir.path,
"gDownloadLastDir should be the expected global last dir");
// File picker should start with expected display dir.
is(MockFilePicker.displayDirectory.path, aDisplayDir.path,
"File picker should start with browser.download.lastDir");
// browser.download.lastDir should be modified on not private windows
is(prefs.getComplexValue("lastDir", Ci.nsIFile).path, aLastDir.path,
"LastDir should be the expected last dir");
// gDownloadLastDir should be usable outside of private windows
is(gDownloadLastDir.file.path, aGlobalLastDir.path,
"gDownloadLastDir should be the expected global last dir");
aCallback();
aCallback();
});
}
testOnWindow(false, function(win, downloadDir) {