toolkit/components/satchel/test/unit/test_notify.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/test/unit/test_notify.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Test suite for satchel notifications
     1.6 + *
     1.7 + * Tests notifications dispatched when modifying form history.
     1.8 + *
     1.9 + */
    1.10 +
    1.11 +var expectedNotification;
    1.12 +var expectedData;
    1.13 +
    1.14 +var TestObserver = {
    1.15 +  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
    1.16 +
    1.17 +  observe : function (subject, topic, data) {
    1.18 +    do_check_eq(topic, "satchel-storage-changed");
    1.19 +    do_check_eq(data, expectedNotification);
    1.20 +
    1.21 +    switch (data) {
    1.22 +        case "formhistory-add":
    1.23 +        case "formhistory-update":
    1.24 +            do_check_true(subject instanceof Ci.nsISupportsString);
    1.25 +            do_check_true(isGUID.test(subject.toString()));
    1.26 +            break;
    1.27 +        case "formhistory-remove":
    1.28 +            do_check_eq(null, subject);
    1.29 +            break;
    1.30 +        default:
    1.31 +            do_throw("Unhandled notification: " + data + " / " + topic);
    1.32 +    }
    1.33 +
    1.34 +    expectedNotification = null;
    1.35 +    expectedData = null;
    1.36 +  }
    1.37 +};
    1.38 +
    1.39 +let testIterator = null;
    1.40 +
    1.41 +function run_test() {
    1.42 +  do_test_pending();
    1.43 +  testIterator = run_test_steps();
    1.44 +  testIterator.next();
    1.45 +}
    1.46 +
    1.47 +function next_test()
    1.48 +{
    1.49 +  testIterator.next();
    1.50 +}
    1.51 +
    1.52 +function run_test_steps() {
    1.53 +
    1.54 +try {
    1.55 +
    1.56 +var testnum = 0;
    1.57 +var testdesc = "Setup of test form history entries";
    1.58 +
    1.59 +var entry1 = ["entry1", "value1"];
    1.60 +var entry2 = ["entry2", "value2"];
    1.61 +
    1.62 +
    1.63 +/* ========== 1 ========== */
    1.64 +var testnum = 1;
    1.65 +var testdesc = "Initial connection to storage module"
    1.66 +
    1.67 +yield updateEntry("remove", null, null, next_test);
    1.68 +yield countEntries(null, null, function (num) { do_check_false(num, "Checking initial DB is empty"); next_test(); });
    1.69 +
    1.70 +// Add the observer
    1.71 +var os = Cc["@mozilla.org/observer-service;1"].
    1.72 +         getService(Ci.nsIObserverService);
    1.73 +os.addObserver(TestObserver, "satchel-storage-changed", false);
    1.74 +
    1.75 +/* ========== 2 ========== */
    1.76 +testnum++;
    1.77 +testdesc = "addEntry";
    1.78 +
    1.79 +expectedNotification = "formhistory-add";
    1.80 +expectedData = entry1;
    1.81 +
    1.82 +yield updateEntry("add", entry1[0], entry1[1], next_test);
    1.83 +do_check_eq(expectedNotification, null); // check that observer got a notification
    1.84 +
    1.85 +yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); });
    1.86 +
    1.87 +/* ========== 3 ========== */
    1.88 +testnum++;
    1.89 +testdesc = "modifyEntry";
    1.90 +
    1.91 +expectedNotification = "formhistory-update";
    1.92 +expectedData = entry1;
    1.93 +// will update previous entry
    1.94 +yield updateEntry("update", entry1[0], entry1[1], next_test);
    1.95 +yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); });
    1.96 +
    1.97 +do_check_eq(expectedNotification, null);
    1.98 +
    1.99 +/* ========== 4 ========== */
   1.100 +testnum++;
   1.101 +testdesc = "removeEntry";
   1.102 +
   1.103 +expectedNotification = "formhistory-remove";
   1.104 +expectedData = entry1;
   1.105 +yield updateEntry("remove", entry1[0], entry1[1], next_test);
   1.106 +
   1.107 +do_check_eq(expectedNotification, null);
   1.108 +yield countEntries(entry1[0], entry1[1], function(num) { do_check_false(num, "doesn't exist after remove"); next_test(); });
   1.109 +
   1.110 +/* ========== 5 ========== */
   1.111 +testnum++;
   1.112 +testdesc = "removeAllEntries";
   1.113 +
   1.114 +expectedNotification = "formhistory-remove";
   1.115 +expectedData = null; // no data expected
   1.116 +yield updateEntry("remove", null, null, next_test);
   1.117 +
   1.118 +do_check_eq(expectedNotification, null);
   1.119 +
   1.120 +/* ========== 6 ========== */
   1.121 +testnum++;
   1.122 +testdesc = "removeAllEntries (again)";
   1.123 +
   1.124 +expectedNotification = "formhistory-remove";
   1.125 +expectedData = null;
   1.126 +yield updateEntry("remove", null, null, next_test);
   1.127 +
   1.128 +do_check_eq(expectedNotification, null);
   1.129 +
   1.130 +/* ========== 7 ========== */
   1.131 +testnum++;
   1.132 +testdesc = "removeEntriesForName";
   1.133 +
   1.134 +expectedNotification = "formhistory-remove";
   1.135 +expectedData = "field2";
   1.136 +yield updateEntry("remove", null, "field2", next_test);
   1.137 +
   1.138 +do_check_eq(expectedNotification, null);
   1.139 +
   1.140 +/* ========== 8 ========== */
   1.141 +testnum++;
   1.142 +testdesc = "removeEntriesByTimeframe";
   1.143 +
   1.144 +expectedNotification = "formhistory-remove";
   1.145 +expectedData = [10, 99999999999];
   1.146 +
   1.147 +yield FormHistory.update({ op: "remove", firstUsedStart: expectedData[0], firstUsedEnd: expectedData[1] },
   1.148 +                         { handleCompletion: function(reason) { if (!reason) next_test() },
   1.149 +                           handleErrors: function (error) {
   1.150 +                             do_throw("Error occurred updating form history: " + error);
   1.151 +                           }
   1.152 +                         });
   1.153 +
   1.154 +do_check_eq(expectedNotification, null);
   1.155 +
   1.156 +os.removeObserver(TestObserver, "satchel-storage-changed", false);
   1.157 +
   1.158 +do_test_finished();
   1.159 +
   1.160 +} catch (e) {
   1.161 +    throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e;
   1.162 +}
   1.163 +};

mercurial