|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * What this is aimed to test: |
|
9 * |
|
10 * Expiring only visits for a page, but not the full page, should fire an |
|
11 * onDeleteVisits notification. |
|
12 */ |
|
13 |
|
14 let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. |
|
15 getService(Ci.nsINavHistoryService); |
|
16 let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. |
|
17 getService(Ci.nsINavBookmarksService); |
|
18 |
|
19 let tests = [ |
|
20 |
|
21 { desc: "Add 1 bookmarked page.", |
|
22 addPages: 1, |
|
23 visitsPerPage: 1, |
|
24 addBookmarks: 1, |
|
25 limitExpiration: -1, |
|
26 expectedNotifications: 1, // Will expire visits for 1 page. |
|
27 }, |
|
28 |
|
29 { desc: "Add 2 pages, 1 bookmarked.", |
|
30 addPages: 2, |
|
31 visitsPerPage: 1, |
|
32 addBookmarks: 1, |
|
33 limitExpiration: -1, |
|
34 expectedNotifications: 1, // Will expire visits for 1 page. |
|
35 }, |
|
36 |
|
37 { desc: "Add 10 pages, none bookmarked.", |
|
38 addPages: 10, |
|
39 visitsPerPage: 1, |
|
40 addBookmarks: 0, |
|
41 limitExpiration: -1, |
|
42 expectedNotifications: 0, // Will expire only full pages. |
|
43 }, |
|
44 |
|
45 { desc: "Add 10 pages, all bookmarked.", |
|
46 addPages: 10, |
|
47 visitsPerPage: 1, |
|
48 addBookmarks: 10, |
|
49 limitExpiration: -1, |
|
50 expectedNotifications: 10, // Will expire visist for all pages. |
|
51 }, |
|
52 |
|
53 { desc: "Add 10 pages with lot of visits, none bookmarked.", |
|
54 addPages: 10, |
|
55 visitsPerPage: 10, |
|
56 addBookmarks: 0, |
|
57 limitExpiration: 10, |
|
58 expectedNotifications: 10, // Will expire 1 visist for each page, but won't |
|
59 }, // expire pages since they still have visits. |
|
60 |
|
61 ]; |
|
62 |
|
63 function run_test() { |
|
64 run_next_test(); |
|
65 } |
|
66 |
|
67 add_task(function test_notifications_onDeleteVisits() { |
|
68 // Set interval to a large value so we don't expire on it. |
|
69 setInterval(3600); // 1h |
|
70 |
|
71 // Expire anything that is expirable. |
|
72 setMaxPages(0); |
|
73 |
|
74 for (let testIndex = 1; testIndex <= tests.length; testIndex++) { |
|
75 let currentTest = tests[testIndex -1]; |
|
76 print("\nTEST " + testIndex + ": " + currentTest.desc); |
|
77 currentTest.receivedNotifications = 0; |
|
78 |
|
79 // Setup visits. |
|
80 let now = getExpirablePRTime(); |
|
81 for (let j = 0; j < currentTest.visitsPerPage; j++) { |
|
82 for (let i = 0; i < currentTest.addPages; i++) { |
|
83 let page = "http://" + testIndex + "." + i + ".mozilla.org/"; |
|
84 yield promiseAddVisits({ uri: uri(page), visitDate: now++ }); |
|
85 } |
|
86 } |
|
87 |
|
88 // Setup bookmarks. |
|
89 currentTest.bookmarks = []; |
|
90 for (let i = 0; i < currentTest.addBookmarks; i++) { |
|
91 let page = "http://" + testIndex + "." + i + ".mozilla.org/"; |
|
92 bs.insertBookmark(bs.unfiledBookmarksFolder, uri(page), |
|
93 bs.DEFAULT_INDEX, null); |
|
94 currentTest.bookmarks.push(page); |
|
95 } |
|
96 |
|
97 // Observe history. |
|
98 historyObserver = { |
|
99 onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {}, |
|
100 onEndUpdateBatch: function PEX_onEndUpdateBatch() {}, |
|
101 onClearHistory: function() {}, |
|
102 onVisit: function() {}, |
|
103 onTitleChanged: function() {}, |
|
104 onDeleteURI: function(aURI, aGUID, aReason) { |
|
105 // Check this uri was not bookmarked. |
|
106 do_check_eq(currentTest.bookmarks.indexOf(aURI.spec), -1); |
|
107 do_check_valid_places_guid(aGUID); |
|
108 do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_EXPIRED); |
|
109 }, |
|
110 onPageChanged: function() {}, |
|
111 onDeleteVisits: function(aURI, aTime, aGUID, aReason) { |
|
112 currentTest.receivedNotifications++; |
|
113 do_check_guid_for_uri(aURI, aGUID); |
|
114 do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_EXPIRED); |
|
115 }, |
|
116 }; |
|
117 hs.addObserver(historyObserver, false); |
|
118 |
|
119 // Expire now. |
|
120 yield promiseForceExpirationStep(currentTest.limitExpiration); |
|
121 |
|
122 hs.removeObserver(historyObserver, false); |
|
123 |
|
124 do_check_eq(currentTest.receivedNotifications, |
|
125 currentTest.expectedNotifications); |
|
126 |
|
127 // Clean up. |
|
128 bs.removeFolderChildren(bs.unfiledBookmarksFolder); |
|
129 yield promiseClearHistory(); |
|
130 } |
|
131 |
|
132 clearMaxPages(); |
|
133 bs.removeFolderChildren(bs.unfiledBookmarksFolder); |
|
134 yield promiseClearHistory(); |
|
135 }); |