toolkit/components/places/tests/browser/browser_bug248970.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:c72dcc0386b4
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // This test performs checks on the history testing area as outlined
6 // https://wiki.mozilla.org/Firefox3.1/PrivateBrowsing/TestPlan#History
7 // http://developer.mozilla.org/en/Using_the_Places_history_service
8
9 let visitedURIs = [
10 "http://www.test-link.com/",
11 "http://www.test-typed.com/",
12 "http://www.test-bookmark.com/",
13 "http://www.test-redirect-permanent.com/",
14 "http://www.test-redirect-temporary.com/",
15 "http://www.test-embed.com/",
16 "http://www.test-framed.com/",
17 "http://www.test-download.com/"
18 ].map(NetUtil.newURI.bind(NetUtil));
19
20 add_task(function () {
21 let windowsToClose = [];
22 let placeItemsCount = 0;
23
24 registerCleanupFunction(function() {
25 windowsToClose.forEach(function(win) {
26 win.close();
27 });
28 });
29
30 yield promiseClearHistory();
31
32 // History database should be empty
33 is(PlacesUtils.history.hasHistoryEntries, false,
34 "History database should be empty");
35
36 // Ensure we wait for the default bookmarks import.
37 let bookmarksDeferred = Promise.defer();
38 waitForCondition(() => {
39 placeItemsCount = getPlacesItemsCount();
40 return placeItemsCount > 0
41 }, bookmarksDeferred.resolve, "Should have default bookmarks");
42 yield bookmarksDeferred.promise;
43
44 // Create a handful of history items with various visit types
45 yield promiseAddVisits([
46 { uri: visitedURIs[0], transition: TRANSITION_LINK },
47 { uri: visitedURIs[1], transition: TRANSITION_TYPED },
48 { uri: visitedURIs[2], transition: TRANSITION_BOOKMARK },
49 { uri: visitedURIs[3], transition: TRANSITION_REDIRECT_PERMANENT },
50 { uri: visitedURIs[4], transition: TRANSITION_REDIRECT_TEMPORARY },
51 { uri: visitedURIs[5], transition: TRANSITION_EMBED },
52 { uri: visitedURIs[6], transition: TRANSITION_FRAMED_LINK },
53 { uri: visitedURIs[7], transition: TRANSITION_DOWNLOAD }
54 ]);
55
56 // History database should have entries
57 is(PlacesUtils.history.hasHistoryEntries, true,
58 "History database should have entries");
59
60 placeItemsCount += 7;
61 // We added 7 new items to history.
62 is(getPlacesItemsCount(), placeItemsCount,
63 "Check the total items count");
64
65 function* testOnWindow(aIsPrivate, aCount) {
66 let deferred = Promise.defer();
67 whenNewWindowLoaded({ private: aIsPrivate }, deferred.resolve);
68 let win = yield deferred.promise;
69 windowsToClose.push(win);
70
71 // History items should be retrievable by query
72 yield checkHistoryItems();
73
74 // Updates the place items count
75 let count = getPlacesItemsCount();
76
77 // Create Bookmark
78 let bookmarkTitle = "title " + windowsToClose.length;
79 let bookmarkKeyword = "keyword " + windowsToClose.length;
80 let bookmarkUri = NetUtil.newURI("http://test-a-" + windowsToClose.length + ".com/");
81
82 let id = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarksMenuFolderId,
83 bookmarkUri,
84 PlacesUtils.bookmarks.DEFAULT_INDEX,
85 bookmarkTitle);
86 PlacesUtils.bookmarks.setKeywordForBookmark(id, bookmarkKeyword);
87 count++;
88
89 ok(PlacesUtils.bookmarks.isBookmarked(bookmarkUri),
90 "Bookmark should be bookmarked, data should be retrievable");
91 is(bookmarkKeyword, PlacesUtils.bookmarks.getKeywordForURI(bookmarkUri),
92 "Check bookmark uri keyword");
93 is(getPlacesItemsCount(), count,
94 "Check the new bookmark items count");
95 is(isBookmarkAltered(), false, "Check if bookmark has been visited");
96 }
97
98 // Test on windows.
99 yield testOnWindow(false);
100 yield testOnWindow(true);
101 yield testOnWindow(false);
102 });
103
104 /**
105 * Function performs a really simple query on our places entries,
106 * and makes sure that the number of entries equal num_places_entries.
107 */
108 function getPlacesItemsCount() {
109 // Get bookmarks count
110 let options = PlacesUtils.history.getNewQueryOptions();
111 options.includeHidden = true;
112 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
113 let root = PlacesUtils.history.executeQuery(
114 PlacesUtils.history.getNewQuery(), options).root;
115 root.containerOpen = true;
116 let cc = root.childCount;
117 root.containerOpen = false;
118
119 // Get history item count
120 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
121 let root = PlacesUtils.history.executeQuery(
122 PlacesUtils.history.getNewQuery(), options).root;
123 root.containerOpen = true;
124 cc += root.childCount;
125 root.containerOpen = false;
126
127 return cc;
128 }
129
130 function* checkHistoryItems() {
131 for (let i = 0; i < visitedURIs.length; i++) {
132 let visitedUri = visitedURIs[i];
133 ok((yield promiseIsURIVisited(visitedUri)), "");
134 if (/embed/.test(visitedUri.spec)) {
135 is(!!pageInDatabase(visitedUri), false, "Check if URI is in database");
136 } else {
137 ok(!!pageInDatabase(visitedUri), "Check if URI is in database");
138 }
139 }
140 }
141
142 /**
143 * Checks if an address is found in the database.
144 * @param aURI
145 * nsIURI or address to look for.
146 * @return place id of the page or 0 if not found
147 */
148 function pageInDatabase(aURI) {
149 let url = (aURI instanceof Ci.nsIURI ? aURI.spec : aURI);
150 let stmt = DBConn().createStatement(
151 "SELECT id FROM moz_places WHERE url = :url"
152 );
153 stmt.params.url = url;
154 try {
155 if (!stmt.executeStep())
156 return 0;
157 return stmt.getInt64(0);
158 } finally {
159 stmt.finalize();
160 }
161 }
162
163 /**
164 * Function attempts to check if Bookmark-A has been visited
165 * during private browsing mode, function should return false
166 *
167 * @returns false if the accessCount has not changed
168 * true if the accessCount has changed
169 */
170 function isBookmarkAltered(){
171 let options = PlacesUtils.history.getNewQueryOptions();
172 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS;
173 options.maxResults = 1; // should only expect a new bookmark
174
175 let query = PlacesUtils.history.getNewQuery();
176 query.setFolders([PlacesUtils.bookmarks.bookmarksMenuFolder], 1);
177
178 let root = PlacesUtils.history.executeQuery(query, options).root;
179 root.containerOpen = true;
180 is(root.childCount, options.maxResults, "Check new bookmarks results");
181 let node = root.getChild(0);
182 root.containerOpen = false;
183
184 return (node.accessCount != 0);
185 }

mercurial