michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * This test uses a formhistory.sqlite with schema version set to 999 (a michael@0: * future version). This exercies the code that allows using a future schema michael@0: * version as long as the expected columns are present. michael@0: * michael@0: * Part A tests this when the columns do match, so the DB is used. michael@0: * Part B tests this when the columns do *not* match, so the DB is reset. michael@0: */ michael@0: michael@0: let iter = tests(); michael@0: michael@0: function run_test() michael@0: { michael@0: do_test_pending(); michael@0: iter.next(); michael@0: } michael@0: michael@0: function next_test() michael@0: { michael@0: iter.next(); michael@0: } michael@0: michael@0: function tests() michael@0: { michael@0: try { michael@0: var testnum = 0; michael@0: michael@0: // ===== test init ===== michael@0: var testfile = do_get_file("formhistory_v999b.sqlite"); michael@0: var profileDir = dirSvc.get("ProfD", Ci.nsIFile); michael@0: michael@0: // Cleanup from any previous tests or failures. michael@0: var destFile = profileDir.clone(); michael@0: destFile.append("formhistory.sqlite"); michael@0: if (destFile.exists()) michael@0: destFile.remove(false); michael@0: michael@0: var bakFile = profileDir.clone(); michael@0: bakFile.append("formhistory.sqlite.corrupt"); michael@0: if (bakFile.exists()) michael@0: bakFile.remove(false); michael@0: michael@0: testfile.copyTo(profileDir, "formhistory.sqlite"); michael@0: do_check_eq(999, getDBVersion(testfile)); michael@0: michael@0: let checkZero = function(num) { do_check_eq(num, 0); next_test(); } michael@0: let checkOne = function(num) { do_check_eq(num, 1); next_test(); } michael@0: michael@0: // ===== 1 ===== michael@0: testnum++; michael@0: michael@0: // Open the DB, ensure that a backup of the corrupt DB is made. michael@0: // DB init is done lazily so the DB shouldn't be created yet. michael@0: do_check_false(bakFile.exists()); michael@0: // Doing any request to the DB should create it. michael@0: yield countEntries("", "", next_test); michael@0: michael@0: do_check_true(bakFile.exists()); michael@0: bakFile.remove(false); michael@0: michael@0: // ===== 2 ===== michael@0: testnum++; michael@0: // File should be empty michael@0: yield countEntries(null, null, function(num) { do_check_false(num); next_test(); }); michael@0: yield countEntries("name-A", "value-A", checkZero); michael@0: // check for current schema. michael@0: do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion); michael@0: michael@0: // ===== 3 ===== michael@0: testnum++; michael@0: // Try adding an entry michael@0: yield updateEntry("add", "name-A", "value-A", next_test); michael@0: yield countEntries(null, null, checkOne); michael@0: yield countEntries("name-A", "value-A", checkOne); michael@0: michael@0: // ===== 4 ===== michael@0: testnum++; michael@0: // Try removing an entry michael@0: yield updateEntry("remove", "name-A", "value-A", next_test); michael@0: yield countEntries(null, null, checkZero); michael@0: yield countEntries("name-A", "value-A", checkZero); michael@0: michael@0: } catch (e) { michael@0: throw "FAILED in test #" + testnum + " -- " + e; michael@0: } michael@0: michael@0: do_test_finished(); michael@0: }