- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / history / misc_search.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 // History api test for Chrome.
6 // browser_tests.exe --gtest_filter=HistoryExtensionApiTest.MiscSearch
7
8 // runHistoryTestFns is defined in ./common.js .
9 runHistoryTestFns([
10   function basicSearch() {
11     // basicSearch callback.
12     function basicSearchTestVerification() {
13       removeItemVisitedListener();
14       var query = { 'text': '' };
15       chrome.history.search(query, function(results) {
16         assertEq(1, results.length);
17         assertEq(GOOGLE_URL, results[0].url);
18
19         // The test has succeeded.
20         chrome.test.succeed();
21       });
22     };
23
24     // basicSearch entry point.
25     chrome.history.deleteAll(function() {
26       setItemVisitedListener(basicSearchTestVerification);
27       populateHistory([GOOGLE_URL], function() { });
28     });
29   },
30
31   function lengthScopedSearch() {
32     var urls = [GOOGLE_URL, PICASA_URL];
33     var urlsAdded = 0;
34
35     function lengthScopedSearchTestVerification() {
36       // Ensure all urls have been added.
37       urlsAdded += 1;
38       if (urlsAdded < urls.length)
39         return;
40
41       removeItemVisitedListener();
42
43       var query = { 'text': '', 'maxResults': 1 };
44       chrome.history.search(query, function(results) {
45         assertEq(1, results.length);
46         assertEq(PICASA_URL, results[0].url);
47
48         // The test has succeeded.
49         chrome.test.succeed();
50       });
51     };
52
53     // lengthScopedSearch entry point.
54     chrome.history.deleteAll(function() {
55       setItemVisitedListener(lengthScopedSearchTestVerification);
56       populateHistory(urls, function() { });
57     });
58   },
59
60   function fullTextSearch() {
61     chrome.history.deleteAll(function() {
62       // The continuation of the test after the windows have been opened.
63       var validateTest = function() {
64         // Continue with the test.
65         // A title search for www.a.com should find a.
66         var query = { 'text': 'www.a.com' };
67         chrome.history.search(query, function(results) {
68           assertEq(1, results.length);
69           assertEq(A_RELATIVE_URL, results[0].url);
70
71           // Text in the body of b.html.
72           query = { 'text': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' };
73           chrome.history.search(query, function(results) {
74             assertEq(1, results.length);
75             assertEq(B_RELATIVE_URL, results[0].url);
76
77             // The test has succeeded.
78             chrome.test.succeed();
79           });
80         });
81       };
82
83       // Setup a callback object for tab events.
84       var urls = [A_RELATIVE_URL, B_RELATIVE_URL];
85       var tabIds = [];
86
87       function listenerCallback() {
88         if (tabIds.length < urls.length) {
89           return;
90         }
91
92         // Ensure both tabs have completed loading.
93         for (var index = 0, id; id = tabIds[index]; index++) {
94           if (!tabsCompleteData[id] ||
95           tabsCompleteData[id] != 'complete') {
96             return;
97           };
98         }
99
100         // Unhook callbacks.
101         tabCompleteCallback = null;
102         chrome.tabs.onUpdated.removeListener(tabsCompleteListener);
103
104         // Allow indexing to occur.
105         waitAFewSeconds(3, function() {
106           validateTest();
107         });
108       }
109
110       tabCompleteCallback = listenerCallback;
111       chrome.tabs.onUpdated.addListener(tabsCompleteListener);
112
113       // Navigate to a few pages.
114       urls.forEach(function(url) {
115         chrome.tabs.create({ 'url': url }, function(tab) {
116           tabIds.push(tab.id);
117         });
118       });
119     });
120   }
121 ]);