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.
5 #include "extensions/browser/api/declarative/rules_registry_service.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "extensions/browser/api/declarative/test_rules_registry.h"
12 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_builder.h"
15 #include "extensions/common/value_builder.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 const char kExtensionId
[] = "foo";
21 void InsertRule(scoped_refptr
<extensions::RulesRegistry
> registry
,
22 const std::string
& id
) {
23 std::vector
<linked_ptr
<extensions::api::events::Rule
>> add_rules
;
24 add_rules
.push_back(make_linked_ptr(new extensions::api::events::Rule
));
25 add_rules
[0]->id
.reset(new std::string(id
));
26 std::string error
= registry
->AddRules(kExtensionId
, add_rules
);
27 EXPECT_TRUE(error
.empty());
30 void VerifyNumberOfRules(scoped_refptr
<extensions::RulesRegistry
> registry
,
31 size_t expected_number_of_rules
) {
32 std::vector
<linked_ptr
<extensions::api::events::Rule
>> get_rules
;
33 registry
->GetAllRules(kExtensionId
, &get_rules
);
34 EXPECT_EQ(expected_number_of_rules
, get_rules
.size());
39 namespace extensions
{
41 class RulesRegistryServiceTest
: public testing::Test
{
43 RulesRegistryServiceTest()
44 : ui_(content::BrowserThread::UI
, &message_loop_
),
45 io_(content::BrowserThread::IO
, &message_loop_
) {}
47 ~RulesRegistryServiceTest() override
{}
49 void TearDown() override
{
50 // Make sure that deletion traits of all registries are executed.
51 message_loop_
.RunUntilIdle();
55 base::MessageLoop message_loop_
;
56 content::TestBrowserThread ui_
;
57 content::TestBrowserThread io_
;
60 TEST_F(RulesRegistryServiceTest
, TestConstructionAndMultiThreading
) {
61 RulesRegistryService
registry_service(NULL
);
63 int key
= RulesRegistryService::kDefaultRulesRegistryID
;
64 TestRulesRegistry
* ui_registry
=
65 new TestRulesRegistry(content::BrowserThread::UI
, "ui", key
);
67 TestRulesRegistry
* io_registry
=
68 new TestRulesRegistry(content::BrowserThread::IO
, "io", key
);
72 registry_service
.RegisterRulesRegistry(make_scoped_refptr(ui_registry
));
73 registry_service
.RegisterRulesRegistry(make_scoped_refptr(io_registry
));
75 EXPECT_TRUE(registry_service
.GetRulesRegistry(key
, "ui").get());
76 EXPECT_TRUE(registry_service
.GetRulesRegistry(key
, "io").get());
77 EXPECT_FALSE(registry_service
.GetRulesRegistry(key
, "foo").get());
79 content::BrowserThread::PostTask(
80 content::BrowserThread::UI
, FROM_HERE
,
81 base::Bind(&InsertRule
, registry_service
.GetRulesRegistry(key
, "ui"),
84 content::BrowserThread::PostTask(
85 content::BrowserThread::IO
, FROM_HERE
,
86 base::Bind(&InsertRule
, registry_service
.GetRulesRegistry(key
, "io"),
89 content::BrowserThread::PostTask(
90 content::BrowserThread::UI
, FROM_HERE
,
91 base::Bind(&VerifyNumberOfRules
,
92 registry_service
.GetRulesRegistry(key
, "ui"), 1));
94 content::BrowserThread::PostTask(
95 content::BrowserThread::IO
, FROM_HERE
,
96 base::Bind(&VerifyNumberOfRules
,
97 registry_service
.GetRulesRegistry(key
, "io"), 1));
99 message_loop_
.RunUntilIdle();
101 // Test extension uninstalling.
102 scoped_ptr
<base::DictionaryValue
> manifest
= DictionaryBuilder()
103 .Set("name", "Extension")
104 .Set("version", "1.0")
105 .Set("manifest_version", 2)
107 scoped_refptr
<Extension
> extension
= ExtensionBuilder()
108 .SetManifest(manifest
.Pass())
111 registry_service
.SimulateExtensionUninstalled(extension
.get());
113 content::BrowserThread::PostTask(
114 content::BrowserThread::UI
, FROM_HERE
,
115 base::Bind(&VerifyNumberOfRules
,
116 registry_service
.GetRulesRegistry(key
, "ui"), 0));
118 content::BrowserThread::PostTask(
119 content::BrowserThread::IO
, FROM_HERE
,
120 base::Bind(&VerifyNumberOfRules
,
121 registry_service
.GetRulesRegistry(key
, "io"), 0));
123 message_loop_
.RunUntilIdle();
126 } // namespace extensions