toolkit/components/places/tests/unit/test_history_observer.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Generic nsINavHistoryObserver that doesn't implement anything, but provides
michael@0 6 * dummy methods to prevent errors about an object not having a certain method.
michael@0 7 */
michael@0 8 function NavHistoryObserver() {
michael@0 9 }
michael@0 10 NavHistoryObserver.prototype = {
michael@0 11 onBeginUpdateBatch: function() { },
michael@0 12 onEndUpdateBatch: function() { },
michael@0 13 onVisit: function() { },
michael@0 14 onTitleChanged: function() { },
michael@0 15 onDeleteURI: function() { },
michael@0 16 onClearHistory: function() { },
michael@0 17 onPageChanged: function() { },
michael@0 18 onDeleteVisits: function() { },
michael@0 19 QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
michael@0 20 };
michael@0 21
michael@0 22 /**
michael@0 23 * Registers a one-time history observer for and calls the callback
michael@0 24 * when the specified nsINavHistoryObserver method is called.
michael@0 25 * Returns a promise that is resolved when the callback returns.
michael@0 26 */
michael@0 27 function onNotify(callback) {
michael@0 28 let deferred = Promise.defer();
michael@0 29 let obs = new NavHistoryObserver();
michael@0 30 obs[callback.name] = function () {
michael@0 31 PlacesUtils.history.removeObserver(this);
michael@0 32 callback.apply(this, arguments);
michael@0 33 deferred.resolve();
michael@0 34 };
michael@0 35 PlacesUtils.history.addObserver(obs, false);
michael@0 36 return deferred.promise;
michael@0 37 }
michael@0 38
michael@0 39 /**
michael@0 40 * Asynchronous task that adds a visit to the history database.
michael@0 41 */
michael@0 42 function task_add_visit(uri, timestamp, transition) {
michael@0 43 uri = uri || NetUtil.newURI("http://firefox.com/");
michael@0 44 timestamp = timestamp || Date.now() * 1000;
michael@0 45 yield promiseAddVisits({
michael@0 46 uri: uri,
michael@0 47 transition: transition || TRANSITION_TYPED,
michael@0 48 visitDate: timestamp
michael@0 49 });
michael@0 50 throw new Task.Result([uri, timestamp]);
michael@0 51 }
michael@0 52
michael@0 53 function run_test() {
michael@0 54 run_next_test();
michael@0 55 }
michael@0 56
michael@0 57 add_task(function test_onVisit() {
michael@0 58 let promiseNotify = onNotify(function onVisit(aURI, aVisitID, aTime,
michael@0 59 aSessionID, aReferringID,
michael@0 60 aTransitionType, aGUID,
michael@0 61 aHidden) {
michael@0 62 do_check_true(aURI.equals(testuri));
michael@0 63 do_check_true(aVisitID > 0);
michael@0 64 do_check_eq(aTime, testtime);
michael@0 65 do_check_eq(aSessionID, 0);
michael@0 66 do_check_eq(aReferringID, 0);
michael@0 67 do_check_eq(aTransitionType, TRANSITION_TYPED);
michael@0 68 do_check_guid_for_uri(aURI, aGUID);
michael@0 69 do_check_false(aHidden);
michael@0 70 });
michael@0 71 let testuri = NetUtil.newURI("http://firefox.com/");
michael@0 72 let testtime = Date.now() * 1000;
michael@0 73 yield task_add_visit(testuri, testtime);
michael@0 74 yield promiseNotify;
michael@0 75 });
michael@0 76
michael@0 77 add_task(function test_onVisit() {
michael@0 78 let promiseNotify = onNotify(function onVisit(aURI, aVisitID, aTime,
michael@0 79 aSessionID, aReferringID,
michael@0 80 aTransitionType, aGUID,
michael@0 81 aHidden) {
michael@0 82 do_check_true(aURI.equals(testuri));
michael@0 83 do_check_true(aVisitID > 0);
michael@0 84 do_check_eq(aTime, testtime);
michael@0 85 do_check_eq(aSessionID, 0);
michael@0 86 do_check_eq(aReferringID, 0);
michael@0 87 do_check_eq(aTransitionType, TRANSITION_FRAMED_LINK);
michael@0 88 do_check_guid_for_uri(aURI, aGUID);
michael@0 89 do_check_true(aHidden);
michael@0 90 });
michael@0 91 let testuri = NetUtil.newURI("http://hidden.firefox.com/");
michael@0 92 let testtime = Date.now() * 1000;
michael@0 93 yield task_add_visit(testuri, testtime, TRANSITION_FRAMED_LINK);
michael@0 94 yield promiseNotify;
michael@0 95 });
michael@0 96
michael@0 97 add_task(function test_onDeleteURI() {
michael@0 98 let promiseNotify = onNotify(function onDeleteURI(aURI, aGUID, aReason) {
michael@0 99 do_check_true(aURI.equals(testuri));
michael@0 100 // Can't use do_check_guid_for_uri() here because the visit is already gone.
michael@0 101 do_check_eq(aGUID, testguid);
michael@0 102 do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_DELETED);
michael@0 103 });
michael@0 104 let [testuri] = yield task_add_visit();
michael@0 105 let testguid = do_get_guid_for_uri(testuri);
michael@0 106 PlacesUtils.bhistory.removePage(testuri);
michael@0 107 yield promiseNotify;
michael@0 108 });
michael@0 109
michael@0 110 add_task(function test_onDeleteVisits() {
michael@0 111 let promiseNotify = onNotify(function onDeleteVisits(aURI, aVisitTime, aGUID,
michael@0 112 aReason) {
michael@0 113 do_check_true(aURI.equals(testuri));
michael@0 114 // Can't use do_check_guid_for_uri() here because the visit is already gone.
michael@0 115 do_check_eq(aGUID, testguid);
michael@0 116 do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_DELETED);
michael@0 117 do_check_eq(aVisitTime, 0); // All visits have been removed.
michael@0 118 });
michael@0 119 let msecs24hrsAgo = Date.now() - (86400 * 1000);
michael@0 120 let [testuri] = yield task_add_visit(undefined, msecs24hrsAgo * 1000);
michael@0 121 // Add a bookmark so the page is not removed.
michael@0 122 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
michael@0 123 testuri,
michael@0 124 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 125 "test");
michael@0 126 let testguid = do_get_guid_for_uri(testuri);
michael@0 127 PlacesUtils.bhistory.removePage(testuri);
michael@0 128 yield promiseNotify;
michael@0 129 });
michael@0 130
michael@0 131 add_task(function test_onTitleChanged() {
michael@0 132 let promiseNotify = onNotify(function onTitleChanged(aURI, aTitle, aGUID) {
michael@0 133 do_check_true(aURI.equals(testuri));
michael@0 134 do_check_eq(aTitle, title);
michael@0 135 do_check_guid_for_uri(aURI, aGUID);
michael@0 136 });
michael@0 137
michael@0 138 let [testuri] = yield task_add_visit();
michael@0 139 let title = "test-title";
michael@0 140 yield promiseAddVisits({
michael@0 141 uri: testuri,
michael@0 142 title: title
michael@0 143 });
michael@0 144 yield promiseNotify;
michael@0 145 });
michael@0 146
michael@0 147 add_task(function test_onPageChanged() {
michael@0 148 let promiseNotify = onNotify(function onPageChanged(aURI, aChangedAttribute,
michael@0 149 aNewValue, aGUID) {
michael@0 150 do_check_eq(aChangedAttribute, Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON);
michael@0 151 do_check_true(aURI.equals(testuri));
michael@0 152 do_check_eq(aNewValue, SMALLPNG_DATA_URI.spec);
michael@0 153 do_check_guid_for_uri(aURI, aGUID);
michael@0 154 });
michael@0 155
michael@0 156 let [testuri] = yield task_add_visit();
michael@0 157
michael@0 158 // The new favicon for the page must have data associated with it in order to
michael@0 159 // receive the onPageChanged notification. To keep this test self-contained,
michael@0 160 // we use an URI representing the smallest possible PNG file.
michael@0 161 PlacesUtils.favicons.setAndFetchFaviconForPage(testuri, SMALLPNG_DATA_URI,
michael@0 162 false,
michael@0 163 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
michael@0 164 null);
michael@0 165 yield promiseNotify;
michael@0 166 });

mercurial