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