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/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) { | |
|
Mike Wittman
2015/06/04 18:40:13
Can you add a comment before each test describing
danduong
2015/06/04 19:56:07
Done.
Mike Wittman
2015/06/04 22:15:15
convention is to put the comments above the TEST d
danduong
2015/06/05 06:41:57
Done.
| |
| 178 base::MessageLoopForUI message_loop; | |
| 179 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 180 | |
| 181 // Create extension | |
| 182 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 183 "{" | |
| 184 " \"name\": \"Test\"," | |
| 185 " \"version\": \"1\"," | |
| 186 " \"event_rules\": [" | |
| 187 " {" | |
| 188 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 189 " \"actions\": [{" | |
| 190 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 191 " }]," | |
| 192 " \"conditions\" : [{" | |
| 193 " \"css\": [\"video\"]," | |
| 194 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 195 " }]" | |
| 196 " }," | |
| 197 " {" | |
| 198 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 199 " \"actions\": [{" | |
| 200 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 201 " }]," | |
| 202 " \"conditions\" : [{" | |
| 203 " \"css\": [\"input[type='password']\"]," | |
| 204 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 205 " }]" | |
| 206 " }" | |
| 207 " ]" | |
| 208 "}"); | |
| 209 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 210 .SetManifest(manifest.Pass()) | |
| 211 .SetID(kExtensionId) | |
| 212 .Build(); | |
| 213 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
| 214 | |
| 215 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 216 browser_context(), "declarativeContent.onPageChanged", | |
| 217 content::BrowserThread::UI, nullptr, key); | |
| 218 // Simulate what RulesRegistryService would do on extension load. | |
| 219 registry->OnExtensionLoaded(kExtensionId); | |
| 220 | |
| 221 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 222 registry->GetAllRules(kExtensionId, &get_rules); | |
| 223 | |
| 224 ASSERT_EQ(2u, get_rules.size()); | |
| 225 ASSERT_EQ(1u, get_rules[0]->actions.size()); | |
| 226 const base::DictionaryValue* action; | |
|
not at google - send to devlin
2015/06/04 17:58:49
it's nice to initialise pointers to null.
danduong
2015/06/04 19:10:59
Done.
| |
| 227 ASSERT_EQ(true, get_rules[0]->actions[0]->GetAsDictionary(&action)); | |
|
not at google - send to devlin
2015/06/04 17:58:49
ASSERT_TRUE
danduong
2015/06/04 19:10:59
Done.
| |
| 228 std::string instance_type; | |
| 229 ASSERT_EQ(true, action->GetStringASCII("instanceType", &instance_type)); | |
| 230 ASSERT_EQ("declarativeContent.ShowPageAction", instance_type); | |
| 231 ASSERT_EQ(1u, get_rules[0]->conditions.size()); | |
| 232 const base::DictionaryValue* condition; | |
| 233 ASSERT_EQ(true, get_rules[0]->conditions[0]->GetAsDictionary(&condition)); | |
| 234 ASSERT_EQ(true, condition->GetStringASCII("instanceType", &instance_type)); | |
| 235 ASSERT_EQ("declarativeContent.PageStateMatcher", instance_type); | |
|
Mike Wittman
2015/06/04 18:40:14
Checks that can fail without causing the test to c
danduong
2015/06/04 19:56:07
Done.
| |
| 236 const base::ListValue* css; | |
| 237 ASSERT_EQ(true, condition->GetList("css", &css)); | |
| 238 ASSERT_EQ(1u, css->GetSize()); | |
| 239 std::string css_value; | |
| 240 ASSERT_EQ(true, css->GetString(0, &css_value)); | |
| 241 ASSERT_EQ("video", css_value); | |
| 242 | |
| 243 ASSERT_EQ(true, get_rules[1]->actions[0]->GetAsDictionary(&action)); | |
| 244 ASSERT_EQ(true, action->GetStringASCII("instanceType", &instance_type)); | |
| 245 ASSERT_EQ("declarativeContent.ShowPageAction", instance_type); | |
| 246 ASSERT_EQ(1u, get_rules[1]->conditions.size()); | |
| 247 ASSERT_EQ(true, get_rules[1]->conditions[0]->GetAsDictionary(&condition)); | |
| 248 ASSERT_EQ(true, condition->GetStringASCII("instanceType", &instance_type)); | |
| 249 ASSERT_EQ("declarativeContent.PageStateMatcher", instance_type); | |
| 250 ASSERT_EQ(true, condition->GetList("css", &css)); | |
| 251 ASSERT_EQ(1u, css->GetSize()); | |
| 252 ASSERT_EQ(true, css->GetString(0, &css_value)); | |
| 253 ASSERT_EQ("input[type='password']", css_value); | |
|
not at google - send to devlin
2015/06/04 17:58:49
You can have a much easier time here defining the
danduong
2015/06/04 19:56:07
Done.
| |
| 254 } | |
| 255 | |
| 256 TEST_F(RulesRegistryTest, DeleteRuleInManifest) { | |
| 257 base::MessageLoopForUI message_loop; | |
| 258 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 259 | |
| 260 // Create extension | |
| 261 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 262 "{" | |
| 263 " \"name\": \"Test\"," | |
| 264 " \"version\": \"1\"," | |
| 265 " \"event_rules\": [{" | |
| 266 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 267 " \"actions\": [{" | |
| 268 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 269 " }]," | |
| 270 " \"conditions\" : [{" | |
| 271 " \"css\": [\"video\"]," | |
| 272 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 273 " }]" | |
| 274 " }]" | |
| 275 "}"); | |
| 276 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 277 .SetManifest(manifest.Pass()) | |
| 278 .SetID(kExtensionId) | |
| 279 .Build(); | |
| 280 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
| 281 | |
| 282 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 283 browser_context(), "declarativeContent.onPageChanged", | |
| 284 content::BrowserThread::UI, nullptr, key); | |
| 285 // Simulate what RulesRegistryService would do on extension load. | |
| 286 registry->OnExtensionLoaded(kExtensionId); | |
| 287 // Add some extra rules outside of the manifest. | |
| 288 std::vector<linked_ptr<RulesRegistry::Rule>> add_rules; | |
| 289 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 290 add_rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); | |
| 291 registry->AddRules(kExtensionId, add_rules); | |
| 292 | |
| 293 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 294 registry->GetAllRules(kExtensionId, &get_rules); | |
| 295 ASSERT_EQ(3u, get_rules.size()); | |
| 296 | |
| 297 // Remove a rule from outside the manifest. | |
| 298 std::vector<std::string> remove_ids; | |
| 299 remove_ids.push_back(*(get_rules[1]->id)); | |
| 300 ASSERT_EQ(true, registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 301 get_rules.clear(); | |
| 302 registry->GetAllRules(kExtensionId, &get_rules); | |
| 303 ASSERT_EQ(2u, get_rules.size()); | |
| 304 | |
| 305 // Attempt to remove rule in manifest. | |
| 306 remove_ids.clear(); | |
| 307 remove_ids.push_back(*(get_rules[0]->id)); | |
| 308 ASSERT_EQ(false, registry->RemoveRules(kExtensionId, remove_ids).empty()); | |
| 309 get_rules.clear(); | |
| 310 registry->GetAllRules(kExtensionId, &get_rules); | |
| 311 ASSERT_EQ(2u, get_rules.size()); | |
| 312 | |
| 313 // Remove all rules. | |
| 314 registry->RemoveAllRules(kExtensionId); | |
| 315 get_rules.clear(); | |
| 316 registry->GetAllRules(kExtensionId, &get_rules); | |
| 317 ASSERT_EQ(0u, get_rules.size()); | |
| 318 } | |
| 319 | |
|
not at google - send to devlin
2015/06/04 17:58:49
Test somewhere around here what happens if you try
danduong
2015/06/04 19:10:59
I believe that is tested unless you mean something
| |
| 320 TEST_F(RulesRegistryTest, TwoBadRulesOneGoodInManifest) { | |
| 321 base::MessageLoopForUI message_loop; | |
| 322 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
| 323 | |
| 324 // Create extension | |
| 325 scoped_ptr<base::DictionaryValue> manifest = ParseDictionary( | |
| 326 "{" | |
| 327 " \"name\": \"Test\"," | |
| 328 " \"version\": \"1\"," | |
| 329 " \"event_rules\": [" | |
| 330 " {" | |
| 331 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 332 " \"actions\": [{" | |
| 333 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 334 " }]," | |
| 335 " \"conditions\" : [{" | |
| 336 " \"css\": [\"video\"]," | |
| 337 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 338 " }]" | |
| 339 " }," | |
| 340 " {" | |
| 341 " \"actions\": [{" | |
| 342 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 343 " }]," | |
| 344 " \"conditions\" : [{" | |
| 345 " \"css\": [\"video\"]," | |
| 346 " \"type\" : \"declarativeContent.PageStateMatcher\"" | |
| 347 " }]" | |
| 348 " }," | |
| 349 " {" | |
| 350 " \"event\": \"declarativeContent.onPageChanged\"," | |
| 351 " \"actions\": [{" | |
| 352 " \"type\": \"declarativeContent.ShowPageAction\"" | |
| 353 " }]," | |
| 354 " \"conditions\" : [{}]" | |
| 355 " }" | |
| 356 " ]" | |
| 357 "}"); | |
| 358 scoped_refptr<Extension> extension = ExtensionBuilder() | |
| 359 .SetManifest(manifest.Pass()) | |
| 360 .SetID(kExtensionId) | |
| 361 .Build(); | |
| 362 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
| 363 | |
| 364 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
| 365 browser_context(), "declarativeContent.onPageChanged", | |
| 366 content::BrowserThread::UI, nullptr, key); | |
| 367 // Simulate what RulesRegistryService would do on extension load. | |
| 368 registry->OnExtensionLoaded(kExtensionId); | |
| 369 | |
| 370 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
| 371 registry->GetAllRules(kExtensionId, &get_rules); | |
| 372 | |
| 373 ASSERT_EQ(1u, get_rules.size()); | |
| 374 ASSERT_EQ(1u, get_rules[0]->actions.size()); | |
| 375 const base::DictionaryValue* action; | |
| 376 ASSERT_EQ(true, get_rules[0]->actions[0]->GetAsDictionary(&action)); | |
| 377 std::string instance_type; | |
| 378 ASSERT_EQ(true, action->GetStringASCII("instanceType", &instance_type)); | |
| 379 ASSERT_EQ("declarativeContent.ShowPageAction", instance_type); | |
| 380 ASSERT_EQ(1u, get_rules[0]->conditions.size()); | |
| 381 const base::DictionaryValue* condition; | |
| 382 ASSERT_EQ(true, get_rules[0]->conditions[0]->GetAsDictionary(&condition)); | |
| 383 ASSERT_EQ(true, condition->GetStringASCII("instanceType", &instance_type)); | |
| 384 ASSERT_EQ("declarativeContent.PageStateMatcher", instance_type); | |
| 385 const base::ListValue* css; | |
| 386 ASSERT_EQ(true, condition->GetList("css", &css)); | |
| 387 ASSERT_EQ(1u, css->GetSize()); | |
| 388 std::string css_value; | |
| 389 ASSERT_EQ(true, css->GetString(0, &css_value)); | |
| 390 ASSERT_EQ("video", css_value); | |
| 391 } | |
| 392 | |
| 393 TEST_F(RulesRegistryTest, NoValidRulesInManifest) { | |
| 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()); | |
| 420 } | |
| 421 | |
| 168 } // namespace extensions | 422 } // namespace extensions |
| OLD | NEW |