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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * Test suite for satchel notifications
     3  *
     4  * Tests notifications dispatched when modifying form history.
     5  *
     6  */
     8 var expectedNotification;
     9 var expectedData;
    11 var TestObserver = {
    12   QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
    14   observe : function (subject, topic, data) {
    15     do_check_eq(topic, "satchel-storage-changed");
    16     do_check_eq(data, expectedNotification);
    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     }
    31     expectedNotification = null;
    32     expectedData = null;
    33   }
    34 };
    36 let testIterator = null;
    38 function run_test() {
    39   do_test_pending();
    40   testIterator = run_test_steps();
    41   testIterator.next();
    42 }
    44 function next_test()
    45 {
    46   testIterator.next();
    47 }
    49 function run_test_steps() {
    51 try {
    53 var testnum = 0;
    54 var testdesc = "Setup of test form history entries";
    56 var entry1 = ["entry1", "value1"];
    57 var entry2 = ["entry2", "value2"];
    60 /* ========== 1 ========== */
    61 var testnum = 1;
    62 var testdesc = "Initial connection to storage module"
    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(); });
    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);
    72 /* ========== 2 ========== */
    73 testnum++;
    74 testdesc = "addEntry";
    76 expectedNotification = "formhistory-add";
    77 expectedData = entry1;
    79 yield updateEntry("add", entry1[0], entry1[1], next_test);
    80 do_check_eq(expectedNotification, null); // check that observer got a notification
    82 yield countEntries(entry1[0], entry1[1], function (num) { do_check_true(num > 0); next_test(); });
    84 /* ========== 3 ========== */
    85 testnum++;
    86 testdesc = "modifyEntry";
    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(); });
    94 do_check_eq(expectedNotification, null);
    96 /* ========== 4 ========== */
    97 testnum++;
    98 testdesc = "removeEntry";
   100 expectedNotification = "formhistory-remove";
   101 expectedData = entry1;
   102 yield updateEntry("remove", entry1[0], entry1[1], next_test);
   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(); });
   107 /* ========== 5 ========== */
   108 testnum++;
   109 testdesc = "removeAllEntries";
   111 expectedNotification = "formhistory-remove";
   112 expectedData = null; // no data expected
   113 yield updateEntry("remove", null, null, next_test);
   115 do_check_eq(expectedNotification, null);
   117 /* ========== 6 ========== */
   118 testnum++;
   119 testdesc = "removeAllEntries (again)";
   121 expectedNotification = "formhistory-remove";
   122 expectedData = null;
   123 yield updateEntry("remove", null, null, next_test);
   125 do_check_eq(expectedNotification, null);
   127 /* ========== 7 ========== */
   128 testnum++;
   129 testdesc = "removeEntriesForName";
   131 expectedNotification = "formhistory-remove";
   132 expectedData = "field2";
   133 yield updateEntry("remove", null, "field2", next_test);
   135 do_check_eq(expectedNotification, null);
   137 /* ========== 8 ========== */
   138 testnum++;
   139 testdesc = "removeEntriesByTimeframe";
   141 expectedNotification = "formhistory-remove";
   142 expectedData = [10, 99999999999];
   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                          });
   151 do_check_eq(expectedNotification, null);
   153 os.removeObserver(TestObserver, "satchel-storage-changed", false);
   155 do_test_finished();
   157 } catch (e) {
   158     throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e;
   159 }
   160 };

mercurial