OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/browser/api/declarative/rules_registry.h" | 5 #include "extensions/browser/api/declarative/rules_registry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 TEST(RulesRegistryTest, FillOptionalIdentifiers) { | 29 TEST(RulesRegistryTest, FillOptionalIdentifiers) { |
30 base::MessageLoopForUI message_loop; | 30 base::MessageLoopForUI message_loop; |
31 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 31 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
32 | 32 |
33 std::string error; | 33 std::string error; |
34 scoped_refptr<RulesRegistry> registry = | 34 scoped_refptr<RulesRegistry> registry = |
35 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 35 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
36 | 36 |
37 // Add rules and check that their identifiers are filled and unique. | 37 // Add rules and check that their identifiers are filled and unique. |
38 | 38 |
39 std::vector<linked_ptr<core_api::events::Rule>> add_rules; | 39 std::vector<linked_ptr<api::events::Rule>> add_rules; |
40 add_rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 40 add_rules.push_back(make_linked_ptr(new api::events::Rule)); |
41 add_rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 41 add_rules.push_back(make_linked_ptr(new api::events::Rule)); |
42 error = registry->AddRules(kExtensionId, add_rules); | 42 error = registry->AddRules(kExtensionId, add_rules); |
43 EXPECT_TRUE(error.empty()) << error; | 43 EXPECT_TRUE(error.empty()) << error; |
44 | 44 |
45 std::vector<linked_ptr<core_api::events::Rule>> get_rules; | 45 std::vector<linked_ptr<api::events::Rule>> get_rules; |
46 registry->GetAllRules(kExtensionId, &get_rules); | 46 registry->GetAllRules(kExtensionId, &get_rules); |
47 | 47 |
48 ASSERT_EQ(2u, get_rules.size()); | 48 ASSERT_EQ(2u, get_rules.size()); |
49 | 49 |
50 ASSERT_TRUE(get_rules[0]->id.get()); | 50 ASSERT_TRUE(get_rules[0]->id.get()); |
51 EXPECT_NE("", *get_rules[0]->id); | 51 EXPECT_NE("", *get_rules[0]->id); |
52 | 52 |
53 ASSERT_TRUE(get_rules[1]->id.get()); | 53 ASSERT_TRUE(get_rules[1]->id.get()); |
54 EXPECT_NE("", *get_rules[1]->id); | 54 EXPECT_NE("", *get_rules[1]->id); |
55 | 55 |
56 EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id); | 56 EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id); |
57 | 57 |
58 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 58 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
59 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 59 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
60 | 60 |
61 // Check that we cannot add a new rule with the same ID. | 61 // Check that we cannot add a new rule with the same ID. |
62 | 62 |
63 std::vector<linked_ptr<core_api::events::Rule>> add_rules_2; | 63 std::vector<linked_ptr<api::events::Rule>> add_rules_2; |
64 add_rules_2.push_back(make_linked_ptr(new core_api::events::Rule)); | 64 add_rules_2.push_back(make_linked_ptr(new api::events::Rule)); |
65 add_rules_2[0]->id.reset(new std::string(*get_rules[0]->id)); | 65 add_rules_2[0]->id.reset(new std::string(*get_rules[0]->id)); |
66 error = registry->AddRules(kExtensionId, add_rules_2); | 66 error = registry->AddRules(kExtensionId, add_rules_2); |
67 EXPECT_FALSE(error.empty()); | 67 EXPECT_FALSE(error.empty()); |
68 | 68 |
69 std::vector<linked_ptr<core_api::events::Rule>> get_rules_2; | 69 std::vector<linked_ptr<api::events::Rule>> get_rules_2; |
70 registry->GetAllRules(kExtensionId, &get_rules_2); | 70 registry->GetAllRules(kExtensionId, &get_rules_2); |
71 ASSERT_EQ(2u, get_rules_2.size()); | 71 ASSERT_EQ(2u, get_rules_2.size()); |
72 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 72 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
73 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 73 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
74 | 74 |
75 // Check that we can register the old rule IDs once they were unregistered. | 75 // Check that we can register the old rule IDs once they were unregistered. |
76 | 76 |
77 std::vector<std::string> remove_rules_3; | 77 std::vector<std::string> remove_rules_3; |
78 remove_rules_3.push_back(*get_rules[0]->id); | 78 remove_rules_3.push_back(*get_rules[0]->id); |
79 error = registry->RemoveRules(kExtensionId, remove_rules_3); | 79 error = registry->RemoveRules(kExtensionId, remove_rules_3); |
80 EXPECT_TRUE(error.empty()) << error; | 80 EXPECT_TRUE(error.empty()) << error; |
81 | 81 |
82 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, | 82 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, |
83 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 83 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
84 | 84 |
85 std::vector<linked_ptr<core_api::events::Rule>> get_rules_3a; | 85 std::vector<linked_ptr<api::events::Rule>> get_rules_3a; |
86 registry->GetAllRules(kExtensionId, &get_rules_3a); | 86 registry->GetAllRules(kExtensionId, &get_rules_3a); |
87 ASSERT_EQ(1u, get_rules_3a.size()); | 87 ASSERT_EQ(1u, get_rules_3a.size()); |
88 | 88 |
89 std::vector<linked_ptr<core_api::events::Rule>> add_rules_3; | 89 std::vector<linked_ptr<api::events::Rule>> add_rules_3; |
90 add_rules_3.push_back(make_linked_ptr(new core_api::events::Rule)); | 90 add_rules_3.push_back(make_linked_ptr(new api::events::Rule)); |
91 add_rules_3[0]->id.reset(new std::string(*get_rules[0]->id)); | 91 add_rules_3[0]->id.reset(new std::string(*get_rules[0]->id)); |
92 error = registry->AddRules(kExtensionId, add_rules_3); | 92 error = registry->AddRules(kExtensionId, add_rules_3); |
93 EXPECT_TRUE(error.empty()) << error; | 93 EXPECT_TRUE(error.empty()) << error; |
94 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 94 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
95 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 95 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
96 | 96 |
97 std::vector<linked_ptr<core_api::events::Rule>> get_rules_3b; | 97 std::vector<linked_ptr<api::events::Rule>> get_rules_3b; |
98 registry->GetAllRules(kExtensionId, &get_rules_3b); | 98 registry->GetAllRules(kExtensionId, &get_rules_3b); |
99 ASSERT_EQ(2u, get_rules_3b.size()); | 99 ASSERT_EQ(2u, get_rules_3b.size()); |
100 | 100 |
101 // Check that we can register a rule with an ID that is not modified. | 101 // Check that we can register a rule with an ID that is not modified. |
102 | 102 |
103 error = registry->RemoveAllRules(kExtensionId); | 103 error = registry->RemoveAllRules(kExtensionId); |
104 EXPECT_TRUE(error.empty()) << error; | 104 EXPECT_TRUE(error.empty()) << error; |
105 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, | 105 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, |
106 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 106 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
107 | 107 |
108 std::vector<linked_ptr<core_api::events::Rule>> get_rules_4a; | 108 std::vector<linked_ptr<api::events::Rule>> get_rules_4a; |
109 registry->GetAllRules(kExtensionId, &get_rules_4a); | 109 registry->GetAllRules(kExtensionId, &get_rules_4a); |
110 ASSERT_TRUE(get_rules_4a.empty()); | 110 ASSERT_TRUE(get_rules_4a.empty()); |
111 | 111 |
112 std::vector<linked_ptr<core_api::events::Rule>> add_rules_4; | 112 std::vector<linked_ptr<api::events::Rule>> add_rules_4; |
113 add_rules_4.push_back(make_linked_ptr(new core_api::events::Rule)); | 113 add_rules_4.push_back(make_linked_ptr(new api::events::Rule)); |
114 add_rules_4[0]->id.reset(new std::string(kRuleId)); | 114 add_rules_4[0]->id.reset(new std::string(kRuleId)); |
115 error = registry->AddRules(kExtensionId, add_rules_4); | 115 error = registry->AddRules(kExtensionId, add_rules_4); |
116 EXPECT_TRUE(error.empty()) << error; | 116 EXPECT_TRUE(error.empty()) << error; |
117 | 117 |
118 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, | 118 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, |
119 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 119 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
120 | 120 |
121 std::vector<linked_ptr<core_api::events::Rule>> get_rules_4b; | 121 std::vector<linked_ptr<api::events::Rule>> get_rules_4b; |
122 registry->GetAllRules(kExtensionId, &get_rules_4b); | 122 registry->GetAllRules(kExtensionId, &get_rules_4b); |
123 | 123 |
124 ASSERT_EQ(1u, get_rules_4b.size()); | 124 ASSERT_EQ(1u, get_rules_4b.size()); |
125 | 125 |
126 ASSERT_TRUE(get_rules_4b[0]->id.get()); | 126 ASSERT_TRUE(get_rules_4b[0]->id.get()); |
127 EXPECT_EQ(kRuleId, *get_rules_4b[0]->id); | 127 EXPECT_EQ(kRuleId, *get_rules_4b[0]->id); |
128 | 128 |
129 // Create extension | 129 // Create extension |
130 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | 130 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( |
131 "{" | 131 "{" |
(...skipping 16 matching lines...) Expand all Loading... |
148 TEST(RulesRegistryTest, FillOptionalPriority) { | 148 TEST(RulesRegistryTest, FillOptionalPriority) { |
149 base::MessageLoopForUI message_loop; | 149 base::MessageLoopForUI message_loop; |
150 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 150 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
151 | 151 |
152 std::string error; | 152 std::string error; |
153 scoped_refptr<RulesRegistry> registry = | 153 scoped_refptr<RulesRegistry> registry = |
154 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 154 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
155 | 155 |
156 // Add rules and check that their priorities are filled if they are empty. | 156 // Add rules and check that their priorities are filled if they are empty. |
157 | 157 |
158 std::vector<linked_ptr<core_api::events::Rule>> add_rules; | 158 std::vector<linked_ptr<api::events::Rule>> add_rules; |
159 add_rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 159 add_rules.push_back(make_linked_ptr(new api::events::Rule)); |
160 add_rules[0]->priority.reset(new int(2)); | 160 add_rules[0]->priority.reset(new int(2)); |
161 add_rules.push_back(make_linked_ptr(new core_api::events::Rule)); | 161 add_rules.push_back(make_linked_ptr(new api::events::Rule)); |
162 error = registry->AddRules(kExtensionId, add_rules); | 162 error = registry->AddRules(kExtensionId, add_rules); |
163 EXPECT_TRUE(error.empty()) << error; | 163 EXPECT_TRUE(error.empty()) << error; |
164 | 164 |
165 std::vector<linked_ptr<core_api::events::Rule>> get_rules; | 165 std::vector<linked_ptr<api::events::Rule>> get_rules; |
166 registry->GetAllRules(kExtensionId, &get_rules); | 166 registry->GetAllRules(kExtensionId, &get_rules); |
167 | 167 |
168 ASSERT_EQ(2u, get_rules.size()); | 168 ASSERT_EQ(2u, get_rules.size()); |
169 | 169 |
170 ASSERT_TRUE(get_rules[0]->priority.get()); | 170 ASSERT_TRUE(get_rules[0]->priority.get()); |
171 ASSERT_TRUE(get_rules[1]->priority.get()); | 171 ASSERT_TRUE(get_rules[1]->priority.get()); |
172 | 172 |
173 // Verify the precondition so that the following EXPECT_EQ statements work. | 173 // Verify the precondition so that the following EXPECT_EQ statements work. |
174 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); | 174 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); |
175 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); | 175 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 scoped_refptr<Extension> extension = ExtensionBuilder() | 220 scoped_refptr<Extension> extension = ExtensionBuilder() |
221 .SetManifest(manifest.Pass()) | 221 .SetManifest(manifest.Pass()) |
222 .SetID(kExtensionId) | 222 .SetID(kExtensionId) |
223 .Build(); | 223 .Build(); |
224 | 224 |
225 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | 225 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
226 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | 226 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); |
227 // Simulate what RulesRegistryService would do on extension load. | 227 // Simulate what RulesRegistryService would do on extension load. |
228 registry->OnExtensionLoaded(extension.get()); | 228 registry->OnExtensionLoaded(extension.get()); |
229 | 229 |
230 std::vector<linked_ptr<core_api::events::Rule>> get_rules; | 230 std::vector<linked_ptr<api::events::Rule>> get_rules; |
231 registry->GetAllRules(kExtensionId, &get_rules); | 231 registry->GetAllRules(kExtensionId, &get_rules); |
232 | 232 |
233 ASSERT_EQ(2u, get_rules.size()); | 233 ASSERT_EQ(2u, get_rules.size()); |
234 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | 234 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( |
235 "{" | 235 "{" |
236 " \"id\": \"000\"," | 236 " \"id\": \"000\"," |
237 " \"priority\": 200," | 237 " \"priority\": 200," |
238 " \"tags\": [\"tagged\"]," | 238 " \"tags\": [\"tagged\"]," |
239 " \"actions\": [{" | 239 " \"actions\": [{" |
240 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | 240 " \"instanceType\": \"declarativeContent.ShowPageAction\"" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 scoped_refptr<Extension> extension = ExtensionBuilder() | 287 scoped_refptr<Extension> extension = ExtensionBuilder() |
288 .SetManifest(manifest.Pass()) | 288 .SetManifest(manifest.Pass()) |
289 .SetID(kExtensionId) | 289 .SetID(kExtensionId) |
290 .Build(); | 290 .Build(); |
291 | 291 |
292 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | 292 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
293 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | 293 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); |
294 // Simulate what RulesRegistryService would do on extension load. | 294 // Simulate what RulesRegistryService would do on extension load. |
295 registry->OnExtensionLoaded(extension.get()); | 295 registry->OnExtensionLoaded(extension.get()); |
296 // Add some extra rules outside of the manifest. | 296 // Add some extra rules outside of the manifest. |
297 std::vector<linked_ptr<core_api::events::Rule>> add_rules; | 297 std::vector<linked_ptr<api::events::Rule>> add_rules; |
298 linked_ptr<core_api::events::Rule> rule_1 = | 298 linked_ptr<api::events::Rule> rule_1 = make_linked_ptr(new api::events::Rule); |
299 make_linked_ptr(new core_api::events::Rule); | |
300 rule_1->id.reset(new std::string("rule_1")); | 299 rule_1->id.reset(new std::string("rule_1")); |
301 linked_ptr<core_api::events::Rule> rule_2 = | 300 linked_ptr<api::events::Rule> rule_2 = make_linked_ptr(new api::events::Rule); |
302 make_linked_ptr(new core_api::events::Rule); | |
303 rule_2->id.reset(new std::string("rule_2")); | 301 rule_2->id.reset(new std::string("rule_2")); |
304 add_rules.push_back(rule_1); | 302 add_rules.push_back(rule_1); |
305 add_rules.push_back(rule_2); | 303 add_rules.push_back(rule_2); |
306 registry->AddRules(kExtensionId, add_rules); | 304 registry->AddRules(kExtensionId, add_rules); |
307 | 305 |
308 std::vector<linked_ptr<core_api::events::Rule>> get_rules; | 306 std::vector<linked_ptr<api::events::Rule>> get_rules; |
309 registry->GetAllRules(kExtensionId, &get_rules); | 307 registry->GetAllRules(kExtensionId, &get_rules); |
310 ASSERT_EQ(3u, get_rules.size()); | 308 ASSERT_EQ(3u, get_rules.size()); |
311 EXPECT_EQ("manifest_rule_0", *(get_rules[0]->id)); | 309 EXPECT_EQ("manifest_rule_0", *(get_rules[0]->id)); |
312 EXPECT_EQ("rule_1", *(get_rules[1]->id)); | 310 EXPECT_EQ("rule_1", *(get_rules[1]->id)); |
313 EXPECT_EQ("rule_2", *(get_rules[2]->id)); | 311 EXPECT_EQ("rule_2", *(get_rules[2]->id)); |
314 | 312 |
315 // Remove a rule from outside the manifest. | 313 // Remove a rule from outside the manifest. |
316 std::vector<std::string> remove_ids; | 314 std::vector<std::string> remove_ids; |
317 remove_ids.push_back("rule_1"); | 315 remove_ids.push_back("rule_1"); |
318 EXPECT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | 316 EXPECT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); |
(...skipping 15 matching lines...) Expand all Loading... |
334 | 332 |
335 // Remove all rules. | 333 // Remove all rules. |
336 registry->RemoveAllRules(kExtensionId); | 334 registry->RemoveAllRules(kExtensionId); |
337 get_rules.clear(); | 335 get_rules.clear(); |
338 registry->GetAllRules(kExtensionId, &get_rules); | 336 registry->GetAllRules(kExtensionId, &get_rules); |
339 ASSERT_EQ(1u, get_rules.size()); | 337 ASSERT_EQ(1u, get_rules.size()); |
340 EXPECT_EQ("manifest_rule_0", *(get_rules[0]->id)); | 338 EXPECT_EQ("manifest_rule_0", *(get_rules[0]->id)); |
341 } | 339 } |
342 | 340 |
343 } // namespace extensions | 341 } // namespace extensions |
OLD | NEW |