OLD | NEW |
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" | |
19 #include "components/search_engines/search_engines_pref_names.h" | 18 #include "components/search_engines/search_engines_pref_names.h" |
20 #include "components/search_engines/template_url_data.h" | 19 #include "components/search_engines/template_url_data.h" |
21 #include "components/search_engines/template_url_data_util.h" | 20 #include "components/search_engines/template_url_data_util.h" |
22 #include "components/search_engines/template_url_prepopulate_data.h" | 21 #include "components/search_engines/template_url_prepopulate_data.h" |
| 22 #include "components/sync_preferences/testing_pref_service_syncable.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 // A dictionary to hold all data related to the Default Search Engine. | 26 // A dictionary to hold all data related to the Default Search Engine. |
27 // Eventually, this should replace all the data stored in the | 27 // Eventually, this should replace all the data stored in the |
28 // default_search_provider.* prefs. | 28 // default_search_provider.* prefs. |
29 const char kDefaultSearchProviderData[] = | 29 const char kDefaultSearchProviderData[] = |
30 "default_search_provider_data.template_url_data"; | 30 "default_search_provider_data.template_url_data"; |
31 | 31 |
32 // Checks that the two TemplateURLs are similar. Does not check the id or | 32 // Checks that the two TemplateURLs are similar. Does not check the id or |
33 // any time-related fields. Neither pointer should be null. | 33 // any time-related fields. Neither pointer should be null. |
34 void ExpectSimilar(const TemplateURLData* expected, | 34 void ExpectSimilar(const TemplateURLData* expected, |
35 const TemplateURLData* actual) { | 35 const TemplateURLData* actual) { |
36 ASSERT_TRUE(expected != NULL); | 36 ASSERT_TRUE(expected != NULL); |
37 ASSERT_TRUE(actual != NULL); | 37 ASSERT_TRUE(actual != NULL); |
38 | 38 |
39 EXPECT_EQ(expected->short_name(), actual->short_name()); | 39 EXPECT_EQ(expected->short_name(), actual->short_name()); |
40 EXPECT_EQ(expected->keyword(), actual->keyword()); | 40 EXPECT_EQ(expected->keyword(), actual->keyword()); |
41 EXPECT_EQ(expected->url(), actual->url()); | 41 EXPECT_EQ(expected->url(), actual->url()); |
42 EXPECT_EQ(expected->suggestions_url, actual->suggestions_url); | 42 EXPECT_EQ(expected->suggestions_url, actual->suggestions_url); |
43 EXPECT_EQ(expected->favicon_url, actual->favicon_url); | 43 EXPECT_EQ(expected->favicon_url, actual->favicon_url); |
44 EXPECT_EQ(expected->alternate_urls, actual->alternate_urls); | 44 EXPECT_EQ(expected->alternate_urls, actual->alternate_urls); |
45 EXPECT_EQ(expected->safe_for_autoreplace, actual->safe_for_autoreplace); | 45 EXPECT_EQ(expected->safe_for_autoreplace, actual->safe_for_autoreplace); |
46 EXPECT_EQ(expected->input_encodings, actual->input_encodings); | 46 EXPECT_EQ(expected->input_encodings, actual->input_encodings); |
47 EXPECT_EQ(expected->search_terms_replacement_key, | 47 EXPECT_EQ(expected->search_terms_replacement_key, |
48 actual->search_terms_replacement_key); | 48 actual->search_terms_replacement_key); |
49 } | 49 } |
50 | 50 |
51 // TODO(caitkp): TemplateURLData-ify this. | 51 // TODO(caitkp): TemplateURLData-ify this. |
52 void SetOverrides(user_prefs::TestingPrefServiceSyncable* prefs, bool update) { | 52 void SetOverrides(sync_preferences::TestingPrefServiceSyncable* prefs, |
| 53 bool update) { |
53 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | 54 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
54 new base::FundamentalValue(1)); | 55 new base::FundamentalValue(1)); |
55 base::ListValue* overrides = new base::ListValue; | 56 base::ListValue* overrides = new base::ListValue; |
56 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 57 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
57 | 58 |
58 entry->SetString("name", update ? "new_foo" : "foo"); | 59 entry->SetString("name", update ? "new_foo" : "foo"); |
59 entry->SetString("keyword", update ? "new_fook" : "fook"); | 60 entry->SetString("keyword", update ? "new_fook" : "fook"); |
60 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); | 61 entry->SetString("search_url", "http://foo.com/s?q={searchTerms}"); |
61 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); | 62 entry->SetString("favicon_url", "http://foi.com/favicon.ico"); |
62 entry->SetString("encoding", "UTF-8"); | 63 entry->SetString("encoding", "UTF-8"); |
(...skipping 13 matching lines...) Expand all Loading... |
76 entry->SetString("encoding", std::string()); | 77 entry->SetString("encoding", std::string()); |
77 overrides->Append(entry->CreateDeepCopy()); | 78 overrides->Append(entry->CreateDeepCopy()); |
78 entry->SetInteger("id", 1003); | 79 entry->SetInteger("id", 1003); |
79 entry->SetString("name", "baz"); | 80 entry->SetString("name", "baz"); |
80 entry->SetString("keyword", "bazk"); | 81 entry->SetString("keyword", "bazk"); |
81 entry->SetString("encoding", "UTF-8"); | 82 entry->SetString("encoding", "UTF-8"); |
82 overrides->Append(entry->CreateDeepCopy()); | 83 overrides->Append(entry->CreateDeepCopy()); |
83 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); | 84 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides); |
84 } | 85 } |
85 | 86 |
86 void SetPolicy(user_prefs::TestingPrefServiceSyncable* prefs, | 87 void SetPolicy(sync_preferences::TestingPrefServiceSyncable* prefs, |
87 bool enabled, | 88 bool enabled, |
88 TemplateURLData* data) { | 89 TemplateURLData* data) { |
89 if (enabled) { | 90 if (enabled) { |
90 EXPECT_FALSE(data->keyword().empty()); | 91 EXPECT_FALSE(data->keyword().empty()); |
91 EXPECT_FALSE(data->url().empty()); | 92 EXPECT_FALSE(data->url().empty()); |
92 } | 93 } |
93 std::unique_ptr<base::DictionaryValue> entry( | 94 std::unique_ptr<base::DictionaryValue> entry( |
94 TemplateURLDataToDictionary(*data)); | 95 TemplateURLDataToDictionary(*data)); |
95 entry->SetBoolean(DefaultSearchManager::kDisabledByPolicy, !enabled); | 96 entry->SetBoolean(DefaultSearchManager::kDisabledByPolicy, !enabled); |
96 prefs->SetManagedPref(kDefaultSearchProviderData, entry.release()); | 97 prefs->SetManagedPref(kDefaultSearchProviderData, entry.release()); |
(...skipping 18 matching lines...) Expand all Loading... |
115 return data; | 116 return data; |
116 } | 117 } |
117 | 118 |
118 } // namespace | 119 } // namespace |
119 | 120 |
120 class DefaultSearchManagerTest : public testing::Test { | 121 class DefaultSearchManagerTest : public testing::Test { |
121 public: | 122 public: |
122 DefaultSearchManagerTest() {}; | 123 DefaultSearchManagerTest() {}; |
123 | 124 |
124 void SetUp() override { | 125 void SetUp() override { |
125 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable); | 126 pref_service_.reset(new sync_preferences::TestingPrefServiceSyncable); |
126 DefaultSearchManager::RegisterProfilePrefs(pref_service_->registry()); | 127 DefaultSearchManager::RegisterProfilePrefs(pref_service_->registry()); |
127 TemplateURLPrepopulateData::RegisterProfilePrefs(pref_service_->registry()); | 128 TemplateURLPrepopulateData::RegisterProfilePrefs(pref_service_->registry()); |
128 } | 129 } |
129 | 130 |
130 user_prefs::TestingPrefServiceSyncable* pref_service() { | 131 sync_preferences::TestingPrefServiceSyncable* pref_service() { |
131 return pref_service_.get(); | 132 return pref_service_.get(); |
132 } | 133 } |
133 | 134 |
134 private: | 135 private: |
135 std::unique_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | 136 std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> pref_service_; |
136 | 137 |
137 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManagerTest); | 138 DISALLOW_COPY_AND_ASSIGN(DefaultSearchManagerTest); |
138 }; | 139 }; |
139 | 140 |
140 // Test that a TemplateURLData object is properly written and read from Prefs. | 141 // Test that a TemplateURLData object is properly written and read from Prefs. |
141 TEST_F(DefaultSearchManagerTest, ReadAndWritePref) { | 142 TEST_F(DefaultSearchManagerTest, ReadAndWritePref) { |
142 DefaultSearchManager manager(pref_service(), | 143 DefaultSearchManager manager(pref_service(), |
143 DefaultSearchManager::ObserverCallback()); | 144 DefaultSearchManager::ObserverCallback()); |
144 TemplateURLData data; | 145 TemplateURLData data; |
145 data.SetShortName(base::UTF8ToUTF16("name1")); | 146 data.SetShortName(base::UTF8ToUTF16("name1")); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 298 |
298 ExpectSimilar(extension_data_3.get(), | 299 ExpectSimilar(extension_data_3.get(), |
299 manager.GetDefaultSearchEngine(&source)); | 300 manager.GetDefaultSearchEngine(&source)); |
300 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source); | 301 EXPECT_EQ(DefaultSearchManager::FROM_EXTENSION, source); |
301 | 302 |
302 manager.ClearExtensionControlledDefaultSearchEngine(); | 303 manager.ClearExtensionControlledDefaultSearchEngine(); |
303 | 304 |
304 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source)); | 305 ExpectSimilar(data.get(), manager.GetDefaultSearchEngine(&source)); |
305 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); | 306 EXPECT_EQ(DefaultSearchManager::FROM_USER, source); |
306 } | 307 } |
OLD | NEW |