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/extensions/extension_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" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using ::testing::Mock; | 13 using ::testing::Mock; |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 void CheckRule(const content_settings::Rule& rule, | 17 void CheckRule(const content_settings::Rule& rule, |
18 const ContentSettingsPattern& primary_pattern, | 18 const ContentSettingsPattern& primary_pattern, |
19 const ContentSettingsPattern& secondary_pattern, | 19 const ContentSettingsPattern& secondary_pattern, |
20 ContentSetting setting) { | 20 ContentSetting setting) { |
21 EXPECT_EQ(primary_pattern.ToString(), rule.primary_pattern.ToString()); | 21 EXPECT_EQ(primary_pattern.ToString(), rule.primary_pattern.ToString()); |
22 EXPECT_EQ(secondary_pattern.ToString(), rule.secondary_pattern.ToString()); | 22 EXPECT_EQ(secondary_pattern.ToString(), rule.secondary_pattern.ToString()); |
23 EXPECT_EQ(setting, content_settings::ValueToContentSetting(rule.value.get())); | 23 EXPECT_EQ(setting, content_settings::ValueToContentSetting(rule.value.get())); |
24 } | 24 } |
25 | 25 |
| 26 namespace extensions { |
| 27 |
26 // Helper class which returns monotonically-increasing base::Time objects. | 28 // Helper class which returns monotonically-increasing base::Time objects. |
27 class FakeTimer { | 29 class FakeTimer { |
28 public: | 30 public: |
29 FakeTimer() : internal_(0) {} | 31 FakeTimer() : internal_(0) {} |
30 | 32 |
31 base::Time GetNext() { | 33 base::Time GetNext() { |
32 return base::Time::FromInternalValue(++internal_); | 34 return base::Time::FromInternalValue(++internal_); |
33 } | 35 } |
34 | 36 |
35 private: | 37 private: |
36 int64 internal_; | 38 int64 internal_; |
37 }; | 39 }; |
38 | 40 |
39 class MockExtensionContentSettingsStoreObserver | 41 class MockContentSettingsStoreObserver |
40 : public ExtensionContentSettingsStore::Observer { | 42 : public ContentSettingsStore::Observer { |
41 public: | 43 public: |
42 MOCK_METHOD2(OnContentSettingChanged, | 44 MOCK_METHOD2(OnContentSettingChanged, |
43 void(const std::string& extension_id, bool incognito)); | 45 void(const std::string& extension_id, bool incognito)); |
44 }; | 46 }; |
45 | 47 |
46 ContentSetting GetContentSettingFromStore( | 48 ContentSetting GetContentSettingFromStore( |
47 const ExtensionContentSettingsStore* store, | 49 const ContentSettingsStore* store, |
48 const GURL& primary_url, const GURL& secondary_url, | 50 const GURL& primary_url, const GURL& secondary_url, |
49 ContentSettingsType content_type, | 51 ContentSettingsType content_type, |
50 const std::string& resource_identifier, | 52 const std::string& resource_identifier, |
51 bool incognito) { | 53 bool incognito) { |
52 scoped_ptr<content_settings::RuleIterator> rule_iterator ( | 54 scoped_ptr<content_settings::RuleIterator> rule_iterator ( |
53 store->GetRuleIterator(content_type, resource_identifier, incognito)); | 55 store->GetRuleIterator(content_type, resource_identifier, incognito)); |
54 scoped_ptr<base::Value> setting( | 56 scoped_ptr<base::Value> setting( |
55 content_settings::GetContentSettingValueAndPatterns( | 57 content_settings::GetContentSettingValueAndPatterns( |
56 rule_iterator.get(), primary_url, secondary_url, NULL, NULL)); | 58 rule_iterator.get(), primary_url, secondary_url, NULL, NULL)); |
57 return content_settings::ValueToContentSetting(setting.get()); | 59 return content_settings::ValueToContentSetting(setting.get()); |
58 } | 60 } |
59 | 61 |
60 void GetSettingsForOneTypeFromStore( | 62 void GetSettingsForOneTypeFromStore( |
61 const ExtensionContentSettingsStore* store, | 63 const ContentSettingsStore* store, |
62 ContentSettingsType content_type, | 64 ContentSettingsType content_type, |
63 const std::string& resource_identifier, | 65 const std::string& resource_identifier, |
64 bool incognito, | 66 bool incognito, |
65 std::vector<content_settings::Rule>* rules) { | 67 std::vector<content_settings::Rule>* rules) { |
66 rules->clear(); | 68 rules->clear(); |
67 scoped_ptr<content_settings::RuleIterator> rule_iterator( | 69 scoped_ptr<content_settings::RuleIterator> rule_iterator( |
68 store->GetRuleIterator(content_type, resource_identifier, incognito)); | 70 store->GetRuleIterator(content_type, resource_identifier, incognito)); |
69 while (rule_iterator->HasNext()) | 71 while (rule_iterator->HasNext()) |
70 rules->push_back(rule_iterator->Next()); | 72 rules->push_back(rule_iterator->Next()); |
71 } | 73 } |
72 | 74 |
73 } // namespace | 75 } // namespace |
74 | 76 |
75 class ExtensionContentSettingsStoreTest : public ::testing::Test { | 77 class ContentSettingsStoreTest : public ::testing::Test { |
76 public: | 78 public: |
77 ExtensionContentSettingsStoreTest() : | 79 ContentSettingsStoreTest() : |
78 store_(new ExtensionContentSettingsStore()) { | 80 store_(new ContentSettingsStore()) { |
79 } | 81 } |
80 | 82 |
81 protected: | 83 protected: |
82 void RegisterExtension(const std::string& ext_id) { | 84 void RegisterExtension(const std::string& ext_id) { |
83 store_->RegisterExtension(ext_id, timer_.GetNext(), true); | 85 store_->RegisterExtension(ext_id, timer_.GetNext(), true); |
84 } | 86 } |
85 | 87 |
86 ExtensionContentSettingsStore* store() { | 88 ContentSettingsStore* store() { |
87 return store_.get(); | 89 return store_.get(); |
88 } | 90 } |
89 | 91 |
90 private: | 92 private: |
91 FakeTimer timer_; | 93 FakeTimer timer_; |
92 scoped_refptr<ExtensionContentSettingsStore> store_; | 94 scoped_refptr<ContentSettingsStore> store_; |
93 }; | 95 }; |
94 | 96 |
95 TEST_F(ExtensionContentSettingsStoreTest, RegisterUnregister) { | 97 TEST_F(ContentSettingsStoreTest, RegisterUnregister) { |
96 ::testing::StrictMock<MockExtensionContentSettingsStoreObserver> observer; | 98 ::testing::StrictMock<MockContentSettingsStoreObserver> observer; |
97 store()->AddObserver(&observer); | 99 store()->AddObserver(&observer); |
98 | 100 |
99 GURL url("http://www.youtube.com"); | 101 GURL url("http://www.youtube.com"); |
100 | 102 |
101 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 103 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
102 GetContentSettingFromStore( | 104 GetContentSettingFromStore( |
103 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | 105 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); |
104 | 106 |
105 // Register first extension | 107 // Register first extension |
106 std::string ext_id("my_extension"); | 108 std::string ext_id("my_extension"); |
107 RegisterExtension(ext_id); | 109 RegisterExtension(ext_id); |
108 | 110 |
109 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 111 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
110 GetContentSettingFromStore( | 112 GetContentSettingFromStore( |
111 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | 113 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); |
112 | 114 |
113 // Set setting | 115 // Set setting |
114 ContentSettingsPattern pattern = | 116 ContentSettingsPattern pattern = |
115 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | 117 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); |
116 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); | 118 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); |
117 store()->SetExtensionContentSetting( | 119 store()->SetContentSetting( |
118 ext_id, | 120 ext_id, |
119 pattern, | 121 pattern, |
120 pattern, | 122 pattern, |
121 CONTENT_SETTINGS_TYPE_COOKIES, | 123 CONTENT_SETTINGS_TYPE_COOKIES, |
122 "", | 124 "", |
123 CONTENT_SETTING_ALLOW, | 125 CONTENT_SETTING_ALLOW, |
124 kExtensionPrefsScopeRegular); | 126 kExtensionPrefsScopeRegular); |
125 Mock::VerifyAndClear(&observer); | 127 Mock::VerifyAndClear(&observer); |
126 | 128 |
127 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 129 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
128 GetContentSettingFromStore( | 130 GetContentSettingFromStore( |
129 store(), | 131 store(), |
130 url, | 132 url, |
131 url, | 133 url, |
132 CONTENT_SETTINGS_TYPE_COOKIES, | 134 CONTENT_SETTINGS_TYPE_COOKIES, |
133 "", | 135 "", |
134 false)); | 136 false)); |
135 | 137 |
136 // Register second extension. | 138 // Register second extension. |
137 std::string ext_id_2("my_second_extension"); | 139 std::string ext_id_2("my_second_extension"); |
138 RegisterExtension(ext_id_2); | 140 RegisterExtension(ext_id_2); |
139 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); | 141 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); |
140 store()->SetExtensionContentSetting( | 142 store()->SetContentSetting( |
141 ext_id_2, | 143 ext_id_2, |
142 pattern, | 144 pattern, |
143 pattern, | 145 pattern, |
144 CONTENT_SETTINGS_TYPE_COOKIES, | 146 CONTENT_SETTINGS_TYPE_COOKIES, |
145 "", | 147 "", |
146 CONTENT_SETTING_BLOCK, | 148 CONTENT_SETTING_BLOCK, |
147 kExtensionPrefsScopeRegular); | 149 kExtensionPrefsScopeRegular); |
148 | 150 |
149 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 151 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
150 GetContentSettingFromStore( | 152 GetContentSettingFromStore( |
(...skipping 26 matching lines...) Expand all Loading... |
177 store(), | 179 store(), |
178 url, | 180 url, |
179 url, | 181 url, |
180 CONTENT_SETTINGS_TYPE_COOKIES, | 182 CONTENT_SETTINGS_TYPE_COOKIES, |
181 "", | 183 "", |
182 false)); | 184 false)); |
183 | 185 |
184 store()->RemoveObserver(&observer); | 186 store()->RemoveObserver(&observer); |
185 } | 187 } |
186 | 188 |
187 TEST_F(ExtensionContentSettingsStoreTest, GetAllSettings) { | 189 TEST_F(ContentSettingsStoreTest, GetAllSettings) { |
188 bool incognito = false; | 190 bool incognito = false; |
189 std::vector<content_settings::Rule> rules; | 191 std::vector<content_settings::Rule> rules; |
190 GetSettingsForOneTypeFromStore( | 192 GetSettingsForOneTypeFromStore( |
191 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 193 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); |
192 ASSERT_EQ(0u, rules.size()); | 194 ASSERT_EQ(0u, rules.size()); |
193 | 195 |
194 // Register first extension. | 196 // Register first extension. |
195 std::string ext_id("my_extension"); | 197 std::string ext_id("my_extension"); |
196 RegisterExtension(ext_id); | 198 RegisterExtension(ext_id); |
197 ContentSettingsPattern pattern = | 199 ContentSettingsPattern pattern = |
198 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | 200 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); |
199 store()->SetExtensionContentSetting( | 201 store()->SetContentSetting( |
200 ext_id, | 202 ext_id, |
201 pattern, | 203 pattern, |
202 pattern, | 204 pattern, |
203 CONTENT_SETTINGS_TYPE_COOKIES, | 205 CONTENT_SETTINGS_TYPE_COOKIES, |
204 "", | 206 "", |
205 CONTENT_SETTING_ALLOW, | 207 CONTENT_SETTING_ALLOW, |
206 kExtensionPrefsScopeRegular); | 208 kExtensionPrefsScopeRegular); |
207 | 209 |
208 GetSettingsForOneTypeFromStore( | 210 GetSettingsForOneTypeFromStore( |
209 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 211 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); |
210 ASSERT_EQ(1u, rules.size()); | 212 ASSERT_EQ(1u, rules.size()); |
211 CheckRule(rules[0], pattern, pattern, CONTENT_SETTING_ALLOW); | 213 CheckRule(rules[0], pattern, pattern, CONTENT_SETTING_ALLOW); |
212 | 214 |
213 // Register second extension. | 215 // Register second extension. |
214 std::string ext_id_2("my_second_extension"); | 216 std::string ext_id_2("my_second_extension"); |
215 RegisterExtension(ext_id_2); | 217 RegisterExtension(ext_id_2); |
216 ContentSettingsPattern pattern_2 = | 218 ContentSettingsPattern pattern_2 = |
217 ContentSettingsPattern::FromURL(GURL("http://www.example.com")); | 219 ContentSettingsPattern::FromURL(GURL("http://www.example.com")); |
218 store()->SetExtensionContentSetting( | 220 store()->SetContentSetting( |
219 ext_id_2, | 221 ext_id_2, |
220 pattern_2, | 222 pattern_2, |
221 pattern_2, | 223 pattern_2, |
222 CONTENT_SETTINGS_TYPE_COOKIES, | 224 CONTENT_SETTINGS_TYPE_COOKIES, |
223 "", | 225 "", |
224 CONTENT_SETTING_BLOCK, | 226 CONTENT_SETTING_BLOCK, |
225 kExtensionPrefsScopeRegular); | 227 kExtensionPrefsScopeRegular); |
226 | 228 |
227 GetSettingsForOneTypeFromStore(store(), | 229 GetSettingsForOneTypeFromStore(store(), |
228 CONTENT_SETTINGS_TYPE_COOKIES, | 230 CONTENT_SETTINGS_TYPE_COOKIES, |
(...skipping 13 matching lines...) Expand all Loading... |
242 ASSERT_EQ(1u, rules.size()); | 244 ASSERT_EQ(1u, rules.size()); |
243 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); | 245 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); |
244 | 246 |
245 // Uninstall second extension. | 247 // Uninstall second extension. |
246 store()->UnregisterExtension(ext_id_2); | 248 store()->UnregisterExtension(ext_id_2); |
247 | 249 |
248 GetSettingsForOneTypeFromStore( | 250 GetSettingsForOneTypeFromStore( |
249 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | 251 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); |
250 ASSERT_EQ(0u, rules.size()); | 252 ASSERT_EQ(0u, rules.size()); |
251 } | 253 } |
| 254 |
| 255 } // namespace extensions |
OLD | NEW |