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