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 // Map all hosts to our local server.
49 std::string
host_rule("MAP * " + test_server_
.host_port_pair().ToString());
50 launch_arguments_
.AppendSwitchASCII(switches::kHostRules
, host_rule
);
52 // Get the url for the test page.
53 search_provider_test_url_
=
54 test_server_
.GetURL("files/is_search_provider_installed.html");
57 struct IsSearchProviderTestData
{
58 IsSearchProviderTestData() {
61 IsSearchProviderTestData(TabProxy
* t
,
69 scoped_refptr
<TabProxy
> tab
;
74 IsSearchProviderTestData
SearchProviderTest::StartIsSearchProviderInstalledTest(
75 BrowserProxy
* browser_proxy
,
77 const char* expected_result
) {
78 // Set-up a new tab for the navigation.
80 if (!browser_proxy
->GetTabCount(&num_tabs
)) {
81 ADD_FAILURE() << "BrowserProxy::GetTabCount failed.";
82 return IsSearchProviderTestData();
85 GURL
blank(chrome::kAboutBlankURL
);
86 if (!browser_proxy
->AppendTab(blank
)) {
87 ADD_FAILURE() << "BrowserProxy::AppendTab failed.";
88 return IsSearchProviderTestData();
91 scoped_refptr
<TabProxy
> tab(browser_proxy
->GetTab(num_tabs
));
93 ADD_FAILURE() << "BrowserProxy::GetTab for the new tab failed.";
94 return IsSearchProviderTestData();
97 // Go to the test page.
98 GURL
test_url(std::string("http://") + host
+
99 search_provider_test_url_
.path() + "#" + expected_result
);
100 EXPECT_TRUE(tab
->NavigateToURLAsync(test_url
));
102 // Bundle up information needed to verify the result.
103 return IsSearchProviderTestData(tab
, host
, test_url
);
106 void SearchProviderTest::FinishIsSearchProviderInstalledTest(
107 const IsSearchProviderTestData
& data
) {
108 ASSERT_TRUE(data
.tab
.get());
110 std::string cookie_name
= data
.host
+ "testResult";
111 std::string escaped_value
=
112 WaitUntilCookieNonEmpty(data
.tab
,
115 TestTimeouts::action_max_timeout_ms());
117 // Unescapes and normalizes the actual result.
118 std::string value
= net::UnescapeURLComponent(
120 net::UnescapeRule::NORMAL
| net::UnescapeRule::SPACES
|
121 net::UnescapeRule::URL_SPECIAL_CHARS
| net::UnescapeRule::CONTROL_CHARS
);
123 ReplaceSubstringsAfterOffset(&value
, 0, "\r", "");
124 EXPECT_STREQ("1\n", value
.c_str());
125 EXPECT_TRUE(data
.tab
->Close(true));
128 // Flaky on XP debug. http://crbug.com/62777
130 #define MAYBE_TestIsSearchProviderInstalled FLAKY_TestIsSearchProviderInstalled
132 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
134 TEST_F(SearchProviderTest
, MAYBE_TestIsSearchProviderInstalled
) {
135 ASSERT_TRUE(test_server_started_
);
137 // Use the default search provider, other installed search provider, and
138 // one not installed as well. (Note that yahoo isn't tested because the
139 // its host name varies a lot for different locales unlike Google and Bing,
140 // which would make the test fail depending on the machine's locale.)
141 const char* test_hosts
[] = { "www.google.com",
144 const char* expected_results
[] = { "2",
147 COMPILE_ASSERT(arraysize(test_hosts
) == arraysize(expected_results
),
148 there_should_be_a_result_for_each_host
);
149 IsSearchProviderTestData test_data
[2 * arraysize(test_hosts
)];
151 // Start results for the normal mode.
152 scoped_refptr
<BrowserProxy
> browser(automation()->GetBrowserWindow(0));
153 ASSERT_TRUE(browser
.get());
154 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
155 test_data
[i
] = StartIsSearchProviderInstalledTest(
156 browser
, test_hosts
[i
], expected_results
[i
]);
157 FinishIsSearchProviderInstalledTest(test_data
[i
]);
160 // Start tests for incognito mode (and verify the result is 0).
161 ASSERT_TRUE(browser
->RunCommand(IDC_NEW_INCOGNITO_WINDOW
));
162 scoped_refptr
<BrowserProxy
> incognito(automation()->GetBrowserWindow(1));
163 ASSERT_TRUE(incognito
.get());
164 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
165 test_data
[i
+ arraysize(test_hosts
)] = StartIsSearchProviderInstalledTest(
166 incognito
, test_hosts
[i
], "0");
167 FinishIsSearchProviderInstalledTest(test_data
[i
+ arraysize(test_hosts
)]);
170 // The following should be re-enabled. At the moment, there are problems with
171 // doing all of these queries in parallel -- see http://crbug.com/60043.
173 // Remove the calls to FinishIsSearchProviderInstalledTest above when
174 // re-enabling this code.
176 // Do the verification.
177 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
178 FinishIsSearchProviderInstalledTest(test_data
[i
]);
183 TEST_F(SearchProviderTest
, TestIsSearchProviderInstalledWithException
) {
184 // Change the url for the test page to one that throws an exception when
185 // toString is called on the argument given to isSearchProviderInstalled.
186 search_provider_test_url_
= test_server_
.GetURL(
187 "files/is_search_provider_installed_with_exception.html");
189 scoped_refptr
<BrowserProxy
> browser(automation()->GetBrowserWindow(0));
190 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
191 browser
, "www.google.com", ""));