| 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 "chrome/browser/extensions/api/declarative/rules_registry.h" | 5 #include "chrome/browser/extensions/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 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 | 40 |
| 41 namespace extensions { | 41 namespace extensions { |
| 42 | 42 |
| 43 class RulesRegistryWithCacheTest : public testing::Test { | 43 class RulesRegistryWithCacheTest : public testing::Test { |
| 44 public: | 44 public: |
| 45 RulesRegistryWithCacheTest() | 45 RulesRegistryWithCacheTest() |
| 46 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 46 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 47 file_thread_(content::BrowserThread::FILE, &message_loop_), | 47 file_thread_(content::BrowserThread::FILE, &message_loop_), |
| 48 registry_(new TestRulesRegistry(content::BrowserThread::UI, | 48 registry_(new TestRulesRegistry(content::BrowserThread::UI, |
| 49 "" /*event_name*/)) {} | 49 "" /*event_name*/, |
| 50 RulesRegistry::WebViewKey(0, 0))) {} |
| 50 | 51 |
| 51 virtual ~RulesRegistryWithCacheTest() {} | 52 virtual ~RulesRegistryWithCacheTest() {} |
| 52 | 53 |
| 53 virtual void TearDown() OVERRIDE { | 54 virtual void TearDown() OVERRIDE { |
| 54 // Make sure that deletion traits of all registries are executed. | 55 // Make sure that deletion traits of all registries are executed. |
| 55 registry_ = NULL; | 56 registry_ = NULL; |
| 56 message_loop_.RunUntilIdle(); | 57 message_loop_.RunUntilIdle(); |
| 57 } | 58 } |
| 58 | 59 |
| 59 std::string AddRule(const std::string& extension_id, | 60 std::string AddRule(const std::string& extension_id, |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 static_cast<extensions::TestExtensionSystem*>( | 223 static_cast<extensions::TestExtensionSystem*>( |
| 223 extensions::ExtensionSystem::Get(&profile)); | 224 extensions::ExtensionSystem::Get(&profile)); |
| 224 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( | 225 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( |
| 225 CommandLine::ForCurrentProcess(), base::FilePath()); | 226 CommandLine::ForCurrentProcess(), base::FilePath()); |
| 226 system->CreateExtensionService( | 227 system->CreateExtensionService( |
| 227 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 228 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 228 // The value store is first created during CreateExtensionService. | 229 // The value store is first created during CreateExtensionService. |
| 229 TestingValueStore* store = system->value_store(); | 230 TestingValueStore* store = system->value_store(); |
| 230 | 231 |
| 231 const std::string event_name("testEvent"); | 232 const std::string event_name("testEvent"); |
| 233 const RulesRegistry::WebViewKey key(0, 0); |
| 232 const std::string rules_stored_key( | 234 const std::string rules_stored_key( |
| 233 RulesCacheDelegate::GetRulesStoredKey( | 235 RulesCacheDelegate::GetRulesStoredKey( |
| 234 event_name, profile.IsOffTheRecord())); | 236 event_name, profile.IsOffTheRecord())); |
| 235 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 237 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 236 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( | 238 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( |
| 237 &profile, event_name, content::BrowserThread::UI, cache_delegate.get())); | 239 &profile, event_name, content::BrowserThread::UI, |
| 240 cache_delegate.get(), |
| 241 RulesRegistry::WebViewKey(0, 0))); |
| 238 | 242 |
| 239 // 1. Test the handling of preferences. | 243 // 1. Test the handling of preferences. |
| 240 // Default value is always true. | 244 // Default value is always true. |
| 241 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); | 245 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 242 | 246 |
| 243 extension_prefs->UpdateExtensionPref( | 247 extension_prefs->UpdateExtensionPref( |
| 244 kExtensionId, rules_stored_key, new base::FundamentalValue(false)); | 248 kExtensionId, rules_stored_key, new base::FundamentalValue(false)); |
| 245 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); | 249 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 246 | 250 |
| 247 extension_prefs->UpdateExtensionPref( | 251 extension_prefs->UpdateExtensionPref( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // TestingProfile::Init makes sure that the factory method for a corresponding | 298 // TestingProfile::Init makes sure that the factory method for a corresponding |
| 295 // extension system creates a TestExtensionSystem. | 299 // extension system creates a TestExtensionSystem. |
| 296 extensions::TestExtensionSystem* system = | 300 extensions::TestExtensionSystem* system = |
| 297 static_cast<extensions::TestExtensionSystem*>( | 301 static_cast<extensions::TestExtensionSystem*>( |
| 298 extensions::ExtensionSystem::Get(&profile)); | 302 extensions::ExtensionSystem::Get(&profile)); |
| 299 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( | 303 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( |
| 300 CommandLine::ForCurrentProcess(), base::FilePath()); | 304 CommandLine::ForCurrentProcess(), base::FilePath()); |
| 301 | 305 |
| 302 const std::string event_name1("testEvent1"); | 306 const std::string event_name1("testEvent1"); |
| 303 const std::string event_name2("testEvent2"); | 307 const std::string event_name2("testEvent2"); |
| 308 const RulesRegistry::WebViewKey key(0, 0); |
| 304 const std::string rules_stored_key1( | 309 const std::string rules_stored_key1( |
| 305 RulesCacheDelegate::GetRulesStoredKey( | 310 RulesCacheDelegate::GetRulesStoredKey( |
| 306 event_name1, profile.IsOffTheRecord())); | 311 event_name1, profile.IsOffTheRecord())); |
| 307 const std::string rules_stored_key2( | 312 const std::string rules_stored_key2( |
| 308 RulesCacheDelegate::GetRulesStoredKey( | 313 RulesCacheDelegate::GetRulesStoredKey( |
| 309 event_name2, profile.IsOffTheRecord())); | 314 event_name2, profile.IsOffTheRecord())); |
| 310 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); | 315 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); |
| 311 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( | 316 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( |
| 312 &profile, event_name1, content::BrowserThread::UI, | 317 &profile, event_name1, content::BrowserThread::UI, |
| 313 cache_delegate1.get())); | 318 cache_delegate1.get(), |
| 319 RulesRegistry::WebViewKey(0, 0))); |
| 314 | 320 |
| 315 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); | 321 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); |
| 316 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( | 322 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( |
| 317 &profile, event_name2, content::BrowserThread::UI, | 323 &profile, event_name2, content::BrowserThread::UI, |
| 318 cache_delegate2.get())); | 324 cache_delegate2.get(), |
| 325 RulesRegistry::WebViewKey(0, 0))); |
| 319 | 326 |
| 320 // Checkt the correct default values. | 327 // Checkt the correct default values. |
| 321 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); | 328 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); |
| 322 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); | 329 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); |
| 323 | 330 |
| 324 // Update the flag for the first registry. | 331 // Update the flag for the first registry. |
| 325 extension_prefs->UpdateExtensionPref( | 332 extension_prefs->UpdateExtensionPref( |
| 326 kExtensionId, rules_stored_key1, new base::FundamentalValue(false)); | 333 kExtensionId, rules_stored_key1, new base::FundamentalValue(false)); |
| 327 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); | 334 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); |
| 328 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); | 335 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 348 Extension::NO_FLAGS, | 355 Extension::NO_FLAGS, |
| 349 kExtensionId, | 356 kExtensionId, |
| 350 &error)); | 357 &error)); |
| 351 ASSERT_TRUE(error.empty()); | 358 ASSERT_TRUE(error.empty()); |
| 352 extension_service->AddExtension(extension.get()); | 359 extension_service->AddExtension(extension.get()); |
| 353 system->SetReady(); | 360 system->SetReady(); |
| 354 | 361 |
| 355 // 2. First run, adding a rule for the extension. | 362 // 2. First run, adding a rule for the extension. |
| 356 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); | 363 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 357 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( | 364 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( |
| 358 &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get())); | 365 &profile, |
| 366 "testEvent", |
| 367 content::BrowserThread::UI, |
| 368 cache_delegate.get(), |
| 369 RulesRegistry::WebViewKey(0, 0))); |
| 359 | 370 |
| 360 AddRule(kExtensionId, kRuleId, registry.get()); | 371 AddRule(kExtensionId, kRuleId, registry.get()); |
| 361 message_loop_.RunUntilIdle(); // Posted tasks store the added rule. | 372 message_loop_.RunUntilIdle(); // Posted tasks store the added rule. |
| 362 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); | 373 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); |
| 363 | 374 |
| 364 // 3. Restart the TestRulesRegistry and see the rule still there. | 375 // 3. Restart the TestRulesRegistry and see the rule still there. |
| 365 cache_delegate.reset( | 376 cache_delegate.reset(new RulesCacheDelegate(false)); |
| 366 new RulesCacheDelegate(false)); | |
| 367 registry = new TestRulesRegistry( | 377 registry = new TestRulesRegistry( |
| 368 &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get()); | 378 &profile, |
| 379 "testEvent", |
| 380 content::BrowserThread::UI, |
| 381 cache_delegate.get(), |
| 382 RulesRegistry::WebViewKey(0, 0)); |
| 369 | 383 |
| 370 message_loop_.RunUntilIdle(); // Posted tasks retrieve the stored rule. | 384 message_loop_.RunUntilIdle(); // Posted tasks retrieve the stored rule. |
| 371 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); | 385 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); |
| 372 } | 386 } |
| 373 | 387 |
| 374 } // namespace extensions | 388 } // namespace extensions |
| OLD | NEW |