OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/command_line.h" |
5 #include "base/string_util.h" | 6 #include "base/string_util.h" |
6 #include "base/test/test_timeouts.h" | 7 #include "base/utf_string_conversions.h" |
7 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/browser/ui/browser.h" |
8 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
9 #include "chrome/common/url_constants.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
10 #include "chrome/test/automation/automation_proxy.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
11 #include "chrome/test/automation/tab_proxy.h" | 12 #include "content/public/browser/web_contents.h" |
12 #include "chrome/test/ui/ui_test.h" | 13 #include "content/public/common/url_constants.h" |
13 #include "net/base/escape.h" | |
14 #include "net/test/test_server.h" | 14 #include "net/test/test_server.h" |
15 | 15 |
16 struct IsSearchProviderTestData; | 16 namespace { |
17 | 17 |
18 class SearchProviderTest : public UITest { | 18 struct IsSearchProviderTestData { |
| 19 IsSearchProviderTestData() : tab(NULL) {} |
| 20 IsSearchProviderTestData(content::WebContents* t, std::string h, GURL url) |
| 21 : tab(t), host(h), test_url(url) { |
| 22 } |
| 23 |
| 24 content::WebContents* tab; |
| 25 std::string host; |
| 26 GURL test_url; |
| 27 }; |
| 28 |
| 29 } |
| 30 |
| 31 class SearchProviderTest : public InProcessBrowserTest { |
19 protected: | 32 protected: |
20 SearchProviderTest(); | 33 SearchProviderTest() {} |
| 34 |
| 35 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 36 ASSERT_TRUE(test_server()->Start()); |
| 37 |
| 38 // Map all hosts to our local server. |
| 39 std::string host_rule( |
| 40 "MAP * " + test_server()->host_port_pair().ToString()); |
| 41 command_line->AppendSwitchASCII(switches::kHostRules, host_rule); |
| 42 // Use no proxy or otherwise this test will fail on a machine that has a |
| 43 // proxy configured. |
| 44 command_line->AppendSwitch(switches::kNoProxyServer); |
| 45 |
| 46 // Get the url for the test page. |
| 47 search_provider_test_url_ = |
| 48 test_server()->GetURL("files/is_search_provider_installed.html"); |
| 49 } |
21 | 50 |
22 IsSearchProviderTestData StartIsSearchProviderInstalledTest( | 51 IsSearchProviderTestData StartIsSearchProviderInstalledTest( |
23 BrowserProxy* browser_proxy, | 52 Browser* browser, |
24 const char* host, | 53 const char* host, |
25 const char* expected_result); | 54 const char* expected_result) { |
| 55 GURL test_url(std::string("http://") + host + |
| 56 search_provider_test_url_.path() + "#" + expected_result); |
| 57 ui_test_utils::NavigateToURLWithDisposition( |
| 58 browser, test_url, NEW_FOREGROUND_TAB, |
| 59 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 60 |
| 61 // Bundle up information needed to verify the result. |
| 62 content::WebContents* tab = browser->GetSelectedWebContents(); |
| 63 return IsSearchProviderTestData(tab, host, test_url); |
| 64 } |
26 | 65 |
27 void FinishIsSearchProviderInstalledTest( | 66 void FinishIsSearchProviderInstalledTest( |
28 const IsSearchProviderTestData& data); | 67 const IsSearchProviderTestData& data) { |
| 68 string16 title = data.tab->GetTitle(); |
| 69 if (title.empty()) { |
| 70 ui_test_utils::TitleWatcher title_watcher(data.tab, ASCIIToUTF16("OK")); |
| 71 title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); |
| 72 title = title_watcher.WaitAndGetTitle(); |
| 73 } |
| 74 EXPECT_EQ(ASCIIToUTF16("OK"), title); |
| 75 } |
29 | 76 |
30 net::TestServer test_server_; | |
31 GURL search_provider_test_url_; | 77 GURL search_provider_test_url_; |
32 bool test_server_started_; | |
33 | 78 |
34 private: | 79 private: |
35 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); | 80 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); |
36 }; | 81 }; |
37 | 82 |
38 SearchProviderTest::SearchProviderTest() | 83 // If this flakes on Windows, use http://crbug.com/62777 |
39 : test_server_(net::TestServer::TYPE_HTTP, | 84 IN_PROC_BROWSER_TEST_F(SearchProviderTest, TestIsSearchProviderInstalled) { |
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 | 85 // Use the default search provider, other installed search provider, and |
139 // one not installed as well. (Note that yahoo isn't tested because the | 86 // 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, | 87 // 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.) | 88 // which would make the test fail depending on the machine's locale.) |
142 const char* test_hosts[] = { "www.google.com", | 89 const char* test_hosts[] = { "www.google.com", |
143 "www.bing.com", | 90 "www.bing.com", |
144 "localhost" }; | 91 "localhost" }; |
145 const char* expected_results[] = { "2", | 92 const char* expected_results[] = { "2", |
146 "1", | 93 "1", |
147 "0" }; | 94 "0" }; |
148 COMPILE_ASSERT(arraysize(test_hosts) == arraysize(expected_results), | 95 COMPILE_ASSERT(arraysize(test_hosts) == arraysize(expected_results), |
149 there_should_be_a_result_for_each_host); | 96 there_should_be_a_result_for_each_host); |
150 IsSearchProviderTestData test_data[2 * arraysize(test_hosts)]; | 97 IsSearchProviderTestData test_data[2 * arraysize(test_hosts)]; |
151 | 98 |
152 // Start results for the normal mode. | 99 // 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) { | 100 for (size_t i = 0; i < arraysize(test_hosts); ++i) { |
156 test_data[i] = StartIsSearchProviderInstalledTest( | 101 test_data[i] = StartIsSearchProviderInstalledTest( |
157 browser, test_hosts[i], expected_results[i]); | 102 browser(), test_hosts[i], expected_results[i]); |
158 FinishIsSearchProviderInstalledTest(test_data[i]); | 103 FinishIsSearchProviderInstalledTest(test_data[i]); |
159 } | 104 } |
160 | 105 |
161 // Start tests for incognito mode (and verify the result is 0). | 106 // Start tests for incognito mode (and verify the result is 0). |
162 ASSERT_TRUE(browser->RunCommand(IDC_NEW_INCOGNITO_WINDOW)); | 107 Browser* incognito_browser = CreateIncognitoBrowser(); |
163 scoped_refptr<BrowserProxy> incognito(automation()->GetBrowserWindow(1)); | |
164 ASSERT_TRUE(incognito.get()); | |
165 for (size_t i = 0; i < arraysize(test_hosts); ++i) { | 108 for (size_t i = 0; i < arraysize(test_hosts); ++i) { |
166 test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest( | 109 test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest( |
167 incognito, test_hosts[i], "0"); | 110 incognito_browser, test_hosts[i], "0"); |
168 FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]); | 111 FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]); |
169 } | 112 } |
170 | 113 |
171 // The following should be re-enabled. At the moment, there are problems with | 114 // 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. | 115 // doing all of these queries in parallel -- see http://crbug.com/60043. |
173 #if 0 | 116 #if 0 |
174 // Remove the calls to FinishIsSearchProviderInstalledTest above when | 117 // Remove the calls to FinishIsSearchProviderInstalledTest above when |
175 // re-enabling this code. | 118 // re-enabling this code. |
176 | 119 |
177 // Do the verification. | 120 // Do the verification. |
178 for (size_t i = 0; i < arraysize(test_data); ++i) { | 121 for (size_t i = 0; i < arraysize(test_data); ++i) { |
179 FinishIsSearchProviderInstalledTest(test_data[i]); | 122 FinishIsSearchProviderInstalledTest(test_data[i]); |
180 } | 123 } |
181 #endif | 124 #endif |
182 } | 125 } |
183 | 126 |
184 TEST_F(SearchProviderTest, TestIsSearchProviderInstalledWithException) { | 127 IN_PROC_BROWSER_TEST_F(SearchProviderTest, |
| 128 TestIsSearchProviderInstalledWithException) { |
185 // Change the url for the test page to one that throws an exception when | 129 // Change the url for the test page to one that throws an exception when |
186 // toString is called on the argument given to isSearchProviderInstalled. | 130 // toString is called on the argument given to isSearchProviderInstalled. |
187 search_provider_test_url_ = test_server_.GetURL( | 131 search_provider_test_url_ = test_server()->GetURL( |
188 "files/is_search_provider_installed_with_exception.html"); | 132 "files/is_search_provider_installed_with_exception.html"); |
189 | 133 |
190 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
191 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest( | 134 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest( |
192 browser, "www.google.com", "")); | 135 browser(), "www.google.com", "")); |
193 } | 136 } |
OLD | NEW |