• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
7 
8 #include "base/basictypes.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/google/google_url_tracker.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/search_terms_data.h"
16 #include "chrome/browser/search_engines/template_url.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.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/variations/entropy_provider.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 
InstantUnitTestBase()28 InstantUnitTestBase::InstantUnitTestBase() {
29   field_trial_list_.reset(new base::FieldTrialList(
30       new metrics::SHA1EntropyProvider("42")));
31 }
32 
~InstantUnitTestBase()33 InstantUnitTestBase::~InstantUnitTestBase() {
34 }
35 
SetUp()36 void InstantUnitTestBase::SetUp() {
37   SetUpHelper();
38 }
39 
SetUpWithoutCacheableNTP()40 void InstantUnitTestBase::SetUpWithoutCacheableNTP() {
41   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
42       "InstantExtended", "Group1 use_cacheable_ntp:0"));
43   SetUpHelper();
44 }
45 
SetUpHelper()46 void InstantUnitTestBase::SetUpHelper() {
47   chrome::EnableQueryExtractionForTesting();
48   BrowserWithTestWindowTest::SetUp();
49 
50   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
51       profile(), &TemplateURLServiceFactory::BuildInstanceFor);
52   template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
53   ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service_);
54 
55   UIThreadSearchTermsData::SetGoogleBaseURL("https://www.google.com/");
56   TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
57   pref_service->SetUserPref(prefs::kLastPromptedGoogleURL,
58                             new base::StringValue("https://www.google.com/"));
59   SetDefaultSearchProvider("{google:baseURL}");
60   instant_service_ = InstantServiceFactory::GetForProfile(profile());
61 }
62 
TearDown()63 void InstantUnitTestBase::TearDown() {
64   UIThreadSearchTermsData::SetGoogleBaseURL("");
65   BrowserWithTestWindowTest::TearDown();
66 }
67 
SetDefaultSearchProvider(const std::string & base_url)68 void InstantUnitTestBase::SetDefaultSearchProvider(
69     const std::string& base_url) {
70   TemplateURLData data;
71   data.SetURL(base_url + "url?bar={searchTerms}");
72   data.instant_url = base_url +
73       "instant?{google:omniboxStartMarginParameter}{google:forceInstantResults}"
74       "foo=foo#foo=foo&strk";
75   data.new_tab_url = base_url + "newtab";
76   data.alternate_urls.push_back(base_url + "alt#quux={searchTerms}");
77   data.search_terms_replacement_key = "strk";
78 
79   TemplateURL* template_url = new TemplateURL(profile(), data);
80   // Takes ownership of |template_url|.
81   template_url_service_->Add(template_url);
82   template_url_service_->SetDefaultSearchProvider(template_url);
83 }
84 
NotifyGoogleBaseURLUpdate(const std::string & new_google_base_url)85 void InstantUnitTestBase::NotifyGoogleBaseURLUpdate(
86     const std::string& new_google_base_url) {
87   // GoogleURLTracker is not created in tests.
88   // (See GoogleURLTrackerFactory::ServiceIsNULLWhileTesting())
89   // For determining google:baseURL for NTP, the following is used:
90   // UIThreadSearchTermsData::GoogleBaseURLValue()
91   // For simulating test behavior, this is overridden below.
92   UIThreadSearchTermsData::SetGoogleBaseURL(new_google_base_url);
93   GoogleURLTracker::UpdatedDetails details(GURL("https://www.google.com/"),
94                                            GURL(new_google_base_url));
95   content::NotificationService::current()->Notify(
96       chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
97       content::Source<Profile>(profile()->GetOriginalProfile()),
98       content::Details<GoogleURLTracker::UpdatedDetails>(&details));
99 }
100 
101 
IsInstantServiceObserver(InstantServiceObserver * observer)102 bool InstantUnitTestBase::IsInstantServiceObserver(
103     InstantServiceObserver* observer) {
104   return instant_service_->observers_.HasObserver(observer);
105 }
106 
107