Home | History | Annotate | Download | only in browser
      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 
     18 namespace {
     19 
     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) {
     24   }
     25 
     26   content::WebContents* tab;
     27   std::string host;
     28   GURL test_url;
     29 };
     30 
     31 }
     32 
     33 class 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     base::string16 title = data.tab->GetTitle();
     72     if (title.empty()) {
     73       content::TitleWatcher title_watcher(data.tab, base::ASCIIToUTF16("OK"));
     74       title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
     75       title = title_watcher.WaitAndGetTitle();
     76     }
     77     EXPECT_EQ(base::ASCIIToUTF16("OK"), title);
     78   }
     79 
     80   GURL search_provider_test_url_;
     81 
     82  private:
     83   DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
     84 };
     85 
     86 #if 1
     87 // Disabled - http://crbug.com/359727 (js has syntax errors which v8 hates)
     88 #define MAYBE_TestIsSearchProviderInstalled \
     89     DISABLED_TestIsSearchProviderInstalled
     90 #elif defined(OS_WIN)
     91 // This is flaking on XP. See http://crbug.com/159530
     92 #define MAYBE_TestIsSearchProviderInstalled \
     93     DISABLED_TestIsSearchProviderInstalled
     94 #else
     95 #define MAYBE_TestIsSearchProviderInstalled TestIsSearchProviderInstalled
     96 #endif
     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",
    104                                "www.bing.com",
    105                                "localhost" };
    106   const char* expected_results[] = { "2",
    107                                      "1",
    108                                      "0" };
    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)];
    112 
    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]);
    118   }
    119 
    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)]);
    126   }
    127 
    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.
    130 #if 0
    131   // Remove the calls to FinishIsSearchProviderInstalledTest above when
    132   // re-enabling this code.
    133 
    134   // Do the verification.
    135   for (size_t i = 0; i < arraysize(test_data); ++i) {
    136     FinishIsSearchProviderInstalledTest(test_data[i]);
    137   }
    138 #endif
    139 }
    140 
    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");
    147 
    148   FinishIsSearchProviderInstalledTest(StartIsSearchProviderInstalledTest(
    149       browser(), "www.google.com", ""));
    150 }
    151