Chromium Code Reviews| Index: extensions/browser/api/declarative/rules_registry_unittest.cc |
| diff --git a/extensions/browser/api/declarative/rules_registry_unittest.cc b/extensions/browser/api/declarative/rules_registry_unittest.cc |
| index 33da872a1667d3d6a2db96dacefff7f5c57ea830..7d9c7107a7871badef7a201c9e7bf601b31acbf7 100644 |
| --- a/extensions/browser/api/declarative/rules_registry_unittest.cc |
| +++ b/extensions/browser/api/declarative/rules_registry_unittest.cc |
| @@ -7,9 +7,15 @@ |
| #include <algorithm> |
| #include "base/message_loop/message_loop.h" |
| +#include "base/values.h" |
| #include "content/public/test/test_browser_thread.h" |
| #include "extensions/browser/api/declarative/rules_registry_service.h" |
| #include "extensions/browser/api/declarative/test_rules_registry.h" |
| +#include "extensions/browser/api_test_utils.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/extensions_test.h" |
| +#include "extensions/common/extension.h" |
| +#include "extensions/common/extension_builder.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace { |
| @@ -20,7 +26,10 @@ const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; |
| namespace extensions { |
| -TEST(RulesRegistryTest, FillOptionalIdentifiers) { |
| +using api_test_utils::ParseDictionary; |
| +using RulesRegistryTest = ExtensionsTest; |
| + |
| +TEST_F(RulesRegistryTest, FillOptionalIdentifiers) { |
| base::MessageLoopForUI message_loop; |
| content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| @@ -129,7 +138,7 @@ TEST(RulesRegistryTest, FillOptionalIdentifiers) { |
| message_loop.RunUntilIdle(); |
| } |
| -TEST(RulesRegistryTest, FillOptionalPriority) { |
| +TEST_F(RulesRegistryTest, FillOptionalPriority) { |
| base::MessageLoopForUI message_loop; |
| content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| @@ -165,4 +174,249 @@ TEST(RulesRegistryTest, FillOptionalPriority) { |
| message_loop.RunUntilIdle(); |
| } |
| +TEST_F(RulesRegistryTest, TwoRulesInManifest) { |
| + // Test verifies 2 rules defined in the manifest appear in the registry. |
| + base::MessageLoopForUI message_loop; |
| + content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| + |
| + // Create extension |
| + scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( |
| + "{" |
| + " \"name\": \"Test\"," |
| + " \"version\": \"1\"," |
| + " \"event_rules\": [" |
| + " {" |
| + " \"event\": \"declarativeContent.onPageChanged\"," |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"type\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + " }," |
| + " {" |
| + " \"event\": \"declarativeContent.onPageChanged\"," |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"input[type='password']\"]," |
| + " \"type\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + " }" |
| + " ]" |
| + "}"); |
| + scoped_refptr<Extension> extension = ExtensionBuilder() |
| + .SetManifest(manifest.Pass()) |
| + .SetID(kExtensionId) |
| + .Build(); |
| + ExtensionRegistry::Get(browser_context())->AddEnabled(extension); |
| + |
| + scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
| + browser_context(), "declarativeContent.onPageChanged", |
| + content::BrowserThread::UI, nullptr, key); |
| + // Simulate what RulesRegistryService would do on extension load. |
| + registry->OnExtensionLoaded(kExtensionId); |
| + |
| + std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + |
| + ASSERT_EQ(2u, get_rules.size()); |
| + scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( |
| + "{" |
| + " \"id\": \"_0_\"," |
| + " \"priority\": 100," |
| + " \"actions\": [{" |
| + " \"instanceType\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + "}"); |
| + EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); |
| + |
| + scoped_ptr<base::DictionaryValue> expected_rule_1 = ParseDictionary( |
| + "{" |
| + " \"id\": \"_1_\"," |
| + " \"priority\": 100," |
| + " \"actions\": [{" |
| + " \"instanceType\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"input[type='password']\"]," |
| + " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + "}"); |
| + EXPECT_TRUE(expected_rule_1->Equals(get_rules[1]->ToValue().get())); |
| +} |
| + |
| +TEST_F(RulesRegistryTest, DeleteRuleInManifest) { |
| + // Tests verifies that rules defined in the manifest cannot be deleted but |
| + // programmatically added rules still can be deleted. |
| + base::MessageLoopForUI message_loop; |
| + content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| + |
| + // Create extension |
| + scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( |
| + "{" |
| + " \"name\": \"Test\"," |
| + " \"version\": \"1\"," |
| + " \"event_rules\": [{" |
| + " \"event\": \"declarativeContent.onPageChanged\"," |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"type\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + " }]" |
| + "}"); |
| + scoped_refptr<Extension> extension = ExtensionBuilder() |
| + .SetManifest(manifest.Pass()) |
| + .SetID(kExtensionId) |
| + .Build(); |
| + ExtensionRegistry::Get(browser_context())->AddEnabled(extension); |
| + |
| + scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
| + browser_context(), "declarativeContent.onPageChanged", |
| + content::BrowserThread::UI, nullptr, key); |
| + // Simulate what RulesRegistryService would do on extension load. |
| + registry->OnExtensionLoaded(kExtensionId); |
| + // Add some extra rules outside of the manifest. |
| + std::vector<linked_ptr<RulesRegistry::Rule>> add_rules; |
| + add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| + add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| + registry->AddRules(kExtensionId, add_rules); |
| + |
| + std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + 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.
|
| + |
| + // Remove a rule from outside the manifest. |
| + std::vector<std::string> remove_ids; |
| + remove_ids.push_back(*(get_rules[1]->id)); |
| + ASSERT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); |
| + get_rules.clear(); |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + ASSERT_EQ(2u, get_rules.size()); |
| + |
| + // Attempt to remove rule in manifest. |
| + remove_ids.clear(); |
| + remove_ids.push_back(*(get_rules[0]->id)); |
| + ASSERT_EQ(false, registry->RemoveRules(kExtensionId, remove_ids).empty()); |
| + get_rules.clear(); |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + ASSERT_EQ(2u, get_rules.size()); |
| + |
| + // Remove all rules. |
| + registry->RemoveAllRules(kExtensionId); |
| + get_rules.clear(); |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + ASSERT_EQ(0u, get_rules.size()); |
| +} |
| + |
| +TEST_F(RulesRegistryTest, TwoBadRulesOneGoodInManifest) { |
| + // Tests what happens when there are 2 bad rule definitions and 1 good |
| + // 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.
|
| + base::MessageLoopForUI message_loop; |
| + content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| + |
| + // Create extension |
| + scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( |
| + "{" |
| + " \"name\": \"Test\"," |
| + " \"version\": \"1\"," |
| + " \"event_rules\": [" |
| + " {" |
| + " \"event\": \"declarativeContent.onPageChanged\"," |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"type\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + " }," |
| + " {" |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"type\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + " }," |
| + " {" |
| + " \"event\": \"declarativeContent.onPageChanged\"," |
| + " \"actions\": [{" |
| + " \"type\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{}]" |
| + " }" |
| + " ]" |
| + "}"); |
| + scoped_refptr<Extension> extension = ExtensionBuilder() |
| + .SetManifest(manifest.Pass()) |
| + .SetID(kExtensionId) |
| + .Build(); |
| + ExtensionRegistry::Get(browser_context())->AddEnabled(extension); |
| + |
| + scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
| + browser_context(), "declarativeContent.onPageChanged", |
| + content::BrowserThread::UI, nullptr, key); |
| + // Simulate what RulesRegistryService would do on extension load. |
| + registry->OnExtensionLoaded(kExtensionId); |
| + |
| + std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + |
| + ASSERT_EQ(1u, get_rules.size()); |
| + scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( |
| + "{" |
| + " \"id\": \"_0_\"," |
| + " \"priority\": 100," |
| + " \"actions\": [{" |
| + " \"instanceType\": \"declarativeContent.ShowPageAction\"" |
| + " }]," |
| + " \"conditions\" : [{" |
| + " \"css\": [\"video\"]," |
| + " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" |
| + " }]" |
| + "}"); |
| + EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); |
| +} |
| + |
| +TEST_F(RulesRegistryTest, NoValidRulesInManifest) { |
| + // Tests what happens when no rules are properly defined in the manifest. |
| + base::MessageLoopForUI message_loop; |
| + content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| + |
| + // Create extension |
| + scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( |
| + "{" |
| + " \"name\": \"Test\"," |
| + " \"version\": \"1\"," |
| + " \"event_rules\": {}" |
| + "}"); |
| + scoped_refptr<Extension> extension = ExtensionBuilder() |
| + .SetManifest(manifest.Pass()) |
| + .SetID(kExtensionId) |
| + .Build(); |
| + ExtensionRegistry::Get(browser_context())->AddEnabled(extension); |
| + |
| + scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( |
| + browser_context(), "declarativeContent.onPageChanged", |
| + content::BrowserThread::UI, nullptr, key); |
| + // Simulate what RulesRegistryService would do on extension load. |
| + registry->OnExtensionLoaded(kExtensionId); |
| + |
| + std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; |
| + registry->GetAllRules(kExtensionId, &get_rules); |
| + |
| + ASSERT_EQ(0u, get_rules.size()); |
|
Mike Wittman
2015/06/04 22:15:15
EXPECT_EQ
danduong
2015/06/04 23:06:37
Done.
|
| +} |
| + |
| } // namespace extensions |