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 <stddef.h> | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/macros.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "build/build_config.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/test/base/in_process_browser_test.h" | |
17 #include "chrome/test/base/ui_test_utils.h" | |
18 #include "components/google/core/browser/google_switches.h" | |
19 #include "components/prefs/pref_service.h" | |
20 #include "components/search_engines/search_engines_pref_names.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "content/public/common/url_constants.h" | |
23 #include "content/public/test/browser_test_utils.h" | |
24 #include "net/test/embedded_test_server/embedded_test_server.h" | |
25 | |
26 namespace { | |
27 | |
28 struct IsSearchProviderTestData { | |
29 IsSearchProviderTestData() : tab(NULL) {} | |
30 IsSearchProviderTestData(content::WebContents* t, | |
31 const std::string& h, | |
32 GURL url) | |
33 : tab(t), host(h), test_url(url) {} | |
34 | |
35 content::WebContents* tab; | |
36 std::string host; | |
37 GURL test_url; | |
38 }; | |
39 | |
40 } // namespace | |
41 | |
42 class SearchProviderTest : public InProcessBrowserTest { | |
43 protected: | |
44 SearchProviderTest() {} | |
45 | |
46 void SetUpCommandLine(base::CommandLine* command_line) override { | |
47 ASSERT_TRUE(embedded_test_server()->Start()); | |
48 | |
49 // Map all hosts to our local server. | |
50 std::string host_rule("MAP * " + | |
51 embedded_test_server()->host_port_pair().ToString()); | |
52 command_line->AppendSwitchASCII(switches::kHostRules, host_rule); | |
53 // Use no proxy or otherwise this test will fail on a machine that has a | |
54 // proxy configured. | |
55 command_line->AppendSwitch(switches::kNoProxyServer); | |
56 | |
57 // Always point google search to a known, non-secure URL. Normally, this | |
58 // varies based on locale and is a HTTPS host. | |
59 command_line->AppendSwitchASCII( | |
60 switches::kGoogleBaseURL, "http://www.google.com"); | |
61 | |
62 // Get the url for the test page. | |
63 search_provider_test_url_ = | |
64 embedded_test_server()->GetURL("/is_search_provider_installed.html"); | |
65 } | |
66 | |
67 void SetUpOnMainThread() override { | |
68 // Force the country to Canada, which has an installed search provider | |
69 // that's HTTP. | |
70 browser()->profile()->GetPrefs()->SetInteger( | |
71 prefs::kCountryIDAtInstall, ('C' << 8) | 'A'); | |
72 } | |
73 | |
74 IsSearchProviderTestData StartIsSearchProviderInstalledTest( | |
75 Browser* browser, | |
76 const char* host, | |
77 const char* expected_result) { | |
78 GURL test_url(std::string("http://") + host + | |
79 search_provider_test_url_.path() + "#" + expected_result); | |
80 ui_test_utils::NavigateToURLWithDisposition( | |
81 browser, test_url, NEW_FOREGROUND_TAB, | |
82 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
83 | |
84 // Bundle up information needed to verify the result. | |
85 content::WebContents* tab = | |
86 browser->tab_strip_model()->GetActiveWebContents(); | |
87 return IsSearchProviderTestData(tab, host, test_url); | |
88 } | |
89 | |
90 void FinishIsSearchProviderInstalledTest( | |
91 const IsSearchProviderTestData& data) { | |
92 base::string16 title = data.tab->GetTitle(); | |
93 if (title.empty()) { | |
94 content::TitleWatcher title_watcher(data.tab, base::ASCIIToUTF16("OK")); | |
95 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | |
96 title = title_watcher.WaitAndGetTitle(); | |
97 } | |
98 EXPECT_EQ(base::ASCIIToUTF16("OK"), title); | |
99 } | |
100 | |
101 GURL search_provider_test_url_; | |
102 | |
103 private: | |
104 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); | |
105 }; | |
106 | |
107 #if defined(OS_WIN) | |
108 // This is flaking on XP. See http://crbug.com/159530 | |
109 #define MAYBE_TestIsSearchProviderInstalled \ | |
110 DISABLED_TestIsSearchProviderInstalled | |
111 #else | |
112 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled | |
113 #endif | |
114 IN_PROC_BROWSER_TEST_F(SearchProviderTest, | |
115 MAYBE_TestIsSearchProviderInstalled) { | |
116 // Use the default search provider, other installed search provider, and | |
117 // one not installed as well. Note, Ask is used here because it's a HTTP host. | |
118 const char* test_hosts[] = { "www.google.com", | |
119 "www.ask.com", | |
120 "localhost" }; | |
121 const char* expected_results[] = { "2", | |
122 "1", | |
123 "0" }; | |
124 static_assert(arraysize(test_hosts) == arraysize(expected_results), | |
125 "each host should have a test result"); | |
126 IsSearchProviderTestData test_data[2 * arraysize(test_hosts)]; | |
127 | |
128 // Start results for the normal mode. | |
129 for (size_t i = 0; i < arraysize(test_hosts); ++i) { | |
130 test_data[i] = StartIsSearchProviderInstalledTest( | |
131 browser(), test_hosts[i], expected_results[i]); | |
132 FinishIsSearchProviderInstalledTest(test_data[i]); | |
133 } | |
134 | |
135 // Start tests for incognito mode (and verify the result is 0). | |
136 Browser* incognito_browser = CreateIncognitoBrowser(); | |
137 for (size_t i = 0; i < arraysize(test_hosts); ++i) { | |
138 test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest( | |
139 incognito_browser, test_hosts[i], "0"); | |
140 FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]); | |
141 } | |
142 | |
143 // The following should be re-enabled. At the moment, there are problems with | |
144 // doing all of these queries in parallel -- see http://crbug.com/60043. | |
145 #if 0 | |
146 // Remove the calls to FinishIsSearchProviderInstalledTest above when | |
147 // re-enabling this code. | |
148 | |
149 // Do the verification. | |
150 for (size_t i = 0; i < arraysize(test_data); ++i) { | |
151 FinishIsSearchProviderInstalledTest(test_data[i]); | |
152 } | |
153 #endif | |
154 } | |
155 | |
156 IN_PROC_BROWSER_TEST_F(SearchProviderTest, | |
157 TestIsSearchProviderInstalledWithException) { | |
158 // Change the url for the test page to one that throws an exception when | |
159 // toString is called on the argument given to isSearchProviderInstalled. | |
160 search_provider_test_url_ = embedded_test_server()->GetURL( | |
161 "/is_search_provider_installed_with_exception.html"); | |
162 | |
163 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest( | |
164 browser(), "www.google.com", "")); | |
165 } | |
OLD | NEW |