summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_history_observer.js
blob: c101cfb61b32ffefe8a2d9ac6216dc49aa4afc86 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Generic nsINavHistoryObserver that doesn't implement anything, but provides
 * dummy methods to prevent errors about an object not having a certain method.
 */
function NavHistoryObserver() {
}
NavHistoryObserver.prototype = {
  onBeginUpdateBatch: function() { },
  onEndUpdateBatch: function() { },
  onVisit: function() { },
  onTitleChanged: function() { },
  onDeleteURI: function() { },
  onClearHistory: function() { },
  onPageChanged: function() { },
  onDeleteVisits: function() { },
  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
};

/**
 * Registers a one-time history observer for and calls the callback
 * when the specified nsINavHistoryObserver method is called.
 * Returns a promise that is resolved when the callback returns.
 */
function onNotify(callback) {
  return new Promise(resolve => {
    let obs = new NavHistoryObserver();
    obs[callback.name] = function () {
      PlacesUtils.history.removeObserver(this);
      callback.apply(this, arguments);
      resolve();
    };
    PlacesUtils.history.addObserver(obs, false);
  });
}

/**
 * Asynchronous task that adds a visit to the history database.
 */
function* task_add_visit(uri, timestamp, transition) {
  uri = uri || NetUtil.newURI("http://firefox.com/");
  timestamp = timestamp || Date.now() * 1000;
  yield PlacesTestUtils.addVisits({
    uri: uri,
    transition: transition || TRANSITION_TYPED,
    visitDate: timestamp
  });
  return [uri, timestamp];
}

add_task(function* test_onVisit() {
  let promiseNotify = onNotify(function onVisit(aURI, aVisitID, aTime,
                                                aSessionID, aReferringID,
                                                aTransitionType, aGUID,
                                                aHidden, aVisitCount, aTyped) {
    Assert.ok(aURI.equals(testuri));
    Assert.ok(aVisitID > 0);
    Assert.equal(aTime, testtime);
    Assert.equal(aSessionID, 0);
    Assert.equal(aReferringID, 0);
    Assert.equal(aTransitionType, TRANSITION_TYPED);
    do_check_guid_for_uri(aURI, aGUID);
    Assert.ok(!aHidden);
    Assert.equal(aVisitCount, 1);
    Assert.equal(aTyped, 1);
  });
  let testuri = NetUtil.newURI("http://firefox.com/");
  let testtime = Date.now() * 1000;
  yield task_add_visit(testuri, testtime);
  yield promiseNotify;
});

add_task(function* test_onVisit() {
  let promiseNotify = onNotify(function onVisit(aURI, aVisitID, aTime,
                                                aSessionID, aReferringID,
                                                aTransitionType, aGUID,
                                                aHidden, aVisitCount, aTyped) {
    Assert.ok(aURI.equals(testuri));
    Assert.ok(aVisitID > 0);
    Assert.equal(aTime, testtime);
    Assert.equal(aSessionID, 0);
    Assert.equal(aReferringID, 0);
    Assert.equal(aTransitionType, TRANSITION_FRAMED_LINK);
    do_check_guid_for_uri(aURI, aGUID);
    Assert.ok(aHidden);
    Assert.equal(aVisitCount, 1);
    Assert.equal(aTyped, 0);
  });
  let testuri = NetUtil.newURI("http://hidden.firefox.com/");
  let testtime = Date.now() * 1000;
  yield task_add_visit(testuri, testtime, TRANSITION_FRAMED_LINK);
  yield promiseNotify;
});

