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