Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / search_engines / template_url_service_test_util.cc
blobadc01399a640a07dfd986d089920a92531a2ec60
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.
5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
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/webdata/web_data_service_factory.h"
13 #include "chrome/test/base/testing_pref_service_syncable.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/google/core/browser/google_url_tracker.h"
16 #include "components/search_engines/default_search_manager.h"
17 #include "components/search_engines/default_search_pref_test_util.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "components/search_engines/testing_search_terms_data.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 // Trivial subclass of TemplateURLService that records the last invocation of
23 // SetKeywordSearchTermsForURL.
24 class TestingTemplateURLService : public TemplateURLService {
25 public:
26 TestingTemplateURLService(Profile* profile,
27 scoped_ptr<SearchTermsData> search_terms_data)
28 : TemplateURLService(
29 profile->GetPrefs(),
30 search_terms_data.Pass(),
31 WebDataServiceFactory::GetKeywordWebDataForProfile(
32 profile, Profile::EXPLICIT_ACCESS),
33 scoped_ptr<TemplateURLServiceClient>(
34 new ChromeTemplateURLServiceClient(profile)), NULL, NULL,
35 base::Closure()) {
38 base::string16 GetAndClearSearchTerm() {
39 base::string16 search_term;
40 search_term.swap(search_term_);
41 return search_term;
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;
52 private:
53 base::string16 search_term_;
55 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
59 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
60 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
61 changed_count_(0),
62 search_terms_data_(NULL) {
63 // Make unique temp directory.
64 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
65 profile_.reset(new TestingProfile(temp_dir_.path()));
67 profile()->CreateWebDataService();
69 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/");
70 model_.reset(new TestingTemplateURLService(
71 profile(), scoped_ptr<SearchTermsData>(search_terms_data_)));
72 model_->AddObserver(this);
75 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
76 ClearModel();
77 profile_.reset();
79 // Flush the message loop to make application verifiers happy.
80 base::RunLoop().RunUntilIdle();
83 void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
84 changed_count_++;
87 int TemplateURLServiceTestUtil::GetObserverCount() {
88 return changed_count_;
91 void TemplateURLServiceTestUtil::ResetObserverCount() {
92 changed_count_ = 0;
95 void TemplateURLServiceTestUtil::VerifyLoad() {
96 ASSERT_FALSE(model()->loaded());
97 model()->Load();
98 base::RunLoop().RunUntilIdle();
99 EXPECT_EQ(1, GetObserverCount());
100 ResetObserverCount();
103 void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
104 model()->ChangeToLoadedState();
105 // Initialize the web data service so that the database gets updated with
106 // any changes made.
108 model()->web_data_service_ =
109 WebDataServiceFactory::GetKeywordWebDataForProfile(
110 profile(), Profile::EXPLICIT_ACCESS);
111 base::RunLoop().RunUntilIdle();
114 void TemplateURLServiceTestUtil::ClearModel() {
115 model_->Shutdown();
116 model_.reset();
117 search_terms_data_ = NULL;
120 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
121 if (model_)
122 ClearModel();
123 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/");
124 model_.reset(new TestingTemplateURLService(
125 profile(), scoped_ptr<SearchTermsData>(search_terms_data_)));
126 model()->AddObserver(this);
127 changed_count_ = 0;
128 if (verify_load)
129 VerifyLoad();
132 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
133 return model_->GetAndClearSearchTerm();
136 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
137 DCHECK(base_url.is_valid());
138 search_terms_data_->set_google_base_url(base_url.spec());
139 model_->GoogleBaseURLChanged();
142 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
143 bool enabled,
144 const std::string& name,
145 const std::string& keyword,
146 const std::string& search_url,
147 const std::string& suggest_url,
148 const std::string& icon_url,
149 const std::string& encodings,
150 const std::string& alternate_url,
151 const std::string& search_terms_replacement_key) {
152 DefaultSearchPrefTestUtil::SetManagedPref(
153 profile()->GetTestingPrefService(),
154 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
155 alternate_url, search_terms_replacement_key);
158 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
159 DefaultSearchPrefTestUtil::RemoveManagedPref(
160 profile()->GetTestingPrefService());
163 TemplateURLService* TemplateURLServiceTestUtil::model() {
164 return model_.get();