OLD | NEW |
| (Empty) |
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 #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_test.h" | |
13 #include "net/base/escape.h" | |
14 #include "net/test/test_server.h" | |
15 | |
16 struct IsSearchProviderTestData; | |
17 | |
18 class SearchProviderTest : public UITest { | |
19 protected: | |
20 SearchProviderTest(); | |
21 | |
22 IsSearchProviderTestData StartIsSearchProviderInstalledTest( | |
23 BrowserProxy* browser_proxy, | |
24 const char* host, | |
25 const char* expected_result); | |
26 | |
27 void FinishIsSearchProviderInstalledTest( | |
28 const IsSearchProviderTestData& data); | |
29 | |
30 net::TestServer test_server_; | |
31 GURL search_provider_test_url_; | |
32 bool test_server_started_; | |
33 | |
34 private: | |
35 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); | |
36 }; | |
37 | |
38 SearchProviderTest::SearchProviderTest() | |
39 : test_server_(net::TestServer::TYPE_HTTP, | |
40 net::TestServer::kLocalhost, | |
41 FilePath(FILE_PATH_LITERAL("chrome/test/data"))), | |
42 test_server_started_(false) { | |
43 // The test_server is started in the constructor (rather than the test body) | |
44 // so the mapping rules below can include the ephemeral port number. | |
45 test_server_started_ = test_server_.Start(); | |
46 if (!test_server_started_) | |
47 return; | |
48 | |
49 // Map all hosts to our local server. | |
50 std::string host_rule("MAP * " + test_server_.host_port_pair().ToString()); | |
51 launch_arguments_.AppendSwitchASCII(switches::kHostRules, host_rule); | |
52 | |
53 // Get the url for the test page. | |
54 search_provider_test_url_ = | |
55 test_server_.GetURL("files/is_search_provider_installed.html"); | |
56 } | |
57 | |
58 struct IsSearchProviderTestData { | |
59 IsSearchProviderTestData() { | |
60 } | |
61 | |
62 IsSearchProviderTestData(TabProxy* t, | |
63 std::string h, | |
64 GURL url) | |
65 : tab(t), | |
66 host(h), | |
67 test_url(url) { | |
68 } | |
69 | |
70 scoped_refptr<TabProxy> tab; | |
71 std::string host; | |
72 GURL test_url; | |
73 }; | |
74 | |
75 IsSearchProviderTestData SearchProviderTest::StartIsSearchProviderInstalledTest( | |
76 BrowserProxy* browser_proxy, | |
77 const char* host, | |
78 const char* expected_result) { | |
79 // Set-up a new tab for the navigation. | |
80 int num_tabs = 0; | |
81 if (!browser_proxy->GetTabCount(&num_tabs)) { | |
82 ADD_FAILURE() << "BrowserProxy::GetTabCount failed."; | |
83 return IsSearchProviderTestData(); | |
84 } | |
85 | |
86 GURL blank(chrome::kAboutBlankURL); | |
87 if (!browser_proxy->AppendTab(blank)) { | |
88 ADD_FAILURE() << "BrowserProxy::AppendTab failed."; | |
89 return IsSearchProviderTestData(); | |
90 } | |
91 | |
92 scoped_refptr<TabProxy> tab(browser_proxy->GetTab(num_tabs)); | |
93 if (!tab.get()) { | |
94 ADD_FAILURE() << "BrowserProxy::GetTab for the new tab failed."; | |
95 return IsSearchProviderTestData(); | |
96 } | |
97 | |
98 // Go to the test page. | |
99 GURL test_url(std::string("http://") + host + | |
100 search_provider_test_url_.path() + "#" + expected_result); | |
101 EXPECT_TRUE(tab->NavigateToURLAsync(test_url)); | |
102 | |
103 // Bundle up information needed to verify the result. | |
104 return IsSearchProviderTestData(tab, host, test_url); | |
105 } | |
106 | |
107 void SearchProviderTest::FinishIsSearchProviderInstalledTest( | |
108 const IsSearchProviderTestData& data) { | |
109 ASSERT_TRUE(data.tab.get()); | |
110 | |
111 std::string cookie_name = data.host + "testResult"; | |
112 std::string escaped_value = | |
113 WaitUntilCookieNonEmpty(data.tab, | |
114 data.test_url, | |
115 cookie_name.c_str(), | |
116 TestTimeouts::action_max_timeout_ms()); | |
117 | |
118 // Unescapes and normalizes the actual result. | |
119 std::string value = net::UnescapeURLComponent( | |
120 escaped_value, | |
121 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES | | |
122 net::UnescapeRule::URL_SPECIAL_CHARS | net::UnescapeRule::CONTROL_CHARS); | |
123 value += "\n"; | |
124 ReplaceSubstringsAfterOffset(&value, 0, "\r", ""); | |
125 EXPECT_STREQ("1\n", value.c_str()); | |
126 EXPECT_TRUE(data.tab->Close(true)); | |
127 } | |
128 | |
129 // Flaky on XP debug. http://crbug.com/62777 | |
130 #if defined(OS_WIN) | |
131 #define MAYBE_TestIsSearchProviderInstalled DISABLED_TestIsSearchProviderInstall
ed | |
132 #else | |
133 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled | |
134 #endif | |
135 TEST_F(SearchProviderTest, MAYBE_TestIsSearchProviderInstalled) { | |
136 ASSERT_TRUE(test_server_started_); | |
137 | |
138 // Use the default search provider, other installed search provider, and | |
139 // one not installed as well. (Note that yahoo isn't tested because the | |
140 // its host name varies a lot for different locales unlike Google and Bing, | |
141 // which would make the test fail depending on the machine's locale.) | |
142 const char* test_hosts[] = { "www.google.com", | |
143 "www.bing.com", | |
144 "localhost" }; | |
145 const char* expected_results[] = { "2", | |
146 "1", | |
147 "0" }; | |
148 COMPILE_ASSERT(arraysize(test_hosts) == arraysize(expected_results), | |
149 there_should_be_a_result_for_each_host); | |
150 IsSearchProviderTestData test_data[2 * arraysize(test_hosts)]; | |
151 | |
152 // Start results for the normal mode. | |
153 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
154 ASSERT_TRUE(browser.get()); | |
155 for (size_t i = 0; i < arraysize(test_hosts); ++i) { | |
156 test_data[i] = StartIsSearchProviderInstalledTest( | |
157 browser, test_hosts[i], expected_results[i]); | |
158 FinishIsSearchProviderInstalledTest(test_data[i]); | |
159 } | |
160 | |
161 // Start tests for incognito mode (and verify the result is 0). | |
162 ASSERT_TRUE(browser->RunCommand(IDC_NEW_INCOGNITO_WINDOW)); | |
163 scoped_refptr<BrowserProxy> incognito(automation()->GetBrowserWindow(1)); | |
164 ASSERT_TRUE(incognito.get()); | |
165 for (size_t i = 0; i < arraysize(test_hosts); ++i) { | |
166 test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest( | |
167 incognito, test_hosts[i], "0"); | |
168 FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]); | |
169 } | |
170 | |
171 // The following should be re-enabled. At the moment, there are problems with | |
172 // doing all of these queries in parallel -- see http://crbug.com/60043. | |
173 #if 0 | |
174 // Remove the calls to FinishIsSearchProviderInstalledTest above when | |
175 // re-enabling this code. | |
176 | |
177 // Do the verification. | |
178 for (size_t i = 0; i < arraysize(test_data); ++i) { | |
179 FinishIsSearchProviderInstalledTest(test_data[i]); | |
180 } | |
181 #endif | |
182 } | |
183 | |
184 TEST_F(SearchProviderTest, TestIsSearchProviderInstalledWithException) { | |
185 // Change the url for the test page to one that throws an exception when | |
186 // toString is called on the argument given to isSearchProviderInstalled. | |
187 search_provider_test_url_ = test_server_.GetURL( | |
188 "files/is_search_provider_installed_with_exception.html"); | |
189 | |
190 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
191 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest( | |
192 browser, "www.google.com", "")); | |
193 } | |
OLD | NEW |