| 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_policy_handler.h" | 5 #include "components/search_engines/default_search_policy_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "components/policy/core/browser/configuration_policy_pref_store.h" | 10 #include "components/policy/core/browser/configuration_policy_pref_store.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 void DefaultSearchPolicyHandlerTest:: | 77 void DefaultSearchPolicyHandlerTest:: |
| 78 BuildDefaultSearchPolicy(PolicyMap* policy) { | 78 BuildDefaultSearchPolicy(PolicyMap* policy) { |
| 79 base::ListValue* encodings = new base::ListValue(); | 79 base::ListValue* encodings = new base::ListValue(); |
| 80 encodings->AppendString("UTF-16"); | 80 encodings->AppendString("UTF-16"); |
| 81 encodings->AppendString("UTF-8"); | 81 encodings->AppendString("UTF-8"); |
| 82 policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, | 82 policy->Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, |
| 83 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 83 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 84 base::MakeUnique<base::Value>(true), nullptr); | 84 base::MakeUnique<base::Value>(true), nullptr); |
| 85 policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, | 85 policy->Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, |
| 86 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 86 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 87 base::MakeUnique<base::StringValue>(kSearchURL), nullptr); | 87 base::MakeUnique<base::Value>(kSearchURL), nullptr); |
| 88 policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY, | 88 policy->Set(key::kDefaultSearchProviderName, POLICY_LEVEL_MANDATORY, |
| 89 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 89 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 90 base::MakeUnique<base::StringValue>(kName), nullptr); | 90 base::MakeUnique<base::Value>(kName), nullptr); |
| 91 policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY, | 91 policy->Set(key::kDefaultSearchProviderKeyword, POLICY_LEVEL_MANDATORY, |
| 92 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 92 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 93 base::MakeUnique<base::StringValue>(kKeyword), nullptr); | 93 base::MakeUnique<base::Value>(kKeyword), nullptr); |
| 94 policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY, | 94 policy->Set(key::kDefaultSearchProviderSuggestURL, POLICY_LEVEL_MANDATORY, |
| 95 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 95 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 96 base::MakeUnique<base::StringValue>(kSuggestURL), nullptr); | 96 base::MakeUnique<base::Value>(kSuggestURL), nullptr); |
| 97 policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY, | 97 policy->Set(key::kDefaultSearchProviderIconURL, POLICY_LEVEL_MANDATORY, |
| 98 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 98 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 99 base::MakeUnique<base::StringValue>(kIconURL), nullptr); | 99 base::MakeUnique<base::Value>(kIconURL), nullptr); |
| 100 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, | 100 policy->Set(key::kDefaultSearchProviderEncodings, POLICY_LEVEL_MANDATORY, |
| 101 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 101 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 102 base::WrapUnique(encodings), nullptr); | 102 base::WrapUnique(encodings), nullptr); |
| 103 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, | 103 policy->Set(key::kDefaultSearchProviderAlternateURLs, POLICY_LEVEL_MANDATORY, |
| 104 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 104 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 105 default_alternate_urls_.CreateDeepCopy(), nullptr); | 105 default_alternate_urls_.CreateDeepCopy(), nullptr); |
| 106 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, | 106 policy->Set(key::kDefaultSearchProviderSearchTermsReplacementKey, |
| 107 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 107 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 108 base::MakeUnique<base::StringValue>(kReplacementKey), nullptr); | 108 base::MakeUnique<base::Value>(kReplacementKey), nullptr); |
| 109 policy->Set(key::kDefaultSearchProviderImageURL, POLICY_LEVEL_MANDATORY, | 109 policy->Set(key::kDefaultSearchProviderImageURL, POLICY_LEVEL_MANDATORY, |
| 110 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 110 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 111 base::MakeUnique<base::StringValue>(kImageURL), nullptr); | 111 base::MakeUnique<base::Value>(kImageURL), nullptr); |
| 112 policy->Set(key::kDefaultSearchProviderImageURLPostParams, | 112 policy->Set(key::kDefaultSearchProviderImageURLPostParams, |
| 113 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 113 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 114 base::MakeUnique<base::StringValue>(kImageParams), nullptr); | 114 base::MakeUnique<base::Value>(kImageParams), nullptr); |
| 115 policy->Set(key::kDefaultSearchProviderNewTabURL, POLICY_LEVEL_MANDATORY, | 115 policy->Set(key::kDefaultSearchProviderNewTabURL, POLICY_LEVEL_MANDATORY, |
| 116 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 116 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 117 base::MakeUnique<base::StringValue>(kNewTabURL), nullptr); | 117 base::MakeUnique<base::Value>(kNewTabURL), nullptr); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Checks that if the default search policy is missing, that no elements of the | 120 // Checks that if the default search policy is missing, that no elements of the |
| 121 // default search policy will be present. | 121 // default search policy will be present. |
| 122 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) { | 122 TEST_F(DefaultSearchPolicyHandlerTest, MissingUrl) { |
| 123 PolicyMap policy; | 123 PolicyMap policy; |
| 124 BuildDefaultSearchPolicy(&policy); | 124 BuildDefaultSearchPolicy(&policy); |
| 125 policy.Erase(key::kDefaultSearchProviderSearchURL); | 125 policy.Erase(key::kDefaultSearchProviderSearchURL); |
| 126 UpdateProviderPolicy(policy); | 126 UpdateProviderPolicy(policy); |
| 127 | 127 |
| 128 const base::Value* temp = nullptr; | 128 const base::Value* temp = nullptr; |
| 129 EXPECT_FALSE(store_->GetValue( | 129 EXPECT_FALSE(store_->GetValue( |
| 130 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); | 130 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 // Checks that if the default search policy is invalid, that no elements of the | 133 // Checks that if the default search policy is invalid, that no elements of the |
| 134 // default search policy will be present. | 134 // default search policy will be present. |
| 135 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) { | 135 TEST_F(DefaultSearchPolicyHandlerTest, Invalid) { |
| 136 PolicyMap policy; | 136 PolicyMap policy; |
| 137 BuildDefaultSearchPolicy(&policy); | 137 BuildDefaultSearchPolicy(&policy); |
| 138 const char bad_search_url[] = "http://test.com/noSearchTerms"; | 138 const char bad_search_url[] = "http://test.com/noSearchTerms"; |
| 139 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, | 139 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, |
| 140 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 140 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 141 base::WrapUnique(new base::StringValue(bad_search_url)), nullptr); | 141 base::WrapUnique(new base::Value(bad_search_url)), nullptr); |
| 142 UpdateProviderPolicy(policy); | 142 UpdateProviderPolicy(policy); |
| 143 | 143 |
| 144 const base::Value* temp = nullptr; | 144 const base::Value* temp = nullptr; |
| 145 EXPECT_FALSE(store_->GetValue( | 145 EXPECT_FALSE(store_->GetValue( |
| 146 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); | 146 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 // Checks that if the default search policy has invalid type for elements, | 149 // Checks that if the default search policy has invalid type for elements, |
| 150 // that no elements of the default search policy will be present in prefs. | 150 // that no elements of the default search policy will be present in prefs. |
| 151 TEST_F(DefaultSearchPolicyHandlerTest, InvalidType) { | 151 TEST_F(DefaultSearchPolicyHandlerTest, InvalidType) { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 278 |
| 279 // Checks that if the policy for default search is valid, i.e. there's a | 279 // Checks that if the policy for default search is valid, i.e. there's a |
| 280 // search URL, that all the elements have been given proper defaults. | 280 // search URL, that all the elements have been given proper defaults. |
| 281 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) { | 281 TEST_F(DefaultSearchPolicyHandlerTest, MinimallyDefined) { |
| 282 PolicyMap policy; | 282 PolicyMap policy; |
| 283 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, | 283 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, |
| 284 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 284 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 285 base::WrapUnique(new base::Value(true)), nullptr); | 285 base::WrapUnique(new base::Value(true)), nullptr); |
| 286 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, | 286 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, |
| 287 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 287 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 288 base::WrapUnique(new base::StringValue(kSearchURL)), nullptr); | 288 base::WrapUnique(new base::Value(kSearchURL)), nullptr); |
| 289 UpdateProviderPolicy(policy); | 289 UpdateProviderPolicy(policy); |
| 290 | 290 |
| 291 const base::Value* temp = NULL; | 291 const base::Value* temp = NULL; |
| 292 const base::DictionaryValue* dictionary; | 292 const base::DictionaryValue* dictionary; |
| 293 std::string value; | 293 std::string value; |
| 294 const base::ListValue* list_value; | 294 const base::ListValue* list_value; |
| 295 EXPECT_TRUE(store_->GetValue( | 295 EXPECT_TRUE(store_->GetValue( |
| 296 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); | 296 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); |
| 297 temp->GetAsDictionary(&dictionary); | 297 temp->GetAsDictionary(&dictionary); |
| 298 | 298 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 337 |
| 338 // Checks that setting a file URL as the default search is reflected properly in | 338 // Checks that setting a file URL as the default search is reflected properly in |
| 339 // the dictionary pref. | 339 // the dictionary pref. |
| 340 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) { | 340 TEST_F(DefaultSearchPolicyHandlerTest, FileURL) { |
| 341 PolicyMap policy; | 341 PolicyMap policy; |
| 342 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, | 342 policy.Set(key::kDefaultSearchProviderEnabled, POLICY_LEVEL_MANDATORY, |
| 343 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 343 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 344 base::WrapUnique(new base::Value(true)), nullptr); | 344 base::WrapUnique(new base::Value(true)), nullptr); |
| 345 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, | 345 policy.Set(key::kDefaultSearchProviderSearchURL, POLICY_LEVEL_MANDATORY, |
| 346 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 346 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
| 347 base::WrapUnique(new base::StringValue(kFileSearchURL)), nullptr); | 347 base::WrapUnique(new base::Value(kFileSearchURL)), nullptr); |
| 348 UpdateProviderPolicy(policy); | 348 UpdateProviderPolicy(policy); |
| 349 | 349 |
| 350 const base::Value* temp = NULL; | 350 const base::Value* temp = NULL; |
| 351 const base::DictionaryValue* dictionary; | 351 const base::DictionaryValue* dictionary; |
| 352 std::string value; | 352 std::string value; |
| 353 | 353 |
| 354 EXPECT_TRUE(store_->GetValue( | 354 EXPECT_TRUE(store_->GetValue( |
| 355 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); | 355 DefaultSearchManager::kDefaultSearchProviderDataPrefName, &temp)); |
| 356 temp->GetAsDictionary(&dictionary); | 356 temp->GetAsDictionary(&dictionary); |
| 357 | 357 |
| 358 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value)); | 358 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kURL, &value)); |
| 359 EXPECT_EQ(kFileSearchURL, value); | 359 EXPECT_EQ(kFileSearchURL, value); |
| 360 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value)); | 360 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kShortName, &value)); |
| 361 EXPECT_EQ("_", value); | 361 EXPECT_EQ("_", value); |
| 362 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value)); | 362 EXPECT_TRUE(dictionary->GetString(DefaultSearchManager::kKeyword, &value)); |
| 363 EXPECT_EQ("_", value); | 363 EXPECT_EQ("_", value); |
| 364 } | 364 } |
| 365 | 365 |
| 366 } // namespace policy | 366 } // namespace policy |
| OLD | NEW |