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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" 6 7 #include "base/bind.h" 8 #include "base/message_loop/message_loop.h" 9 #include "chrome/browser/extensions/api/declarative/test_rules_registry.h" 10 #include "content/public/test/test_browser_thread.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace { 14 const char kExtensionId[] = "foo"; 15 16 void InsertRule(scoped_refptr<extensions::RulesRegistry> registry, 17 const std::string& id) { 18 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > add_rules; 19 add_rules.push_back(make_linked_ptr(new extensions::RulesRegistry::Rule)); 20 add_rules[0]->id.reset(new std::string(id)); 21 std::string error = registry->AddRules(kExtensionId, add_rules); 22 EXPECT_TRUE(error.empty()); 23 } 24 25 void VerifyNumberOfRules(scoped_refptr<extensions::RulesRegistry> registry, 26 size_t expected_number_of_rules) { 27 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > get_rules; 28 std::string error = registry->GetAllRules(kExtensionId, &get_rules); 29 EXPECT_EQ(expected_number_of_rules, get_rules.size()); 30 } 31 32 } // namespace 33 34 namespace extensions { 35 36 class RulesRegistryServiceTest : public testing::Test { 37 public: 38 RulesRegistryServiceTest() 39 : ui_(content::BrowserThread::UI, &message_loop_), 40 io_(content::BrowserThread::IO, &message_loop_) {} 41 42 virtual ~RulesRegistryServiceTest() {} 43 44 virtual void TearDown() OVERRIDE { 45 // Make sure that deletion traits of all registries are executed. 46 message_loop_.RunUntilIdle(); 47 } 48 49 protected: 50 base::MessageLoop message_loop_; 51 content::TestBrowserThread ui_; 52 content::TestBrowserThread io_; 53 }; 54 55 TEST_F(RulesRegistryServiceTest, TestConstructionAndMultiThreading) { 56 TestRulesRegistry* ui_registry = 57 new TestRulesRegistry(content::BrowserThread::UI, "ui"); 58 59 TestRulesRegistry* io_registry = 60 new TestRulesRegistry(content::BrowserThread::IO, "io"); 61 62 // Test registration. 63 64 RulesRegistryService registry_service(NULL); 65 registry_service.RegisterRulesRegistry(make_scoped_refptr(ui_registry)); 66 registry_service.RegisterRulesRegistry(make_scoped_refptr(io_registry)); 67 68 EXPECT_TRUE(registry_service.GetRulesRegistry("ui").get()); 69 EXPECT_TRUE(registry_service.GetRulesRegistry("io").get()); 70 EXPECT_FALSE(registry_service.GetRulesRegistry("foo").get()); 71 72 content::BrowserThread::PostTask( 73 content::BrowserThread::UI, FROM_HERE, 74 base::Bind(&InsertRule, registry_service.GetRulesRegistry("ui"), 75 "ui_task")); 76 77 content::BrowserThread::PostTask( 78 content::BrowserThread::IO, FROM_HERE, 79 base::Bind(&InsertRule, registry_service.GetRulesRegistry("io"), 80 "io_task")); 81 82 content::BrowserThread::PostTask( 83 content::BrowserThread::UI, FROM_HERE, 84 base::Bind(&VerifyNumberOfRules, 85 registry_service.GetRulesRegistry("ui"), 1)); 86 87 content::BrowserThread::PostTask( 88 content::BrowserThread::IO, FROM_HERE, 89 base::Bind(&VerifyNumberOfRules, 90 registry_service.GetRulesRegistry("io"), 1)); 91 92 message_loop_.RunUntilIdle(); 93 94 // Test extension unloading. 95 96 registry_service.SimulateExtensionUnloaded(kExtensionId); 97 98 content::BrowserThread::PostTask( 99 content::BrowserThread::UI, FROM_HERE, 100 base::Bind(&VerifyNumberOfRules, 101 registry_service.GetRulesRegistry("ui"), 0)); 102 103 content::BrowserThread::PostTask( 104 content::BrowserThread::IO, FROM_HERE, 105 base::Bind(&VerifyNumberOfRules, 106 registry_service.GetRulesRegistry("io"), 0)); 107 108 message_loop_.RunUntilIdle(); 109 } 110 111 } // namespace extensions 112