external_extension_browsertest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/command_line.h"
6#include "base/string_util.h"
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_tabstrip.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/test_server.h"
17
18namespace {
19
20struct IsSearchProviderTestData {
21  IsSearchProviderTestData() : tab(NULL) {}
22  IsSearchProviderTestData(content::WebContents* t, std::string h, GURL url)
23      : tab(t), host(h), test_url(url) {
24  }
25
26  content::WebContents* tab;
27  std::string host;
28  GURL test_url;
29};
30
31}
32
33class SearchProviderTest : public InProcessBrowserTest {
34 protected:
35  SearchProviderTest() {}
36
37  virtual void SetUpCommandLine(CommandLine* command_line) {
38    ASSERT_TRUE(test_server()->Start());
39
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
45    // proxy configured.
46    command_line->AppendSwitch(switches::kNoProxyServer);
47
48    // Get the url for the test page.
49    search_provider_test_url_ =
50        test_server()->GetURL("files/is_search_provider_installed.html");
51  }
52
53  IsSearchProviderTestData StartIsSearchProviderInstalledTest(
54      Browser* browser,
55      const char* host,
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_TAB);
62
63    // Bundle up information needed to verify the result.
64    content::WebContents* tab = chrome::GetActiveWebContents(browser);
65    return IsSearchProviderTestData(tab, host, test_url);
66  }
67
68  void FinishIsSearchProviderInstalledTest(
69      const IsSearchProviderTestData& data) {
70    string16 title = data.tab->GetTitle();
71    if (title.empty()) {
72      content::TitleWatcher title_watcher(data.tab, ASCIIToUTF16("OK"));
73      title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
74      title = title_watcher.WaitAndGetTitle();
75    }
76    EXPECT_EQ(ASCIIToUTF16("OK"), title);
77  }
78
79  GURL search_provider_test_url_;
80
81 private:
82  DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
83};
84
85#if defined(OS_WIN)
86// This is flaking on XP. See http://crbug.com/159530
87#define MAYBE_TestIsSearchProviderInstalled \
88    DISABLED_TestIsSearchProviderInstalled
89#else
90#define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
91#endif
92IN_PROC_BROWSER_TEST_F(SearchProviderTest,
93                       MAYBE_TestIsSearchProviderInstalled) {
94  // Use the default search provider, other installed search provider, and
95  // one not installed as well. (Note that yahoo isn't tested because the
96  // its host name varies a lot for different locales unlike Google and Bing,
97  // which would make the test fail depending on the machine's locale.)
98  const char* test_hosts[] = { "www.google.com",
99                               "www.bing.com",
100                               "localhost" };
101  const char* expected_results[] = { "2",
102                                     "1",
103                                     "0" };
104  COMPILE_ASSERT(arraysize(test_hosts) == arraysize(expected_results),
105                 there_should_be_a_result_for_each_host);
106  IsSearchProviderTestData test_data[2 * arraysize(test_hosts)];
107
108  // Start results for the normal mode.
109  for (size_t i = 0; i < arraysize(test_hosts); ++i) {
110    test_data[i] = StartIsSearchProviderInstalledTest(
111        browser(), test_hosts[i], expected_results[i]);
112    FinishIsSearchProviderInstalledTest(test_data[i]);
113  }
114
115  // Start tests for incognito mode (and verify the result is 0).
116  Browser* incognito_browser = CreateIncognitoBrowser();
117  for (size_t i = 0; i < arraysize(test_hosts); ++i) {
118    test_data[i + arraysize(test_hosts)] = StartIsSearchProviderInstalledTest(
119        incognito_browser, test_hosts[i], "0");
120    FinishIsSearchProviderInstalledTest(test_data[i + arraysize(test_hosts)]);
121  }
122
123  // The following should be re-enabled. At the moment, there are problems with
124  // doing all of these queries in parallel -- see http://crbug.com/60043.
125#if 0
126  // Remove the calls to FinishIsSearchProviderInstalledTest above when
127  // re-enabling this code.
128
129  // Do the verification.
130  for (size_t i = 0; i < arraysize(test_data); ++i) {
131    FinishIsSearchProviderInstalledTest(test_data[i]);
132  }
133#endif
134}
135
136IN_PROC_BROWSER_TEST_F(SearchProviderTest,
137                       TestIsSearchProviderInstalledWithException) {
138  // Change the url for the test page to one that throws an exception when
139  // toString is called on the argument given to isSearchProviderInstalled.
140  search_provider_test_url_ = test_server()->GetURL(
141      "files/is_search_provider_installed_with_exception.html");
142
143  FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
144      browser(), "www.google.com", ""));
145}
146