| OLD | NEW |
| 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/extensions/api/content_settings/content_settings_store.
h" | 5 #include "chrome/browser/extensions/api/content_settings/content_settings_store.
h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/content_settings/content_settings_rule.h" | 8 #include "chrome/browser/content_settings/content_settings_rule.h" |
| 9 #include "chrome/browser/content_settings/content_settings_utils.h" | 9 #include "chrome/browser/content_settings/content_settings_utils.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 scoped_refptr<ContentSettingsStore> store_; | 94 scoped_refptr<ContentSettingsStore> store_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 TEST_F(ContentSettingsStoreTest, RegisterUnregister) { | 97 TEST_F(ContentSettingsStoreTest, RegisterUnregister) { |
| 98 ::testing::StrictMock<MockContentSettingsStoreObserver> observer; | 98 ::testing::StrictMock<MockContentSettingsStoreObserver> observer; |
| 99 store()->AddObserver(&observer); | 99 store()->AddObserver(&observer); |
| 100 | 100 |
| 101 GURL url("http://www.youtube.com"); | 101 GURL url("http://www.youtube.com"); |
| 102 | 102 |
| 103 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 103 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 104 GetContentSettingFromStore( | 104 GetContentSettingFromStore(store(), |
| 105 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | 105 url, |
| 106 url, |
| 107 CONTENT_SETTINGS_TYPE_COOKIES, |
| 108 std::string(), |
| 109 false)); |
| 106 | 110 |
| 107 // Register first extension | 111 // Register first extension |
| 108 std::string ext_id("my_extension"); | 112 std::string ext_id("my_extension"); |
| 109 RegisterExtension(ext_id); | 113 RegisterExtension(ext_id); |
| 110 | 114 |
| 111 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 115 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 112 GetContentSettingFromStore( | 116 GetContentSettingFromStore(store(), |
| 113 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | 117 url, |
| 118 url, |
| 119 CONTENT_SETTINGS_TYPE_COOKIES, |
| 120 std::string(), |
| 121 false)); |
| 114 | 122 |
| 115 // Set setting | 123 // Set setting |
| 116 ContentSettingsPattern pattern = | 124 ContentSettingsPattern pattern = |
| 117 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | 125 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); |
| 118 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); | 126 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); |
| 119 store()->SetExtensionContentSetting( | 127 store()->SetExtensionContentSetting(ext_id, |
| 120 ext_id, | 128 pattern, |
| 121 pattern, | 129 pattern, |
| 122 pattern, | 130 CONTENT_SETTINGS_TYPE_COOKIES, |
| 123 CONTENT_SETTINGS_TYPE_COOKIES, | 131 std::string(), |
| 124 "", | 132 CONTENT_SETTING_ALLOW, |
| 125 CONTENT_SETTING_ALLOW, | 133 kExtensionPrefsScopeRegular); |
| 126 kExtensionPrefsScopeRegular); | |
| 127 Mock::VerifyAndClear(&observer); | 134 Mock::VerifyAndClear(&observer); |
| 128 | 135 |
| 129 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 136 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 130 GetContentSettingFromStore( | 137 GetContentSettingFromStore(store(), |
| 131 store(), | 138 url, |
| 132 url, | 139 url, |
| 133 url, | 140 CONTENT_SETTINGS_TYPE_COOKIES, |
| 134 CONTENT_SETTINGS_TYPE_COOKIES, | 141 std::string(), |
| 135 "", | 142 false)); |
| 136 false)); | |
| 137 | 143 |
| 138 // Register second extension. | 144 // Register second extension. |
| 139 std::string ext_id_2("my_second_extension"); | 145 std::string ext_id_2("my_second_extension"); |
| 140 RegisterExtension(ext_id_2); | 146 RegisterExtension(ext_id_2); |
| 141 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); | 147 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); |
| 142 store()->SetExtensionContentSetting( | 148 store()->SetExtensionContentSetting(ext_id_2, |
| 143 ext_id_2, | 149 pattern, |
| 144 pattern, | 150 pattern, |
| 145 pattern, | 151 CONTENT_SETTINGS_TYPE_COOKIES, |
| 146 CONTENT_SETTINGS_TYPE_COOKIES, | 152 std::string(), |
| 147 "", | 153 CONTENT_SETTING_BLOCK, |
| 148 CONTENT_SETTING_BLOCK, | 154 kExtensionPrefsScopeRegular); |
| 149 kExtensionPrefsScopeRegular); | |
| 150 | 155 |
| 151 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 156 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 152 GetContentSettingFromStore( | 157 GetContentSettingFromStore(store(), |
| 153 store(), | 158 url, |
| 154 url, | 159 url, |
| 155 url, | 160 CONTENT_SETTINGS_TYPE_COOKIES, |
| 156 CONTENT_SETTINGS_TYPE_COOKIES, | 161 std::string(), |
| 157 "", | 162 false)); |
| 158 false)); | |
| 159 | 163 |
| 160 // Unregister first extension. This shouldn't change the setting. | 164 // Unregister first extension. This shouldn't change the setting. |
| 161 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); | 165 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); |
| 162 store()->UnregisterExtension(ext_id); | 166 store()->UnregisterExtension(ext_id); |
| 163 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 167 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 164 GetContentSettingFromStore( | 168 GetContentSettingFromStore(store(), |
| 165 store(), | 169 url, |
| 166 url, | 170 url, |
| 167 url, | 171 CONTENT_SETTINGS_TYPE_COOKIES, |
| 168 CONTENT_SETTINGS_TYPE_COOKIES, | 172 std::string(), |
| 169 "", | 173 false)); |
| 170 false)); | |
| 171 Mock::VerifyAndClear(&observer); | 174 Mock::VerifyAndClear(&observer); |
| 172 | 175 |
| 173 // Unregister second extension. This should reset the setting to its default | 176 // Unregister second extension. This should reset the setting to its default |
| 174 // value. | 177 // value. |
| 175 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); | 178 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); |
| 176 store()->UnregisterExtension(ext_id_2); | 179 store()->UnregisterExtension(ext_id_2); |
| 177 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 180 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 178 GetContentSettingFromStore( | 181 GetContentSettingFromStore(store(), |
| 179 store(), | 182 url, |
| 180 url, | 183 url, |
| 181 url, | 184 CONTENT_SETTINGS_TYPE_COOKIES, |
| 182 CONTENT_SETTINGS_TYPE_COOKIES, | 185 std::string(), |
| 183 "", | 186 false)); |
| 184 false)); | |
| 185 | 187 |
| 186 store()->RemoveObserver(&observer); | 188 store()->RemoveObserver(&observer); |
| 187 } | 189 } |
| 188 | 190 |
| 189 TEST_F(ContentSettingsStoreTest, GetAllSettings) { | 191 TEST_F(ContentSettingsStoreTest, GetAllSettings) { |
| 190 bool incognito = false; | 192 bool incognito = false; |
| 191 std::vector<content_settings::Rule> rules; | 193 std::vector<content_settings::Rule> rules; |
| 192 GetSettingsForOneTypeFromStore( | 194 GetSettingsForOneTypeFromStore( |
| 193 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 195 store(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), incognito, &rules); |
| 194 ASSERT_EQ(0u, rules.size()); | 196 ASSERT_EQ(0u, rules.size()); |
| 195 | 197 |
| 196 // Register first extension. | 198 // Register first extension. |
| 197 std::string ext_id("my_extension"); | 199 std::string ext_id("my_extension"); |
| 198 RegisterExtension(ext_id); | 200 RegisterExtension(ext_id); |
| 199 ContentSettingsPattern pattern = | 201 ContentSettingsPattern pattern = |
| 200 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | 202 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); |
| 201 store()->SetExtensionContentSetting( | 203 store()->SetExtensionContentSetting(ext_id, |
| 202 ext_id, | 204 pattern, |
| 203 pattern, | 205 pattern, |
| 204 pattern, | 206 CONTENT_SETTINGS_TYPE_COOKIES, |
| 205 CONTENT_SETTINGS_TYPE_COOKIES, | 207 std::string(), |
| 206 "", | 208 CONTENT_SETTING_ALLOW, |
| 207 CONTENT_SETTING_ALLOW, | 209 kExtensionPrefsScopeRegular); |
| 208 kExtensionPrefsScopeRegular); | |
| 209 | 210 |
| 210 GetSettingsForOneTypeFromStore( | 211 GetSettingsForOneTypeFromStore( |
| 211 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 212 store(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), incognito, &rules); |
| 212 ASSERT_EQ(1u, rules.size()); | 213 ASSERT_EQ(1u, rules.size()); |
| 213 CheckRule(rules[0], pattern, pattern, CONTENT_SETTING_ALLOW); | 214 CheckRule(rules[0], pattern, pattern, CONTENT_SETTING_ALLOW); |
| 214 | 215 |
| 215 // Register second extension. | 216 // Register second extension. |
| 216 std::string ext_id_2("my_second_extension"); | 217 std::string ext_id_2("my_second_extension"); |
| 217 RegisterExtension(ext_id_2); | 218 RegisterExtension(ext_id_2); |
| 218 ContentSettingsPattern pattern_2 = | 219 ContentSettingsPattern pattern_2 = |
| 219 ContentSettingsPattern::FromURL(GURL("http://www.example.com")); | 220 ContentSettingsPattern::FromURL(GURL("http://www.example.com")); |
| 220 store()->SetExtensionContentSetting( | 221 store()->SetExtensionContentSetting(ext_id_2, |
| 221 ext_id_2, | 222 pattern_2, |
| 222 pattern_2, | 223 pattern_2, |
| 223 pattern_2, | 224 CONTENT_SETTINGS_TYPE_COOKIES, |
| 224 CONTENT_SETTINGS_TYPE_COOKIES, | 225 std::string(), |
| 225 "", | 226 CONTENT_SETTING_BLOCK, |
| 226 CONTENT_SETTING_BLOCK, | 227 kExtensionPrefsScopeRegular); |
| 227 kExtensionPrefsScopeRegular); | |
| 228 | 228 |
| 229 GetSettingsForOneTypeFromStore(store(), | 229 GetSettingsForOneTypeFromStore( |
| 230 CONTENT_SETTINGS_TYPE_COOKIES, | 230 store(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), incognito, &rules); |
| 231 "", | |
| 232 incognito, | |
| 233 &rules); | |
| 234 ASSERT_EQ(2u, rules.size()); | 231 ASSERT_EQ(2u, rules.size()); |
| 235 // Rules appear in the reverse installation order of the extensions. | 232 // Rules appear in the reverse installation order of the extensions. |
| 236 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); | 233 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); |
| 237 CheckRule(rules[1], pattern, pattern, CONTENT_SETTING_ALLOW); | 234 CheckRule(rules[1], pattern, pattern, CONTENT_SETTING_ALLOW); |
| 238 | 235 |
| 239 // Disable first extension. | 236 // Disable first extension. |
| 240 store()->SetExtensionState(ext_id, false); | 237 store()->SetExtensionState(ext_id, false); |
| 241 | 238 |
| 242 GetSettingsForOneTypeFromStore( | 239 GetSettingsForOneTypeFromStore( |
| 243 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 240 store(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), incognito, &rules); |
| 244 ASSERT_EQ(1u, rules.size()); | 241 ASSERT_EQ(1u, rules.size()); |
| 245 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); | 242 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); |
| 246 | 243 |
| 247 // Uninstall second extension. | 244 // Uninstall second extension. |
| 248 store()->UnregisterExtension(ext_id_2); | 245 store()->UnregisterExtension(ext_id_2); |
| 249 | 246 |
| 250 GetSettingsForOneTypeFromStore( | 247 GetSettingsForOneTypeFromStore( |
| 251 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 248 store(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), incognito, &rules); |
| 252 ASSERT_EQ(0u, rules.size()); | 249 ASSERT_EQ(0u, rules.size()); |
| 253 } | 250 } |
| 254 | 251 |
| 255 } // namespace extensions | 252 } // namespace extensions |
| OLD | NEW |