Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Test suite for satchel notifications |
michael@0 | 3 | * |
michael@0 | 4 | * Tests notifications dispatched when modifying form history. |
michael@0 | 5 | * |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | var expectedNotification; |
michael@0 | 9 | var expectedData; |
michael@0 | 10 | |
michael@0 | 11 | var TestObserver = { |
michael@0 | 12 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]), |
michael@0 | 13 | |
michael@0 | 14 | observe : function (subject, topic, data) { |
michael@0 | 15 | do_check_eq(topic, "satchel-storage-changed"); |
michael@0 | 16 | do_check_eq(data, expectedNotification); |
michael@0 | 17 | |
michael@0 | 18 | switch (data) { |
michael@0 | 19 | case "formhistory-add": |
michael@0 | 20 | case "formhistory-update": |
michael@0 | 21 | do_check_true(subject instanceof Ci.nsISupportsString); |
michael@0 | 22 | do_check_true(isGUID.test(subject.toString())); |
michael@0 | 23 | break; |
michael@0 | 24 | case "formhistory-remove": |
michael@0 | 25 | do_check_eq(null, subject); |
michael@0 | 26 | break; |
michael@0 | 27 | default: |
michael@0 | 28 | do_throw("Unhandled notification: " + data + " / " + topic); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | expectedNotification = null; |
michael@0 | 32 | expectedData = null; |
michael@0 | 33 | } |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | let testIterator = null; |
michael@0 | 37 | |
michael@0 | 38 | function run_test() { |
michael@0 | 39 | do_test_pending(); |
michael@0 | 40 | testIterator = run_test_steps(); |
michael@0 | 41 | testIterator.next(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function next_test() |
michael@0 | 45 | { |
michael@0 | 46 | testIterator.next(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function run_test_steps() { |
michael@0 | 50 | |
michael@0 | 51 | try { |
michael@0 | 52 | |
michael@0 | 53 | var testnum = 0; |
michael@0 | 54 | var testdesc = "Setup of test form history entries"; |
michael@0 | 55 | |
michael@0 | 56 | var entry1 = ["entry1", "value1"]; |
michael@0 | 57 | var entry2 = ["entry2", "value2"]; |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | /* ========== 1 ========== */ |
michael@0 | 61 | var testnum = 1; |
michael@0 | 62 | var testdesc = "Initial connection to storage module" |
michael@0 | 63 | |
michael@0 | 64 | yield updateEntry("remove", null, null, next_test); |
michael@0 | 65 | yield countEntries(null, null, function (num) { do_check_false(num, "Checking initial DB is empty"); next_test(); }); |
michael@0 | 66 | |
michael@0 | 67 | // Add the observer |
michael@0 | 68 | var os = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 69 | getService(Ci.nsIObserverService); |
michael@0 | 70 | os.addObserver(TestObserver, "satchel-storage-changed", false); |
michael@0 | 71 | |
michael@0 | 72 | /* ========== 2 ========== */ |
michael@0 | 73 | testnum++; |
michael@0 | 74 | testdesc = "addEntry"; |
michael@0 | 75 | |
michael@0 | 76 | expectedNotification = "formhistory-add"; |
michael@0 | 77 | expectedData = entry1; |
michael@0 | 78 | |
michael@0 | 79 | yield updateEntry("add", entry1[0], entry1[1], next_test); |
michael@0 | 80 | do_check_eq(expectedNotification, null); // check that observer got a notification |
michael@0 | 81 | |
michael@0 | 82 | yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); }); |
michael@0 | 83 | |
michael@0 | 84 | /* ========== 3 ========== */ |
michael@0 | 85 | testnum++; |
michael@0 | 86 | testdesc = "modifyEntry"; |
michael@0 | 87 | |
michael@0 | 88 | expectedNotification = "formhistory-update"; |
michael@0 | 89 | expectedData = entry1; |
michael@0 | 90 | // will update previous entry |
michael@0 | 91 | yield updateEntry("update", entry1[0], entry1[1], next_test); |
michael@0 | 92 | yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); }); |
michael@0 | 93 | |
michael@0 | 94 | do_check_eq(expectedNotification, null); |
michael@0 | 95 | |
michael@0 | 96 | /* ========== 4 ========== */ |
michael@0 | 97 | testnum++; |
michael@0 | 98 | testdesc = "removeEntry"; |
michael@0 | 99 | |
michael@0 | 100 | expectedNotification = "formhistory-remove"; |
michael@0 | 101 | expectedData = entry1; |
michael@0 | 102 | yield updateEntry("remove", entry1[0], entry1[1], next_test); |
michael@0 | 103 | |
michael@0 | 104 | do_check_eq(expectedNotification, null); |
michael@0 | 105 | yield countEntries(entry1[0], entry1[1], function(num) { do_check_false(num, "doesn't exist after remove"); next_test(); }); |
michael@0 | 106 | |
michael@0 | 107 | /* ========== 5 ========== */ |
michael@0 | 108 | testnum++; |
michael@0 | 109 | testdesc = "removeAllEntries"; |
michael@0 | 110 | |
michael@0 | 111 | expectedNotification = "formhistory-remove"; |
michael@0 | 112 | expectedData = null; // no data expected |
michael@0 | 113 | yield updateEntry("remove", null, null, next_test); |
michael@0 | 114 | |
michael@0 | 115 | do_check_eq(expectedNotification, null); |
michael@0 | 116 | |
michael@0 | 117 | /* ========== 6 ========== */ |
michael@0 | 118 | testnum++; |
michael@0 | 119 | testdesc = "removeAllEntries (again)"; |
michael@0 | 120 | |
michael@0 | 121 | expectedNotification = "formhistory-remove"; |
michael@0 | 122 | expectedData = null; |
michael@0 | 123 | yield updateEntry("remove", null, null, next_test); |
michael@0 | 124 | |
michael@0 | 125 | do_check_eq(expectedNotification, null); |
michael@0 | 126 | |
michael@0 | 127 | /* ========== 7 ========== */ |
michael@0 | 128 | testnum++; |
michael@0 | 129 | testdesc = "removeEntriesForName"; |
michael@0 | 130 | |
michael@0 | 131 | expectedNotification = "formhistory-remove"; |
michael@0 | 132 | expectedData = "field2"; |
michael@0 | 133 | yield updateEntry("remove", null, "field2", next_test); |
michael@0 | 134 | |
michael@0 | 135 | do_check_eq(expectedNotification, null); |
michael@0 | 136 | |
michael@0 | 137 | /* ========== 8 ========== */ |
michael@0 | 138 | testnum++; |
michael@0 | 139 | testdesc = "removeEntriesByTimeframe"; |
michael@0 | 140 | |
michael@0 | 141 | expectedNotification = "formhistory-remove"; |
michael@0 | 142 | expectedData = [10, 99999999999]; |
michael@0 | 143 | |
michael@0 | 144 | yield FormHistory.update({ op: "remove", firstUsedStart: expectedData[0], firstUsedEnd: expectedData[1] }, |
michael@0 | 145 | { handleCompletion: function(reason) { if (!reason) next_test() }, |
michael@0 | 146 | handleErrors: function (error) { |
michael@0 | 147 | do_throw("Error occurred updating form history: " + error); |
michael@0 | 148 | } |
michael@0 | 149 | }); |
michael@0 | 150 | |
michael@0 | 151 | do_check_eq(expectedNotification, null); |
michael@0 | 152 | |
michael@0 | 153 | os.removeObserver(TestObserver, "satchel-storage-changed", false); |
michael@0 | 154 | |
michael@0 | 155 | do_test_finished(); |
michael@0 | 156 | |
michael@0 | 157 | } catch (e) { |
michael@0 | 158 | throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e; |
michael@0 | 159 | } |
michael@0 | 160 | }; |