| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/content_settings/content_settings_policy_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_policy_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 15 #include "chrome/browser/content_settings/content_settings_utils.h" | 15 #include "chrome/browser/content_settings/content_settings_utils.h" |
| 16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "chrome/test/base/testing_pref_service_syncable.h" | 19 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/content_settings/core/browser/content_settings_rule.h" | 21 #include "components/content_settings/core/browser/content_settings_rule.h" |
| 22 #include "content/public/test/test_browser_thread.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 25 | 24 |
| 26 using ::testing::_; | 25 using ::testing::_; |
| 27 using content::BrowserThread; | |
| 28 | 26 |
| 29 namespace content_settings { | 27 namespace content_settings { |
| 30 | 28 |
| 31 typedef std::vector<Rule> Rules; | 29 typedef std::vector<Rule> Rules; |
| 32 | 30 |
| 33 class PolicyProviderTest : public testing::Test { | 31 TEST(PolicyProviderTest, DefaultGeolocationContentSetting) { |
| 34 public: | |
| 35 PolicyProviderTest() | |
| 36 : ui_thread_(BrowserThread::UI, &message_loop_) { | |
| 37 } | |
| 38 | |
| 39 protected: | |
| 40 // TODO(markusheintz): Check if it's possible to derive the provider class | |
| 41 // from NonThreadSafe and to use native thread identifiers instead of | |
| 42 // BrowserThread IDs. Then we could get rid of the message_loop and ui_thread | |
| 43 // fields. | |
| 44 base::MessageLoop message_loop_; | |
| 45 content::TestBrowserThread ui_thread_; | |
| 46 }; | |
| 47 | |
| 48 TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) { | |
| 49 TestingProfile profile; | 32 TestingProfile profile; |
| 50 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 33 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 51 PolicyProvider provider(prefs); | 34 PolicyProvider provider(prefs); |
| 52 | 35 |
| 53 Rules rules; | 36 Rules rules; |
| 54 | 37 |
| 55 scoped_ptr<RuleIterator> rule_iterator( | 38 scoped_ptr<RuleIterator> rule_iterator( |
| 56 provider.GetRuleIterator( | 39 provider.GetRuleIterator( |
| 57 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 40 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 58 std::string(), | 41 std::string(), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 72 Rule rule = rule_iterator->Next(); | 55 Rule rule = rule_iterator->Next(); |
| 73 EXPECT_FALSE(rule_iterator->HasNext()); | 56 EXPECT_FALSE(rule_iterator->HasNext()); |
| 74 | 57 |
| 75 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 58 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); |
| 76 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 59 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); |
| 77 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 60 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); |
| 78 | 61 |
| 79 provider.ShutdownOnUIThread(); | 62 provider.ShutdownOnUIThread(); |
| 80 } | 63 } |
| 81 | 64 |
| 82 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) { | 65 TEST(PolicyProviderTest, ManagedDefaultContentSettings) { |
| 83 TestingProfile profile; | 66 TestingProfile profile; |
| 84 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 67 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 85 PolicyProvider provider(prefs); | 68 PolicyProvider provider(prefs); |
| 86 | 69 |
| 87 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 70 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, |
| 88 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 71 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); |
| 89 | 72 |
| 90 scoped_ptr<RuleIterator> rule_iterator( | 73 scoped_ptr<RuleIterator> rule_iterator( |
| 91 provider.GetRuleIterator( | 74 provider.GetRuleIterator( |
| 92 CONTENT_SETTINGS_TYPE_PLUGINS, | 75 CONTENT_SETTINGS_TYPE_PLUGINS, |
| 93 std::string(), | 76 std::string(), |
| 94 false)); | 77 false)); |
| 95 EXPECT_TRUE(rule_iterator->HasNext()); | 78 EXPECT_TRUE(rule_iterator->HasNext()); |
| 96 Rule rule = rule_iterator->Next(); | 79 Rule rule = rule_iterator->Next(); |
| 97 EXPECT_FALSE(rule_iterator->HasNext()); | 80 EXPECT_FALSE(rule_iterator->HasNext()); |
| 98 | 81 |
| 99 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); | 82 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern); |
| 100 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); | 83 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern); |
| 101 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); | 84 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get())); |
| 102 | 85 |
| 103 provider.ShutdownOnUIThread(); | 86 provider.ShutdownOnUIThread(); |
| 104 } | 87 } |
| 105 | 88 |
| 106 // When a default-content-setting is set to a managed setting a | 89 // When a default-content-setting is set to a managed setting a |
| 107 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen | 90 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen |
| 108 // if the managed setting is removed. | 91 // if the managed setting is removed. |
| 109 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) { | 92 TEST(PolicyProviderTest, ObserveManagedSettingsChange) { |
| 110 TestingProfile profile; | 93 TestingProfile profile; |
| 111 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 94 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 112 PolicyProvider provider(prefs); | 95 PolicyProvider provider(prefs); |
| 113 | 96 |
| 114 MockObserver mock_observer; | 97 MockObserver mock_observer; |
| 115 EXPECT_CALL(mock_observer, | 98 EXPECT_CALL(mock_observer, |
| 116 OnContentSettingChanged(_, | 99 OnContentSettingChanged(_, |
| 117 _, | 100 _, |
| 118 CONTENT_SETTINGS_TYPE_DEFAULT, | 101 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 119 "")); | 102 "")); |
| 120 provider.AddObserver(&mock_observer); | 103 provider.AddObserver(&mock_observer); |
| 121 | 104 |
| 122 // Set the managed default-content-setting. | 105 // Set the managed default-content-setting. |
| 123 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, | 106 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting, |
| 124 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 107 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); |
| 125 ::testing::Mock::VerifyAndClearExpectations(&mock_observer); | 108 ::testing::Mock::VerifyAndClearExpectations(&mock_observer); |
| 126 EXPECT_CALL(mock_observer, | 109 EXPECT_CALL(mock_observer, |
| 127 OnContentSettingChanged(_, | 110 OnContentSettingChanged(_, |
| 128 _, | 111 _, |
| 129 CONTENT_SETTINGS_TYPE_DEFAULT, | 112 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 130 "")); | 113 "")); |
| 131 // Remove the managed default-content-setting. | 114 // Remove the managed default-content-setting. |
| 132 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); | 115 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting); |
| 133 provider.ShutdownOnUIThread(); | 116 provider.ShutdownOnUIThread(); |
| 134 } | 117 } |
| 135 | 118 |
| 136 TEST_F(PolicyProviderTest, GettingManagedContentSettings) { | 119 TEST(PolicyProviderTest, GettingManagedContentSettings) { |
| 137 TestingProfile profile; | 120 TestingProfile profile; |
| 138 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 121 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 139 | 122 |
| 140 base::ListValue* value = new base::ListValue(); | 123 base::ListValue* value = new base::ListValue(); |
| 141 value->Append(new base::StringValue("[*.]google.com")); | 124 value->Append(new base::StringValue("[*.]google.com")); |
| 142 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, | 125 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls, |
| 143 value); | 126 value); |
| 144 | 127 |
| 145 PolicyProvider provider(prefs); | 128 PolicyProvider provider(prefs); |
| 146 | 129 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 GetContentSetting(&provider, | 181 GetContentSetting(&provider, |
| 199 youtube_url, | 182 youtube_url, |
| 200 youtube_url, | 183 youtube_url, |
| 201 CONTENT_SETTINGS_TYPE_COOKIES, | 184 CONTENT_SETTINGS_TYPE_COOKIES, |
| 202 std::string(), | 185 std::string(), |
| 203 false)); | 186 false)); |
| 204 | 187 |
| 205 provider.ShutdownOnUIThread(); | 188 provider.ShutdownOnUIThread(); |
| 206 } | 189 } |
| 207 | 190 |
| 208 TEST_F(PolicyProviderTest, ResourceIdentifier) { | 191 TEST(PolicyProviderTest, ResourceIdentifier) { |
| 209 TestingProfile profile; | 192 TestingProfile profile; |
| 210 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 193 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 211 | 194 |
| 212 base::ListValue* value = new base::ListValue(); | 195 base::ListValue* value = new base::ListValue(); |
| 213 value->Append(new base::StringValue("[*.]google.com")); | 196 value->Append(new base::StringValue("[*.]google.com")); |
| 214 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, | 197 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls, |
| 215 value); | 198 value); |
| 216 | 199 |
| 217 PolicyProvider provider(prefs); | 200 PolicyProvider provider(prefs); |
| 218 | 201 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 235 false)); | 218 false)); |
| 236 | 219 |
| 237 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 220 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 238 GetContentSetting( | 221 GetContentSetting( |
| 239 &provider, google_url, google_url, | 222 &provider, google_url, google_url, |
| 240 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false)); | 223 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false)); |
| 241 | 224 |
| 242 provider.ShutdownOnUIThread(); | 225 provider.ShutdownOnUIThread(); |
| 243 } | 226 } |
| 244 | 227 |
| 245 TEST_F(PolicyProviderTest, AutoSelectCertificateList) { | 228 TEST(PolicyProviderTest, AutoSelectCertificateList) { |
| 246 TestingProfile profile; | 229 TestingProfile profile; |
| 247 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 230 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
| 248 | 231 |
| 249 PolicyProvider provider(prefs); | 232 PolicyProvider provider(prefs); |
| 250 GURL google_url("https://mail.google.com"); | 233 GURL google_url("https://mail.google.com"); |
| 251 // Tests the default setting for auto selecting certificates | 234 // Tests the default setting for auto selecting certificates |
| 252 EXPECT_EQ( | 235 EXPECT_EQ( |
| 253 NULL, | 236 NULL, |
| 254 GetContentSettingValue(&provider, | 237 GetContentSettingValue(&provider, |
| 255 google_url, | 238 google_url, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType()); | 270 ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType()); |
| 288 base::DictionaryValue* dict_value = | 271 base::DictionaryValue* dict_value = |
| 289 static_cast<base::DictionaryValue*>(cert_filter.get()); | 272 static_cast<base::DictionaryValue*>(cert_filter.get()); |
| 290 std::string actual_common_name; | 273 std::string actual_common_name; |
| 291 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); | 274 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name)); |
| 292 EXPECT_EQ("issuer name", actual_common_name); | 275 EXPECT_EQ("issuer name", actual_common_name); |
| 293 provider.ShutdownOnUIThread(); | 276 provider.ShutdownOnUIThread(); |
| 294 } | 277 } |
| 295 | 278 |
| 296 } // namespace content_settings | 279 } // namespace content_settings |
| OLD | NEW |