Bug 566746 - Use Asynchronous FormHistory.jsm for test_db_corrupt.js, p=enndeakin,felix, r=dolske

This commit is contained in:
Felix Fung 2011-12-16 15:53:29 -08:00
parent 5746f24edd
commit 427ed2430c

View File

@ -2,65 +2,119 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
function run_test()
{
try {
var testnum = 0;
let bakFile;
function run_test() {
// ===== test init =====
var testfile = do_get_file("formhistory_CORRUPT.sqlite");
var profileDir = dirSvc.get("ProfD", Ci.nsIFile);
let testfile = do_get_file("formhistory_CORRUPT.sqlite");
let profileDir = dirSvc.get("ProfD", Ci.nsIFile);
// Cleanup from any previous tests or failures.
var destFile = profileDir.clone();
let destFile = profileDir.clone();
destFile.append("formhistory.sqlite");
if (destFile.exists())
destFile.remove(false);
var bakFile = profileDir.clone();
bakFile = profileDir.clone();
bakFile.append("formhistory.sqlite.corrupt");
if (bakFile.exists())
bakFile.remove(false);
testfile.copyTo(profileDir, "formhistory.sqlite");
run_next_test();
}
add_test(function test_corruptFormHistoryDB_lazyCorruptInit1() {
do_log_info("ensure FormHistory backs up a corrupt DB on initialization.");
// ===== 1 =====
testnum++;
// Open the DB, ensure that a backup of the corrupt DB is made.
do_check_false(bakFile.exists());
var fh = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
// DB init is done lazily so the DB shouldn't be created yet.
do_check_false(bakFile.exists());
// Doing any request to the DB should create it.
fh.DBConnection;
FormHistory.count({}, {
onSuccess : function(aNumEntries) {
run_next_test();
},
onFailure : function(aError) {
do_throw("DB initialization failed.");
}
});
});
add_test(function test_corruptFormHistoryDB_lazyCorruptInit2() {
do_check_true(bakFile.exists());
bakFile.remove(false);
// ===== 2 =====
testnum++;
// File should be empty
do_check_false(fh.hasEntries);
do_check_false(fh.entryExists("name-A", "value-A"));
run_next_test();
});
// ===== 3 =====
testnum++;
// Try adding an entry
fh.addEntry("name-A", "value-A");
do_check_true(fh.hasEntries);
do_check_true(fh.entryExists("name-A", "value-A"));
add_test(function test_corruptFormHistoryDB_emptyInit() {
do_log_info("test that FormHistory initializes an empty DB in place of corrupt DB.");
FormHistory.count({}, {
onSuccess : function(aNumEntries) {
do_check_true(aNumEntries == 0);
FormHistory.count({ fieldname : "name-A", value : "value-A" }, {
onSuccess : function(aNumEntries2) {
do_check_true(aNumEntries2 == 0);
run_next_test();
},
onFailure : function(aError2) {
do_throw("DB initialized after reading a corrupt DB file found an entry.");
}
});
},
onFailure : function (aError) {
do_throw("DB initialized after reading a corrupt DB file is not empty.");
}
});
});
// ===== 4 =====
testnum++;
// Try removing an entry
fh.removeEntry("name-A", "value-A");
do_check_false(fh.hasEntries);
do_check_false(fh.entryExists("name-A", "value-A"));
add_test(function test_corruptFormHistoryDB_addEntry() {
do_log_info("test adding an entry to the empty DB.");
FormHistory.update({
op : "add",
fieldname : "name-A",
value : "value-A"
}, {
onSuccess : function() {
FormHistory.count({ fieldname : "name-A", value : "value-A" }, {
onSuccess : function(aNumEntries) {
do_check_true(aNumEntries == 1);
run_next_test();
},
onFailure : function(aError) {
do_throw("Newly added entry cannot be found in DB.");
}
});
},
onFailure : function(aError) {
do_throw("Unable to add new entry to empty DB.");
}
});
});
} catch (e) {
throw "FAILED in test #" + testnum + " -- " + e;
}
}
add_test(function test_corruptFormHistoryDB_removeEntry() {
do_log_info("test removing an entry to the empty DB.");
FormHistory.update({
op : "remove",
fieldname : "name-A",
value : "value-A"
}, {
onSuccess : function() {
FormHistory.count({ fieldname : "name-A", value : "value-A" }, {
onSuccess : function(aNumEntries) {
do_check_true(aNumEntries == 0);
run_next_test();
},
onFailure : function(aError) {
do_throw("Removed entry still being counted in DB.");
}
});
},
onFailure : function(aError) {
do_throw("Unable to remove entry in DB.");
}
});
});