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 extensions { |
| 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 void CheckRule(const content_settings::Rule& rule, | 19 void CheckRule(const content_settings::Rule& rule, |
18 const ContentSettingsPattern& primary_pattern, | 20 const ContentSettingsPattern& primary_pattern, |
19 const ContentSettingsPattern& secondary_pattern, | 21 const ContentSettingsPattern& secondary_pattern, |
20 ContentSetting setting) { | 22 ContentSetting setting) { |
21 EXPECT_EQ(primary_pattern.ToString(), rule.primary_pattern.ToString()); | 23 EXPECT_EQ(primary_pattern.ToString(), rule.primary_pattern.ToString()); |
22 EXPECT_EQ(secondary_pattern.ToString(), rule.secondary_pattern.ToString()); | 24 EXPECT_EQ(secondary_pattern.ToString(), rule.secondary_pattern.ToString()); |
23 EXPECT_EQ(setting, content_settings::ValueToContentSetting(rule.value.get())); | 25 EXPECT_EQ(setting, content_settings::ValueToContentSetting(rule.value.get())); |
24 } | 26 } |
25 | 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"); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 = |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |