OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/extension_content_settings_store.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/content_settings/content_settings_rule.h" | |
9 #include "chrome/browser/content_settings/content_settings_utils.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 using ::testing::Mock; | |
14 | |
15 namespace { | |
16 | |
17 void CheckRule(const content_settings::Rule& rule, | |
18 const ContentSettingsPattern& primary_pattern, | |
19 const ContentSettingsPattern& secondary_pattern, | |
20 ContentSetting setting) { | |
21 EXPECT_EQ(primary_pattern.ToString(), rule.primary_pattern.ToString()); | |
22 EXPECT_EQ(secondary_pattern.ToString(), rule.secondary_pattern.ToString()); | |
23 EXPECT_EQ(setting, content_settings::ValueToContentSetting(rule.value.get())); | |
24 } | |
25 | |
26 // Helper class which returns monotonically-increasing base::Time objects. | |
27 class FakeTimer { | |
28 public: | |
29 FakeTimer() : internal_(0) {} | |
30 | |
31 base::Time GetNext() { | |
32 return base::Time::FromInternalValue(++internal_); | |
33 } | |
34 | |
35 private: | |
36 int64 internal_; | |
37 }; | |
38 | |
39 class MockExtensionContentSettingsStoreObserver | |
40 : public ExtensionContentSettingsStore::Observer { | |
41 public: | |
42 MOCK_METHOD2(OnContentSettingChanged, | |
43 void(const std::string& extension_id, bool incognito)); | |
44 }; | |
45 | |
46 ContentSetting GetContentSettingFromStore( | |
47 const ExtensionContentSettingsStore* store, | |
48 const GURL& primary_url, const GURL& secondary_url, | |
49 ContentSettingsType content_type, | |
50 const std::string& resource_identifier, | |
51 bool incognito) { | |
52 scoped_ptr<content_settings::RuleIterator> rule_iterator ( | |
53 store->GetRuleIterator(content_type, resource_identifier, incognito)); | |
54 scoped_ptr<base::Value> setting( | |
55 content_settings::GetContentSettingValueAndPatterns( | |
56 rule_iterator.get(), primary_url, secondary_url, NULL, NULL)); | |
57 return content_settings::ValueToContentSetting(setting.get()); | |
58 } | |
59 | |
60 void GetSettingsForOneTypeFromStore( | |
61 const ExtensionContentSettingsStore* store, | |
62 ContentSettingsType content_type, | |
63 const std::string& resource_identifier, | |
64 bool incognito, | |
65 std::vector<content_settings::Rule>* rules) { | |
66 rules->clear(); | |
67 scoped_ptr<content_settings::RuleIterator> rule_iterator( | |
68 store->GetRuleIterator(content_type, resource_identifier, incognito)); | |
69 while (rule_iterator->HasNext()) | |
70 rules->push_back(rule_iterator->Next()); | |
71 } | |
72 | |
73 } // namespace | |
74 | |
75 class ExtensionContentSettingsStoreTest : public ::testing::Test { | |
76 public: | |
77 ExtensionContentSettingsStoreTest() : | |
78 store_(new ExtensionContentSettingsStore()) { | |
79 } | |
80 | |
81 protected: | |
82 void RegisterExtension(const std::string& ext_id) { | |
83 store_->RegisterExtension(ext_id, timer_.GetNext(), true); | |
84 } | |
85 | |
86 ExtensionContentSettingsStore* store() { | |
87 return store_.get(); | |
88 } | |
89 | |
90 private: | |
91 FakeTimer timer_; | |
92 scoped_refptr<ExtensionContentSettingsStore> store_; | |
93 }; | |
94 | |
95 TEST_F(ExtensionContentSettingsStoreTest, RegisterUnregister) { | |
96 ::testing::StrictMock<MockExtensionContentSettingsStoreObserver> observer; | |
97 store()->AddObserver(&observer); | |
98 | |
99 GURL url("http://www.youtube.com"); | |
100 | |
101 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
102 GetContentSettingFromStore( | |
103 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | |
104 | |
105 // Register first extension | |
106 std::string ext_id("my_extension"); | |
107 RegisterExtension(ext_id); | |
108 | |
109 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
110 GetContentSettingFromStore( | |
111 store(), url, url, CONTENT_SETTINGS_TYPE_COOKIES, "", false)); | |
112 | |
113 // Set setting | |
114 ContentSettingsPattern pattern = | |
115 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | |
116 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); | |
117 store()->SetExtensionContentSetting( | |
118 ext_id, | |
119 pattern, | |
120 pattern, | |
121 CONTENT_SETTINGS_TYPE_COOKIES, | |
122 "", | |
123 CONTENT_SETTING_ALLOW, | |
124 kExtensionPrefsScopeRegular); | |
125 Mock::VerifyAndClear(&observer); | |
126 | |
127 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
128 GetContentSettingFromStore( | |
129 store(), | |
130 url, | |
131 url, | |
132 CONTENT_SETTINGS_TYPE_COOKIES, | |
133 "", | |
134 false)); | |
135 | |
136 // Register second extension. | |
137 std::string ext_id_2("my_second_extension"); | |
138 RegisterExtension(ext_id_2); | |
139 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); | |
140 store()->SetExtensionContentSetting( | |
141 ext_id_2, | |
142 pattern, | |
143 pattern, | |
144 CONTENT_SETTINGS_TYPE_COOKIES, | |
145 "", | |
146 CONTENT_SETTING_BLOCK, | |
147 kExtensionPrefsScopeRegular); | |
148 | |
149 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
150 GetContentSettingFromStore( | |
151 store(), | |
152 url, | |
153 url, | |
154 CONTENT_SETTINGS_TYPE_COOKIES, | |
155 "", | |
156 false)); | |
157 | |
158 // Unregister first extension. This shouldn't change the setting. | |
159 EXPECT_CALL(observer, OnContentSettingChanged(ext_id, false)); | |
160 store()->UnregisterExtension(ext_id); | |
161 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
162 GetContentSettingFromStore( | |
163 store(), | |
164 url, | |
165 url, | |
166 CONTENT_SETTINGS_TYPE_COOKIES, | |
167 "", | |
168 false)); | |
169 Mock::VerifyAndClear(&observer); | |
170 | |
171 // Unregister second extension. This should reset the setting to its default | |
172 // value. | |
173 EXPECT_CALL(observer, OnContentSettingChanged(ext_id_2, false)); | |
174 store()->UnregisterExtension(ext_id_2); | |
175 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
176 GetContentSettingFromStore( | |
177 store(), | |
178 url, | |
179 url, | |
180 CONTENT_SETTINGS_TYPE_COOKIES, | |
181 "", | |
182 false)); | |
183 | |
184 store()->RemoveObserver(&observer); | |
185 } | |
186 | |
187 TEST_F(ExtensionContentSettingsStoreTest, GetAllSettings) { | |
188 bool incognito = false; | |
189 std::vector<content_settings::Rule> rules; | |
190 GetSettingsForOneTypeFromStore( | |
191 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | |
192 ASSERT_EQ(0u, rules.size()); | |
193 | |
194 // Register first extension. | |
195 std::string ext_id("my_extension"); | |
196 RegisterExtension(ext_id); | |
197 ContentSettingsPattern pattern = | |
198 ContentSettingsPattern::FromURL(GURL("http://www.youtube.com")); | |
199 store()->SetExtensionContentSetting( | |
200 ext_id, | |
201 pattern, | |
202 pattern, | |
203 CONTENT_SETTINGS_TYPE_COOKIES, | |
204 "", | |
205 CONTENT_SETTING_ALLOW, | |
206 kExtensionPrefsScopeRegular); | |
207 | |
208 GetSettingsForOneTypeFromStore( | |
209 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | |
210 ASSERT_EQ(1u, rules.size()); | |
211 CheckRule(rules[0], pattern, pattern, CONTENT_SETTING_ALLOW); | |
212 | |
213 // Register second extension. | |
214 std::string ext_id_2("my_second_extension"); | |
215 RegisterExtension(ext_id_2); | |
216 ContentSettingsPattern pattern_2 = | |
217 ContentSettingsPattern::FromURL(GURL("http://www.example.com")); | |
218 store()->SetExtensionContentSetting( | |
219 ext_id_2, | |
220 pattern_2, | |
221 pattern_2, | |
222 CONTENT_SETTINGS_TYPE_COOKIES, | |
223 "", | |
224 CONTENT_SETTING_BLOCK, | |
225 kExtensionPrefsScopeRegular); | |
226 | |
227 GetSettingsForOneTypeFromStore(store(), | |
228 CONTENT_SETTINGS_TYPE_COOKIES, | |
229 "", | |
230 incognito, | |
231 &rules); | |
232 ASSERT_EQ(2u, rules.size()); | |
233 // Rules appear in the reverse installation order of the extensions. | |
234 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); | |
235 CheckRule(rules[1], pattern, pattern, CONTENT_SETTING_ALLOW); | |
236 | |
237 // Disable first extension. | |
238 store()->SetExtensionState(ext_id, false); | |
239 | |
240 GetSettingsForOneTypeFromStore( | |
241 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | |
242 ASSERT_EQ(1u, rules.size()); | |
243 CheckRule(rules[0], pattern_2, pattern_2, CONTENT_SETTING_BLOCK); | |
244 | |
245 // Uninstall second extension. | |
246 store()->UnregisterExtension(ext_id_2); | |
247 | |
248 GetSettingsForOneTypeFromStore( | |
249 store(), CONTENT_SETTINGS_TYPE_COOKIES, "", incognito, &rules); | |
250 ASSERT_EQ(0u, rules.size()); | |
251 } | |
OLD | NEW |