Home | History | Annotate | Download | only in search
      1 // Copyright 2013 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 "chrome/browser/ui/search/instant_test_utils.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/search/instant_service.h"
     13 #include "chrome/browser/search/instant_service_factory.h"
     14 #include "chrome/browser/search_engines/template_url_service.h"
     15 #include "chrome/browser/search_engines/template_url_service_factory.h"
     16 #include "chrome/browser/ui/omnibox/omnibox_view.h"
     17 #include "chrome/browser/ui/search/instant_ntp.h"
     18 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
     19 #include "chrome/common/chrome_switches.h"
     20 #include "chrome/common/pref_names.h"
     21 #include "chrome/test/base/interactive_test_utils.h"
     22 #include "chrome/test/base/ui_test_utils.h"
     23 #include "content/public/browser/notification_service.h"
     24 #include "content/public/browser/render_process_host.h"
     25 #include "content/public/browser/web_contents.h"
     26 #include "content/public/common/result_codes.h"
     27 #include "content/public/test/browser_test_utils.h"
     28 
     29 namespace {
     30 
     31 std::string WrapScript(const std::string& script) {
     32   return "domAutomationController.send(" + script + ")";
     33 }
     34 
     35 }  // namespace
     36 
     37 // InstantTestBase -----------------------------------------------------------
     38 
     39 void InstantTestBase::SetupInstant(Browser* browser) {
     40   browser_ = browser;
     41   TemplateURLService* service =
     42       TemplateURLServiceFactory::GetForProfile(browser_->profile());
     43   ui_test_utils::WaitForTemplateURLServiceToLoad(service);
     44 
     45   TemplateURLData data;
     46   // Necessary to use exact URL for both the main URL and the alternate URL for
     47   // search term extraction to work in InstantExtended.
     48   data.SetURL(instant_url_.spec() +
     49               "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
     50   data.instant_url = instant_url_.spec();
     51   data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
     52   data.search_terms_replacement_key = "strk";
     53 
     54   TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
     55   service->Add(template_url);  // Takes ownership of |template_url|.
     56   service->SetDefaultSearchProvider(template_url);
     57 
     58   InstantService* instant_service =
     59       InstantServiceFactory::GetForProfile(browser_->profile());
     60   ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
     61   instant_service->ntp_prerenderer()->ReloadStaleNTP();
     62 }
     63 
     64 void InstantTestBase::SetInstantURL(const std::string& url) {
     65   TemplateURLService* service =
     66       TemplateURLServiceFactory::GetForProfile(browser_->profile());
     67   ui_test_utils::WaitForTemplateURLServiceToLoad(service);
     68 
     69   TemplateURLData data;
     70   data.SetURL(url);
     71   data.instant_url = url;
     72 
     73   TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
     74   service->Add(template_url);  // Takes ownership of |template_url|.
     75   service->SetDefaultSearchProvider(template_url);
     76 }
     77 
     78 void InstantTestBase::Init(const GURL& instant_url) {
     79   instant_url_ = instant_url;
     80 }
     81 
     82 void InstantTestBase::FocusOmnibox() {
     83   // If the omnibox already has focus, just notify Instant.
     84   if (omnibox()->model()->has_focus()) {
     85     instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE,
     86                                    OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL);
     87   } else {
     88     browser_->window()->GetLocationBar()->FocusLocation(false);
     89   }
     90 }
     91 
     92 void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() {
     93   content::WindowedNotificationObserver ntp_observer(
     94       chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
     95       content::NotificationService::AllSources());
     96   FocusOmnibox();
     97 
     98   InstantService* instant_service =
     99       InstantServiceFactory::GetForProfile(browser_->profile());
    100   ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
    101   if (!instant_service->ntp_prerenderer()->ntp() ||
    102       !instant_service->ntp_prerenderer()->ntp()->supports_instant())
    103     ntp_observer.Wait();
    104 }
    105 
    106 void InstantTestBase::SetOmniboxText(const std::string& text) {
    107   FocusOmnibox();
    108   omnibox()->SetUserText(UTF8ToUTF16(text));
    109 }
    110 
    111 void InstantTestBase::PressEnterAndWaitForNavigation() {
    112   content::WindowedNotificationObserver nav_observer(
    113       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
    114       content::NotificationService::AllSources());
    115   browser_->window()->GetLocationBar()->AcceptInput();
    116   nav_observer.Wait();
    117 }
    118 
    119 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
    120                                     const std::string& script,
    121                                     bool* result) {
    122   return content::ExecuteScriptAndExtractBool(
    123       contents, WrapScript(script), result);
    124 }
    125 
    126 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
    127                                    const std::string& script,
    128                                    int* result) {
    129   return content::ExecuteScriptAndExtractInt(
    130       contents, WrapScript(script), result);
    131 }
    132 
    133 bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
    134                                       const std::string& script,
    135                                       std::string* result) {
    136   return content::ExecuteScriptAndExtractString(
    137       contents, WrapScript(script), result);
    138 }
    139 
    140 bool InstantTestBase::ExecuteScript(const std::string& script) {
    141   InstantService* instant_service =
    142       InstantServiceFactory::GetForProfile(browser_instant()->profile());
    143   if (!instant_service)
    144     return false;
    145   return content::ExecuteScript(instant_service->GetNTPContents(), script);
    146 }
    147 
    148 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
    149                                         bool expected) {
    150   bool actual = !expected;  // Purposely start with a mis-match.
    151   // We can only use ASSERT_*() in a method that returns void, hence this
    152   // convoluted check.
    153   return GetBoolFromJS(contents, "!document.webkitHidden", &actual) &&
    154       actual == expected;
    155 }
    156 
    157 std::string InstantTestBase::GetOmniboxText() {
    158   return UTF16ToUTF8(omnibox()->GetText());
    159 }
    160 
    161 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
    162                                 const std::string& image,
    163                                 bool* loaded) {
    164   std::string js_chrome =
    165       "var img = document.createElement('img');"
    166       "img.onerror = function() { domAutomationController.send(false); };"
    167       "img.onload  = function() { domAutomationController.send(true); };"
    168       "img.src = '" + image + "';";
    169   return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
    170 }
    171 
    172 string16 InstantTestBase::GetBlueText() {
    173   size_t start = 0, end = 0;
    174   omnibox()->GetSelectionBounds(&start, &end);
    175   if (start > end)
    176     std::swap(start, end);
    177   return omnibox()->GetText().substr(start, end - start);
    178 }
    179