template_url_service_test_util.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2012 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_engines/template_url_service_test_util.h"
6
7#include "base/bind.h"
8#include "base/run_loop.h"
9#include "base/threading/thread.h"
10#include "chrome/browser/chrome_notification_types.h"
11#include "chrome/browser/google/google_url_tracker.h"
12#include "chrome/browser/search_engines/search_terms_data.h"
13#include "chrome/browser/search_engines/template_url_service.h"
14#include "chrome/browser/search_engines/template_url_service_factory.h"
15#include "chrome/browser/webdata/web_data_service_factory.h"
16#include "chrome/common/pref_names.h"
17#include "chrome/test/base/testing_pref_service_syncable.h"
18#include "chrome/test/base/testing_profile.h"
19#include "content/public/browser/notification_service.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22#if defined(OS_CHROMEOS)
23#include "chrome/browser/google/google_util_chromeos.h"
24#endif
25
26// Trivial subclass of TemplateURLService that records the last invocation of
27// SetKeywordSearchTermsForURL.
28class TestingTemplateURLService : public TemplateURLService {
29 public:
30  static BrowserContextKeyedService* Build(content::BrowserContext* profile) {
31    return new TestingTemplateURLService(static_cast<Profile*>(profile));
32  }
33
34  explicit TestingTemplateURLService(Profile* profile)
35      : TemplateURLService(profile) {
36  }
37
38  base::string16 GetAndClearSearchTerm() {
39    base::string16 search_term;
40    search_term.swap(search_term_);
41    return search_term;
42  }
43
44 protected:
45  virtual void SetKeywordSearchTermsForURL(
46      const TemplateURL* t_url,
47      const GURL& url,
48      const base::string16& term) OVERRIDE {
49    search_term_ = term;
50  }
51
52 private:
53  base::string16 search_term_;
54
55  DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
56};
57
58// TemplateURLServiceTestUtilBase ---------------------------------------------
59
60TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase()
61    : changed_count_(0) {
62}
63
64TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() {
65}
66
67void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() {
68  profile()->CreateWebDataService();
69
70  TemplateURLService* service = static_cast<TemplateURLService*>(
71      TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
72          profile(), TestingTemplateURLService::Build));
73  service->AddObserver(this);
74}
75
76void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() {
77  changed_count_++;
78}
79
80int TemplateURLServiceTestUtilBase::GetObserverCount() {
81  return changed_count_;
82}
83
84void TemplateURLServiceTestUtilBase::ResetObserverCount() {
85  changed_count_ = 0;
86}
87
88void TemplateURLServiceTestUtilBase::VerifyLoad() {
89  ASSERT_FALSE(model()->loaded());
90  model()->Load();
91  base::RunLoop().RunUntilIdle();
92  EXPECT_EQ(1, GetObserverCount());
93  ResetObserverCount();
94}
95
96void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() {
97  model()->ChangeToLoadedState();
98  // Initialize the web data service so that the database gets updated with
99  // any changes made.
100
101  model()->service_ = WebDataService::FromBrowserContext(profile());
102  base::RunLoop().RunUntilIdle();
103}
104
105void TemplateURLServiceTestUtilBase::ClearModel() {
106  TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
107      profile(), NULL);
108}
109
110void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) {
111  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
112      profile(), TestingTemplateURLService::Build);
113  model()->AddObserver(this);
114  changed_count_ = 0;
115  if (verify_load)
116    VerifyLoad();
117}
118
119string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() {
120  return
121      static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
122}
123
124void TemplateURLServiceTestUtilBase::SetGoogleBaseURL(
125    const GURL& base_url) const {
126  DCHECK(base_url.is_valid());
127  UIThreadSearchTermsData data(profile());
128  GoogleURLTracker::UpdatedDetails urls(GURL(data.GoogleBaseURLValue()),
129                                        base_url);
130  UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
131  content::NotificationService::current()->Notify(
132      chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
133      content::Source<Profile>(profile()),
134      content::Details<GoogleURLTracker::UpdatedDetails>(&urls));
135}
136
137void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences(
138    bool enabled,
139    const std::string& name,
140    const std::string& keyword,
141    const std::string& search_url,
142    const std::string& suggest_url,
143    const std::string& icon_url,
144    const std::string& encodings,
145    const std::string& alternate_url,
146    const std::string& search_terms_replacement_key) {
147  TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
148  pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
149                               Value::CreateBooleanValue(enabled));
150  pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
151                               Value::CreateStringValue(name));
152  pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
153                               Value::CreateStringValue(keyword));
154  pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
155                               Value::CreateStringValue(search_url));
156  pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
157                               Value::CreateStringValue(suggest_url));
158  pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
159                               Value::CreateStringValue(icon_url));
160  pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
161                               Value::CreateStringValue(encodings));
162  scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue());
163  if (!alternate_url.empty())
164    alternate_url_list->Append(Value::CreateStringValue(alternate_url));
165  pref_service->SetManagedPref(prefs::kDefaultSearchProviderAlternateURLs,
166                               alternate_url_list.release());
167  pref_service->SetManagedPref(
168      prefs::kDefaultSearchProviderSearchTermsReplacementKey,
169      Value::CreateStringValue(search_terms_replacement_key));
170  model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
171                   content::NotificationService::AllSources(),
172                   content::NotificationService::NoDetails());
173}
174
175void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() {
176  TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService();
177  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
178  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
179  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
180  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
181  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
182  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
183  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
184  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderAlternateURLs);
185  pref_service->RemoveManagedPref(
186      prefs::kDefaultSearchProviderSearchTermsReplacementKey);
187  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
188  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
189  model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED,
190                   content::NotificationService::AllSources(),
191                   content::NotificationService::NoDetails());
192}
193
194TemplateURLService* TemplateURLServiceTestUtilBase::model() const {
195  return TemplateURLServiceFactory::GetForProfile(profile());
196}
197
198
199// TemplateURLServiceTestUtil -------------------------------------------------
200
201TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
202    : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
203}
204
205TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
206}
207
208void TemplateURLServiceTestUtil::SetUp() {
209  // Make unique temp directory.
210  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
211  profile_.reset(new TestingProfile(temp_dir_.path()));
212
213  TemplateURLServiceTestUtilBase::CreateTemplateUrlService();
214
215#if defined(OS_CHROMEOS)
216  google_util::chromeos::ClearBrandForCurrentSession();
217#endif
218}
219
220void TemplateURLServiceTestUtil::TearDown() {
221  profile_.reset();
222
223  UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
224
225  // Flush the message loop to make application verifiers happy.
226  base::RunLoop().RunUntilIdle();
227}
228
229TestingProfile* TemplateURLServiceTestUtil::profile() const {
230  return profile_.get();
231}
232