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 "base/strings/string_util.h"
11 #include "extensions/browser/extension_registry_observer.h"
12 #include "extensions/browser/uninstall_reason.h"
13 #include "extensions/common/test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace extensions
{
19 typedef testing::Test ExtensionRegistryTest
;
21 testing::AssertionResult
HasSingleExtension(
22 const ExtensionList
& list
,
23 const scoped_refptr
<const Extension
>& extension
) {
25 return testing::AssertionFailure() << "No extensions in list";
27 return testing::AssertionFailure() << list
.size()
28 << " 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();
33 return testing::AssertionSuccess();
36 class TestObserver
: public ExtensionRegistryObserver
{
45 const ExtensionList
& loaded() { return loaded_
; }
46 const ExtensionList
& unloaded() { return unloaded_
; }
47 const ExtensionList
& installed() { return installed_
; }
48 const ExtensionList
& uninstalled() { return uninstalled_
; }
51 virtual void OnExtensionLoaded(content::BrowserContext
* browser_context
,
52 const Extension
* extension
) OVERRIDE
{
53 loaded_
.push_back(extension
);
56 virtual void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
57 const Extension
* extension
,
58 UnloadedExtensionInfo::Reason reason
)
60 unloaded_
.push_back(extension
);
63 virtual void OnExtensionWillBeInstalled(
64 content::BrowserContext
* browser_context
,
65 const Extension
* extension
,
68 const std::string
& old_name
) OVERRIDE
{
69 installed_
.push_back(extension
);
72 virtual void OnExtensionUninstalled(
73 content::BrowserContext
* browser_context
,
74 const Extension
* extension
,
75 extensions::UninstallReason reason
) OVERRIDE
{
76 uninstalled_
.push_back(extension
);
79 virtual void OnShutdown(extensions::ExtensionRegistry
* registry
) OVERRIDE
{
83 ExtensionList loaded_
;
84 ExtensionList unloaded_
;
85 ExtensionList installed_
;
86 ExtensionList uninstalled_
;
89 TEST_F(ExtensionRegistryTest
, FillAndClearRegistry
) {
90 ExtensionRegistry
registry(NULL
);
91 scoped_refptr
<Extension
> extension1
= test_util::CreateExtensionWithID("id1");
92 scoped_refptr
<Extension
> extension2
= test_util::CreateExtensionWithID("id2");
93 scoped_refptr
<Extension
> extension3
= test_util::CreateExtensionWithID("id3");
94 scoped_refptr
<Extension
> extension4
= test_util::CreateExtensionWithID("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::CreateExtensionWithID("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::CreateExtensionWithID("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
=
167 test_util::CreateExtensionWithID("enabled");
168 scoped_refptr
<Extension
> disabled
=
169 test_util::CreateExtensionWithID("disabled");
170 scoped_refptr
<Extension
> terminated
=
171 test_util::CreateExtensionWithID("terminated");
172 scoped_refptr
<Extension
> blacklisted
=
173 test_util::CreateExtensionWithID("blacklisted");
175 // Add an extension to each set.
176 registry
.AddEnabled(enabled
);
177 registry
.AddDisabled(disabled
);
178 registry
.AddTerminated(terminated
);
179 registry
.AddBlacklisted(blacklisted
);
181 // Enabled is part of everything and the enabled list.
183 registry
.GetExtensionById("enabled", ExtensionRegistry::EVERYTHING
));
185 registry
.GetExtensionById("enabled", ExtensionRegistry::ENABLED
));
187 registry
.GetExtensionById("enabled", ExtensionRegistry::DISABLED
));
189 registry
.GetExtensionById("enabled", ExtensionRegistry::TERMINATED
));
191 registry
.GetExtensionById("enabled", ExtensionRegistry::BLACKLISTED
));
193 // Disabled is part of everything and the disabled list.
195 registry
.GetExtensionById("disabled", ExtensionRegistry::EVERYTHING
));
197 registry
.GetExtensionById("disabled", ExtensionRegistry::ENABLED
));
199 registry
.GetExtensionById("disabled", ExtensionRegistry::DISABLED
));
201 registry
.GetExtensionById("disabled", ExtensionRegistry::TERMINATED
));
203 registry
.GetExtensionById("disabled", ExtensionRegistry::BLACKLISTED
));
205 // Terminated is part of everything and the terminated list.
207 registry
.GetExtensionById("terminated", ExtensionRegistry::EVERYTHING
));
209 registry
.GetExtensionById("terminated", ExtensionRegistry::ENABLED
));
211 registry
.GetExtensionById("terminated", ExtensionRegistry::DISABLED
));
213 registry
.GetExtensionById("terminated", ExtensionRegistry::TERMINATED
));
215 registry
.GetExtensionById("terminated", ExtensionRegistry::BLACKLISTED
));
217 // Blacklisted is part of everything and the blacklisted list.
219 registry
.GetExtensionById("blacklisted", ExtensionRegistry::EVERYTHING
));
221 registry
.GetExtensionById("blacklisted", ExtensionRegistry::ENABLED
));
223 registry
.GetExtensionById("blacklisted", ExtensionRegistry::DISABLED
));
225 registry
.GetExtensionById("blacklisted", ExtensionRegistry::TERMINATED
));
227 registry
.GetExtensionById("blacklisted", ExtensionRegistry::BLACKLISTED
));
229 // Enabled can be found with multiple flags set.
230 EXPECT_TRUE(registry
.GetExtensionById(
231 "enabled", ExtensionRegistry::ENABLED
| ExtensionRegistry::TERMINATED
));
233 // Enabled isn't found if the wrong flags are set.
234 EXPECT_FALSE(registry
.GetExtensionById(
235 "enabled", ExtensionRegistry::DISABLED
| ExtensionRegistry::BLACKLISTED
));
238 TEST_F(ExtensionRegistryTest
, Observer
) {
239 ExtensionRegistry
registry(NULL
);
240 TestObserver observer
;
241 registry
.AddObserver(&observer
);
243 EXPECT_TRUE(observer
.loaded().empty());
244 EXPECT_TRUE(observer
.unloaded().empty());
245 EXPECT_TRUE(observer
.installed().empty());
247 scoped_refptr
<const Extension
> extension
=
248 test_util::CreateExtensionWithID("id");
250 registry
.TriggerOnWillBeInstalled(
251 extension
.get(), false, false, base::EmptyString());
252 EXPECT_TRUE(HasSingleExtension(observer
.installed(), extension
.get()));
254 registry
.AddEnabled(extension
);
255 registry
.TriggerOnLoaded(extension
.get());
257 registry
.TriggerOnWillBeInstalled(extension
.get(), true, false, "foo");
259 EXPECT_TRUE(HasSingleExtension(observer
.loaded(), extension
.get()));
260 EXPECT_TRUE(observer
.unloaded().empty());
263 registry
.RemoveEnabled(extension
->id());
264 registry
.TriggerOnUnloaded(extension
.get(),
265 UnloadedExtensionInfo::REASON_DISABLE
);
267 EXPECT_TRUE(observer
.loaded().empty());
268 EXPECT_TRUE(HasSingleExtension(observer
.unloaded(), extension
.get()));
271 registry
.TriggerOnUninstalled(extension
.get(),
272 extensions::UNINSTALL_REASON_FOR_TESTING
);
273 EXPECT_TRUE(observer
.installed().empty());
274 EXPECT_TRUE(HasSingleExtension(observer
.uninstalled(), extension
.get()));
276 registry
.RemoveObserver(&observer
);
280 } // namespace extensions