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

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

Issue 2541493002: Refactor search_engines tests to better reuse code (Closed)
Patch Set: Fixed 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/default_search_pref_test_util.h"
14 #include "components/search_engines/keyword_table.h" 13 #include "components/search_engines/keyword_table.h"
15 #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"
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 "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace { 22 namespace {
23 23
24 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient { 24 class TestingTemplateURLServiceClient : public ChromeTemplateURLServiceClient {
25 public: 25 public:
26 TestingTemplateURLServiceClient(history::HistoryService* history_service, 26 TestingTemplateURLServiceClient(history::HistoryService* history_service,
27 base::string16* search_term) 27 base::string16* search_term)
28 : ChromeTemplateURLServiceClient(history_service), 28 : ChromeTemplateURLServiceClient(history_service),
29 search_term_(search_term) {} 29 search_term_(search_term) {}
30 30
31 void SetKeywordSearchTermsForURL(const GURL& url, 31 void SetKeywordSearchTermsForURL(const GURL& url,
32 TemplateURLID id, 32 TemplateURLID id,
33 const base::string16& term) override { 33 const base::string16& term) override {
34 *search_term_ = term; 34 *search_term_ = term;
35 } 35 }
36 36
37 private: 37 private:
38 base::string16* search_term_; 38 base::string16* search_term_;
39 39
40 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient); 40 DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLServiceClient);
41 }; 41 };
42 42
43 } // namespace 43 } // namespace
44 44
45 void SetManagedDefaultSearchPreferences(const TemplateURLData& managed_data,
46 bool enabled,
47 TestingProfile* profile) {
48 auto dict = TemplateURLDataToDictionary(managed_data);
49 dict->SetBoolean(DefaultSearchManager::kDisabledByPolicy, !enabled);
50
51 profile->GetTestingPrefService()->SetManagedPref(
52 DefaultSearchManager::kDefaultSearchProviderDataPrefName, dict.release());
53 }
54
55 void RemoveManagedDefaultSearchPreferences(TestingProfile* profile) {
56 profile->GetTestingPrefService()->RemoveManagedPref(
57 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
58 }
59
45 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() 60 TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
46 : changed_count_(0), 61 : changed_count_(0),
47 search_terms_data_(NULL) { 62 search_terms_data_(NULL) {
48 // Make unique temp directory. 63 // Make unique temp directory.
49 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); 64 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
50 profile_.reset(new TestingProfile(temp_dir_.GetPath())); 65 profile_.reset(new TestingProfile(temp_dir_.GetPath()));
51 66
52 scoped_refptr<WebDatabaseService> web_database_service = 67 scoped_refptr<WebDatabaseService> web_database_service =
53 new WebDatabaseService(temp_dir_.GetPath().AppendASCII("webdata"), 68 new WebDatabaseService(temp_dir_.GetPath().AppendASCII("webdata"),
54 base::ThreadTaskRunnerHandle::Get(), 69 base::ThreadTaskRunnerHandle::Get(),
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 base::string16 search_term; 147 base::string16 search_term;
133 search_term.swap(search_term_); 148 search_term.swap(search_term_);
134 return search_term; 149 return search_term;
135 } 150 }
136 151
137 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { 152 void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
138 DCHECK(base_url.is_valid()); 153 DCHECK(base_url.is_valid());
139 search_terms_data_->set_google_base_url(base_url.spec()); 154 search_terms_data_->set_google_base_url(base_url.spec());
140 model_->GoogleBaseURLChanged(); 155 model_->GoogleBaseURLChanged();
141 } 156 }
142
143 void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
144 bool enabled,
145 const std::string& name,
146 const std::string& keyword,
147 const std::string& search_url,
148 const std::string& suggest_url,
149 const std::string& icon_url,
150 const std::string& encodings,
151 const std::string& alternate_url,
152 const std::string& search_terms_replacement_key) {
153 DefaultSearchPrefTestUtil::SetManagedPref(
154 profile()->GetTestingPrefService(),
155 enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
156 alternate_url, search_terms_replacement_key);
157 }
158
159 void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
160 DefaultSearchPrefTestUtil::RemoveManagedPref(
161 profile()->GetTestingPrefService());
162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698