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. * Part B tests this when the columns do *not* match, so the DB is reset.
*/ */
let iter = tests();
function run_test() function run_test()
{
do_test_pending();
iter.next();
}
function next_test()
{
iter.next();
}
function tests()
{ {
try { try {
var testnum = 0; var testnum = 0;
@ -29,32 +42,34 @@ function run_test()
testfile.copyTo(profileDir, "formhistory.sqlite"); testfile.copyTo(profileDir, "formhistory.sqlite");
do_check_eq(999, getDBVersion(testfile)); do_check_eq(999, getDBVersion(testfile));
var fh = Cc["@mozilla.org/satchel/form-history;1"]. let checkZero = function(num) { do_check_eq(num, 0); next_test(); }
getService(Ci.nsIFormHistory2); let checkOne = function(num) { do_check_eq(num, 1); next_test(); }
// ===== 1 ===== // ===== 1 =====
testnum++; testnum++;
// Check for expected contents. // Check for expected contents.
do_check_true(fh.hasEntries); yield countEntries(null, null, function(num) { do_check_true(num > 0); next_test(); });
do_check_true(fh.entryExists("name-A", "value-A")); yield countEntries("name-A", "value-A", checkOne);
do_check_true(fh.entryExists("name-B", "value-B")); yield countEntries("name-B", "value-B", checkOne);
do_check_true(fh.entryExists("name-C", "value-C1")); yield countEntries("name-C", "value-C1", checkOne);
do_check_true(fh.entryExists("name-C", "value-C2")); yield countEntries("name-C", "value-C2", checkOne);
do_check_true(fh.entryExists("name-E", "value-E")); yield countEntries("name-E", "value-E", checkOne);
// check for downgraded schema. // check for downgraded schema.
do_check_eq(CURRENT_SCHEMA, fh.DBConnection.schemaVersion); do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
// ===== 2 ===== // ===== 2 =====
testnum++; testnum++;
// Exercise adding and removing a name/value pair // Exercise adding and removing a name/value pair
do_check_false(fh.entryExists("name-D", "value-D")); yield countEntries("name-D", "value-D", checkZero);
fh.addEntry("name-D", "value-D"); yield updateEntry("add", "name-D", "value-D", next_test);
do_check_true(fh.entryExists("name-D", "value-D")); yield countEntries("name-D", "value-D", checkOne);
fh.removeEntry("name-D", "value-D"); yield updateEntry("remove", "name-D", "value-D", next_test);
do_check_false(fh.entryExists("name-D", "value-D")); yield countEntries("name-D", "value-D", checkZero);
} catch (e) { } catch (e) {
throw "FAILED in test #" + testnum + " -- " + 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. * Part B tests this when the columns do *not* match, so the DB is reset.
*/ */
let iter = tests();
function run_test() function run_test()
{
do_test_pending();
iter.next();
}
function next_test()
{
iter.next();
}
function tests()
{ {
try { try {
var testnum = 0; var testnum = 0;
@ -34,43 +47,53 @@ function run_test()
testfile.copyTo(profileDir, "formhistory.sqlite"); testfile.copyTo(profileDir, "formhistory.sqlite");
do_check_eq(999, getDBVersion(testfile)); 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 ===== // ===== 1 =====
testnum++; testnum++;
// Open the DB, ensure that a backup of the corrupt DB is made. // 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. // DB init is done lazily so the DB shouldn't be created yet.
do_check_false(bakFile.exists()); do_check_false(bakFile.exists());
// Doing any request to the DB should create it. // 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()); do_check_true(bakFile.exists());
bakFile.remove(false); bakFile.remove(false);
// ===== 2 ===== // ===== 2 =====
testnum++; testnum++;
// File should be empty // File should be empty
do_check_false(fh.hasEntries); yield countEntries(null, null, function(num) { do_check_false(num); next_test(); });
do_check_false(fh.entryExists("name-A", "value-A")); yield countEntries("name-A", "value-A", checkZero);
// check for current schema. // check for current schema.
do_check_eq(CURRENT_SCHEMA, fh.DBConnection.schemaVersion); do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
// ===== 3 ===== // ===== 3 =====
testnum++; testnum++;
// Try adding an entry // Try adding an entry
fh.addEntry("name-A", "value-A"); yield updateEntry("add", "name-A", "value-A", next_test);
do_check_true(fh.hasEntries); yield countEntries(null, null, checkOne);
do_check_true(fh.entryExists("name-A", "value-A")); yield countEntries("name-A", "value-A", checkOne);
// ===== 4 ===== // ===== 4 =====
testnum++; testnum++;
// Try removing an entry // Try removing an entry
fh.removeEntry("name-A", "value-A"); yield updateEntry("remove", "name-A", "value-A", next_test);
do_check_false(fh.hasEntries); yield countEntries(null, null, checkZero);
do_check_false(fh.entryExists("name-A", "value-A")); yield countEntries("name-A", "value-A", checkZero);
} catch (e) { } catch (e) {
throw "FAILED in test #" + testnum + " -- " + e; throw "FAILED in test #" + testnum + " -- " + e;
} }
do_test_finished();
} }