add_task(function* test_multiple_onVisit() {
  let testuri = NetUtil.newURI("http://self.firefox.com/");
  let promiseNotifications = new Promise(resolve => {
    let observer = {
      _c: 0,
      __proto__: NavHistoryObserver.prototype,
      onVisit(uri, id, time, unused, referrerId, transition, guid,
              hidden, visitCount, typed) {
        Assert.ok(testuri.equals(uri));
        Assert.ok(id > 0);
        Assert.ok(time > 0);
        Assert.ok(!hidden);
        do_check_guid_for_uri(uri, guid);
        switch (++this._c) {
          case 1:
            Assert.equal(referrerId, 0);
            Assert.equal(transition, TRANSITION_LINK);
            Assert.equal(visitCount, 1);
            Assert.equal(typed, 0);
            break;
          case 2:
            Assert.ok(referrerId > 0);
            Assert.equal(transition, TRANSITION_LINK);
            Assert.equal(visitCount, 2);
            Assert.equal(typed, 0);
            break;
          case 3:
            Assert.equal(referrerId, 0);
            Assert.equal(transition, TRANSITION_TYPED);
            Assert.equal(visitCount, 3);
            Assert.equal(typed, 1);

            PlacesUtils.history.removeObserver(observer, false);
            resolve();
            break;
        }
      }
    };
    PlacesUtils.history.addObserver(observer, false);
  });
  yield PlacesTestUtils.addVisits([
    { uri: testuri, transition: TRANSITION_LINK },
    { uri: testuri, referrer: testuri, transition: TRANSITION_LINK },
    { uri: testuri, transition: TRANSITION_TYPED },
  ]);
  yield promiseNotifications;
});

add_task(function* test_onDeleteURI() {
  let promiseNotify = onNotify(function onDeleteURI(aURI, aGUID, aReason) {
    Assert.ok(aURI.equals(testuri));
    // Can't use do_check_guid_for_uri() here because the visit is already gone.
    Assert.equal(aGUID, testguid);
    Assert.equal(aReason, Ci.nsINavHistoryObserver.REASON_DELETED);
  });
  let [testuri] = yield task_add_visit();
  let testguid = do_get_guid_for_uri(testuri);
  PlacesUtils.bhistory.removePage(testuri);
  yield promiseNotify;
});

add_task(function* test_onDeleteVisits() {
  let promiseNotify = onNotify(function onDeleteVisits(aURI, aVisitTime, aGUID,
                                                       aReason) {
    Assert.ok(aURI.equals(testuri));
    // Can't use do_check_guid_for_uri() here because the visit is already gone.
    Assert.equal(aGUID, testguid);
    Assert.equal(aReason, Ci.nsINavHistoryObserver.REASON_DELETED);
    Assert.equal(aVisitTime, 0); // All visits have been removed.
  });
  let msecs24hrsAgo = Date.now() - (86400 * 1000);
  let [testuri] = yield task_add_visit(undefined, msecs24hrsAgo * 1000);
  // Add a bookmark so the page is not removed.
  PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
                                       testuri,
                                       PlacesUtils.bookmarks.DEFAULT_INDEX,
                                       "test");
  let testguid = do_get_guid_for_uri(testuri);
  PlacesUtils.bhistory.removePage(testuri);
  yield promiseNotify;
});

add_task(function* test_onTitleChanged() {
  let promiseNotify = onNotify(function onTitleChanged(aURI, aTitle, aGUID) {
    Assert.ok(aURI.equals(testuri));
    Assert.equal(aTitle, title);
    do_check_guid_for_uri(aURI, aGUID);
  });

  let [testuri] = yield task_add_visit();
  let title = "test-title";
  yield PlacesTestUtils.addVisits({
    uri: testuri,
    title: title
  });
  yield promiseNotify;
});

add_task(function* test_onPageChanged() {
  let promiseNotify = onNotify(function onPageChanged(aURI, aChangedAttribute,
                                                      aNewValue, aGUID) {
    Assert.equal(aChangedAttribute, Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON);
    Assert.ok(aURI.equals(testuri));
    Assert.equal(aNewValue, SMALLPNG_DATA_URI.spec);
    do_check_guid_for_uri(aURI, aGUID);
  });

  let [testuri] = yield task_add_visit();

  // The new favicon for the page must have data associated with it in order to
  // receive the onPageChanged notification.  To keep this test self-contained,
  // we use an URI representing the smallest possible PNG file.
  PlacesUtils.favicons.setAndFetchFaviconForPage(testuri, SMALLPNG_DATA_URI,
                                                 false,
                                                 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
                                                 null,
                                                 Services.scriptSecurityManager.getSystemPrincipal());
  yield promiseNotify;
});