| 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 CommandLine::ForCurrentProcess(), base::FilePath()); | 225 CommandLine::ForCurrentProcess(), base::FilePath()); |
| 226 system->CreateExtensionService( | 226 system->CreateExtensionService( |
| 227 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 227 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 228 // The value store is first created during CreateExtensionService. | 228 // The value store is first created during CreateExtensionService. |
| 229 TestingValueStore* store = system->value_store(); | 229 TestingValueStore* store = system->value_store(); |
| 230 | 230 |
| 231 const std::string event_name("testEvent"); | 231 const std::string event_name("testEvent"); |
| 232 const std::string rules_stored_key( | 232 const std::string rules_stored_key( |
| 233 RulesCacheDelegate::GetRulesStoredKey( | 233 RulesCacheDelegate::GetRulesStoredKey( |
| 234 event_name, profile.IsOffTheRecord())); | 234 event_name, profile.IsOffTheRecord())); |
| 235 scoped_ptr<RulesCacheDelegate> ui_part; | 235 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 236 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( | 236 scoped_refptr<RulesRegistry> registry(new TestRulesRegistry( |
| 237 &profile, event_name, content::BrowserThread::UI, &ui_part)); | 237 &profile, event_name, content::BrowserThread::UI, cache_delegate.get())); |
| 238 | 238 |
| 239 // 1. Test the handling of preferences. | 239 // 1. Test the handling of preferences. |
| 240 // Default value is always true. | 240 // Default value is always true. |
| 241 EXPECT_TRUE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 241 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 242 | 242 |
| 243 extension_prefs->UpdateExtensionPref( | 243 extension_prefs->UpdateExtensionPref( |
| 244 kExtensionId, rules_stored_key, new base::FundamentalValue(false)); | 244 kExtensionId, rules_stored_key, new base::FundamentalValue(false)); |
| 245 EXPECT_FALSE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 245 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 246 | 246 |
| 247 extension_prefs->UpdateExtensionPref( | 247 extension_prefs->UpdateExtensionPref( |
| 248 kExtensionId, rules_stored_key, new base::FundamentalValue(true)); | 248 kExtensionId, rules_stored_key, new base::FundamentalValue(true)); |
| 249 EXPECT_TRUE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 249 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 250 | 250 |
| 251 // 2. Test writing behavior. | 251 // 2. Test writing behavior. |
| 252 int write_count = store->write_count(); | 252 int write_count = store->write_count(); |
| 253 | 253 |
| 254 scoped_ptr<base::ListValue> value(new base::ListValue); | 254 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 255 value->AppendBoolean(true); | 255 value->AppendBoolean(true); |
| 256 ui_part->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); | 256 cache_delegate->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); |
| 257 EXPECT_TRUE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 257 EXPECT_TRUE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 258 message_loop_.RunUntilIdle(); | 258 message_loop_.RunUntilIdle(); |
| 259 EXPECT_EQ(write_count + 1, store->write_count()); | 259 EXPECT_EQ(write_count + 1, store->write_count()); |
| 260 write_count = store->write_count(); | 260 write_count = store->write_count(); |
| 261 | 261 |
| 262 value.reset(new base::ListValue); | 262 value.reset(new base::ListValue); |
| 263 ui_part->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); | 263 cache_delegate->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); |
| 264 EXPECT_FALSE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 264 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 265 message_loop_.RunUntilIdle(); | 265 message_loop_.RunUntilIdle(); |
| 266 // No rules currently, but previously there were, so we expect a write. | 266 // No rules currently, but previously there were, so we expect a write. |
| 267 EXPECT_EQ(write_count + 1, store->write_count()); | 267 EXPECT_EQ(write_count + 1, store->write_count()); |
| 268 write_count = store->write_count(); | 268 write_count = store->write_count(); |
| 269 | 269 |
| 270 value.reset(new base::ListValue); | 270 value.reset(new base::ListValue); |
| 271 ui_part->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); | 271 cache_delegate->WriteToStorage(kExtensionId, value.PassAs<base::Value>()); |
| 272 EXPECT_FALSE(ui_part->GetDeclarativeRulesStored(kExtensionId)); | 272 EXPECT_FALSE(cache_delegate->GetDeclarativeRulesStored(kExtensionId)); |
| 273 message_loop_.RunUntilIdle(); | 273 message_loop_.RunUntilIdle(); |
| 274 EXPECT_EQ(write_count, store->write_count()); | 274 EXPECT_EQ(write_count, store->write_count()); |
| 275 | 275 |
| 276 // 3. Test reading behavior. | 276 // 3. Test reading behavior. |
| 277 int read_count = store->read_count(); | 277 int read_count = store->read_count(); |
| 278 | 278 |
| 279 ui_part->SetDeclarativeRulesStored(kExtensionId, false); | 279 cache_delegate->SetDeclarativeRulesStored(kExtensionId, false); |
| 280 ui_part->ReadFromStorage(kExtensionId); | 280 cache_delegate->ReadFromStorage(kExtensionId); |
| 281 message_loop_.RunUntilIdle(); | 281 message_loop_.RunUntilIdle(); |
| 282 EXPECT_EQ(read_count, store->read_count()); | 282 EXPECT_EQ(read_count, store->read_count()); |
| 283 read_count = store->read_count(); | 283 read_count = store->read_count(); |
| 284 | 284 |
| 285 ui_part->SetDeclarativeRulesStored(kExtensionId, true); | 285 cache_delegate->SetDeclarativeRulesStored(kExtensionId, true); |
| 286 ui_part->ReadFromStorage(kExtensionId); | 286 cache_delegate->ReadFromStorage(kExtensionId); |
| 287 message_loop_.RunUntilIdle(); | 287 message_loop_.RunUntilIdle(); |
| 288 EXPECT_EQ(read_count + 1, store->read_count()); | 288 EXPECT_EQ(read_count + 1, store->read_count()); |
| 289 } | 289 } |
| 290 | 290 |
| 291 // Test that each registry has its own "are some rules stored" flag. | 291 // Test that each registry has its own "are some rules stored" flag. |
| 292 TEST_F(RulesRegistryWithCacheTest, RulesStoredFlagMultipleRegistries) { | 292 TEST_F(RulesRegistryWithCacheTest, RulesStoredFlagMultipleRegistries) { |
| 293 TestingProfile profile; | 293 TestingProfile profile; |
| 294 // TestingProfile::Init makes sure that the factory method for a corresponding | 294 // TestingProfile::Init makes sure that the factory method for a corresponding |
| 295 // extension system creates a TestExtensionSystem. | 295 // extension system creates a TestExtensionSystem. |
| 296 extensions::TestExtensionSystem* system = | 296 extensions::TestExtensionSystem* system = |
| 297 static_cast<extensions::TestExtensionSystem*>( | 297 static_cast<extensions::TestExtensionSystem*>( |
| 298 extensions::ExtensionSystem::Get(&profile)); | 298 extensions::ExtensionSystem::Get(&profile)); |
| 299 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( | 299 ExtensionPrefs* extension_prefs = system->CreateExtensionPrefs( |
| 300 CommandLine::ForCurrentProcess(), base::FilePath()); | 300 CommandLine::ForCurrentProcess(), base::FilePath()); |
| 301 | 301 |
| 302 const std::string event_name1("testEvent1"); | 302 const std::string event_name1("testEvent1"); |
| 303 const std::string event_name2("testEvent2"); | 303 const std::string event_name2("testEvent2"); |
| 304 const std::string rules_stored_key1( | 304 const std::string rules_stored_key1( |
| 305 RulesCacheDelegate::GetRulesStoredKey( | 305 RulesCacheDelegate::GetRulesStoredKey( |
| 306 event_name1, profile.IsOffTheRecord())); | 306 event_name1, profile.IsOffTheRecord())); |
| 307 const std::string rules_stored_key2( | 307 const std::string rules_stored_key2( |
| 308 RulesCacheDelegate::GetRulesStoredKey( | 308 RulesCacheDelegate::GetRulesStoredKey( |
| 309 event_name2, profile.IsOffTheRecord())); | 309 event_name2, profile.IsOffTheRecord())); |
| 310 scoped_ptr<RulesCacheDelegate> ui_part1; | 310 scoped_ptr<RulesCacheDelegate> cache_delegate1(new RulesCacheDelegate(false)); |
| 311 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( | 311 scoped_refptr<RulesRegistry> registry1(new TestRulesRegistry( |
| 312 &profile, event_name1, content::BrowserThread::UI, &ui_part1)); | 312 &profile, event_name1, content::BrowserThread::UI, |
| 313 scoped_ptr<RulesCacheDelegate> ui_part2; | 313 cache_delegate1.get())); |
| 314 |
| 315 scoped_ptr<RulesCacheDelegate> cache_delegate2(new RulesCacheDelegate(false)); |
| 314 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( | 316 scoped_refptr<RulesRegistry> registry2(new TestRulesRegistry( |
| 315 &profile, event_name2, content::BrowserThread::UI, &ui_part2)); | 317 &profile, event_name2, content::BrowserThread::UI, |
| 318 cache_delegate2.get())); |
| 316 | 319 |
| 317 // Checkt the correct default values. | 320 // Checkt the correct default values. |
| 318 EXPECT_TRUE(ui_part1->GetDeclarativeRulesStored(kExtensionId)); | 321 EXPECT_TRUE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); |
| 319 EXPECT_TRUE(ui_part2->GetDeclarativeRulesStored(kExtensionId)); | 322 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); |
| 320 | 323 |
| 321 // Update the flag for the first registry. | 324 // Update the flag for the first registry. |
| 322 extension_prefs->UpdateExtensionPref( | 325 extension_prefs->UpdateExtensionPref( |
| 323 kExtensionId, rules_stored_key1, new base::FundamentalValue(false)); | 326 kExtensionId, rules_stored_key1, new base::FundamentalValue(false)); |
| 324 EXPECT_FALSE(ui_part1->GetDeclarativeRulesStored(kExtensionId)); | 327 EXPECT_FALSE(cache_delegate1->GetDeclarativeRulesStored(kExtensionId)); |
| 325 EXPECT_TRUE(ui_part2->GetDeclarativeRulesStored(kExtensionId)); | 328 EXPECT_TRUE(cache_delegate2->GetDeclarativeRulesStored(kExtensionId)); |
| 326 } | 329 } |
| 327 | 330 |
| 328 TEST_F(RulesRegistryWithCacheTest, RulesPreservedAcrossRestart) { | 331 TEST_F(RulesRegistryWithCacheTest, RulesPreservedAcrossRestart) { |
| 329 // This test makes sure that rules are restored from the rule store | 332 // This test makes sure that rules are restored from the rule store |
| 330 // on registry (in particular, browser) restart. | 333 // on registry (in particular, browser) restart. |
| 331 | 334 |
| 332 TestingProfile profile; | 335 TestingProfile profile; |
| 333 extensions::TestExtensionSystem* system = | 336 extensions::TestExtensionSystem* system = |
| 334 static_cast<extensions::TestExtensionSystem*>( | 337 static_cast<extensions::TestExtensionSystem*>( |
| 335 extensions::ExtensionSystem::Get(&profile)); | 338 extensions::ExtensionSystem::Get(&profile)); |
| 336 ExtensionService* extension_service = system->CreateExtensionService( | 339 ExtensionService* extension_service = system->CreateExtensionService( |
| 337 CommandLine::ForCurrentProcess(), base::FilePath(), false); | 340 CommandLine::ForCurrentProcess(), base::FilePath(), false); |
| 338 | 341 |
| 339 // 1. Add an extension, before rules registry gets created. | 342 // 1. Add an extension, before rules registry gets created. |
| 340 std::string error; | 343 std::string error; |
| 341 scoped_refptr<Extension> extension( | 344 scoped_refptr<Extension> extension( |
| 342 LoadManifestUnchecked("permissions", | 345 LoadManifestUnchecked("permissions", |
| 343 "web_request_all_host_permissions.json", | 346 "web_request_all_host_permissions.json", |
| 344 Manifest::INVALID_LOCATION, | 347 Manifest::INVALID_LOCATION, |
| 345 Extension::NO_FLAGS, | 348 Extension::NO_FLAGS, |
| 346 kExtensionId, | 349 kExtensionId, |
| 347 &error)); | 350 &error)); |
| 348 ASSERT_TRUE(error.empty()); | 351 ASSERT_TRUE(error.empty()); |
| 349 extension_service->AddExtension(extension.get()); | 352 extension_service->AddExtension(extension.get()); |
| 350 system->SetReady(); | 353 system->SetReady(); |
| 351 | 354 |
| 352 // 2. First run, adding a rule for the extension. | 355 // 2. First run, adding a rule for the extension. |
| 353 scoped_ptr<RulesCacheDelegate> ui_part; | 356 scoped_ptr<RulesCacheDelegate> cache_delegate(new RulesCacheDelegate(false)); |
| 354 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( | 357 scoped_refptr<TestRulesRegistry> registry(new TestRulesRegistry( |
| 355 &profile, "testEvent", content::BrowserThread::UI, &ui_part)); | 358 &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get())); |
| 359 |
| 356 AddRule(kExtensionId, kRuleId, registry.get()); | 360 AddRule(kExtensionId, kRuleId, registry.get()); |
| 357 message_loop_.RunUntilIdle(); // Posted tasks store the added rule. | 361 message_loop_.RunUntilIdle(); // Posted tasks store the added rule. |
| 358 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); | 362 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); |
| 359 | 363 |
| 360 // 3. Restart the TestRulesRegistry and see the rule still there. | 364 // 3. Restart the TestRulesRegistry and see the rule still there. |
| 365 cache_delegate.reset( |
| 366 new RulesCacheDelegate(false)); |
| 361 registry = new TestRulesRegistry( | 367 registry = new TestRulesRegistry( |
| 362 &profile, "testEvent", content::BrowserThread::UI, &ui_part); | 368 &profile, "testEvent", content::BrowserThread::UI, cache_delegate.get()); |
| 369 |
| 363 message_loop_.RunUntilIdle(); // Posted tasks retrieve the stored rule. | 370 message_loop_.RunUntilIdle(); // Posted tasks retrieve the stored rule. |
| 364 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); | 371 EXPECT_EQ(1, GetNumberOfRules(kExtensionId, registry.get())); |
| 365 } | 372 } |
| 366 | 373 |
| 367 } // namespace extensions | 374 } // namespace extensions |
| OLD | NEW |