| 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 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 EXPECT_EQ(1, GetNumberOfRules(extension2_->id())); | 224 EXPECT_EQ(1, GetNumberOfRules(extension2_->id())); |
| 225 } | 225 } |
| 226 | 226 |
| 227 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { | 227 TEST_F(RulesRegistryWithCacheTest, DeclarativeRulesStored) { |
| 228 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); | 228 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); |
| 229 | 229 |
| 230 const std::string event_name("testEvent"); | 230 const std::string event_name("testEvent"); |
| 231 const std::string rules_stored_key( | 231 const std::string rules_stored_key( |
| 232 RulesCacheDelegate::GetRulesStoredKey( | 232 RulesCacheDelegate::GetRulesStoredKey( |
| 233 event_name, profile()->IsOffTheRecord())); | 233 event_name, profile()->IsOffTheRecord())); |
| 234 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 234 std::unique_ptr<RulesCacheDelegate> cache_delegate( |
| 235 new RulesCacheDelegate(false)); |
| 235 scoped_refptr<RulesRegistry> registry( | 236 scoped_refptr<RulesRegistry> registry( |
| 236 new TestRulesRegistry(profile(), event_name, content::BrowserThread::UI, | 237 new TestRulesRegistry(profile(), event_name, content::BrowserThread::UI, |
| 237 cache_delegate.get(), kRulesRegistryID)); | 238 cache_delegate.get(), kRulesRegistryID)); |
| 238 | 239 |
| 239 // 1. Test the handling of preferences. | 240 // 1. Test the handling of preferences. |
| 240 // Default value is always true. | 241 // Default value is always true. |
| 241 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 242 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 242 | 243 |
| 243 extension_prefs->UpdateExtensionPref( | 244 extension_prefs->UpdateExtensionPref( |
| 244 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); | 245 extension1_->id(), rules_stored_key, new base::FundamentalValue(false)); |
| 245 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 246 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 246 | 247 |
| 247 extension_prefs->UpdateExtensionPref( | 248 extension_prefs->UpdateExtensionPref( |
| 248 extension1_->id(), rules_stored_key, new base::FundamentalValue(true)); | 249 extension1_->id(), rules_stored_key, new base::FundamentalValue(true)); |
| 249 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 250 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 250 | 251 |
| 251 // 2. Test writing behavior. | 252 // 2. Test writing behavior. |
| 252 // The ValueStore is lazily created when reading/writing. None done yet so | 253 // The ValueStore is lazily created when reading/writing. None done yet so |
| 253 // verify that not yet created. | 254 // verify that not yet created. |
| 254 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); | 255 TestingValueStore* store = env_.GetExtensionSystem()->value_store(); |
| 255 // No write yet so no store should have been created. | 256 // No write yet so no store should have been created. |
| 256 ASSERT_FALSE(store); | 257 ASSERT_FALSE(store); |
| 257 | 258 |
| 258 scoped_ptr<base::ListValue> value(new base::ListValue); | 259 std::unique_ptr<base::ListValue> value(new base::ListValue); |
| 259 value->AppendBoolean(true); | 260 value->AppendBoolean(true); |
| 260 cache_delegate->WriteToStorage(extension1_->id(), std::move(value)); | 261 cache_delegate->WriteToStorage(extension1_->id(), std::move(value)); |
| 261 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); | 262 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(extension1_->id())); |
| 262 base::RunLoop().RunUntilIdle(); | 263 base::RunLoop().RunUntilIdle(); |
| 263 store = env_.GetExtensionSystem()->value_store(); | 264 store = env_.GetExtensionSystem()->value_store(); |
| 264 ASSERT_TRUE(store); | 265 ASSERT_TRUE(store); |
| 265 EXPECT_EQ(1, store->write_count()); | 266 EXPECT_EQ(1, store->write_count()); |
| 266 int write_count = store->write_count(); | 267 int write_count = store->write_count(); |
| 267 | 268 |
| 268 value.reset(new base::ListValue); | 269 value.reset(new base::ListValue); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 299 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); | 300 ExtensionPrefs* extension_prefs = env_.GetExtensionPrefs(); |
| 300 | 301 |
| 301 const std::string event_name1("testEvent1"); | 302 const std::string event_name1("testEvent1"); |
| 302 const std::string event_name2("testEvent2"); | 303 const std::string event_name2("testEvent2"); |
| 303 const std::string rules_stored_key1( | 304 const std::string rules_stored_key1( |
| 304 RulesCacheDelegate::GetRulesStoredKey( | 305 RulesCacheDelegate::GetRulesStoredKey( |
| 305 event_name1, profile()->IsOffTheRecord())); | 306 event_name1, profile()->IsOffTheRecord())); |
| 306 const std::string rules_stored_key2( | 307 const std::string rules_stored_key2( |
| 307 RulesCacheDelegate::GetRulesStoredKey( | 308 RulesCacheDelegate::GetRulesStoredKey( |
| 308 event_name2, profile()->IsOffTheRecord())); | 309 event_name2, profile()->IsOffTheRecord())); |
| 309 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); | 310 std::unique_ptr<RulesCacheDelegate> cache_delegate1( |
| 311 new RulesCacheDelegate(false)); |
| 310 scoped_refptr<RulesRegistry> registry1( | 312 scoped_refptr<RulesRegistry> registry1( |
| 311 new TestRulesRegistry(profile(), event_name1, content::BrowserThread::UI, | 313 new TestRulesRegistry(profile(), event_name1, content::BrowserThread::UI, |
| 312 cache_delegate1.get(), kRulesRegistryID)); | 314 cache_delegate1.get(), kRulesRegistryID)); |
| 313 | 315 |
| 314 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); | 316 std::unique_ptr<RulesCacheDelegate> cache_delegate2( |
| 317 new RulesCacheDelegate(false)); |
| 315 scoped_refptr<RulesRegistry> registry2( | 318 scoped_refptr<RulesRegistry> registry2( |
| 316 new TestRulesRegistry(profile(), event_name2, content::BrowserThread::UI, | 319 new TestRulesRegistry(profile(), event_name2, content::BrowserThread::UI, |
| 317 cache_delegate2.get(), kRulesRegistryID)); | 320 cache_delegate2.get(), kRulesRegistryID)); |
| 318 | 321 |
| 319 // Checkt the correct default values. | 322 // Checkt the correct default values. |
| 320 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); | 323 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(extension1_->id())); |
| 321 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); | 324 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(extension1_->id())); |
| 322 | 325 |
| 323 // Update the flag for the first registry. | 326 // Update the flag for the first registry. |
| 324 extension_prefs->UpdateExtensionPref( | 327 extension_prefs->UpdateExtensionPref( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 349 ASSERT_TRUE(error.empty()); | 352 ASSERT_TRUE(error.empty()); |
| 350 extension_service->AddExtension(extension.get()); | 353 extension_service->AddExtension(extension.get()); |
| 351 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) | 354 EXPECT_TRUE(extensions::ExtensionRegistry::Get(env_.profile()) |
| 352 ->enabled_extensions() | 355 ->enabled_extensions() |
| 353 .Contains(extension->id())); | 356 .Contains(extension->id())); |
| 354 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( | 357 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission( |
| 355 APIPermission::kDeclarativeWebRequest)); | 358 APIPermission::kDeclarativeWebRequest)); |
| 356 env_.GetExtensionSystem()->SetReady(); | 359 env_.GetExtensionSystem()->SetReady(); |
| 357 | 360 |
| 358 // 2. First run, adding a rule for the extension. | 361 // 2. First run, adding a rule for the extension. |
| 359 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 362 std::unique_ptr<RulesCacheDelegate> cache_delegate( |
| 363 new RulesCacheDelegate(false)); |
| 360 scoped_refptr<TestRulesRegistry> registry( | 364 scoped_refptr<TestRulesRegistry> registry( |
| 361 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, | 365 new TestRulesRegistry(profile(), "testEvent", content::BrowserThread::UI, |
| 362 cache_delegate.get(), kRulesRegistryID)); | 366 cache_delegate.get(), kRulesRegistryID)); |
| 363 | 367 |
| 364 AddRule(extension1_->id(), kRuleId, registry.get()); | 368 AddRule(extension1_->id(), kRuleId, registry.get()); |
| 365 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. | 369 base::RunLoop().RunUntilIdle(); // Posted tasks store the added rule. |
| 366 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); | 370 EXPECT_EQ(1, GetNumberOfRules(extension1_->id(), registry.get())); |
| 367 | 371 |
| 368 // 3. Restart the TestRulesRegistry and see the rule still there. | 372 // 3. Restart the TestRulesRegistry and see the rule still there. |
| 369 cache_delegate.reset(new RulesCacheDelegate(false)); | 373 cache_delegate.reset(new RulesCacheDelegate(false)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 387 | 391 |
| 388 int write_count = 0; | 392 int write_count = 0; |
| 389 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); | 393 EXPECT_EQ("", AddRule(extension1_->id(), kRuleId)); |
| 390 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); | 394 EXPECT_EQ("", AddRule(extension2_->id(), kRule2Id)); |
| 391 env_.GetExtensionSystem()->SetReady(); | 395 env_.GetExtensionSystem()->SetReady(); |
| 392 base::RunLoop().RunUntilIdle(); | 396 base::RunLoop().RunUntilIdle(); |
| 393 EXPECT_EQ(write_count + 2, system->value_store()->write_count()); | 397 EXPECT_EQ(write_count + 2, system->value_store()->write_count()); |
| 394 } | 398 } |
| 395 | 399 |
| 396 } // namespace extensions | 400 } // namespace extensions |
| OLD | NEW |