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