1 // Copyright 2013 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.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "extensions/browser/api/declarative/rules_registry_service.h"
12 #include "extensions/browser/api/declarative/test_rules_registry.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 const char kExtensionId
[] = "foobar";
17 const char kRuleId
[] = "foo";
18 const int key
= extensions::RulesRegistryService::kDefaultRulesRegistryID
;
21 namespace extensions
{
23 TEST(RulesRegistryTest
, FillOptionalIdentifiers
) {
24 base::MessageLoopForUI message_loop
;
25 content::TestBrowserThread
thread(content::BrowserThread::UI
, &message_loop
);
28 scoped_refptr
<RulesRegistry
> registry
=
29 new TestRulesRegistry(content::BrowserThread::UI
, "" /*event_name*/, key
);
31 // Add rules and check that their identifiers are filled and unique.
33 std::vector
<linked_ptr
<RulesRegistry::Rule
> > add_rules
;
34 add_rules
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
35 add_rules
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
36 error
= registry
->AddRules(kExtensionId
, add_rules
);
37 EXPECT_TRUE(error
.empty()) << error
;
39 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules
;
40 registry
->GetAllRules(kExtensionId
, &get_rules
);
42 ASSERT_EQ(2u, get_rules
.size());
44 ASSERT_TRUE(get_rules
[0]->id
.get());
45 EXPECT_NE("", *get_rules
[0]->id
);
47 ASSERT_TRUE(get_rules
[1]->id
.get());
48 EXPECT_NE("", *get_rules
[1]->id
);
50 EXPECT_NE(*get_rules
[0]->id
, *get_rules
[1]->id
);
52 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/,
53 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
55 // Check that we cannot add a new rule with the same ID.
57 std::vector
<linked_ptr
<RulesRegistry::Rule
> > add_rules_2
;
58 add_rules_2
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
59 add_rules_2
[0]->id
.reset(new std::string(*get_rules
[0]->id
));
60 error
= registry
->AddRules(kExtensionId
, add_rules_2
);
61 EXPECT_FALSE(error
.empty());
63 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules_2
;
64 registry
->GetAllRules(kExtensionId
, &get_rules_2
);
65 ASSERT_EQ(2u, get_rules_2
.size());
66 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/,
67 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
69 // Check that we can register the old rule IDs once they were unregistered.
71 std::vector
<std::string
> remove_rules_3
;
72 remove_rules_3
.push_back(*get_rules
[0]->id
);
73 error
= registry
->RemoveRules(kExtensionId
, remove_rules_3
);
74 EXPECT_TRUE(error
.empty()) << error
;
76 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/,
77 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
79 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules_3a
;
80 registry
->GetAllRules(kExtensionId
, &get_rules_3a
);
81 ASSERT_EQ(1u, get_rules_3a
.size());
83 std::vector
<linked_ptr
<RulesRegistry::Rule
> > add_rules_3
;
84 add_rules_3
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
85 add_rules_3
[0]->id
.reset(new std::string(*get_rules
[0]->id
));
86 error
= registry
->AddRules(kExtensionId
, add_rules_3
);
87 EXPECT_TRUE(error
.empty()) << error
;
88 EXPECT_EQ(1u /*extensions*/ + 2u /*rules*/,
89 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
91 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules_3b
;
92 registry
->GetAllRules(kExtensionId
, &get_rules_3b
);
93 ASSERT_EQ(2u, get_rules_3b
.size());
95 // Check that we can register a rule with an ID that is not modified.
97 error
= registry
->RemoveAllRules(kExtensionId
);
98 EXPECT_TRUE(error
.empty()) << error
;
99 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/,
100 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
102 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules_4a
;
103 registry
->GetAllRules(kExtensionId
, &get_rules_4a
);
104 ASSERT_TRUE(get_rules_4a
.empty());
106 std::vector
<linked_ptr
<RulesRegistry::Rule
> > add_rules_4
;
107 add_rules_4
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
108 add_rules_4
[0]->id
.reset(new std::string(kRuleId
));
109 error
= registry
->AddRules(kExtensionId
, add_rules_4
);
110 EXPECT_TRUE(error
.empty()) << error
;
112 EXPECT_EQ(1u /*extensions*/ + 1u /*rules*/,
113 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
115 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules_4b
;
116 registry
->GetAllRules(kExtensionId
, &get_rules_4b
);
118 ASSERT_EQ(1u, get_rules_4b
.size());
120 ASSERT_TRUE(get_rules_4b
[0]->id
.get());
121 EXPECT_EQ(kRuleId
, *get_rules_4b
[0]->id
);
123 registry
->OnExtensionUninstalled(kExtensionId
);
124 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/,
125 registry
->GetNumberOfUsedRuleIdentifiersForTesting());
127 // Make sure that deletion traits of registry are executed.
129 message_loop
.RunUntilIdle();
132 TEST(RulesRegistryTest
, FillOptionalPriority
) {
133 base::MessageLoopForUI message_loop
;
134 content::TestBrowserThread
thread(content::BrowserThread::UI
, &message_loop
);
137 scoped_refptr
<RulesRegistry
> registry
=
138 new TestRulesRegistry(content::BrowserThread::UI
, "" /*event_name*/, key
);
140 // Add rules and check that their priorities are filled if they are empty.
142 std::vector
<linked_ptr
<RulesRegistry::Rule
> > add_rules
;
143 add_rules
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
144 add_rules
[0]->priority
.reset(new int(2));
145 add_rules
.push_back(make_linked_ptr(new RulesRegistry::Rule
));
146 error
= registry
->AddRules(kExtensionId
, add_rules
);
147 EXPECT_TRUE(error
.empty()) << error
;
149 std::vector
<linked_ptr
<RulesRegistry::Rule
> > get_rules
;
150 registry
->GetAllRules(kExtensionId
, &get_rules
);
152 ASSERT_EQ(2u, get_rules
.size());
154 ASSERT_TRUE(get_rules
[0]->priority
.get());
155 ASSERT_TRUE(get_rules
[1]->priority
.get());
157 // Verify the precondition so that the following EXPECT_EQ statements work.
158 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY
, 2);
159 EXPECT_EQ(2, std::min(*get_rules
[0]->priority
, *get_rules
[1]->priority
));
160 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY
,
161 std::max(*get_rules
[0]->priority
, *get_rules
[1]->priority
));
163 // Make sure that deletion traits of registry are executed.
165 message_loop
.RunUntilIdle();
168 } // namespace extensions