OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/declarative/rules_registry.h" | 5 #include "extensions/browser/api/declarative/rules_registry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/test/values_test_util.h" | |
11 #include "base/values.h" | |
10 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
11 #include "extensions/browser/api/declarative/rules_registry_service.h" | 13 #include "extensions/browser/api/declarative/rules_registry_service.h" |
12 #include "extensions/browser/api/declarative/test_rules_registry.h" | 14 #include "extensions/browser/api/declarative/test_rules_registry.h" |
15 #include "extensions/browser/extension_registry.h" | |
16 #include "extensions/browser/extensions_test.h" | |
17 #include "extensions/common/extension.h" | |
18 #include "extensions/common/extension_builder.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
14 | 20 |
15 namespace { | 21 namespace { |
16 const char kExtensionId[] = "foobar"; | 22 const char kExtensionId[] = "foobar"; |
17 const char kRuleId[] = "foo"; | 23 const char kRuleId[] = "foo"; |
18 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; | 24 const int key = extensions::RulesRegistryService::kDefaultRulesRegistryID; |
19 } // namespace | 25 } // namespace |
20 | 26 |
21 namespace extensions { | 27 namespace extensions { |
22 | 28 |
23 TEST(RulesRegistryTest, FillOptionalIdentifiers) { | 29 using base::test::ParseJson; |
30 | |
31 class RulesRegistryTest : public ExtensionsTest { | |
32 public: | |
33 RulesRegistryTest() {} | |
not at google - send to devlin
2015/06/02 22:44:50
Empty constructor isn't necessary, but this needs
danduong
2015/06/03 00:18:06
Done.
| |
34 }; | |
35 | |
36 TEST_F(RulesRegistryTest, FillOptionalIdentifiers) { | |
24 base::MessageLoopForUI message_loop; | 37 base::MessageLoopForUI message_loop; |
25 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 38 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
26 | 39 |
27 std::string error; | 40 std::string error; |
28 scoped_refptr<RulesRegistry> registry = | 41 scoped_refptr<RulesRegistry> registry = |
29 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 42 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
30 | 43 |
31 // Add rules and check that their identifiers are filled and unique. | 44 // Add rules and check that their identifiers are filled and unique. |
32 | 45 |
33 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 46 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 135 |
123 registry->OnExtensionUninstalled(kExtensionId); | 136 registry->OnExtensionUninstalled(kExtensionId); |
124 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, | 137 EXPECT_EQ(0u /*extensions*/ + 0u /*rules*/, |
125 registry->GetNumberOfUsedRuleIdentifiersForTesting()); | 138 registry->GetNumberOfUsedRuleIdentifiersForTesting()); |
126 | 139 |
127 // Make sure that deletion traits of registry are executed. | 140 // Make sure that deletion traits of registry are executed. |
128 registry = NULL; | 141 registry = NULL; |
129 message_loop.RunUntilIdle(); | 142 message_loop.RunUntilIdle(); |
130 } | 143 } |
131 | 144 |
132 TEST(RulesRegistryTest, FillOptionalPriority) { | 145 TEST_F(RulesRegistryTest, FillOptionalPriority) { |
133 base::MessageLoopForUI message_loop; | 146 base::MessageLoopForUI message_loop; |
134 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | 147 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); |
135 | 148 |
136 std::string error; | 149 std::string error; |
137 scoped_refptr<RulesRegistry> registry = | 150 scoped_refptr<RulesRegistry> registry = |
138 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); | 151 new TestRulesRegistry(content::BrowserThread::UI, "" /*event_name*/, key); |
139 | 152 |
140 // Add rules and check that their priorities are filled if they are empty. | 153 // Add rules and check that their priorities are filled if they are empty. |
141 | 154 |
142 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; | 155 std::vector<linked_ptr<RulesRegistry::Rule> > add_rules; |
(...skipping 15 matching lines...) Expand all Loading... | |
158 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); | 171 EXPECT_GT(RulesRegistry::DEFAULT_PRIORITY, 2); |
159 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); | 172 EXPECT_EQ(2, std::min(*get_rules[0]->priority, *get_rules[1]->priority)); |
160 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, | 173 EXPECT_EQ(RulesRegistry::DEFAULT_PRIORITY, |
161 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); | 174 std::max(*get_rules[0]->priority, *get_rules[1]->priority)); |
162 | 175 |
163 // Make sure that deletion traits of registry are executed. | 176 // Make sure that deletion traits of registry are executed. |
164 registry = NULL; | 177 registry = NULL; |
165 message_loop.RunUntilIdle(); | 178 message_loop.RunUntilIdle(); |
166 } | 179 } |
167 | 180 |
181 TEST_F(RulesRegistryTest, ExtensionWithRulesInManifest) { | |
Mike Wittman
2015/06/02 23:11:59
Can you add tests for handling and reporting of er
| |
182 base::MessageLoopForUI message_loop; | |
183 content::TestBrowserThread thread(content::BrowserThread::UI, &message_loop); | |
184 | |
185 // Create extension | |
186 scoped_ptr<base::DictionaryValue> manifest( | |
187 (base::DictionaryValue*)ParseJson( | |
not at google - send to devlin
2015/06/02 22:44:50
Use C++ style static_cast<>, not C style cast.
Ho
danduong
2015/06/03 00:18:06
Done.
| |
188 "{" | |
189 " \"name\" : \"Test\"," | |
190 " \"version\" : \"1\"," | |
191 " \"declarativeContent\" : {" | |
192 " \"onPageChanged\": [" | |
193 " {" | |
194 " \"actions\": [" | |
195 " {" | |
196 " \"instanceType\": \"declarativeContent.ShowPageAction\"" | |
not at google - send to devlin
2015/06/02 22:44:50
This is a bit of an awkward API. Better to try to
danduong
2015/06/03 00:18:06
Where should we specify onPageChanged? How should
not at google - send to devlin
2015/06/03 16:21:44
Oh right, I suppose the event declaration syntax s
danduong
2015/06/03 16:45:53
I don't think that's valid JSON. Do you mean:
"co
| |
197 " }" | |
198 " ]," | |
199 " \"conditions\" : [" | |
200 " {" | |
201 " \"css\": [\"video\"]," | |
202 " \"instanceType\" : " | |
203 " \"declarativeContent.PageStateMatcher\"" | |
204 " }" | |
205 " ]" | |
206 " }" | |
207 " ]" | |
208 " }" | |
209 "}").release()); | |
210 scoped_refptr<Extension> extension = ExtensionBuilder() | |
211 .SetManifest(manifest.Pass()) | |
212 .SetID(kExtensionId) | |
213 .Build(); | |
214 ExtensionRegistry::Get(browser_context())->AddEnabled(extension); | |
215 | |
216 scoped_refptr<RulesRegistry> registry = new TestRulesRegistry( | |
Mike Wittman
2015/06/02 23:11:59
I'm not sure why this stored in a scoped_refptr in
not at google - send to devlin
2015/06/02 23:14:40
TestRulesRegistry --> RulesRegistry --> RefCounted
Mike Wittman
2015/06/02 23:16:00
Ah, should have known.
| |
217 browser_context(), "declarativeContent.onPageChanged", | |
218 content::BrowserThread::UI, NULL, key); | |
not at google - send to devlin
2015/06/02 22:44:50
nullptr not NULL - though I think that if you remo
danduong
2015/06/03 00:18:06
Done.
| |
219 // Simulate what RulesRegistryService would do on extension load. | |
220 registry->OnExtensionLoaded(kExtensionId); | |
221 | |
222 std::vector<linked_ptr<RulesRegistry::Rule>> get_rules; | |
223 registry->GetAllRules(kExtensionId, &get_rules); | |
224 | |
225 ASSERT_EQ(1u, get_rules.size()); | |
not at google - send to devlin
2015/06/02 22:44:50
Could you assert that this rule matches the one co
danduong
2015/06/03 00:18:06
Done.
| |
226 } | |
227 | |
168 } // namespace extensions | 228 } // namespace extensions |
OLD | NEW |