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/search/instant_unittest_base.h"
      6 
      7 #include <string>
      8 
      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/search.h"
     15 #include "chrome/browser/search_engines/template_url.h"
     16 #include "chrome/browser/search_engines/template_url_service.h"
     17 #include "chrome/browser/search_engines/template_url_service_factory.h"
     18 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
     19 #include "chrome/common/pref_names.h"
     20 #include "chrome/test/base/browser_with_test_window_test.h"
     21 #include "chrome/test/base/testing_pref_service_syncable.h"
     22 #include "chrome/test/base/ui_test_utils.h"
     23 #include "components/google/core/browser/google_pref_names.h"
     24 #include "components/google/core/browser/google_url_tracker.h"
     25 #include "components/variations/entropy_provider.h"
     26 
     27 InstantUnitTestBase::InstantUnitTestBase() {
     28   field_trial_list_.reset(new base::FieldTrialList(
     29       new metrics::SHA1EntropyProvider("42")));
     30 }
     31 
     32 InstantUnitTestBase::~InstantUnitTestBase() {
     33 }
     34 
     35 void InstantUnitTestBase::SetUp() {
     36   chrome::EnableQueryExtractionForTesting();
     37   SetUpHelper();
     38 }
     39 
     40 void InstantUnitTestBase::TearDown() {
     41   UIThreadSearchTermsData::SetGoogleBaseURL("");
     42   BrowserWithTestWindowTest::TearDown();
     43 }
     44 
     45 #if !defined(OS_IOS) && !defined(OS_ANDROID)
     46 void InstantUnitTestBase::SetUpWithoutQueryExtraction() {
     47   SetUpHelper();
     48 }
     49 #endif
     50 
     51 void InstantUnitTestBase::SetUserSelectedDefaultSearchProvider(
     52     const std::string& base_url) {
     53   TemplateURLData data;
     54   data.SetKeyword(base::UTF8ToUTF16(base_url));
     55   data.SetURL(base_url + "url?bar={searchTerms}");
     56   data.instant_url = base_url +
     57       "instant?{google:omniboxStartMarginParameter}{google:forceInstantResults}"
     58       "foo=foo#foo=foo&strk";
     59   data.new_tab_url = base_url + "newtab";
     60   data.alternate_urls.push_back(base_url + "alt#quux={searchTerms}");
     61   data.search_terms_replacement_key = "strk";
     62 
     63   TemplateURL* template_url = new TemplateURL(data);
     64   // Takes ownership of |template_url|.
     65   template_url_service_->Add(template_url);
     66   template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
     67 }
     68 
     69 void InstantUnitTestBase::NotifyGoogleBaseURLUpdate(
     70     const std::string& new_google_base_url) {
     71   // GoogleURLTracker is not created in tests.
     72   // (See GoogleURLTrackerFactory::ServiceIsNULLWhileTesting())
     73   // For determining google:baseURL for NTP, the following is used:
     74   // UIThreadSearchTermsData::GoogleBaseURLValue()
     75   // For simulating test behavior, this is overridden below.
     76   UIThreadSearchTermsData::SetGoogleBaseURL(new_google_base_url);
     77   TemplateURLServiceFactory::GetForProfile(profile())->OnGoogleURLUpdated(
     78       GURL("https://www.google.com"), GURL(new_google_base_url));
     79 }
     80 
     81 bool InstantUnitTestBase::IsInstantServiceObserver(
     82     InstantServiceObserver* observer) {
     83   return instant_service_->observers_.HasObserver(observer);
     84 }
     85 
     86 TestingProfile* InstantUnitTestBase::CreateProfile() {
     87   TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile();
     88   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
     89       profile, &TemplateURLServiceFactory::BuildInstanceFor);
     90   return profile;
     91 }
     92 
     93 void InstantUnitTestBase::SetUpHelper() {
     94   BrowserWithTestWindowTest::SetUp();
     95 
     96   template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
     97   ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_);
     98 
     99   UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/");
    100   TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
    101   pref_service->SetUserPref(prefs::kLastPromptedGoogleURL,
    102                             new base::StringValue("https://www.google.com/"));
    103   SetUserSelectedDefaultSearchProvider("{google:baseURL}");
    104   instant_service_ = InstantServiceFactory::GetForProfile(profile());
    105 }
    106