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 // Disabled - http://crbug.com/359727 (js has syntax errors which v8 hates)
88 #define MAYBE_TestIsSearchProviderInstalled \
89 DISABLED_TestIsSearchProviderInstalled
91 // This is flaking on XP. See http://crbug.com/159530
92 #define MAYBE_TestIsSearchProviderInstalled \
93 DISABLED_TestIsSearchProviderInstalled
95 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
97 IN_PROC_BROWSER_TEST_F(SearchProviderTest
,
98 MAYBE_TestIsSearchProviderInstalled
) {
99 // Use the default search provider, other installed search provider, and
100 // one not installed as well. (Note that yahoo isn't tested because the
101 // its host name varies a lot for different locales unlike Google and Bing,
102 // which would make the test fail depending on the machine's locale.)
103 const char* test_hosts
[] = { "www.google.com",
106 const char* expected_results
[] = { "2",
109 COMPILE_ASSERT(arraysize(test_hosts
) == arraysize(expected_results
),
110 there_should_be_a_result_for_each_host
);
111 IsSearchProviderTestData test_data
[2 * arraysize(test_hosts
)];
113 // Start results for the normal mode.
114 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
115 test_data
[i
] = StartIsSearchProviderInstalledTest(
116 browser(), test_hosts
[i
], expected_results
[i
]);
117 FinishIsSearchProviderInstalledTest(test_data
[i
]);
120 // Start tests for incognito mode (and verify the result is 0).
121 Browser
* incognito_browser
= CreateIncognitoBrowser();
122 for (size_t i
= 0; i
< arraysize(test_hosts
); ++i
) {
123 test_data
[i
+ arraysize(test_hosts
)] = StartIsSearchProviderInstalledTest(
124 incognito_browser
, test_hosts
[i
], "0");
125 FinishIsSearchProviderInstalledTest(test_data
[i
+ arraysize(test_hosts
)]);
128 // The following should be re-enabled. At the moment, there are problems with
129 // doing all of these queries in parallel -- see http://crbug.com/60043.
131 // Remove the calls to FinishIsSearchProviderInstalledTest above when
132 // re-enabling this code.
134 // Do the verification.
135 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
136 FinishIsSearchProviderInstalledTest(test_data
[i
]);
141 IN_PROC_BROWSER_TEST_F(SearchProviderTest
,
142 TestIsSearchProviderInstalledWithException
) {
143 // Change the url for the test page to one that throws an exception when
144 // toString is called on the argument given to isSearchProviderInstalled.
145 search_provider_test_url_
= test_server()->GetURL(
146 "files/is_search_provider_installed_with_exception.html");
148 FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
149 browser(), "www.google.com", ""));