| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Here we test the TestRulesRegistry which is the simplest possible | 7 // Here we test the TestRulesRegistry which is the simplest possible |
| 8 // implementation of RulesRegistryWithCache as a proxy for | 8 // implementation of RulesRegistryWithCache as a proxy for |
| 9 // RulesRegistryWithCache. | 9 // RulesRegistryWithCache. |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/manifest_constants.h" | 26 #include "extensions/common/manifest_constants.h" |
| 27 #include "extensions/common/permissions/permissions_data.h" | 27 #include "extensions/common/permissions/permissions_data.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 using extension_test_util::LoadManifestUnchecked; | 30 using extension_test_util::LoadManifestUnchecked; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 const char kRuleId[] = "rule"; | 33 const char kRuleId[] = "rule"; |
| 34 const char kRule2Id[] = "rule2"; | 34 const char kRule2Id[] = "rule2"; |
| 35 const int kRulesRegistryID = 0; |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace extensions { | 38 namespace extensions { |
| 38 | 39 |
| 39 class RulesRegistryWithCacheTest : public testing::Test { | 40 class RulesRegistryWithCacheTest : public testing::Test { |
| 40 public: | 41 public: |
| 41 RulesRegistryWithCacheTest() | 42 RulesRegistryWithCacheTest() |
| 42 : cache_delegate_(/*log_storage_init_delay=*/false), | 43 : cache_delegate_(/*log_storage_init_delay=*/false), |
| 43 registry_(new TestRulesRegistry(profile(), | 44 registry_(new TestRulesRegistry(profile(), |
| 44 /*event_name=*/"", | 45 /*event_name=*/"", |
| 45 content::BrowserThread::UI, | 46 content::BrowserThread::UI, |
| 46 &cache_delegate_, | 47 &cache_delegate_, |
| 47 RulesRegistry::WebViewKey(0, 0))) {} | 48 kRulesRegistryID)) {} |
| 48 | 49 |
| 49 void SetUp() override { | 50 void SetUp() override { |
| 50 env_.GetExtensionPrefs(); // Force creation before adding extensions. | 51 env_.GetExtensionPrefs(); // Force creation before adding extensions. |
| 51 // Note that env_.MakeExtension below also forces the creation of | 52 // Note that env_.MakeExtension below also forces the creation of |
| 52 // ExtensionService. | 53 // ExtensionService. |
| 53 | 54 |
| 54 base::DictionaryValue manifest_extra; | 55 base::DictionaryValue manifest_extra; |
| 55 std::string key; | 56 std::string key; |
| 56 CHECK(Extension::ProducePEM("test extension 1", &key)); | 57 CHECK(Extension::ProducePEM("test extension 1", &key)); |
| 57 manifest_extra.SetString(manifest_keys::kPublicKey, key); | 58 manifest_extra.SetString(manifest_keys::kPublicKey, key); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { | 227 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { |
| 227 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); | 228 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); |
| 228 // The value store is first created during GetExtensionService. | 229 // The value store is first created during GetExtensionService. |
| 229 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); | 230 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); |
| 230 | 231 |
| 231 const std::string event_name("testEvent"); | 232 const std::string event_name("testEvent"); |
| 232 const std::string rules_stored_key( | 233 const std::string rules_stored_key( |
| 233 RulesCacheDelegate::GetRulesStoredKey( | 234 RulesCacheDelegate::GetRulesStoredKey( |
| 234 event_name, profile()->IsOffTheRecord())); | 235 event_name, profile()->IsOffTheRecord())); |
| 235 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 236 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 236 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( | 237 scoped_refptr<RulesRegistry> registry( |
| 237 profile(), event_name, content::BrowserThread::UI, | 238 new TestRulesRegistry(profile(), event_name, content::BrowserThread::UI, |
| 238 cache_delegate.get(), | 239 cache_delegate.get(), kRulesRegistryID)); |
| 239 RulesRegistry::WebViewKey(0, 0))); | |
| 240 | 240 |
| 241 // 1. Test the handling of preferences. | 241 // 1. Test the handling of preferences. |
| 242 // Default value is always true. | 242 // Default value is always true. |
| 243 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 243 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 244 | 244 |
| 245 extension_prefs->UpdateExtensionPref( | 245 extension_prefs->UpdateExtensionPref( |
| 246 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); | 246 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); |
| 247 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 247 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 248 | 248 |
| 249 extension_prefs->UpdateExtensionPref( | 249 extension_prefs->UpdateExtensionPref( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 296 |
| 297 const std::string event_name1("testEvent1"); | 297 const std::string event_name1("testEvent1"); |
| 298 const std::string event_name2("testEvent2"); | 298 const std::string event_name2("testEvent2"); |
| 299 const std::string rules_stored_key1( | 299 const std::string rules_stored_key1( |
| 300 RulesCacheDelegate::GetRulesStoredKey( | 300 RulesCacheDelegate::GetRulesStoredKey( |
| 301 event_name1, profile()->IsOffTheRecord())); | 301 event_name1, profile()->IsOffTheRecord())); |
| 302 const std::string rules_stored_key2( | 302 const std::string rules_stored_key2( |
| 303 RulesCacheDelegate::GetRulesStoredKey( | 303 RulesCacheDelegate::GetRulesStoredKey( |
| 304 event_name2, profile()->IsOffTheRecord())); | 304 event_name2, profile()->IsOffTheRecord())); |
| 305 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); | 305 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); |
| 306 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( | 306 scoped_refptr<RulesRegistry> registry1( |
| 307 profile(), event_name1, content::BrowserThread::UI, | 307 new TestRulesRegistry(profile(), event_name1, content::BrowserThread::UI, |
| 308 cache_delegate1.get(), | 308 cache_delegate1.get(), kRulesRegistryID)); |
| 309 RulesRegistry::WebViewKey(0, 0))); | |
| 310 | 309 |
| 311 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); | 310 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); |
| 312 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( | 311 scoped_refptr<RulesRegistry> registry2( |
| 313 profile(), event_name2, content::BrowserThread::UI, | 312 new TestRulesRegistry(profile(), event_name2, content::BrowserThread::UI, |
| 314 cache_delegate2.get(), | 313 cache_delegate2.get(), kRulesRegistryID)); |
| 315 RulesRegistry::WebViewKey(0, 0))); | |
| 316 | 314 |
| 317 // Checkt the correct default values. | 315 // Checkt the correct default values. |
| 318 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); | 316 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); |
| 319 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); | 317 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); |
| 320 | 318 |
| 321 // Update the flag for the first registry. | 319 // Update the flag for the first registry. |
| 322 extension_prefs->UpdateExtensionPref( | 320 extension_prefs->UpdateExtensionPref( |
| 323 extension1_->id(), rules_stored_key1, new base::FundamentalValue(false)); | 321 extension1_->id(), rules_stored_key1, new base::FundamentalValue(false)); |
| 324 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); | 322 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); |
| 325 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); | 323 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 348 extension_service->AddExtension(extension.get()); | 346 extension_service->AddExtension(extension.get()); |
| 349 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) | 347 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) |
| 350 ->enabled_extensions() | 348 ->enabled_extensions() |
| 351 .Contains(extension->id())); | 349 .Contains(extension->id())); |
| 352 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( | 350 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 353 APIPermission::kDeclarativeWebRequest)); | 351 APIPermission::kDeclarativeWebRequest)); |
| 354 env_.GetExtensionSystem()->SetReady(); | 352 env_.GetExtensionSystem()->SetReady(); |
| 355 | 353 |
| 356 // 2. First run, adding a rule for the extension. | 354 // 2. First run, adding a rule for the extension. |
| 357 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 355 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 358 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( | 356 scoped_refptr<TestRulesRegistry> registry( |
| 359 profile(), | 357 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, |
| 360 "testEvent", | 358 cache_delegate.get(), kRulesRegistryID)); |
| 361 content::BrowserThread::UI, | |
| 362 cache_delegate.get(), | |
| 363 RulesRegistry::WebViewKey(0, 0))); | |
| 364 | 359 |
| 365 AddRule(extension1_->id(), kRuleId, registry.get()); | 360 AddRule(extension1_->id(), kRuleId, registry.get()); |
| 366 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. | 361 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. |
| 367 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); | 362 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); |
| 368 | 363 |
| 369 // 3. Restart the TestRulesRegistry and see the rule still there. | 364 // 3. Restart the TestRulesRegistry and see the rule still there. |
| 370 cache_delegate.reset(new RulesCacheDelegate(false)); | 365 cache_delegate.reset(new RulesCacheDelegate(false)); |
| 371 registry = new TestRulesRegistry( | 366 registry = |
| 372 profile(), | 367 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, |
| 373 "testEvent", | 368 cache_delegate.get(), kRulesRegistryID); |
| 374 content::BrowserThread::UI, | |
| 375 cache_delegate.get(), | |
| 376 RulesRegistry::WebViewKey(0, 0)); | |
| 377 | 369 |
| 378 base::RunLoop().RunUntilIdle(); // Posted tasks retrieve the stored rule. | 370 base::RunLoop().RunUntilIdle(); // Posted tasks retrieve the stored rule. |
| 379 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); | 371 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); |
| 380 } | 372 } |
| 381 | 373 |
| 382 TEST_F(RulesRegistryWithCacheTest, ConcurrentStoringOfRules) { | 374 TEST_F(RulesRegistryWithCacheTest, ConcurrentStoringOfRules) { |
| 383 // When an extension updates its rules, the new set of rules is stored to disk | 375 // When an extension updates its rules, the new set of rules is stored to disk |
| 384 // with some delay. While it is acceptable for a quick series of updates for a | 376 // with some delay. While it is acceptable for a quick series of updates for a |
| 385 // single extension to only write the last one, we should never forget to | 377 // single extension to only write the last one, we should never forget to |
| 386 // write a rules update for extension A, just because it is immediately | 378 // write a rules update for extension A, just because it is immediately |
| 387 // followed by a rules update for extension B. | 379 // followed by a rules update for extension B. |
| 388 extensions::TestExtensionSystem* system = env_.GetExtensionSystem(); | 380 extensions::TestExtensionSystem* system = env_.GetExtensionSystem(); |
| 389 TestingValueStore* store = system->value_store(); | 381 TestingValueStore* store = system->value_store(); |
| 390 | 382 |
| 391 int write_count = store->write_count(); | 383 int write_count = store->write_count(); |
| 392 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); | 384 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); |
| 393 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); | 385 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); |
| 394 env_.GetExtensionSystem()->SetReady(); | 386 env_.GetExtensionSystem()->SetReady(); |
| 395 base::RunLoop().RunUntilIdle(); | 387 base::RunLoop().RunUntilIdle(); |
| 396 EXPECT_EQ(write_count + 2, store->write_count()); | 388 EXPECT_EQ(write_count + 2, store->write_count()); |
| 397 } | 389 } |
| 398 | 390 |
| 399 } // namespace extensions | 391 } // namespace extensions |
| OLD | NEW |