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 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 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 " \"id\": \"000\"," | |
| 197 " \"priority\": 200," | |
| 198 " \"tags\": [\"tagged\"]," | |
| 199 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 200 " \"actions\": [{" | |
| 201 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 202 " }]," | |
| 203 " \"conditions\" : [{" | |
| 204 " \"css\": [\"video\"]," | |
| 205 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 206 " }]" | |
| 207 " }," | |
| 208 " {" | |
| 209 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 210 " \"actions\": [{" | |
| 211 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 212 " }]," | |
| 213 " \"conditions\" : [{" | |
| 214 " \"css\": [\"input[type='password']\"]," | |
| 215 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 216 " }]" | |
| 217 " }" | |
| 218 " ]" | |
| 219 "}"); | |
| 220 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 221 .SetManifest(manifest.Pass()) | |
| 222 .SetID(kExtensionId) | |
| 223 .Build(); | |
| 224 | |
| 225 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 226 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 227 // Simulate what RulesRegistryService would do on extension load. | |
| 228 registry->OnExtensionLoaded(extension.get()); | |
| 229 | |
| 230 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 231 registry->GetAllRules(kExtensionId, &get_rules); | |
| 232 | |
| 233 ASSERT_EQ(2u, get_rules.size()); | |
| 234 scoped_ptr<base::DictionaryValue> expected_rule_0 = ParseDictionary( | |
| 235 "{" | |
| 236 " \"id\": \"000\"," | |
| 237 " \"priority\": 200," | |
| 238 " \"tags\": [\"tagged\"]," | |
| 239 " \"actions\": [{" | |
| 240 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
| 241 " }]," | |
| 242 " \"conditions\" : [{" | |
| 243 " \"css\": [\"video\"]," | |
| 244 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
| 245 " }]" | |
| 246 "}"); | |
| 247 EXPECT_TRUE(expected_rule_0->Equals(get_rules[0]->ToValue().get())); | |
| 248 | |
| 249 scoped_ptr<base::DictionaryValue> expected_rule_1 = ParseDictionary( | |
| 250 "{" | |
| 251 " \"id\": \"_0_\"," | |
| 252 " \"priority\": 100," | |
| 253 " \"actions\": [{" | |
| 254 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
| 255 " }]," | |
| 256 " \"conditions\" : [{" | |
| 257 " \"css\": [\"input[type='password']\"]," | |
| 258 " \"instanceType\" : \"declarativeContent.PageStateMatcher\"" | |
| 259 " }]" | |
| 260 "}"); | |
| 261 EXPECT_TRUE(expected_rule_1->Equals(get_rules[1]->ToValue().get())); | |
| 262 } | |
| 263 | |
| 264 // Tests verifies that rules defined in the manifest cannot be deleted but | |
| 265 // programmatically added rules still can be deleted. | |
| 266 TEST(RulesRegistryTest, DeleteRuleInManifest) { | |
| 267 base::MessageLoopForUI message_loop; | |
| 268 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 269 | |
| 270 // Create extension | |
| 271 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 272 "{" | |
| 273 " \"name\": \"Test\"," | |
| 274 " \"version\": \"1\"," | |
| 275 " \"event_rules\": [{" | |
| 276 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 277 " \"actions\": [{" | |
| 278 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 279 " }]," | |
| 280 " \"conditions\" : [{" | |
| 281 " \"css\": [\"video\"]," | |
| 282 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 283 " }]" | |
| 284 " }]" | |
| 285 "}"); | |
| 286 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 287 .SetManifest(manifest.Pass()) | |
| 288 .SetID(kExtensionId) | |
| 289 .Build(); | |
| 290 | |
| 291 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 292 content::BrowserThread::UI, "declarativeContent.onPageChanged", key); | |
| 293 // Simulate what RulesRegistryService would do on extension load. | |
| 294 registry->OnExtensionLoaded(extension.get()); | |
| 295 // Add some extra rules outside of the manifest. | |
| 296 std::vector<linked_ptr<RulesRegistry::Rule>> add_rules; | |
| 297 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 298 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 299 registry->AddRules(kExtensionId, add_rules); | |
| 300 | |
| 301 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 302 registry->GetAllRules(kExtensionId, &get_rules); | |
| 303 ASSERT_EQ(3u, get_rules.size()); | |
| 304 | |
| 305 // Remove a rule from outside the manifest. | |
| 306 std::vector<std::string> remove_ids; | |
| 307 remove_ids.push_back(*(get_rules[1]->id)); | |
| 308 EXPECT_TRUE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 309 get_rules.clear(); | |
| 310 registry->GetAllRules(kExtensionId, &get_rules); | |
| 311 EXPECT_EQ(2u, get_rules.size()); | |
| 312 | |
| 313 // Attempt to remove rule in manifest. | |
| 314 remove_ids.clear(); | |
| 315 remove_ids.push_back(*(get_rules[0]->id)); | |
| 316 EXPECT_FALSE(registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 317 get_rules.clear(); | |
| 318 registry->GetAllRules(kExtensionId, &get_rules); | |
| 319 EXPECT_EQ(2u, get_rules.size()); | |
| 320 | |
| 321 // Remove all rules. | |
| 322 registry->RemoveAllRules(kExtensionId); | |
| 323 get_rules.clear(); | |
| 324 registry->GetAllRules(kExtensionId, &get_rules); | |
| 325 EXPECT_EQ(1u, get_rules.size()); | |
|
not at google - send to devlin
2015/06/08 21:44:58
For this whole test, is it actually possible to te
danduong
2015/06/09 01:21:26
Done.
| |
| 326 } | |
| 327 | |
| 168 } // namespace extensions | 328 } // namespace extensions |
| OLD | NEW |