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_service_factory.h" 16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" 17 #include "chrome/common/pref_names.h" 18 #include "chrome/test/base/browser_with_test_window_test.h" 19 #include "chrome/test/base/testing_pref_service_syncable.h" 20 #include "chrome/test/base/ui_test_utils.h" 21 #include "components/google/core/browser/google_pref_names.h" 22 #include "components/google/core/browser/google_url_tracker.h" 23 #include "components/search_engines/template_url.h" 24 #include "components/search_engines/template_url_service.h" 25 #include "components/variations/entropy_provider.h" 26 InstantUnitTestBase()27InstantUnitTestBase::InstantUnitTestBase() { 28 field_trial_list_.reset(new base::FieldTrialList( 29 new metrics::SHA1EntropyProvider("42"))); 30 } 31 ~InstantUnitTestBase()32InstantUnitTestBase::~InstantUnitTestBase() { 33 } 34 SetUp()35void InstantUnitTestBase::SetUp() { 36 chrome::EnableQueryExtractionForTesting(); 37 SetUpHelper(); 38 } 39 TearDown()40void InstantUnitTestBase::TearDown() { 41 UIThreadSearchTermsData::SetGoogleBaseURL(""); 42 BrowserWithTestWindowTest::TearDown(); 43 } 44 45 #if !defined(OS_IOS) && !defined(OS_ANDROID) SetUpWithoutQueryExtraction()46void InstantUnitTestBase::SetUpWithoutQueryExtraction() { 47 SetUpHelper(); 48 } 49 #endif 50 SetUserSelectedDefaultSearchProvider(const std::string & base_url)51void 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 NotifyGoogleBaseURLUpdate(const std::string & new_google_base_url)69void 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())->GoogleBaseURLChanged(); 78 } 79 IsInstantServiceObserver(InstantServiceObserver * observer)80bool InstantUnitTestBase::IsInstantServiceObserver( 81 InstantServiceObserver* observer) { 82 return instant_service_->observers_.HasObserver(observer); 83 } 84 CreateProfile()85TestingProfile* InstantUnitTestBase::CreateProfile() { 86 TestingProfile* profile = BrowserWithTestWindowTest::CreateProfile(); 87 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 88 profile, &TemplateURLServiceFactory::BuildInstanceFor); 89 return profile; 90 } 91 SetUpHelper()92void InstantUnitTestBase::SetUpHelper() { 93 BrowserWithTestWindowTest::SetUp(); 94 95 template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile()); 96 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_); 97 98 UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/"); 99 TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); 100 pref_service->SetUserPref(prefs::kLastPromptedGoogleURL, 101 new base::StringValue("https://www.google.com/")); 102 SetUserSelectedDefaultSearchProvider("{google:baseURL}"); 103 instant_service_ = InstantServiceFactory::GetForProfile(profile()); 104 } 105