Bug 566746 - Use Asynchronous FormHistory.jsm for test_db, p=enndeakin,felix, r=mak

This commit is contained in:
Felix Fung 2012-01-01 00:49:22 -05:00
parent 56d8509dbb
commit 4ef991cb4a
2 changed files with 67 additions and 29 deletions

View File

@ -11,7 +11,20 @@
* Part B tests this when the columns do *not* match, so the DB is reset.
*/
let iter = tests();
function run_test()
{
do_test_pending();
iter.next();
}
function next_test()
{
iter.next();
}
function tests()
{
try {
var testnum = 0;
@ -29,32 +42,34 @@ function run_test()
testfile.copyTo(profileDir, "formhistory.sqlite");
do_check_eq(999, getDBVersion(testfile));
var fh = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
let checkZero = function(num) { do_check_eq(num, 0); next_test(); }
let checkOne = function(num) { do_check_eq(num, 1); next_test(); }
// ===== 1 =====
testnum++;
// Check for expected contents.
do_check_true(fh.hasEntries);
do_check_true(fh.entryExists("name-A", "value-A"));
do_check_true(fh.entryExists("name-B", "value-B"));
do_check_true(fh.entryExists("name-C", "value-C1"));
do_check_true(fh.entryExists("name-C", "value-C2"));
do_check_true(fh.entryExists("name-E", "value-E"));
yield countEntries(null, null, function(num) { do_check_true(num > 0); next_test(); });
yield countEntries("name-A", "value-A", checkOne);
yield countEntries("name-B", "value-B", checkOne);
yield countEntries("name-C", "value-C1", checkOne);
yield countEntries("name-C", "value-C2", checkOne);
yield countEntries("name-E", "value-E", checkOne);
// check for downgraded schema.
do_check_eq(CURRENT_SCHEMA, fh.DBConnection.schemaVersion);
do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
// ===== 2 =====
testnum++;
// Exercise adding and removing a name/value pair
do_check_false(fh.entryExists("name-D", "value-D"));
fh.addEntry("name-D", "value-D");
do_check_true(fh.entryExists("name-D", "value-D"));
fh.removeEntry("name-D", "value-D");
do_check_false(fh.entryExists("name-D", "value-D"));
yield countEntries("name-D", "value-D", checkZero);
yield updateEntry("add", "name-D", "value-D", next_test);
yield countEntries("name-D", "value-D", checkOne);
yield updateEntry("remove", "name-D", "value-D", next_test);
yield countEntries("name-D", "value-D", checkZero);
} catch (e) {
throw "FAILED in test #" + testnum + " -- " + e;
}
do_test_finished();
}

View File

@ -11,7 +11,20 @@
* Part B tests this when the columns do *not* match, so the DB is reset.
*/
let iter = tests();
function run_test()
{
do_test_pending();
iter.next();
}
function next_test()
{
iter.next();
}
function tests()
{
try {
var testnum = 0;
@ -34,43 +47,53 @@ function run_test()
testfile.copyTo(profileDir, "formhistory.sqlite");
do_check_eq(999, getDBVersion(testfile));
let checkZero = function(num) { do_check_eq(num, 0); next_test(); }
let checkOne = function(num) { do_check_eq(num, 1); next_test(); }
// ===== 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;
yield FormHistory.count({}, {
onSuccess : function(aNumEntries) {
next_test();
},
onFailure : function(aError) {
do_throw("DB initialization failed.");
}
});
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"));
yield countEntries(null, null, function(num) { do_check_false(num); next_test(); });
yield countEntries("name-A", "value-A", checkZero);
// check for current schema.
do_check_eq(CURRENT_SCHEMA, fh.DBConnection.schemaVersion);
do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
// ===== 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"));
yield updateEntry("add", "name-A", "value-A", next_test);
yield countEntries(null, null, checkOne);
yield countEntries("name-A", "value-A", checkOne);
// ===== 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"));
yield updateEntry("remove", "name-A", "value-A", next_test);
yield countEntries(null, null, checkZero);
yield countEntries("name-A", "value-A", checkZero);
} catch (e) {
throw "FAILED in test #" + testnum + " -- " + e;
}
do_test_finished();
}