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 "content/public/test/test_browser_thread.h" | 11 #include "content/public/test/test_browser_thread.h" |
11 #include "extensions/browser/api/declarative/rules_registry_service.h" | 12 #include "extensions/browser/api/declarative/rules_registry_service.h" |
12 #include "extensions/browser/api/declarative/test_rules_registry.h" | 13 #include "extensions/browser/api/declarative/test_rules_registry.h" |
14 #include "extensions/browser/api_test_utils.h" | |
15 #include "extensions/browser/extension_registry.h" | |
16 #include "extensions/browser/extensions_test.h" | |
17 #include "extensions/common/extension.h" | |
18 #include "extensions/common/extension_builder.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
14 | 20 |
15 namespace { | 21 namespace { |
16 const char kExtensionId[] = "foobar"; | 22 const char kExtensionId[] = "foobar"; |
17 const char kRuleId[] = "foo"; | 23 const char kRuleId[] = "foo"; |
18 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; | 24 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; |
19 } // namespace | 25 } // namespace |
20 | 26 |
21 namespace extensions { | 27 namespace extensions { |
22 | 28 |
23 TEST(RulesRegistryTest, FillOptionalIdentifiers) { | 29 using api_test_utils::ParseDictionary; |
30 using RulesRegistryTest = ExtensionsTest; | |
31 | |
32 TEST_F(RulesRegistryTest, FillOptionalIdentifiers) { | |
24 base::MessageLoopForUI message_loop; | 33 base::MessageLoopForUI message_loop; |
25 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 34 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
26 | 35 |
27 std::string error; | 36 std::string error; |
28 scoped_refptr<RulesRegistry> registry = | 37 scoped_refptr<RulesRegistry> registry = |
29 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 38 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
30 | 39 |
31 // Add rules and check that their identifiers are filled and unique. | 40 // Add rules and check that their identifiers are filled and unique. |
32 | 41 |
33 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 42 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 131 |
123 registry->OnExtensionUninstalled(kExtensionId); | 132 registry->OnExtensionUninstalled(kExtensionId); |
124 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, | 133 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, |
125 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 134 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
126 | 135 |
127 // Make sure that deletion traits of registry are executed. | 136 // Make sure that deletion traits of registry are executed. |
128 registry = NULL; | 137 registry = NULL; |
129 message_loop.RunUntilIdle(); | 138 message_loop.RunUntilIdle(); |
130 } | 139 } |
131 | 140 |
132 TEST(RulesRegistryTest, FillOptionalPriority) { | 141 TEST_F(RulesRegistryTest, FillOptionalPriority) { |
133 base::MessageLoopForUI message_loop; | 142 base::MessageLoopForUI message_loop; |
134 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 143 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
135 | 144 |
136 std::string error; | 145 std::string error; |
137 scoped_refptr<RulesRegistry> registry = | 146 scoped_refptr<RulesRegistry> registry = |
138 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 147 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
139 | 148 |
140 // Add rules and check that their priorities are filled if they are empty. | 149 // Add rules and check that their priorities are filled if they are empty. |
141 | 150 |
142 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 151 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
(...skipping 15 matching lines...) Expand all Loading... | |
158 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); | 167 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); |
159 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); | 168 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); |
160 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, | 169 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, |
161 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); | 170 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); |
162 | 171 |
163 // Make sure that deletion traits of registry are executed. | 172 // Make sure that deletion traits of registry are executed. |
164 registry = NULL; | 173 registry = NULL; |
165 message_loop.RunUntilIdle(); | 174 message_loop.RunUntilIdle(); |
166 } | 175 } |
167 | 176 |
177 TEST_F(RulesRegistryTest, TwoRulesInManifest) { | |
178 // Test verifies 2 rules defined in the manifest appear in the registry. | |
179 base::MessageLoopForUI message_loop; | |
180 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
181 | |
182 // Create extension | |
183 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
184 "{" | |
185 " \"name\": \"Test\"," | |
186 " \"version\": \"1\"," | |
187 " \"event_rules\": [" | |
188 " {" | |
189 " \"event\": \"declarativeContent.onPageChanged\"," | |
190 " \"actions\": [{" | |
191 " \"type\": \"declarativeContent.ShowPageAction\"" | |
192 " }]," | |
193 " \"conditions\" : [{" | |
194 " \"css\": [\"video\"]," | |
195 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
196 " }]" | |
197 " }," | |
198 " {" | |
199 " \"event\": \"declarativeContent.onPageChanged\"," | |
200 " \"actions\": [{" | |
201 " \"type\": \"declarativeContent.ShowPageAction\"" | |
202 " }]," | |
203 " \"conditions\" : [{" | |
204 " \"css\": [\"input[type='password']\"]," | |
205 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
206 " }]" | |
207 " }" | |
208 " ]" | |
209 "}"); | |
210 scoped_refptr<Extension> extension = ExtensionBuilder() | |
211 .SetManifest(manifest.Pass()) | |
212 .SetID(kExtensionId) | |
213 .Build(); | |
214 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
215 | |
216 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
217 browser_context(), "declarativeContent.onPageChanged", | |
218 content::BrowserThread::UI, nullptr, key); | |
219 // Simulate what RulesRegistryService would do on extension load. | |
220 registry->OnExtensionLoaded(kExtensionId); | |
221 | |
222 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
223 registry->GetAllRules(kExtensionId, &get_rules); | |
224 | |
225 ASSERT_EQ(2u, get_rules.size()); | |
226 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | |
227 "{" | |
228 " \"id\": \"_0_\"," | |
229 " \"priority\": 100," | |
230 " \"actions\": [{" | |
231 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
232 " }]," | |
233 " \"conditions\" : [{" | |
234 " \"css\": [\"video\"]," | |
235 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
236 " }]" | |
237 "}"); | |
238 EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); | |
239 | |
240 scoped_ptr<base::DictionaryValue> expected_rule_1 = ParseDictionary( | |
241 "{" | |
242 " \"id\": \"_1_\"," | |
243 " \"priority\": 100," | |
244 " \"actions\": [{" | |
245 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
246 " }]," | |
247 " \"conditions\" : [{" | |
248 " \"css\": [\"input[type='password']\"]," | |
249 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
250 " }]" | |
251 "}"); | |
252 EXPECT_TRUE(expected_rule_1->Equals(get_rules[1]->ToValue().get())); | |
253 } | |
254 | |
255 TEST_F(RulesRegistryTest, DeleteRuleInManifest) { | |
256 // Tests verifies that rules defined in the manifest cannot be deleted but | |
257 // programmatically added rules still can be deleted. | |
258 base::MessageLoopForUI message_loop; | |
259 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
260 | |
261 // Create extension | |
262 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
263 "{" | |
264 " \"name\": \"Test\"," | |
265 " \"version\": \"1\"," | |
266 " \"event_rules\": [{" | |
267 " \"event\": \"declarativeContent.onPageChanged\"," | |
268 " \"actions\": [{" | |
269 " \"type\": \"declarativeContent.ShowPageAction\"" | |
270 " }]," | |
271 " \"conditions\" : [{" | |
272 " \"css\": [\"video\"]," | |
273 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
274 " }]" | |
275 " }]" | |
276 "}"); | |
277 scoped_refptr<Extension> extension = ExtensionBuilder() | |
278 .SetManifest(manifest.Pass()) | |
279 .SetID(kExtensionId) | |
280 .Build(); | |
281 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
282 | |
283 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
284 browser_context(), "declarativeContent.onPageChanged", | |
285 content::BrowserThread::UI, nullptr, key); | |
286 // Simulate what RulesRegistryService would do on extension load. | |
287 registry->OnExtensionLoaded(kExtensionId); | |
288 // Add some extra rules outside of the manifest. | |
289 std::vector<linked_ptr<RulesRegistry::Rule>> add_rules; | |
290 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
291 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
292 registry->AddRules(kExtensionId, add_rules); | |
293 | |
294 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
295 registry->GetAllRules(kExtensionId, &get_rules); | |
296 ASSERT_EQ(3u, get_rules.size()); | |
Mike Wittman
2015/06/04 22:15:15
ASSERTs in this test can all be EXPECTs if you're
danduong
2015/06/04 23:06:37
Done.
| |
297 | |
298 // Remove a rule from outside the manifest. | |
299 std::vector<std::string> remove_ids; | |
300 remove_ids.push_back(*(get_rules[1]->id)); | |
301 ASSERT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
302 get_rules.clear(); | |
303 registry->GetAllRules(kExtensionId, &get_rules); | |
304 ASSERT_EQ(2u, get_rules.size()); | |
305 | |
306 // Attempt to remove rule in manifest. | |
307 remove_ids.clear(); | |
308 remove_ids.push_back(*(get_rules[0]->id)); | |
309 ASSERT_EQ(false, registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
310 get_rules.clear(); | |
311 registry->GetAllRules(kExtensionId, &get_rules); | |
312 ASSERT_EQ(2u, get_rules.size()); | |
313 | |
314 // Remove all rules. | |
315 registry->RemoveAllRules(kExtensionId); | |
316 get_rules.clear(); | |
317 registry->GetAllRules(kExtensionId, &get_rules); | |
318 ASSERT_EQ(0u, get_rules.size()); | |
319 } | |
320 | |
321 TEST_F(RulesRegistryTest, TwoBadRulesOneGoodInManifest) { | |
322 // Tests what happens when there are 2 bad rule definitions and 1 good | |
323 // definition in the manifest. | |
Mike Wittman
2015/06/04 22:15:15
... and the expected behavior is ???
danduong
2015/06/04 23:06:37
Done.
| |
324 base::MessageLoopForUI message_loop; | |
325 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
326 | |
327 // Create extension | |
328 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
329 "{" | |
330 " \"name\": \"Test\"," | |
331 " \"version\": \"1\"," | |
332 " \"event_rules\": [" | |
333 " {" | |
334 " \"event\": \"declarativeContent.onPageChanged\"," | |
335 " \"actions\": [{" | |
336 " \"type\": \"declarativeContent.ShowPageAction\"" | |
337 " }]," | |
338 " \"conditions\" : [{" | |
339 " \"css\": [\"video\"]," | |
340 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
341 " }]" | |
342 " }," | |
343 " {" | |
344 " \"actions\": [{" | |
345 " \"type\": \"declarativeContent.ShowPageAction\"" | |
346 " }]," | |
347 " \"conditions\" : [{" | |
348 " \"css\": [\"video\"]," | |
349 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
350 " }]" | |
351 " }," | |
352 " {" | |
353 " \"event\": \"declarativeContent.onPageChanged\"," | |
354 " \"actions\": [{" | |
355 " \"type\": \"declarativeContent.ShowPageAction\"" | |
356 " }]," | |
357 " \"conditions\" : [{}]" | |
358 " }" | |
359 " ]" | |
360 "}"); | |
361 scoped_refptr<Extension> extension = ExtensionBuilder() | |
362 .SetManifest(manifest.Pass()) | |
363 .SetID(kExtensionId) | |
364 .Build(); | |
365 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
366 | |
367 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
368 browser_context(), "declarativeContent.onPageChanged", | |
369 content::BrowserThread::UI, nullptr, key); | |
370 // Simulate what RulesRegistryService would do on extension load. | |
371 registry->OnExtensionLoaded(kExtensionId); | |
372 | |
373 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
374 registry->GetAllRules(kExtensionId, &get_rules); | |
375 | |
376 ASSERT_EQ(1u, get_rules.size()); | |
377 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | |
378 "{" | |
379 " \"id\": \"_0_\"," | |
380 " \"priority\": 100," | |
381 " \"actions\": [{" | |
382 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
383 " }]," | |
384 " \"conditions\" : [{" | |
385 " \"css\": [\"video\"]," | |
386 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
387 " }]" | |
388 "}"); | |
389 EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); | |
390 } | |
391 | |
392 TEST_F(RulesRegistryTest, NoValidRulesInManifest) { | |
393 // Tests what happens when no rules are properly defined in the manifest. | |
394 base::MessageLoopForUI message_loop; | |
395 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
396 | |
397 // Create extension | |
398 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
399 "{" | |
400 " \"name\": \"Test\"," | |
401 " \"version\": \"1\"," | |
402 " \"event_rules\": {}" | |
403 "}"); | |
404 scoped_refptr<Extension> extension = ExtensionBuilder() | |
405 .SetManifest(manifest.Pass()) | |
406 .SetID(kExtensionId) | |
407 .Build(); | |
408 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
409 | |
410 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
411 browser_context(), "declarativeContent.onPageChanged", | |
412 content::BrowserThread::UI, nullptr, key); | |
413 // Simulate what RulesRegistryService would do on extension load. | |
414 registry->OnExtensionLoaded(kExtensionId); | |
415 | |
416 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
417 registry->GetAllRules(kExtensionId, &get_rules); | |
418 | |
419 ASSERT_EQ(0u, get_rules.size()); | |
Mike Wittman
2015/06/04 22:15:15
EXPECT_EQ
danduong
2015/06/04 23:06:37
Done.
| |
420 } | |
421 | |
168 } // namespace extensions | 422 } // namespace extensions |
OLD | NEW |