Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: chrome/browser/search_engines/template_url_service_test_util.cc

Issue 2479113002: Make extensions DSE persistent in browser prefs (Closed)
Patch Set: Tests updated(rewritten) after review Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/search_engines/template_url_service_test_util.h" 5 #include "chrome/browser/search_engines/template_url_service_test_util.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 #include "chrome/browser/history/history_service_factory.h" 10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" 11 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
12 #include "chrome/test/base/testing_profile.h" 12 #include "chrome/test/base/testing_profile.h"
13 #include "components/search_engines/keyword_table.h" 13 #include "components/search_engines/keyword_table.h"
14 #include "components/search_engines/keyword_web_data_service.h" 14 #include "components/search_engines/keyword_web_data_service.h"
15 #include "components/search_engines/template_url_data_util.h" 15 #include "components/search_engines/template_url_data_util.h"
16 #include "components/search_engines/template_url_service.h" 16 #include "components/search_engines/template_url_service.h"
17 #include "components/search_engines/testing_search_terms_data.h" 17 #include "components/search_engines/testing_search_terms_data.h"
18 #include "components/sync_preferences/testing_pref_service_syncable.h" 18 #include "components/sync_preferences/testing_pref_service_syncable.h"
19 #include "components/webdata/common/web_database_service.h" 19 #include "components/webdata/common/web_database_service.h"
20 #include "extensions/browser/extension_pref_value_map.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace { 23 namespace {
23 24
24 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient { 25 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
25 public: 26 public:
26 TestingTemplateURLServiceClient(history::HistoryService* history_service, 27 TestingTemplateURLServiceClient(history::HistoryService* history_service,
28 ExtensionPrefValueMap* ext_pref_map,
27 base::string16* search_term) 29 base::string16* search_term)
28 : ChromeTemplateURLServiceClient(history_service), 30 : ChromeTemplateURLServiceClient(history_service, ext_pref_map),
29 search_term_(search_term) {} 31 search_term_(search_term) {}
30 32
31 void SetKeywordSearchTermsForURL(const GURL& url, 33 void SetKeywordSearchTermsForURL(const GURL& url,
32 TemplateURLID id, 34 TemplateURLID id,
33 const base::string16& term) override { 35 const base::string16& term) override {
34 *search_term_ = term; 36 *search_term_ = term;
35 } 37 }
36 38
37 private: 39 private:
38 base::string16* search_term_; 40 base::string16* search_term_;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 base::ThreadTaskRunnerHandle::Get()); 72 base::ThreadTaskRunnerHandle::Get());
71 web_database_service->AddTable( 73 web_database_service->AddTable(
72 std::unique_ptr<WebDatabaseTable>(new KeywordTable())); 74 std::unique_ptr<WebDatabaseTable>(new KeywordTable()));
73 web_database_service->LoadDatabase(); 75 web_database_service->LoadDatabase();
74 76
75 web_data_service_ = new KeywordWebDataService( 77 web_data_service_ = new KeywordWebDataService(
76 web_database_service.get(), base::ThreadTaskRunnerHandle::Get(), 78 web_database_service.get(), base::ThreadTaskRunnerHandle::Get(),
77 KeywordWebDataService::ProfileErrorCallback()); 79 KeywordWebDataService::ProfileErrorCallback());
78 web_data_service_->Init(); 80 web_data_service_->Init();
79 81
82 ext_pref_map_.reset(new ExtensionPrefValueMap());
83
80 ResetModel(false); 84 ResetModel(false);
81 } 85 }
82 86
83 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { 87 TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
84 ClearModel(); 88 ClearModel();
85 profile_.reset(); 89 profile_.reset();
86 90
87 // Flush the message loop to make application verifiers happy. 91 // Flush the message loop to make application verifiers happy.
88 base::RunLoop().RunUntilIdle(); 92 base::RunLoop().RunUntilIdle();
89 } 93 }
(...skipping 30 matching lines...) Expand all
120 void TemplateURLServiceTestUtil::ClearModel() { 124 void TemplateURLServiceTestUtil::ClearModel() {
121 model_->Shutdown(); 125 model_->Shutdown();
122 model_.reset(); 126 model_.reset();
123 search_terms_data_ = NULL; 127 search_terms_data_ = NULL;
124 } 128 }
125 129
126 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { 130 void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
127 if (model_) 131 if (model_)
128 ClearModel(); 132 ClearModel();
129 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/"); 133 search_terms_data_ = new TestingSearchTermsData("http://www.google.com/");
134
130 model_.reset(new TemplateURLService( 135 model_.reset(new TemplateURLService(
131 profile()->GetPrefs(), 136 profile()->GetPrefs(),
132 std::unique_ptr<SearchTermsData>(search_terms_data_), 137 std::unique_ptr<SearchTermsData>(search_terms_data_),
133 web_data_service_.get(), 138 web_data_service_.get(),
134 std::unique_ptr<TemplateURLServiceClient>( 139 std::unique_ptr<TemplateURLServiceClient>(
135 new TestingTemplateURLServiceClient( 140 new TestingTemplateURLServiceClient(
136 HistoryServiceFactory::GetForProfileIfExists( 141 HistoryServiceFactory::GetForProfileIfExists(
137 profile(), ServiceAccessType::EXPLICIT_ACCESS), 142 profile(), ServiceAccessType::EXPLICIT_ACCESS),
138 &search_term_)), 143 ext_pref_map_.get(), &search_term_)),
139 NULL, NULL, base::Closure())); 144 NULL, NULL, base::Closure()));
140 model()->AddObserver(this); 145 model()->AddObserver(this);
141 changed_count_ = 0; 146 changed_count_ = 0;
142 if (verify_load) 147 if (verify_load)
143 VerifyLoad(); 148 VerifyLoad();
144 } 149 }
145 150
146 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { 151 base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
147 base::string16 search_term; 152 base::string16 search_term;
148 search_term.swap(search_term_); 153 search_term.swap(search_term_);
149 return search_term; 154 return search_term;
150 } 155 }
151 156
152 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { 157 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
153 DCHECK(base_url.is_valid()); 158 DCHECK(base_url.is_valid());
154 search_terms_data_->set_google_base_url(base_url.spec()); 159 search_terms_data_->set_google_base_url(base_url.spec());
155 model_->GoogleBaseURLChanged(); 160 model_->GoogleBaseURLChanged();
156 } 161 }
162
163 void TemplateURLServiceTestUtil::SetExtensionDefaultSearchInPrefs(
164 const std::string& ext_id,
165 const TemplateURLData& ext_data) {
166 auto dict = TemplateURLDataToDictionary(ext_data);
vasilii 2016/12/06 19:16:42 Readabilty would improve with an explicit type..
Alexander Yashkin 2016/12/09 08:19:52 Expanded.
167 ext_pref_map_->RegisterExtension(ext_id, base::Time::Now(), true, true);
168 // Set mapping between extension and pref.
169 ext_pref_map_->SetExtensionPref(
170 ext_id, DefaultSearchManager::kDefaultSearchProviderDataPrefName,
171 extensions::ExtensionPrefsScope::kExtensionPrefsScopeRegular,
172 dict->DeepCopy());
173 // Set extension overriden value in test pref service.
174 profile()->GetTestingPrefService()->SetExtensionPref(
175 DefaultSearchManager::kDefaultSearchProviderDataPrefName, dict.release());
176 }
177
178 void TemplateURLServiceTestUtil::RemoveExtensionDefaultSearchFromPrefs(
179 const std::string& ext_id) {
180 ext_pref_map_->UnregisterExtension(ext_id);
181 // If no more extensions control DSE pref, remove from test pref service.
182 auto pref_ext_id = ext_pref_map_->GetExtensionControllingPref(
183 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
184 if (pref_ext_id.empty()) {
185 profile()->GetTestingPrefService()->RemoveExtensionPref(
186 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
187 }
188 }
189
190 TemplateURL* TemplateURLServiceTestUtil::AddExtensionControlledTURL(
191 std::unique_ptr<TemplateURL> ext_dse,
192 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> ext_info) {
193 TemplateURL::AssociatedExtensionInfo ext_info_copy = *ext_info;
194 TemplateURLData ext_data = ext_dse->data();
195 TemplateURL* result = model()->AddExtensionControlledTURL(
196 std::move(ext_dse), std::move(ext_info));
197 if (ext_info_copy.wants_to_be_default_engine) {
198 // Set extension overriden DSE value to prefs.
199 SetExtensionDefaultSearchInPrefs(ext_info_copy.extension_id, ext_data);
200 }
201 return result;
202 }
203
204 void TemplateURLServiceTestUtil::RemoveExtensionControlledTURL(
205 const std::string& ext_id) {
206 TemplateURL* ext_turl = model()->FindTemplateURLForExtension(
207 ext_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
208 ASSERT_TRUE(ext_turl);
209 ASSERT_TRUE(ext_turl->extension_info_for_tests());
210 if (ext_turl->extension_info_for_tests()->wants_to_be_default_engine) {
211 RemoveExtensionDefaultSearchFromPrefs(ext_id);
212 }
213 model()->RemoveExtensionControlledTURL(
214 ext_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698