Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / external_extension_browsertest.cc
blob8b05ec5c593973426b72823127fadebebe7ef391
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"
22 namespace {
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;
31 std::string host;
32 GURL test_url;
35 } // namespace
37 class SearchProviderTest : public InProcessBrowserTest {
38 protected:
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
49 // proxy configured.
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
64 // that's HTTP.
65 browser()->profile()->GetPrefs()->SetInteger(
66 prefs::kCountryIDAtInstall, ('C' << 8) | 'A');
69 IsSearchProviderTestData StartIsSearchProviderInstalledTest(
70 Browser* browser,
71 const char* host,
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();
88 if (title.empty()) {
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_;
98 private:
99 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
102 #if defined(OS_WIN)
103 // This is flaking on XP. See http://crbug.com/159530
104 #define MAYBE_TestIsSearchProviderInstalled \
105 DISABLED_TestIsSearchProviderInstalled
106 #else
107 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
108 #endif
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",
114 "www.ask.com",
115 "localhost" };
116 const char* expected_results[] = { "2",
117 "1",
118 "0" };
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.
140 #if 0
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]);
148 #endif
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", ""));