summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/unit/test_notify.js
blob: 556ecd4b08c5a7d26bb6d883312dbf5317cb14d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Test suite for satchel notifications
 *
 * Tests notifications dispatched when modifying form history.
 *
 */

var expectedNotification;
var expectedData;

var TestObserver = {
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),

  observe : function (subject, topic, data) {
    do_check_eq(topic, "satchel-storage-changed");
    do_check_eq(data, expectedNotification);

    switch (data) {
        case "formhistory-add":
        case "formhistory-update":
            do_check_true(subject instanceof Ci.nsISupportsString);
            do_check_true(isGUID.test(subject.toString()));
            break;
        case "formhistory-remove":
            do_check_eq(null, subject);
            break;
        default:
            do_throw("Unhandled notification: " + data + " / " + topic);
    }

    expectedNotification = null;
    expectedData = null;
  }
};

var testIterator = null;

function run_test() {
  do_test_pending();
  testIterator = run_test_steps();
  testIterator.next();
}

function next_test()
{
  testIterator.next();
}

function* run_test_steps() {

try {

var testnum = 0;
var testdesc = "Setup of test form history entries";

var entry1 = ["entry1", "value1"];

/* ========== 1 ========== */
testnum = 1;
testdesc = "Initial connection to storage module"

yield updateEntry("remove", null, null, next_test);
yield countEntries(null, null, function (num) { do_check_false(num, "Checking initial DB is empty"); next_test(); });

// Add the observer
var os = Cc["@mozilla.org/observer-service;1"].
         getService(Ci.nsIObserverService);
os.addObserver(TestObserver, "satchel-storage-changed", false);

/* ========== 2 ========== */
testnum++;
testdesc = "addEntry";

expectedNotification = "formhistory-add";
expectedData = entry1;

yield updateEntry("add", entry1[0], entry1[1], next_test);
do_check_eq(expectedNotification, null); // check that observer got a notification

yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); });

/* ========== 3 ========== */
testnum++;
testdesc = "modifyEntry";

expectedNotification = "formhistory-update";
expectedData = entry1;
// will update previous entry
yield updateEntry("update", entry1[0], entry1[1], next_test);
yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); });

do_check_eq(expectedNotification, null);

/* ========== 4 ========== */
testnum++;
testdesc = "removeEntry";

expectedNotification = "formhistory-remove";
expectedData = entry1;
yield updateEntry("remove", entry1[0], entry1[1], next_test);

do_check_eq(expectedNotification, null);
yield countEntries(entry1[0], entry1[1], function(num) { do_check_false(num, "doesn't exist after remove"); next_test(); });

/* ========== 5 ========== */
testnum++;
testdesc = "removeAllEntries";

expectedNotification = "formhistory-remove";
expectedData = null; // no data expected
yield updateEntry("remove", null, null, next_test);

do_check_eq(expectedNotification, null);

/* ========== 6 ========== */
testnum++;
testdesc = "removeAllEntries (again)";

expectedNotification = "formhistory-remove";
expectedData = null;
yield updateEntry("remove", null, null, next_test);

do_check_eq(expectedNotification, null);

/* ========== 7 ========== */
testnum++;
testdesc = "removeEntriesForName";

expectedNotification = "formhistory-remove";
expectedData = "field2";
yield updateEntry("remove", null, "field2", next_test);

do_check_eq(expectedNotification, null);

/* ========== 8 ========== */
testnum++;
testdesc = "removeEntriesByTimeframe";

expectedNotification = "formhistory-remove";
expectedData = [10, 99999999999];

yield FormHistory.update({ op: "remove", firstUsedStart: expectedData[0], firstUsedEnd: expectedData[1] },
                         { handleCompletion: function(reason) { if (!reason) next_test() },
                           handleErrors: function (error) {
                             do_throw("Error occurred updating form history: " + error);
                           }
                         });

do_check_eq(expectedNotification, null);

os.removeObserver(TestObserver, "satchel-storage-changed", false);

do_test_finished();

} catch (e) {
    throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e;
}
}