| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 std::unique_ptr<base::DictionaryValue> dict = |
| 167 TemplateURLDataToDictionary(ext_data); |
| 168 ext_pref_map_->RegisterExtension(ext_id, base::Time::Now(), true, true); |
| 169 // Set mapping between extension and pref. |
| 170 ext_pref_map_->SetExtensionPref( |
| 171 ext_id, DefaultSearchManager::kDefaultSearchProviderDataPrefName, |
| 172 extensions::ExtensionPrefsScope::kExtensionPrefsScopeRegular, |
| 173 dict->DeepCopy()); |
| 174 // Set extension overriden value in test pref service. |
| 175 profile()->GetTestingPrefService()->SetExtensionPref( |
| 176 DefaultSearchManager::kDefaultSearchProviderDataPrefName, dict.release()); |
| 177 } |
| 178 |
| 179 void TemplateURLServiceTestUtil::RemoveExtensionDefaultSearchFromPrefs( |
| 180 const std::string& ext_id) { |
| 181 ext_pref_map_->UnregisterExtension(ext_id); |
| 182 // If no more extensions control DSE pref, remove from test pref service. |
| 183 auto pref_ext_id = ext_pref_map_->GetExtensionControllingPref( |
| 184 DefaultSearchManager::kDefaultSearchProviderDataPrefName); |
| 185 if (pref_ext_id.empty()) { |
| 186 profile()->GetTestingPrefService()->RemoveExtensionPref( |
| 187 DefaultSearchManager::kDefaultSearchProviderDataPrefName); |
| 188 } |
| 189 } |
| 190 |
| 191 TemplateURL* TemplateURLServiceTestUtil::AddExtensionControlledTURL( |
| 192 std::unique_ptr<TemplateURL> ext_dse, |
| 193 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> ext_info) { |
| 194 TemplateURL::AssociatedExtensionInfo ext_info_copy = *ext_info; |
| 195 TemplateURLData ext_data = ext_dse->data(); |
| 196 TemplateURL* result = model()->AddExtensionControlledTURL( |
| 197 std::move(ext_dse), std::move(ext_info)); |
| 198 if (ext_info_copy.wants_to_be_default_engine) { |
| 199 // Set extension overriden DSE value to prefs. |
| 200 SetExtensionDefaultSearchInPrefs(ext_info_copy.extension_id, ext_data); |
| 201 } |
| 202 return result; |
| 203 } |
| 204 |
| 205 void TemplateURLServiceTestUtil::RemoveExtensionControlledTURL( |
| 206 const std::string& ext_id) { |
| 207 TemplateURL* ext_turl = model()->FindTemplateURLForExtension( |
| 208 ext_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 209 ASSERT_TRUE(ext_turl); |
| 210 ASSERT_TRUE(ext_turl->GetExtensionInfoForTesting()); |
| 211 if (ext_turl->GetExtensionInfoForTesting()->wants_to_be_default_engine) { |
| 212 RemoveExtensionDefaultSearchFromPrefs(ext_id); |
| 213 } |
| 214 model()->RemoveExtensionControlledTURL( |
| 215 ext_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 216 } |
| OLD | NEW |