Chromium Code Reviews| 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/common/extension.h" | |
| 16 #include "extensions/common/extension_builder.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 const char kExtensionId[] = "foobar"; | 20 const char kExtensionId[] = "foobar"; |
| 17 const char kRuleId[] = "foo"; | 21 const char kRuleId[] = "foo"; |
| 18 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; | 22 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; |
| 19 } // namespace | 23 } // namespace |
| 20 | 24 |
| 21 namespace extensions { | 25 namespace extensions { |
| 22 | 26 |
| 27 using api_test_utils::ParseDictionary; | |
| 28 | |
| 23 TEST(RulesRegistryTest, FillOptionalIdentifiers) { | 29 TEST(RulesRegistryTest, FillOptionalIdentifiers) { |
| 24 base::MessageLoopForUI message_loop; | 30 base::MessageLoopForUI message_loop; |
| 25 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 31 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| 26 | 32 |
| 27 std::string error; | 33 std::string error; |
| 28 scoped_refptr<RulesRegistry> registry = | 34 scoped_refptr<RulesRegistry> registry = |
| 29 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 35 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
| 30 | 36 |
| 31 // Add rules and check that their identifiers are filled and unique. | 37 // Add rules and check that their identifiers are filled and unique. |
| 32 | 38 |
| 33 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 39 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
| 34 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 40 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 35 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 41 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 36 error = registry->AddRules(kExtensionId, add_rules); | 42 error = registry->AddRules(kExtensionId, add_rules, false); |
| 37 EXPECT_TRUE(error.empty()) << error; | 43 EXPECT_TRUE(error.empty()) << error; |
| 38 | 44 |
| 39 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; | 45 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; |
| 40 registry->GetAllRules(kExtensionId, &get_rules); | 46 registry->GetAllRules(kExtensionId, &get_rules); |
| 41 | 47 |
| 42 ASSERT_EQ(2u, get_rules.size()); | 48 ASSERT_EQ(2u, get_rules.size()); |
| 43 | 49 |
| 44 ASSERT_TRUE(get_rules[0]->id.get()); | 50 ASSERT_TRUE(get_rules[0]->id.get()); |
| 45 EXPECT_NE("", *get_rules[0]->id); | 51 EXPECT_NE("", *get_rules[0]->id); |
| 46 | 52 |
| 47 ASSERT_TRUE(get_rules[1]->id.get()); | 53 ASSERT_TRUE(get_rules[1]->id.get()); |
| 48 EXPECT_NE("", *get_rules[1]->id); | 54 EXPECT_NE("", *get_rules[1]->id); |
| 49 | 55 |
| 50 EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id); | 56 EXPECT_NE(*get_rules[0]->id, *get_rules[1]->id); |
| 51 | 57 |
| 52 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 58 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
| 53 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 59 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 54 | 60 |
| 55 // 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. |
| 56 | 62 |
| 57 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_2; | 63 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_2; |
| 58 add_rules_2.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 64 add_rules_2.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 59 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)); |
| 60 error = registry->AddRules(kExtensionId, add_rules_2); | 66 error = registry->AddRules(kExtensionId, add_rules_2, false); |
| 61 EXPECT_FALSE(error.empty()); | 67 EXPECT_FALSE(error.empty()); |
| 62 | 68 |
| 63 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_2; | 69 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_2; |
| 64 registry->GetAllRules(kExtensionId, &get_rules_2); | 70 registry->GetAllRules(kExtensionId, &get_rules_2); |
| 65 ASSERT_EQ(2u, get_rules_2.size()); | 71 ASSERT_EQ(2u, get_rules_2.size()); |
| 66 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 72 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
| 67 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 73 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 68 | 74 |
| 69 // 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. |
| 70 | 76 |
| 71 std::vector<std::string> remove_rules_3; | 77 std::vector<std::string> remove_rules_3; |
| 72 remove_rules_3.push_back(*get_rules[0]->id); | 78 remove_rules_3.push_back(*get_rules[0]->id); |
| 73 error = registry->RemoveRules(kExtensionId, remove_rules_3); | 79 error = registry->RemoveRules(kExtensionId, remove_rules_3); |
| 74 EXPECT_TRUE(error.empty()) << error; | 80 EXPECT_TRUE(error.empty()) << error; |
| 75 | 81 |
| 76 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, | 82 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, |
| 77 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 83 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 78 | 84 |
| 79 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3a; | 85 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3a; |
| 80 registry->GetAllRules(kExtensionId, &get_rules_3a); | 86 registry->GetAllRules(kExtensionId, &get_rules_3a); |
| 81 ASSERT_EQ(1u, get_rules_3a.size()); | 87 ASSERT_EQ(1u, get_rules_3a.size()); |
| 82 | 88 |
| 83 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_3; | 89 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_3; |
| 84 add_rules_3.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 90 add_rules_3.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 85 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)); |
| 86 error = registry->AddRules(kExtensionId, add_rules_3); | 92 error = registry->AddRules(kExtensionId, add_rules_3, false); |
| 87 EXPECT_TRUE(error.empty()) << error; | 93 EXPECT_TRUE(error.empty()) << error; |
| 88 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, | 94 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/, |
| 89 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 95 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 90 | 96 |
| 91 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3b; | 97 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_3b; |
| 92 registry->GetAllRules(kExtensionId, &get_rules_3b); | 98 registry->GetAllRules(kExtensionId, &get_rules_3b); |
| 93 ASSERT_EQ(2u, get_rules_3b.size()); | 99 ASSERT_EQ(2u, get_rules_3b.size()); |
| 94 | 100 |
| 95 // 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. |
| 96 | 102 |
| 97 error = registry->RemoveAllRules(kExtensionId); | 103 error = registry->RemoveAllRules(kExtensionId); |
| 98 EXPECT_TRUE(error.empty()) << error; | 104 EXPECT_TRUE(error.empty()) << error; |
| 99 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, | 105 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, |
| 100 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 106 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 101 | 107 |
| 102 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4a; | 108 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4a; |
| 103 registry->GetAllRules(kExtensionId, &get_rules_4a); | 109 registry->GetAllRules(kExtensionId, &get_rules_4a); |
| 104 ASSERT_TRUE(get_rules_4a.empty()); | 110 ASSERT_TRUE(get_rules_4a.empty()); |
| 105 | 111 |
| 106 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_4; | 112 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules_4; |
| 107 add_rules_4.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 113 add_rules_4.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 108 add_rules_4[0]->id.reset(new std::string(kRuleId)); | 114 add_rules_4[0]->id.reset(new std::string(kRuleId)); |
| 109 error = registry->AddRules(kExtensionId, add_rules_4); | 115 error = registry->AddRules(kExtensionId, add_rules_4, false); |
| 110 EXPECT_TRUE(error.empty()) << error; | 116 EXPECT_TRUE(error.empty()) << error; |
| 111 | 117 |
| 112 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, | 118 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/, |
| 113 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 119 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 114 | 120 |
| 115 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4b; | 121 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules_4b; |
| 116 registry->GetAllRules(kExtensionId, &get_rules_4b); | 122 registry->GetAllRules(kExtensionId, &get_rules_4b); |
| 117 | 123 |
| 118 ASSERT_EQ(1u, get_rules_4b.size()); | 124 ASSERT_EQ(1u, get_rules_4b.size()); |
| 119 | 125 |
| 120 ASSERT_TRUE(get_rules_4b[0]->id.get()); | 126 ASSERT_TRUE(get_rules_4b[0]->id.get()); |
| 121 EXPECT_EQ(kRuleId, *get_rules_4b[0]->id); | 127 EXPECT_EQ(kRuleId, *get_rules_4b[0]->id); |
| 122 | 128 |
| 123 registry->OnExtensionUninstalled(kExtensionId); | 129 // Create extension |
| 130 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 131 "{" | |
| 132 " \"name\": \"Test\"," | |
| 133 " \"version\": \"1\"" | |
| 134 "}"); | |
| 135 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 136 .SetManifest(manifest.Pass()) | |
| 137 .SetID(kExtensionId) | |
| 138 .Build(); | |
| 139 registry->OnExtensionUninstalled(extension.get()); | |
| 124 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, | 140 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, |
| 125 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 141 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
| 126 | 142 |
| 127 // Make sure that deletion traits of registry are executed. | 143 // Make sure that deletion traits of registry are executed. |
| 128 registry = NULL; | 144 registry = NULL; |
| 129 message_loop.RunUntilIdle(); | 145 message_loop.RunUntilIdle(); |
| 130 } | 146 } |
| 131 | 147 |
| 132 TEST(RulesRegistryTest, FillOptionalPriority) { | 148 TEST(RulesRegistryTest, FillOptionalPriority) { |
| 133 base::MessageLoopForUI message_loop; | 149 base::MessageLoopForUI message_loop; |
| 134 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 150 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
| 135 | 151 |
| 136 std::string error; | 152 std::string error; |
| 137 scoped_refptr<RulesRegistry> registry = | 153 scoped_refptr<RulesRegistry> registry = |
| 138 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 154 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
| 139 | 155 |
| 140 // 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. |
| 141 | 157 |
| 142 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 158 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
| 143 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 159 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 144 add_rules[0]->priority.reset(new int(2)); | 160 add_rules[0]->priority.reset(new int(2)); |
| 145 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | 161 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); |
| 146 error = registry->AddRules(kExtensionId, add_rules); | 162 error = registry->AddRules(kExtensionId, add_rules, false); |
| 147 EXPECT_TRUE(error.empty()) << error; | 163 EXPECT_TRUE(error.empty()) << error; |
| 148 | 164 |
| 149 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; | 165 std::vector<linked_ptr<RulesRegistry::Rule> > get_rules; |
| 150 registry->GetAllRules(kExtensionId, &get_rules); | 166 registry->GetAllRules(kExtensionId, &get_rules); |
| 151 | 167 |
| 152 ASSERT_EQ(2u, get_rules.size()); | 168 ASSERT_EQ(2u, get_rules.size()); |
| 153 | 169 |
| 154 ASSERT_TRUE(get_rules[0]->priority.get()); | 170 ASSERT_TRUE(get_rules[0]->priority.get()); |
| 155 ASSERT_TRUE(get_rules[1]->priority.get()); | 171 ASSERT_TRUE(get_rules[1]->priority.get()); |
| 156 | 172 |
| 157 // Verify the precondition so that the following EXPECT_EQ statements work. | 173 // Verify the precondition so that the following EXPECT_EQ statements work. |
| 158 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); | 174 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); |
| 159 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)); |
| 160 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, | 176 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, |
| 161 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); | 177 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); |
| 162 | 178 |
| 163 // Make sure that deletion traits of registry are executed. | 179 // Make sure that deletion traits of registry are executed. |
| 164 registry = NULL; | 180 registry = NULL; |
| 165 message_loop.RunUntilIdle(); | 181 message_loop.RunUntilIdle(); |
| 166 } | 182 } |
| 167 | 183 |
| 184 // Test verifies 2 rules defined in the manifest appear in the registry. | |
| 185 TEST(RulesRegistryTest, TwoRulesInManifest) { | |
| 186 base::MessageLoopForUI message_loop; | |
| 187 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 188 | |
| 189 // Create extension | |
| 190 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 191 "{" | |
| 192 " \"name\": \"Test\"," | |
| 193 " \"version\": \"1\"," | |
| 194 " \"event_rules\": [" | |
| 195 " {" | |
| 196 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 197 " \"actions\": [{" | |
| 198 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 199 " }]," | |
| 200 " \"conditions\" : [{" | |
| 201 " \"css\": [\"video\"]," | |
| 202 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 203 " }]" | |
| 204 " }," | |
| 205 " {" | |
| 206 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 207 " \"actions\": [{" | |
| 208 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 209 " }]," | |
| 210 " \"conditions\" : [{" | |
| 211 " \"css\": [\"input[type='password']\"]," | |
| 212 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 213 " }]" | |
|
not at google - send to devlin
2015/06/05 16:16:14
It would be worth also setting an ID and tags on o
danduong
2015/06/05 21:35:07
Done.
| |
| 214 " }" | |
| 215 " ]" | |
| 216 "}"); | |
| 217 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 218 .SetManifest(manifest.Pass()) | |
| 219 .SetID(kExtensionId) | |
| 220 .Build(); | |
| 221 | |
| 222 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 223 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 224 // Simulate what RulesRegistryService would do on extension load. | |
| 225 registry->OnExtensionLoaded(extension.get()); | |
| 226 | |
| 227 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 228 registry->GetAllRules(kExtensionId, &get_rules); | |
| 229 | |
| 230 ASSERT_EQ(2u, get_rules.size()); | |
| 231 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | |
| 232 "{" | |
| 233 " \"id\": \"_0_\"," | |
| 234 " \"priority\": 100," | |
| 235 " \"actions\": [{" | |
| 236 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
| 237 " }]," | |
| 238 " \"conditions\" : [{" | |
| 239 " \"css\": [\"video\"]," | |
| 240 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
| 241 " }]" | |
| 242 "}"); | |
| 243 EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); | |
| 244 | |
| 245 scoped_ptr<base::DictionaryValue> expected_rule_1 = ParseDictionary( | |
| 246 "{" | |
| 247 " \"id\": \"_1_\"," | |
| 248 " \"priority\": 100," | |
| 249 " \"actions\": [{" | |
| 250 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
| 251 " }]," | |
| 252 " \"conditions\" : [{" | |
| 253 " \"css\": [\"input[type='password']\"]," | |
| 254 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
| 255 " }]" | |
| 256 "}"); | |
| 257 EXPECT_TRUE(expected_rule_1->Equals(get_rules[1]->ToValue().get())); | |
| 258 } | |
| 259 | |
| 260 // Tests verifies that rules defined in the manifest cannot be deleted but | |
| 261 // programmatically added rules still can be deleted. | |
| 262 TEST(RulesRegistryTest, DeleteRuleInManifest) { | |
| 263 base::MessageLoopForUI message_loop; | |
| 264 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 265 | |
| 266 // Create extension | |
| 267 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 268 "{" | |
| 269 " \"name\": \"Test\"," | |
| 270 " \"version\": \"1\"," | |
| 271 " \"event_rules\": [{" | |
| 272 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 273 " \"actions\": [{" | |
| 274 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 275 " }]," | |
| 276 " \"conditions\" : [{" | |
| 277 " \"css\": [\"video\"]," | |
| 278 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 279 " }]" | |
| 280 " }]" | |
| 281 "}"); | |
| 282 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 283 .SetManifest(manifest.Pass()) | |
| 284 .SetID(kExtensionId) | |
| 285 .Build(); | |
| 286 | |
| 287 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 288 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 289 // Simulate what RulesRegistryService would do on extension load. | |
| 290 registry->OnExtensionLoaded(extension.get()); | |
| 291 // Add some extra rules outside of the manifest. | |
| 292 std::vector<linked_ptr<RulesRegistry::Rule>> add_rules; | |
| 293 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 294 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 295 registry->AddRules(kExtensionId, add_rules, false); | |
| 296 | |
| 297 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 298 registry->GetAllRules(kExtensionId, &get_rules); | |
| 299 EXPECT_EQ(3u, get_rules.size()); | |
| 300 | |
| 301 // Remove a rule from outside the manifest. | |
| 302 std::vector<std::string> remove_ids; | |
| 303 remove_ids.push_back(*(get_rules[1]->id)); | |
| 304 EXPECT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 305 get_rules.clear(); | |
| 306 registry->GetAllRules(kExtensionId, &get_rules); | |
| 307 EXPECT_EQ(2u, get_rules.size()); | |
| 308 | |
| 309 // Attempt to remove rule in manifest. | |
| 310 remove_ids.clear(); | |
| 311 remove_ids.push_back(*(get_rules[0]->id)); | |
| 312 EXPECT_FALSE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 313 get_rules.clear(); | |
| 314 registry->GetAllRules(kExtensionId, &get_rules); | |
| 315 EXPECT_EQ(2u, get_rules.size()); | |
| 316 | |
| 317 // Remove all rules. | |
|
not at google - send to devlin
2015/06/05 16:16:14
Why does removing all rules also remove the ones f
danduong
2015/06/05 21:35:07
Good catch. Some reason I thought this was interna
| |
| 318 registry->RemoveAllRules(kExtensionId); | |
| 319 get_rules.clear(); | |
| 320 registry->GetAllRules(kExtensionId, &get_rules); | |
| 321 EXPECT_EQ(0u, get_rules.size()); | |
| 322 } | |
| 323 | |
| 324 // Tests what happens when there are 2 bad rule definitions and 1 good | |
| 325 // definition in the manifest. Only the valid rule should be added to the | |
| 326 // registry. | |
| 327 TEST(RulesRegistryTest, TwoBadRulesOneGoodInManifest) { | |
| 328 base::MessageLoopForUI message_loop; | |
| 329 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 330 | |
| 331 // Create extension | |
| 332 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 333 "{" | |
| 334 " \"name\": \"Test\"," | |
| 335 " \"version\": \"1\"," | |
| 336 " \"event_rules\": [" | |
| 337 " {" | |
| 338 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 339 " \"actions\": [{" | |
| 340 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 341 " }]," | |
| 342 " \"conditions\" : [{" | |
| 343 " \"css\": [\"video\"]," | |
| 344 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 345 " }]" | |
| 346 " }," | |
| 347 " {" | |
| 348 " \"actions\": [{" | |
| 349 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 350 " }]," | |
| 351 " \"conditions\" : [{" | |
| 352 " \"css\": [\"video\"]," | |
| 353 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 354 " }]" | |
| 355 " }," | |
| 356 " {" | |
| 357 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 358 " \"actions\": [{" | |
| 359 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 360 " }]," | |
| 361 " \"conditions\" : [{}]" | |
| 362 " }" | |
| 363 " ]" | |
| 364 "}"); | |
| 365 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 366 .SetManifest(manifest.Pass()) | |
| 367 .SetID(kExtensionId) | |
| 368 .Build(); | |
| 369 | |
| 370 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 371 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 372 // Simulate what RulesRegistryService would do on extension load. | |
| 373 registry->OnExtensionLoaded(extension.get()); | |
| 374 | |
| 375 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 376 registry->GetAllRules(kExtensionId, &get_rules); | |
| 377 | |
| 378 ASSERT_EQ(1u, get_rules.size()); | |
| 379 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | |
| 380 "{" | |
| 381 " \"id\": \"_0_\"," | |
| 382 " \"priority\": 100," | |
| 383 " \"actions\": [{" | |
| 384 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
| 385 " }]," | |
| 386 " \"conditions\" : [{" | |
| 387 " \"css\": [\"video\"]," | |
| 388 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
| 389 " }]" | |
| 390 "}"); | |
| 391 EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); | |
| 392 } | |
| 393 | |
| 394 // Tests what happens when no rules are properly defined in the manifest. This | |
| 395 // should result in no crash/additions. | |
| 396 TEST(RulesRegistryTest, NoValidRulesInManifest) { | |
| 397 base::MessageLoopForUI message_loop; | |
| 398 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 399 | |
| 400 // Create extension | |
| 401 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 402 "{" | |
| 403 " \"name\": \"Test\"," | |
| 404 " \"version\": \"1\"," | |
| 405 " \"event_rules\": {}" | |
| 406 "}"); | |
| 407 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 408 .SetManifest(manifest.Pass()) | |
| 409 .SetID(kExtensionId) | |
| 410 .Build(); | |
| 411 | |
| 412 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 413 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 414 // Simulate what RulesRegistryService would do on extension load. | |
| 415 registry->OnExtensionLoaded(extension.get()); | |
| 416 | |
| 417 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 418 registry->GetAllRules(kExtensionId, &get_rules); | |
| 419 | |
| 420 EXPECT_EQ(0u, get_rules.size()); | |
| 421 } | |
| 422 | |
| 168 } // namespace extensions | 423 } // namespace extensions |
| OLD | NEW |