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/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
20 struct IsSearchProviderTestData
{
21 IsSearchProviderTestData() : tab(NULL
) {}
22 IsSearchProviderTestData(content::WebContents
* t
, std::string h
, GURL url
)
23 : tab(t
), host(h
), test_url(url
) {
26 content::WebContents
* tab
;
33 class SearchProviderTest
: public InProcessBrowserTest
{
35 SearchProviderTest() {}
37 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
38 ASSERT_TRUE(test_server()->Start());
40 // Map all hosts to our local server.
41 std::string
host_rule(
42 "MAP * " + test_server()->host_port_pair().ToString());
43 command_line
->AppendSwitchASCII(switches::kHostRules
, host_rule
);
44 // Use no proxy or otherwise this test will fail on a machine that has a
46 command_line
->AppendSwitch(switches::kNoProxyServer
);
48 // Get the url for the test page.
49 search_provider_test_url_
=
50 test_server()->GetURL("files/is_search_provider_installed.html");
53 IsSearchProviderTestData
StartIsSearchProviderInstalledTest(
56 const char* expected_result
) {
57 GURL
test_url(std::string("http://") + host
+
58 search_provider_test_url_
.path() + "#" + expected_result
);
59 ui_test_utils::NavigateToURLWithDisposition(
60 browser
, test_url
, NEW_FOREGROUND_TAB
,
61 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
63 // Bundle up information needed to verify the result.
64 content::WebContents
* tab
=
65 browser
->tab_strip_model()->GetActiveWebContents();
66 return IsSearchProviderTestData(tab
, host
, test_url
);
69 void FinishIsSearchProviderInstalledTest(
70 const IsSearchProviderTestData
& data
) {
71 base::string16 title
= data
.tab
->GetTitle();
73 content::TitleWatcher
title_watcher(data
.tab
, base::ASCIIToUTF16("OK"));
74 title_watcher
.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
75 title
= title_watcher
.WaitAndGetTitle();
77 EXPECT_EQ(base::ASCIIToUTF16("OK"), title
);
80 GURL search_provider_test_url_
;
83 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest
);
87 // This is flaking on XP. See http://crbug.com/159530
88 #define MAYBE_TestIsSearchProviderInstalled \
89 DISABLED_TestIsSearchProviderInstalled
91 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
93 IN_PROC_BROWSER_TEST_F(SearchProviderTest
,
94 MAYBE_TestIsSearchProviderInstalled
) {
95 // Use the default search provider, other installed search provider, and
96 // one not installed as well. (Note that yahoo isn't tested because the
97 // its host name varies a lot for different locales unlike Google and Bing,
98 // which would make the test fail depending on the machine's locale.)
99 const char* test_hosts
[] = { "www.google.com",
102 const char* expected_results
[] = { "2",
105 COMPILE_ASSERT(arraysize(test_hosts
) == arraysize(expected_results
),
106 there_should_be_a_result_for_each_host
);
107 IsSearchProviderTestData test_data
[2 * arraysize(test_hosts
)];
109 // Start results for the normal mode.
110 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
111 test_data
[i
] = StartIsSearchProviderInstalledTest(
112 browser(), test_hosts
[i
], expected_results
[i
]);
113 FinishIsSearchProviderInstalledTest(test_data
[i
]);
116 // Start tests for incognito mode (and verify the result is 0).
117 Browser
* incognito_browser
= CreateIncognitoBrowser();
118 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
119 test_data
[i
+ arraysize(test_hosts
)] = StartIsSearchProviderInstalledTest(
120 incognito_browser
, test_hosts
[i
], "0");
121 FinishIsSearchProviderInstalledTest(test_data
[i
+ arraysize(test_hosts
)]);
124 // The following should be re-enabled. At the moment, there are problems with
125 // doing all of these queries in parallel -- see http://crbug.com/60043.
127 // Remove the calls to FinishIsSearchProviderInstalledTest above when
128 // re-enabling this code.
130 // Do the verification.
131 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
132 FinishIsSearchProviderInstalledTest(test_data
[i
]);
137 IN_PROC_BROWSER_TEST_F(SearchProviderTest
,
138 TestIsSearchProviderInstalledWithException
) {
139 // Change the url for the test page to one that throws an exception when
140 // toString is called on the argument given to isSearchProviderInstalled.
141 search_provider_test_url_
= test_server()->GetURL(
142 "files/is_search_provider_installed_with_exception.html");
144 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
145 browser(), "www.google.com", ""));