- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / history / common.js
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.
4
5 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7 var assertEq = chrome.test.assertEq;
8 var assertTrue = chrome.test.assertTrue;
9
10 var GOOGLE_URL = 'http://www.google.com/';
11 var PICASA_URL = 'http://www.picasa.com/';
12
13 // PORT will be changed to the port of the test server.
14 var A_RELATIVE_URL =
15     'http://www.a.com:PORT/extensions/api_test/history/a.html';
16 var B_RELATIVE_URL =
17     'http://www.b.com:PORT/extensions/api_test/history/b.html';
18
19 /**
20  * A helper function to flip the setTimeout arguments and make the code
21  * more readable.
22  * @param {number} seconds The number of seconds to wait.
23  * @param {function} callback Closure.
24  */
25 function waitAFewSeconds(seconds, callback) {
26   setTimeout(callback, seconds * 1000);
27 };
28
29 /**
30  * Object used for listening to the chrome.history.onVisited events.  The
31  * global object 'itemVisited' stores the last item received.
32  */
33 var itemVisitedCallback = null;
34 function itemVisitedListener(visited) {
35   if (null != itemVisitedCallback) {
36     itemVisitedCallback(visited);
37   };
38 };
39
40 function removeItemVisitedListener() {
41   chrome.history.onVisited.removeListener(itemVisitedListener);
42   itemVisitedCallback = null;
43 }
44
45 function setItemVisitedListener(callback) {
46   chrome.history.onVisited.addListener(itemVisitedListener);
47   itemVisitedCallback = callback;
48 }
49
50 function setNextItemVisitedListener(callback) {
51   itemVisitedCallback = callback;
52 }
53
54 /**
55  * An object used for listening to the chrome.history.onVisitRemoved events.
56  * The global object 'itemRemovedInfo' stores the information from the last
57  * callback.
58  */
59 var itemRemovedCallback = null;
60 function itemRemovedListener(removed) {
61   if (null != itemRemovedCallback) {
62     itemRemovedCallback(removed);
63   };
64 };
65
66 function removeItemRemovedListener() {
67   chrome.history.onVisited.removeListener(itemRemovedListener);
68   itemRemovedCallback = null;
69 }
70
71 function setItemRemovedListener(callback) {
72   chrome.history.onVisitRemoved.addListener(itemRemovedListener);
73   itemRemovedCallback = callback;
74 }
75
76 function setNextItemRemovedListener(callback) {
77   itemRemovedCallback = callback;
78 }
79
80 /**
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.
85  */
86 var tabCompleteCallback = null;
87 var tabsCompleteData = {};
88 function tabsCompleteListener(tabId, changeInfo) {
89   if (changeInfo && changeInfo.status) {
90     tabsCompleteData[tabId] = changeInfo.status;
91   };
92   if (null != tabCompleteCallback) {
93     tabCompleteCallback();
94   };
95 };
96
97 /**
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.
101  */
102 function countItemsInHistory(callback) {
103   var query = {'text': ''};
104   chrome.history.search(query, function(results) {
105     callback(results.length);
106   });
107 }
108
109 /**
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.
113  */
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)
119         callback()
120     });
121   });
122 }
123
124 /**
125  * Tests call this function to invoke specific tests.
126  * @param {Array.<funcion>} testFns The tests to run.
127  */
128 function runHistoryTestFns(testFns) {
129   chrome.test.getConfig(function(config) {
130     var fixPort = function(url) {
131       return url.replace(/PORT/, config.testServer.port);
132     };
133     A_RELATIVE_URL = fixPort(A_RELATIVE_URL);
134     B_RELATIVE_URL = fixPort(B_RELATIVE_URL);
135
136     chrome.test.runTests(testFns);
137   });
138 }
139
140 /**
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.
148  */
149 function addUrlsWithTimeline(urls, callback) {
150   // If a test needs more than two urls, this could be generalized.
151   assertEq(2, urls.length);
152
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);
163
164             // Don't assume anything about the order of history records in
165             // |historyItems|.
166             var firstUrlTime = Math.min(historyItems[0].lastVisitTime,
167                                         historyItems[1].lastVisitTime);
168             var secondUrlTime = Math.max(historyItems[0].lastVisitTime,
169                                          historyItems[1].lastVisitTime);
170
171             callback({
172               before: firstUrlTime - 100.0,
173               between: (firstUrlTime + secondUrlTime) / 2.0,
174               after: secondUrlTime + 100.0
175             });
176           });
177         });
178       });
179     });
180   });
181 }