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

Side by Side Diff: components/search_engines/default_search_manager_unittest.cc

Issue 2479113002: Make extensions DSE persistent in browser prefs (Closed)
Patch Set: Added DSE tests 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/search_engines/default_search_manager.h" 5 #include "components/search_engines/default_search_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "components/pref_registry/pref_registry_syncable.h" 17 #include "components/pref_registry/pref_registry_syncable.h"
18 #include "components/pref_registry/testing_pref_service_syncable.h" 18 #include "components/pref_registry/testing_pref_service_syncable.h"
19 #include "components/search_engines/search_engines_pref_names.h" 19 #include "components/search_engines/search_engines_pref_names.h"
20 #include "components/search_engines/search_engines_test_util.h"
20 #include "components/search_engines/template_url_data.h" 21 #include "components/search_engines/template_url_data.h"
21 #include "components/search_engines/template_url_data_util.h" 22 #include "components/search_engines/template_url_data_util.h"
22 #include "components/search_engines/template_url_prepopulate_data.h" 23 #include "components/search_engines/template_url_prepopulate_data.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace { 26 namespace {
26 // A dictionary to hold all data related to the Default Search Engine. 27 // A dictionary to hold all data related to the Default Search Engine.
27 // Eventually, this should replace all the data stored in the 28 // Eventually, this should replace all the data stored in the
28 // default_search_provider.* prefs. 29 // default_search_provider.* prefs.
29 const char kDefaultSearchProviderData[] = 30 const char kDefaultSearchProviderData[] =
30 "default_search_provider_data.template_url_data"; 31 "default_search_provider_data.template_url_data";
31 32
32 // Checks that the two TemplateURLs are similar. Does not check the id, the
33 // date_created or the last_modified time. Neither pointer should be NULL.
34 void ExpectSimilar(const TemplateURLData* expected,
35 const TemplateURLData* actual) {
36 ASSERT_TRUE(expected != NULL);
37 ASSERT_TRUE(actual != NULL);
38
39 EXPECT_EQ(expected->short_name(), actual->short_name());
40 EXPECT_EQ(expected->keyword(), actual->keyword());
41 EXPECT_EQ(expected->url(), actual->url());
42 EXPECT_EQ(expected->suggestions_url, actual->suggestions_url);
43 EXPECT_EQ(expected->favicon_url, actual->favicon_url);
44 EXPECT_EQ(expected->alternate_urls, actual->alternate_urls);
45 EXPECT_EQ(expected->safe_for_autoreplace, actual->safe_for_autoreplace);
46 EXPECT_EQ(expected->input_encodings, actual->input_encodings);
47 EXPECT_EQ(expected->search_terms_replacement_key,
48 actual->search_terms_replacement_key);
49 }
50
51 // TODO(caitkp): TemplateURLData-ify this. 33 // TODO(caitkp): TemplateURLData-ify this.
52 void SetOverrides(user_prefs::TestingPrefServiceSyncable* prefs, bool update) { 34 void SetOverrides(user_prefs::TestingPrefServiceSyncable* prefs, bool update) {
53 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, 35 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion,
54 new base::FundamentalValue(1)); 36 new base::FundamentalValue(1));
55 base::ListValue* overrides = new base::ListValue; 37 base::ListValue* overrides = new base::ListValue;
56 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 38 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
57 39
58 entry->SetString("name", update ? "new_foo" : "foo"); 40 entry->SetString("name", update ? "new_foo" : "foo");
59 entry->SetString("keyword", update ? "new_fook" : "fook"); 41 entry->SetString("keyword", update ? "new_fook" : "fook");
60 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); 42 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}");
(...skipping 28 matching lines...) Expand all
89 if (enabled) { 71 if (enabled) {
90 EXPECT_FALSE(data->keyword().empty()); 72 EXPECT_FALSE(data->keyword().empty());
91 EXPECT_FALSE(data->url().empty()); 73 EXPECT_FALSE(data->url().empty());
92 } 74 }
93 std::unique_ptr<base::DictionaryValue> entry( 75 std::unique_ptr<base::DictionaryValue> entry(
94 TemplateURLDataToDictionary(*data)); 76 TemplateURLDataToDictionary(*data));
95 entry->SetBoolean(DefaultSearchManager::kDisabledByPolicy, !enabled); 77 entry->SetBoolean(DefaultSearchManager::kDisabledByPolicy, !enabled);
96 prefs->SetManagedPref(kDefaultSearchProviderData, entry.release()); 78 prefs->SetManagedPref(kDefaultSearchProviderData, entry.release());
97 } 79 }
98 80
99 std::unique_ptr<TemplateURLData> GenerateDummyTemplateURLData(
100 const std::string& type) {
101 std::unique_ptr<TemplateURLData> data(new TemplateURLData());
102 data->SetShortName(base::UTF8ToUTF16(std::string(type).append("name")));
103 data->SetKeyword(base::UTF8ToUTF16(std::string(type).append("key")));
104 data->SetURL(std::string("http://").append(type).append("foo/{searchTerms}"));
105 data->suggestions_url = std::string("http://").append(type).append("sugg");
106 data->alternate_urls.push_back(
107 std::string("http://").append(type).append("foo/alt"));
108 data->favicon_url = GURL("http://icon1");
109 data->safe_for_autoreplace = true;
110 data->input_encodings = base::SplitString(
111 "UTF-8;UTF-16", ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
112 data->date_created = base::Time();
113 data->last_modified = base::Time();
114 return data;
115 }
116
117 } // namespace 81 } // namespace
118 82
119 class DefaultSearchManagerTest : public testing::Test { 83 class DefaultSearchManagerTest : public testing::Test {
120 public: 84 public:
121 DefaultSearchManagerTest() {}; 85 DefaultSearchManagerTest() {};
122 86
123 void SetUp() override { 87 void SetUp() override {
124 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable); 88 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable);
125 DefaultSearchManager::RegisterProfilePrefs(pref_service_->registry()); 89 DefaultSearchManager::RegisterProfilePrefs(pref_service_->registry());
126 TemplateURLPrepopulateData::RegisterProfilePrefs(pref_service_->registry()); 90 TemplateURLPrepopulateData::RegisterProfilePrefs(pref_service_->registry());
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 212
249 TemplateURLData null_policy_data; 213 TemplateURLData null_policy_data;
250 SetPolicy(pref_service(), false, &null_policy_data); 214 SetPolicy(pref_service(), false, &null_policy_data);
251 EXPECT_EQ(NULL, manager.GetDefaultSearchEngine(&source)); 215 EXPECT_EQ(NULL, manager.GetDefaultSearchEngine(&source));
252 EXPECT_EQ(DefaultSearchManager::FROM_POLICY, source); 216 EXPECT_EQ(DefaultSearchManager::FROM_POLICY, source);
253 217
254 pref_service()->RemoveManagedPref(kDefaultSearchProviderData); 218 pref_service()->RemoveManagedPref(kDefaultSearchProviderData);
255 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source)); 219 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source));
256 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); 220 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
257 } 221 }
258
259 // Test DefaultSearchManager handles extension-controlled DSEs correctly.
260 TEST_F(DefaultSearchManagerTest, DefaultSearchSetByExtension) {
vasilii 2016/11/30 14:00:03 I think the test is still useful. Why do you remov
Alexander Yashkin 2016/12/05 18:16:48 Restored and made it work.
261 DefaultSearchManager manager(pref_service(),
262 DefaultSearchManager::ObserverCallback());
263 std::unique_ptr<TemplateURLData> data = GenerateDummyTemplateURLData("user");
264 manager.SetUserSelectedDefaultSearchEngine(*data);
265
266 DefaultSearchManager::Source source = DefaultSearchManager::FROM_FALLBACK;
267 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source));
268 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
269
270 // Extension trumps prefs:
271 std::unique_ptr<TemplateURLData> extension_data_1 =
272 GenerateDummyTemplateURLData("ext1");
273 manager.SetExtensionControlledDefaultSearchEngine(*extension_data_1);
274
275 ExpectSimilar(extension_data_1.get(),
276 manager.GetDefaultSearchEngine(&source));
277 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source);
278
279 // Policy trumps extension:
280 std::unique_ptr<TemplateURLData> policy_data =
281 GenerateDummyTemplateURLData("policy");
282 SetPolicy(pref_service(), true, policy_data.get());
283
284 ExpectSimilar(policy_data.get(), manager.GetDefaultSearchEngine(&source));
285 EXPECT_EQ(DefaultSearchManager::FROM_POLICY, source);
286 pref_service()->RemoveManagedPref(kDefaultSearchProviderData);
287
288 // Extensions trump each other:
289 std::unique_ptr<TemplateURLData> extension_data_2 =
290 GenerateDummyTemplateURLData("ext2");
291 std::unique_ptr<TemplateURLData> extension_data_3 =
292 GenerateDummyTemplateURLData("ext3");
293 manager.SetExtensionControlledDefaultSearchEngine(*extension_data_2);
294 manager.SetExtensionControlledDefaultSearchEngine(*extension_data_3);
295
296 ExpectSimilar(extension_data_3.get(),
297 manager.GetDefaultSearchEngine(&source));
298 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source);
299
300 manager.ClearExtensionControlledDefaultSearchEngine();
301
302 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source));
303 EXPECT_EQ(DefaultSearchManager::FROM_USER, source);
304 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698