template_url_service_test_util.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/strings/string_split.h"
10#include "base/threading/thread.h"
11#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
12#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
13#include "chrome/browser/webdata/web_data_service_factory.h"
14#include "chrome/test/base/testing_pref_service_syncable.h"
15#include "chrome/test/base/testing_profile.h"
16#include "components/google/core/browser/google_url_tracker.h"
17#include "components/search_engines/default_search_manager.h"
18#include "components/search_engines/default_search_pref_test_util.h"
19#include "components/search_engines/template_url_service.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22#if defined(OS_CHROMEOS)
23#include "chrome/browser/google/google_brand_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  explicit TestingTemplateURLService(Profile* profile)
31      : TemplateURLService(
32            profile->GetPrefs(),
33            scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
34            WebDataServiceFactory::GetKeywordWebDataForProfile(
35                profile, Profile::EXPLICIT_ACCESS),
36            scoped_ptr<TemplateURLServiceClient>(
37                new ChromeTemplateURLServiceClient(profile)), NULL, NULL,
38            base::Closure()) {
39  }
40
41  base::string16 GetAndClearSearchTerm() {
42    base::string16 search_term;
43    search_term.swap(search_term_);
44    return search_term;
45  }
46
47 protected:
48  virtual void SetKeywordSearchTermsForURL(
49      const TemplateURL* t_url,
50      const GURL& url,
51      const base::string16& term) OVERRIDE {
52    search_term_ = term;
53  }
54
55 private:
56  base::string16 search_term_;
57
58  DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
59};
60
61
62TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
63    : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
64      changed_count_(0) {
65  // Make unique temp directory.
66  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
67  profile_.reset(new TestingProfile(temp_dir_.path()));
68
69  profile()->CreateWebDataService();
70
71  model_.reset(new TestingTemplateURLService(profile()));
72  model_->AddObserver(this);
73
74#if defined(OS_CHROMEOS)
75  google_brand::chromeos::ClearBrandForCurrentSession();
76#endif
77}
78
79TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
80  ClearModel();
81  profile_.reset();
82
83  UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
84
85  // Flush the message loop to make application verifiers happy.
86  base::RunLoop().RunUntilIdle();
87}
88
89void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
90  changed_count_++;
91}
92
93int TemplateURLServiceTestUtil::GetObserverCount() {
94  return changed_count_;
95}
96
97void TemplateURLServiceTestUtil::ResetObserverCount() {
98  changed_count_ = 0;
99}
100
101void TemplateURLServiceTestUtil::VerifyLoad() {
102  ASSERT_FALSE(model()->loaded());
103  model()->Load();
104  base::RunLoop().RunUntilIdle();
105  EXPECT_EQ(1, GetObserverCount());
106  ResetObserverCount();
107}
108
109void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
110  model()->ChangeToLoadedState();
111  // Initialize the web data service so that the database gets updated with
112  // any changes made.
113
114  model()->web_data_service_ =
115      WebDataServiceFactory::GetKeywordWebDataForProfile(
116          profile(), Profile::EXPLICIT_ACCESS);
117  base::RunLoop().RunUntilIdle();
118}
119
120void TemplateURLServiceTestUtil::ClearModel() {
121  model_->Shutdown();
122  model_.reset();
123}
124
125void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
126  if (model_)
127    ClearModel();
128  model_.reset(new TestingTemplateURLService(profile()));
129  model()->AddObserver(this);
130  changed_count_ = 0;
131  if (verify_load)
132    VerifyLoad();
133}
134
135base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
136  return model_->GetAndClearSearchTerm();
137}
138
139void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
140  DCHECK(base_url.is_valid());
141  UIThreadSearchTermsData data(profile());
142  UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
143  model_->GoogleBaseURLChanged();
144}
145
146void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
147    bool enabled,
148    const std::string& name,
149    const std::string& keyword,
150    const std::string& search_url,
151    const std::string& suggest_url,
152    const std::string& icon_url,
153    const std::string& encodings,
154    const std::string& alternate_url,
155    const std::string& search_terms_replacement_key) {
156  DefaultSearchPrefTestUtil::SetManagedPref(
157      profile()->GetTestingPrefService(),
158      enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
159      alternate_url, search_terms_replacement_key);
160}
161
162void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
163  DefaultSearchPrefTestUtil::RemoveManagedPref(
164      profile()->GetTestingPrefService());
165}
166
167TemplateURLService* TemplateURLServiceTestUtil::model() {
168  return model_.get();
169}
170