external_extension_browsertest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/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"
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) OVERRIDE {
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_NAVIGATION);
62
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);
67  }
68
69  void FinishIsSearchProviderInstalledTest(
70      const IsSearchProviderTestData& data) {
71    string16 title = data.tab->GetTitle();
72    if (title.empty()) {
73      content::TitleWatcher title_watcher(data.tab, ASCIIToUTF16("OK"));
74      title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
75      title = title_watcher.WaitAndGetTitle();
76    }
77    EXPECT_EQ(ASCIIToUTF16("OK"), title);
78  }
79
80  GURL search_provider_test_url_;
81
82 private:
83  DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
84};
85
86#if defined(OS_WIN)
87// This is flaking on XP. See http://crbug.com/159530
88#define MAYBE_TestIsSearchProviderInstalled \
89    DISABLED_TestIsSearchProviderInstalled
90#else
91#define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
92#endif
93IN_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",
100                               "www.bing.com",
101                               "localhost" };
102  const char* expected_results[] = { "2",
103                                     "1",
104                                     "0" };
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)];
108
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]);
114  }
115
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)]);
122  }
123
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.
126#if 0
127  // Remove the calls to FinishIsSearchProviderInstalledTest above when
128  // re-enabling this code.
129
130  // Do the verification.
131  for (size_t i = 0; i < arraysize(test_data); ++i) {
132    FinishIsSearchProviderInstalledTest(test_data[i]);
133  }
134#endif
135}
136
137IN_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");
143
144  FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
145      browser(), "www.google.com", ""));
146}
147