michael@0: /* michael@0: * Test suite for satchel notifications michael@0: * michael@0: * Tests notifications dispatched when modifying form history. michael@0: * michael@0: */ michael@0: michael@0: var expectedNotification; michael@0: var expectedData; michael@0: michael@0: var TestObserver = { michael@0: QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]), michael@0: michael@0: observe : function (subject, topic, data) { michael@0: do_check_eq(topic, "satchel-storage-changed"); michael@0: do_check_eq(data, expectedNotification); michael@0: michael@0: switch (data) { michael@0: case "formhistory-add": michael@0: case "formhistory-update": michael@0: do_check_true(subject instanceof Ci.nsISupportsString); michael@0: do_check_true(isGUID.test(subject.toString())); michael@0: break; michael@0: case "formhistory-remove": michael@0: do_check_eq(null, subject); michael@0: break; michael@0: default: michael@0: do_throw("Unhandled notification: " + data + " / " + topic); michael@0: } michael@0: michael@0: expectedNotification = null; michael@0: expectedData = null; michael@0: } michael@0: }; michael@0: michael@0: let testIterator = null; michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: testIterator = run_test_steps(); michael@0: testIterator.next(); michael@0: } michael@0: michael@0: function next_test() michael@0: { michael@0: testIterator.next(); michael@0: } michael@0: michael@0: function run_test_steps() { michael@0: michael@0: try { michael@0: michael@0: var testnum = 0; michael@0: var testdesc = "Setup of test form history entries"; michael@0: michael@0: var entry1 = ["entry1", "value1"]; michael@0: var entry2 = ["entry2", "value2"]; michael@0: michael@0: michael@0: /* ========== 1 ========== */ michael@0: var testnum = 1; michael@0: var testdesc = "Initial connection to storage module" michael@0: michael@0: yield updateEntry("remove", null, null, next_test); michael@0: yield countEntries(null, null, function (num) { do_check_false(num, "Checking initial DB is empty"); next_test(); }); michael@0: michael@0: // Add the observer michael@0: var os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: os.addObserver(TestObserver, "satchel-storage-changed", false); michael@0: michael@0: /* ========== 2 ========== */ michael@0: testnum++; michael@0: testdesc = "addEntry"; michael@0: michael@0: expectedNotification = "formhistory-add"; michael@0: expectedData = entry1; michael@0: michael@0: yield updateEntry("add", entry1[0], entry1[1], next_test); michael@0: do_check_eq(expectedNotification, null); // check that observer got a notification michael@0: michael@0: yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); }); michael@0: michael@0: /* ========== 3 ========== */ michael@0: testnum++; michael@0: testdesc = "modifyEntry"; michael@0: michael@0: expectedNotification = "formhistory-update"; michael@0: expectedData = entry1; michael@0: // will update previous entry michael@0: yield updateEntry("update", entry1[0], entry1[1], next_test); michael@0: yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); }); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: michael@0: /* ========== 4 ========== */ michael@0: testnum++; michael@0: testdesc = "removeEntry"; michael@0: michael@0: expectedNotification = "formhistory-remove"; michael@0: expectedData = entry1; michael@0: yield updateEntry("remove", entry1[0], entry1[1], next_test); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: yield countEntries(entry1[0], entry1[1], function(num) { do_check_false(num, "doesn't exist after remove"); next_test(); }); michael@0: michael@0: /* ========== 5 ========== */ michael@0: testnum++; michael@0: testdesc = "removeAllEntries"; michael@0: michael@0: expectedNotification = "formhistory-remove"; michael@0: expectedData = null; // no data expected michael@0: yield updateEntry("remove", null, null, next_test); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: michael@0: /* ========== 6 ========== */ michael@0: testnum++; michael@0: testdesc = "removeAllEntries (again)"; michael@0: michael@0: expectedNotification = "formhistory-remove"; michael@0: expectedData = null; michael@0: yield updateEntry("remove", null, null, next_test); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: michael@0: /* ========== 7 ========== */ michael@0: testnum++; michael@0: testdesc = "removeEntriesForName"; michael@0: michael@0: expectedNotification = "formhistory-remove"; michael@0: expectedData = "field2"; michael@0: yield updateEntry("remove", null, "field2", next_test); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: michael@0: /* ========== 8 ========== */ michael@0: testnum++; michael@0: testdesc = "removeEntriesByTimeframe"; michael@0: michael@0: expectedNotification = "formhistory-remove"; michael@0: expectedData = [10, 99999999999]; michael@0: michael@0: yield FormHistory.update({ op: "remove", firstUsedStart: expectedData[0], firstUsedEnd: expectedData[1] }, michael@0: { handleCompletion: function(reason) { if (!reason) next_test() }, michael@0: handleErrors: function (error) { michael@0: do_throw("Error occurred updating form history: " + error); michael@0: } michael@0: }); michael@0: michael@0: do_check_eq(expectedNotification, null); michael@0: michael@0: os.removeObserver(TestObserver, "satchel-storage-changed", false); michael@0: michael@0: do_test_finished(); michael@0: michael@0: } catch (e) { michael@0: throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e; michael@0: } michael@0: };