1 // Copyright (c) 2011 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 #include "base/string_util.h"
6 #include "base/test/test_timeouts.h"
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/common/chrome_switches.h"
9 #include "chrome/common/url_constants.h"
10 #include "chrome/test/automation/automation_proxy.h"
11 #include "chrome/test/automation/tab_proxy.h"
12 #include "chrome/test/ui/ui_layout_test.h"
13 #include "net/base/escape.h"
14 #include "net/test/test_server.h"
16 struct IsSearchProviderTestData
;
18 class SearchProviderTest
: public UITest
{
22 IsSearchProviderTestData
StartIsSearchProviderInstalledTest(
23 BrowserProxy
* browser_proxy
,
25 const char* expected_result
);
27 void FinishIsSearchProviderInstalledTest(
28 const IsSearchProviderTestData
& data
);
30 net::TestServer test_server_
;
31 GURL search_provider_test_url_
;
32 bool test_server_started_
;
35 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest
);
38 SearchProviderTest::SearchProviderTest()
39 : test_server_(net::TestServer::TYPE_HTTP
,
40 FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
41 test_server_started_(false) {
42 // The test_server is started in the constructor (rather than the test body)
43 // so the mapping rules below can include the ephemeral port number.
44 test_server_started_
= test_server_
.Start();
45 if (!test_server_started_
)
48 // Enable the search provider additions.
49 launch_arguments_
.AppendSwitch(switches::kEnableSearchProviderApiV2
);
51 // Map all hosts to our local server.
52 std::string
host_rule("MAP * " + test_server_
.host_port_pair().ToString());
53 launch_arguments_
.AppendSwitchASCII(switches::kHostRules
, host_rule
);
55 // Get the url for the test page.
56 search_provider_test_url_
=
57 test_server_
.GetURL("files/is_search_provider_installed.html");
60 struct IsSearchProviderTestData
{
61 IsSearchProviderTestData() {
64 IsSearchProviderTestData(TabProxy
* t
,
72 scoped_refptr
<TabProxy
> tab
;
77 IsSearchProviderTestData
SearchProviderTest::StartIsSearchProviderInstalledTest(
78 BrowserProxy
* browser_proxy
,
80 const char* expected_result
) {
81 // Set-up a new tab for the navigation.
83 if (!browser_proxy
->GetTabCount(&num_tabs
)) {
84 ADD_FAILURE() << "BrowserProxy::GetTabCount failed.";
85 return IsSearchProviderTestData();
88 GURL
blank(chrome::kAboutBlankURL
);
89 if (!browser_proxy
->AppendTab(blank
)) {
90 ADD_FAILURE() << "BrowserProxy::AppendTab failed.";
91 return IsSearchProviderTestData();
94 scoped_refptr
<TabProxy
> tab(browser_proxy
->GetTab(num_tabs
));
96 ADD_FAILURE() << "BrowserProxy::GetTab for the new tab failed.";
97 return IsSearchProviderTestData();
100 // Go to the test page.
101 GURL
test_url(std::string("http://") + host
+
102 search_provider_test_url_
.path() + "#" + expected_result
);
103 EXPECT_TRUE(tab
->NavigateToURLAsync(test_url
));
105 // Bundle up information needed to verify the result.
106 return IsSearchProviderTestData(tab
, host
, test_url
);
109 void SearchProviderTest::FinishIsSearchProviderInstalledTest(
110 const IsSearchProviderTestData
& data
) {
111 ASSERT_TRUE(data
.tab
.get());
113 std::string cookie_name
= data
.host
+ "testResult";
114 std::string escaped_value
=
115 WaitUntilCookieNonEmpty(data
.tab
,
118 TestTimeouts::action_max_timeout_ms());
120 // Unescapes and normalizes the actual result.
121 std::string value
= net::UnescapeURLComponent(
123 UnescapeRule::NORMAL
| UnescapeRule::SPACES
|
124 UnescapeRule::URL_SPECIAL_CHARS
| UnescapeRule::CONTROL_CHARS
);
126 ReplaceSubstringsAfterOffset(&value
, 0, "\r", "");
127 EXPECT_STREQ("1\n", value
.c_str());
128 EXPECT_TRUE(data
.tab
->Close(true));
131 // Flaky on XP debug. http://crbug.com/62777
133 #define MAYBE_TestIsSearchProviderInstalled FLAKY_TestIsSearchProviderInstalled
135 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
137 TEST_F(SearchProviderTest
, MAYBE_TestIsSearchProviderInstalled
) {
138 ASSERT_TRUE(test_server_started_
);
140 // Use the default search provider, other installed search provider, and
141 // one not installed as well. (Note that yahoo isn't tested because the
142 // its host name varies a lot for different locales unlike Google and Bing,
143 // which would make the test fail depending on the machine's locale.)
144 const char* test_hosts
[] = { "www.google.com",
147 const char* expected_results
[] = { "2",
150 COMPILE_ASSERT(arraysize(test_hosts
) == arraysize(expected_results
),
151 there_should_be_a_result_for_each_host
);
152 IsSearchProviderTestData test_data
[2 * arraysize(test_hosts
)];
154 // Start results for the normal mode.
155 scoped_refptr
<BrowserProxy
> browser(automation()->GetBrowserWindow(0));
156 ASSERT_TRUE(browser
.get());
157 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
158 test_data
[i
] = StartIsSearchProviderInstalledTest(
159 browser
, test_hosts
[i
], expected_results
[i
]);
160 FinishIsSearchProviderInstalledTest(test_data
[i
]);
163 // Start tests for incognito mode (and verify the result is 0).
164 ASSERT_TRUE(browser
->RunCommand(IDC_NEW_INCOGNITO_WINDOW
));
165 scoped_refptr
<BrowserProxy
> incognito(automation()->GetBrowserWindow(1));
166 ASSERT_TRUE(incognito
.get());
167 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
168 test_data
[i
+ arraysize(test_hosts
)] = StartIsSearchProviderInstalledTest(
169 incognito
, test_hosts
[i
], "0");
170 FinishIsSearchProviderInstalledTest(test_data
[i
+ arraysize(test_hosts
)]);
173 // The following should be re-enabled. At the moment, there are problems with
174 // doing all of these queries in parallel -- see http://crbug.com/60043.
176 // Remove the calls to FinishIsSearchProviderInstalledTest above when
177 // re-enabling this code.
179 // Do the verification.
180 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
181 FinishIsSearchProviderInstalledTest(test_data
[i
]);
186 TEST_F(SearchProviderTest
, TestIsSearchProviderInstalledWithException
) {
187 // Change the url for the test page to one that throws an exception when
188 // toString is called on the argument given to isSearchProviderInstalled.
189 search_provider_test_url_
= test_server_
.GetURL(
190 "files/is_search_provider_installed_with_exception.html");
192 scoped_refptr
<BrowserProxy
> browser(automation()->GetBrowserWindow(0));
193 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
194 browser
, "www.google.com", ""));