1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7 var assertEq = chrome.test.assertEq;
8 var assertTrue = chrome.test.assertTrue;
10 var GOOGLE_URL = 'http://www.google.com/';
11 var PICASA_URL = 'http://www.picasa.com/';
13 // PORT will be changed to the port of the test server.
15 'http://www.a.com:PORT/extensions/api_test/history/a.html';
17 'http://www.b.com:PORT/extensions/api_test/history/b.html';
20 * A helper function to flip the setTimeout arguments and make the code
22 * @param {number} seconds The number of seconds to wait.
23 * @param {function} callback Closure.
25 function waitAFewSeconds(seconds, callback) {
26 setTimeout(callback, seconds * 1000);
30 * Object used for listening to the chrome.history.onVisited events. The
31 * global object 'itemVisited' stores the last item received.
33 var itemVisitedCallback = null;
34 function itemVisitedListener(visited) {
35 if (null != itemVisitedCallback) {
36 itemVisitedCallback(visited);
40 function removeItemVisitedListener() {
41 chrome.history.onVisited.removeListener(itemVisitedListener);
42 itemVisitedCallback = null;
45 function setItemVisitedListener(callback) {
46 chrome.history.onVisited.addListener(itemVisitedListener);
47 itemVisitedCallback = callback;
50 function setNextItemVisitedListener(callback) {
51 itemVisitedCallback = callback;
55 * An object used for listening to the chrome.history.onVisitRemoved events.
56 * The global object 'itemRemovedInfo' stores the information from the last
59 var itemRemovedCallback = null;
60 function itemRemovedListener(removed) {
61 if (null != itemRemovedCallback) {
62 itemRemovedCallback(removed);
66 function removeItemRemovedListener() {
67 chrome.history.onVisited.removeListener(itemRemovedListener);
68 itemRemovedCallback = null;
71 function setItemRemovedListener(callback) {
72 chrome.history.onVisitRemoved.addListener(itemRemovedListener);
73 itemRemovedCallback = callback;
76 function setNextItemRemovedListener(callback) {
77 itemRemovedCallback = callback;
81 * An object used for listening to the chrome.history.onVisitRemoved events.
82 * Set 'tabCompleteCallback' to a function to add extra processing to the
83 * callback. The global object 'tabsCompleteData' contains a list of the
84 * last known state of every tab.
86 var tabCompleteCallback = null;
87 var tabsCompleteData = {};
88 function tabsCompleteListener(tabId, changeInfo) {
89 if (changeInfo && changeInfo.status) {
90 tabsCompleteData[tabId] = changeInfo.status;
92 if (null != tabCompleteCallback) {
93 tabCompleteCallback();
98 * Queries the entire history for items, calling the closure with an argument
99 * specifying the the number of items in the query.
100 * @param {function(number)} callback The closure.
102 function countItemsInHistory(callback) {
103 var query = {'text': ''};
104 chrome.history.search(query, function(results) {
105 callback(results.length);
110 * Populates the history by calling addUrl for each url in the array urls.
111 * @param {Array.<string>} urls The array of urls to populate the history.
112 * @param {function} callback Closure.
114 function populateHistory(urls, callback) {
115 var num_urls_added = 0;
116 urls.forEach(function(url) {
117 chrome.history.addUrl({ 'url': url }, function() {
118 if (++num_urls_added == urls.length)
125 * Tests call this function to invoke specific tests.
126 * @param {Array.<funcion>} testFns The tests to run.
128 function runHistoryTestFns(testFns) {
129 chrome.test.getConfig(function(config) {
130 var fixPort = function(url) {
131 return url.replace(/PORT/, config.testServer.port);
133 A_RELATIVE_URL = fixPort(A_RELATIVE_URL);
134 B_RELATIVE_URL = fixPort(B_RELATIVE_URL);
136 chrome.test.runTests(testFns);
141 * Add two URLs to the history. Compute three times, in ms since the epoch:
142 * 'before': A time before both URLs were added.
143 * 'between': A time between the times teh URLs were added.
144 * 'after': A time after both were added.
145 * All times are passed to |callback| as properties of its object parameter.
146 * @param {Array.<string>} urls An array of two URLs to add to the history.
147 * @param {function(object)} callback Called with the times described above.
149 function addUrlsWithTimeline(urls, callback) {
150 // If a test needs more than two urls, this could be generalized.
151 assertEq(2, urls.length);
153 // Add the first URL now.
154 chrome.history.addUrl({url: urls[0]}, function() {
155 waitAFewSeconds(1, function() {
156 chrome.history.addUrl({url: urls[1]}, function() {
157 waitAFewSeconds(1, function() {
158 // Use search to get the times of the two URLs, and compute times
159 // to pass to the callback.
160 chrome.history.search({text: ''}, function(historyItems) {
161 // Check that both URLs were added.
162 assertEq(urls.length, historyItems.length);
164 // Don't assume anything about the order of history records in
166 var firstUrlTime = Math.min(historyItems[0].lastVisitTime,
167 historyItems[1].lastVisitTime);
168 var secondUrlTime = Math.max(historyItems[0].lastVisitTime,
169 historyItems[1].lastVisitTime);
172 before: firstUrlTime - 100.0,
173 between: (firstUrlTime + secondUrlTime) / 2.0,
174 after: secondUrlTime + 100.0