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/extension_registry.h"
9 #include "base/memory/ref_counted.h"
10 #include "extensions/browser/extension_registry_observer.h"
11 #include "extensions/browser/uninstall_reason.h"
12 #include "extensions/common/test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
18 typedef testing::Test ExtensionRegistryTest
;
20 testing::AssertionResult
HasSingleExtension(
21 const ExtensionList
& list
,
22 const scoped_refptr
<const Extension
>& extension
) {
24 return testing::AssertionFailure() << "No extensions in list";
25 if (list
.size() > 1) {
26 return testing::AssertionFailure() << list
.size()
27 << " extensions, expected 1";
29 const Extension
* did_load
= list
[0].get();
30 if (did_load
!= extension
.get()) {
31 return testing::AssertionFailure() << "Expected " << extension
->id()
32 << " found " << did_load
->id();
34 return testing::AssertionSuccess();
37 class TestObserver
: public ExtensionRegistryObserver
{
48 const ExtensionList
& loaded() { return loaded_
; }
49 const ExtensionList
& unloaded() { return unloaded_
; }
50 const ExtensionList
& installed() { return installed_
; }
51 const ExtensionList
& uninstalled() { return uninstalled_
; }
54 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
55 const Extension
* extension
) override
{
56 loaded_
.push_back(extension
);
59 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
60 const Extension
* extension
,
61 UnloadedExtensionInfo::Reason reason
) override
{
62 unloaded_
.push_back(extension
);
65 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
66 const Extension
* extension
,
69 const std::string
& old_name
) override
{
70 installed_
.push_back(extension
);
73 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
74 const Extension
* extension
,
75 extensions::UninstallReason reason
) override
{
76 uninstalled_
.push_back(extension
);
79 void OnShutdown(extensions::ExtensionRegistry
* registry
) override
{ Reset(); }
81 ExtensionList loaded_
;
82 ExtensionList unloaded_
;
83 ExtensionList installed_
;
84 ExtensionList uninstalled_
;
86 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
89 TEST_F(ExtensionRegistryTest
, FillAndClearRegistry
) {
90 ExtensionRegistry
registry(NULL
);
91 scoped_refptr
<Extension
> extension1
= test_util::CreateEmptyExtension("id1");
92 scoped_refptr
<Extension
> extension2
= test_util::CreateEmptyExtension("id2");
93 scoped_refptr
<Extension
> extension3
= test_util::CreateEmptyExtension("id3");
94 scoped_refptr
<Extension
> extension4
= test_util::CreateEmptyExtension("id4");
96 // All the sets start empty.
97 EXPECT_EQ(0u, registry
.enabled_extensions().size());
98 EXPECT_EQ(0u, registry
.disabled_extensions().size());
99 EXPECT_EQ(0u, registry
.terminated_extensions().size());
100 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
102 // Extensions can be added to each set.
103 registry
.AddEnabled(extension1
);
104 registry
.AddDisabled(extension2
);
105 registry
.AddTerminated(extension3
);
106 registry
.AddBlacklisted(extension4
);
108 EXPECT_EQ(1u, registry
.enabled_extensions().size());
109 EXPECT_EQ(1u, registry
.disabled_extensions().size());
110 EXPECT_EQ(1u, registry
.terminated_extensions().size());
111 EXPECT_EQ(1u, registry
.blacklisted_extensions().size());
113 // Clearing the registry clears all sets.
116 EXPECT_EQ(0u, registry
.enabled_extensions().size());
117 EXPECT_EQ(0u, registry
.disabled_extensions().size());
118 EXPECT_EQ(0u, registry
.terminated_extensions().size());
119 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
122 // A simple test of adding and removing things from sets.
123 TEST_F(ExtensionRegistryTest
, AddAndRemoveExtensionFromRegistry
) {
124 ExtensionRegistry
registry(NULL
);
126 // Adding an extension works.
127 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension("id");
128 EXPECT_TRUE(registry
.AddEnabled(extension
));
129 EXPECT_EQ(1u, registry
.enabled_extensions().size());
131 // The extension was only added to one set.
132 EXPECT_EQ(0u, registry
.disabled_extensions().size());
133 EXPECT_EQ(0u, registry
.terminated_extensions().size());
134 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
136 // Removing an extension works.
137 EXPECT_TRUE(registry
.RemoveEnabled(extension
->id()));
138 EXPECT_EQ(0u, registry
.enabled_extensions().size());
140 // Trying to remove an extension that isn't in the set fails cleanly.
141 EXPECT_FALSE(registry
.RemoveEnabled(extension
->id()));
144 TEST_F(ExtensionRegistryTest
, AddExtensionToRegistryTwice
) {
145 ExtensionRegistry
registry(NULL
);
146 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension("id");
148 // An extension can exist in two sets at once. It would be nice to eliminate
149 // this functionality, but some users of ExtensionRegistry need it.
150 EXPECT_TRUE(registry
.AddEnabled(extension
));
151 EXPECT_TRUE(registry
.AddDisabled(extension
));
153 EXPECT_EQ(1u, registry
.enabled_extensions().size());
154 EXPECT_EQ(1u, registry
.disabled_extensions().size());
155 EXPECT_EQ(0u, registry
.terminated_extensions().size());
156 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
159 TEST_F(ExtensionRegistryTest
, GetExtensionById
) {
160 ExtensionRegistry
registry(NULL
);
162 // Trying to get an extension fails cleanly when the sets are empty.
164 registry
.GetExtensionById("id", ExtensionRegistry::EVERYTHING
));
166 scoped_refptr
<Extension
> enabled
= test_util::CreateEmptyExtension("enabled");
167 scoped_refptr
<Extension
> disabled
=
168 test_util::CreateEmptyExtension("disabled");
169 scoped_refptr
<Extension
> terminated
=
170 test_util::CreateEmptyExtension("terminated");
171 scoped_refptr
<Extension
> blacklisted
=
172 test_util::CreateEmptyExtension("blacklisted");
174 // Add an extension to each set.
175 registry
.AddEnabled(enabled
);
176 registry
.AddDisabled(disabled
);
177 registry
.AddTerminated(terminated
);
178 registry
.AddBlacklisted(blacklisted
);
180 // Enabled is part of everything and the enabled list.
182 registry
.GetExtensionById("enabled", ExtensionRegistry::EVERYTHING
));
184 registry
.GetExtensionById("enabled", ExtensionRegistry::ENABLED
));
186 registry
.GetExtensionById("enabled", ExtensionRegistry::DISABLED
));
188 registry
.GetExtensionById("enabled", ExtensionRegistry::TERMINATED
));
190 registry
.GetExtensionById("enabled", ExtensionRegistry::BLACKLISTED
));
192 // Disabled is part of everything and the disabled list.
194 registry
.GetExtensionById("disabled", ExtensionRegistry::EVERYTHING
));
196 registry
.GetExtensionById("disabled", ExtensionRegistry::ENABLED
));
198 registry
.GetExtensionById("disabled", ExtensionRegistry::DISABLED
));
200 registry
.GetExtensionById("disabled", ExtensionRegistry::TERMINATED
));
202 registry
.GetExtensionById("disabled", ExtensionRegistry::BLACKLISTED
));
204 // Terminated is part of everything and the terminated list.
206 registry
.GetExtensionById("terminated", ExtensionRegistry::EVERYTHING
));
208 registry
.GetExtensionById("terminated", ExtensionRegistry::ENABLED
));
210 registry
.GetExtensionById("terminated", ExtensionRegistry::DISABLED
));
212 registry
.GetExtensionById("terminated", ExtensionRegistry::TERMINATED
));
214 registry
.GetExtensionById("terminated", ExtensionRegistry::BLACKLISTED
));
216 // Blacklisted is part of everything and the blacklisted list.
218 registry
.GetExtensionById("blacklisted", ExtensionRegistry::EVERYTHING
));
220 registry
.GetExtensionById("blacklisted", ExtensionRegistry::ENABLED
));
222 registry
.GetExtensionById("blacklisted", ExtensionRegistry::DISABLED
));
224 registry
.GetExtensionById("blacklisted", ExtensionRegistry::TERMINATED
));
226 registry
.GetExtensionById("blacklisted", ExtensionRegistry::BLACKLISTED
));
228 // Enabled can be found with multiple flags set.
229 EXPECT_TRUE(registry
.GetExtensionById(
230 "enabled", ExtensionRegistry::ENABLED
| ExtensionRegistry::TERMINATED
));
232 // Enabled isn't found if the wrong flags are set.
233 EXPECT_FALSE(registry
.GetExtensionById(
234 "enabled", ExtensionRegistry::DISABLED
| ExtensionRegistry::BLACKLISTED
));
237 TEST_F(ExtensionRegistryTest
, Observer
) {
238 ExtensionRegistry
registry(NULL
);
239 TestObserver observer
;
240 registry
.AddObserver(&observer
);
242 EXPECT_TRUE(observer
.loaded().empty());
243 EXPECT_TRUE(observer
.unloaded().empty());
244 EXPECT_TRUE(observer
.installed().empty());
246 scoped_refptr
<const Extension
> extension
=
247 test_util::CreateEmptyExtension("id");
249 registry
.TriggerOnWillBeInstalled(extension
.get(), false, false,
251 EXPECT_TRUE(HasSingleExtension(observer
.installed(), extension
.get()));
253 registry
.AddEnabled(extension
);
254 registry
.TriggerOnLoaded(extension
.get());
256 registry
.TriggerOnWillBeInstalled(extension
.get(), true, false, "foo");
258 EXPECT_TRUE(HasSingleExtension(observer
.loaded(), extension
.get()));
259 EXPECT_TRUE(observer
.unloaded().empty());
262 registry
.RemoveEnabled(extension
->id());
263 registry
.TriggerOnUnloaded(extension
.get(),
264 UnloadedExtensionInfo::REASON_DISABLE
);
266 EXPECT_TRUE(observer
.loaded().empty());
267 EXPECT_TRUE(HasSingleExtension(observer
.unloaded(), extension
.get()));
270 registry
.TriggerOnUninstalled(extension
.get(), UNINSTALL_REASON_FOR_TESTING
);
271 EXPECT_TRUE(observer
.installed().empty());
272 EXPECT_TRUE(HasSingleExtension(observer
.uninstalled(), extension
.get()));
274 registry
.RemoveObserver(&observer
);
278 } // namespace extensions