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 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
52 const Extension
* extension
) override
{
53 loaded_
.push_back(extension
);
56 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
57 const Extension
* extension
,
58 UnloadedExtensionInfo::Reason reason
) override
{
59 unloaded_
.push_back(extension
);
62 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
63 const Extension
* extension
,
66 const std::string
& old_name
) override
{
67 installed_
.push_back(extension
);
70 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
71 const Extension
* extension
,
72 extensions::UninstallReason reason
) override
{
73 uninstalled_
.push_back(extension
);
76 void OnShutdown(extensions::ExtensionRegistry
* registry
) override
{ Reset(); }
78 ExtensionList loaded_
;
79 ExtensionList unloaded_
;
80 ExtensionList installed_
;
81 ExtensionList uninstalled_
;
84 TEST_F(ExtensionRegistryTest
, FillAndClearRegistry
) {
85 ExtensionRegistry
registry(NULL
);
86 scoped_refptr
<Extension
> extension1
= test_util::CreateEmptyExtension("id1");
87 scoped_refptr
<Extension
> extension2
= test_util::CreateEmptyExtension("id2");
88 scoped_refptr
<Extension
> extension3
= test_util::CreateEmptyExtension("id3");
89 scoped_refptr
<Extension
> extension4
= test_util::CreateEmptyExtension("id4");
91 // All the sets start empty.
92 EXPECT_EQ(0u, registry
.enabled_extensions().size());
93 EXPECT_EQ(0u, registry
.disabled_extensions().size());
94 EXPECT_EQ(0u, registry
.terminated_extensions().size());
95 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
97 // Extensions can be added to each set.
98 registry
.AddEnabled(extension1
);
99 registry
.AddDisabled(extension2
);
100 registry
.AddTerminated(extension3
);
101 registry
.AddBlacklisted(extension4
);
103 EXPECT_EQ(1u, registry
.enabled_extensions().size());
104 EXPECT_EQ(1u, registry
.disabled_extensions().size());
105 EXPECT_EQ(1u, registry
.terminated_extensions().size());
106 EXPECT_EQ(1u, registry
.blacklisted_extensions().size());
108 // Clearing the registry clears all sets.
111 EXPECT_EQ(0u, registry
.enabled_extensions().size());
112 EXPECT_EQ(0u, registry
.disabled_extensions().size());
113 EXPECT_EQ(0u, registry
.terminated_extensions().size());
114 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
117 // A simple test of adding and removing things from sets.
118 TEST_F(ExtensionRegistryTest
, AddAndRemoveExtensionFromRegistry
) {
119 ExtensionRegistry
registry(NULL
);
121 // Adding an extension works.
122 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension("id");
123 EXPECT_TRUE(registry
.AddEnabled(extension
));
124 EXPECT_EQ(1u, registry
.enabled_extensions().size());
126 // The extension was only added to one set.
127 EXPECT_EQ(0u, registry
.disabled_extensions().size());
128 EXPECT_EQ(0u, registry
.terminated_extensions().size());
129 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
131 // Removing an extension works.
132 EXPECT_TRUE(registry
.RemoveEnabled(extension
->id()));
133 EXPECT_EQ(0u, registry
.enabled_extensions().size());
135 // Trying to remove an extension that isn't in the set fails cleanly.
136 EXPECT_FALSE(registry
.RemoveEnabled(extension
->id()));
139 TEST_F(ExtensionRegistryTest
, AddExtensionToRegistryTwice
) {
140 ExtensionRegistry
registry(NULL
);
141 scoped_refptr
<Extension
> extension
= test_util::CreateEmptyExtension("id");
143 // An extension can exist in two sets at once. It would be nice to eliminate
144 // this functionality, but some users of ExtensionRegistry need it.
145 EXPECT_TRUE(registry
.AddEnabled(extension
));
146 EXPECT_TRUE(registry
.AddDisabled(extension
));
148 EXPECT_EQ(1u, registry
.enabled_extensions().size());
149 EXPECT_EQ(1u, registry
.disabled_extensions().size());
150 EXPECT_EQ(0u, registry
.terminated_extensions().size());
151 EXPECT_EQ(0u, registry
.blacklisted_extensions().size());
154 TEST_F(ExtensionRegistryTest
, GetExtensionById
) {
155 ExtensionRegistry
registry(NULL
);
157 // Trying to get an extension fails cleanly when the sets are empty.
159 registry
.GetExtensionById("id", ExtensionRegistry::EVERYTHING
));
161 scoped_refptr
<Extension
> enabled
= test_util::CreateEmptyExtension("enabled");
162 scoped_refptr
<Extension
> disabled
=
163 test_util::CreateEmptyExtension("disabled");
164 scoped_refptr
<Extension
> terminated
=
165 test_util::CreateEmptyExtension("terminated");
166 scoped_refptr
<Extension
> blacklisted
=
167 test_util::CreateEmptyExtension("blacklisted");
169 // Add an extension to each set.
170 registry
.AddEnabled(enabled
);
171 registry
.AddDisabled(disabled
);
172 registry
.AddTerminated(terminated
);
173 registry
.AddBlacklisted(blacklisted
);
175 // Enabled is part of everything and the enabled list.
177 registry
.GetExtensionById("enabled", ExtensionRegistry::EVERYTHING
));
179 registry
.GetExtensionById("enabled", ExtensionRegistry::ENABLED
));
181 registry
.GetExtensionById("enabled", ExtensionRegistry::DISABLED
));
183 registry
.GetExtensionById("enabled", ExtensionRegistry::TERMINATED
));
185 registry
.GetExtensionById("enabled", ExtensionRegistry::BLACKLISTED
));
187 // Disabled is part of everything and the disabled list.
189 registry
.GetExtensionById("disabled", ExtensionRegistry::EVERYTHING
));
191 registry
.GetExtensionById("disabled", ExtensionRegistry::ENABLED
));
193 registry
.GetExtensionById("disabled", ExtensionRegistry::DISABLED
));
195 registry
.GetExtensionById("disabled", ExtensionRegistry::TERMINATED
));
197 registry
.GetExtensionById("disabled", ExtensionRegistry::BLACKLISTED
));
199 // Terminated is part of everything and the terminated list.
201 registry
.GetExtensionById("terminated", ExtensionRegistry::EVERYTHING
));
203 registry
.GetExtensionById("terminated", ExtensionRegistry::ENABLED
));
205 registry
.GetExtensionById("terminated", ExtensionRegistry::DISABLED
));
207 registry
.GetExtensionById("terminated", ExtensionRegistry::TERMINATED
));
209 registry
.GetExtensionById("terminated", ExtensionRegistry::BLACKLISTED
));
211 // Blacklisted is part of everything and the blacklisted list.
213 registry
.GetExtensionById("blacklisted", ExtensionRegistry::EVERYTHING
));
215 registry
.GetExtensionById("blacklisted", ExtensionRegistry::ENABLED
));
217 registry
.GetExtensionById("blacklisted", ExtensionRegistry::DISABLED
));
219 registry
.GetExtensionById("blacklisted", ExtensionRegistry::TERMINATED
));
221 registry
.GetExtensionById("blacklisted", ExtensionRegistry::BLACKLISTED
));
223 // Enabled can be found with multiple flags set.
224 EXPECT_TRUE(registry
.GetExtensionById(
225 "enabled", ExtensionRegistry::ENABLED
| ExtensionRegistry::TERMINATED
));
227 // Enabled isn't found if the wrong flags are set.
228 EXPECT_FALSE(registry
.GetExtensionById(
229 "enabled", ExtensionRegistry::DISABLED
| ExtensionRegistry::BLACKLISTED
));
232 TEST_F(ExtensionRegistryTest
, Observer
) {
233 ExtensionRegistry
registry(NULL
);
234 TestObserver observer
;
235 registry
.AddObserver(&observer
);
237 EXPECT_TRUE(observer
.loaded().empty());
238 EXPECT_TRUE(observer
.unloaded().empty());
239 EXPECT_TRUE(observer
.installed().empty());
241 scoped_refptr
<const Extension
> extension
=
242 test_util::CreateEmptyExtension("id");
244 registry
.TriggerOnWillBeInstalled(
245 extension
.get(), false, false, base::EmptyString());
246 EXPECT_TRUE(HasSingleExtension(observer
.installed(), extension
.get()));
248 registry
.AddEnabled(extension
);
249 registry
.TriggerOnLoaded(extension
.get());
251 registry
.TriggerOnWillBeInstalled(extension
.get(), true, false, "foo");
253 EXPECT_TRUE(HasSingleExtension(observer
.loaded(), extension
.get()));
254 EXPECT_TRUE(observer
.unloaded().empty());
257 registry
.RemoveEnabled(extension
->id());
258 registry
.TriggerOnUnloaded(extension
.get(),
259 UnloadedExtensionInfo::REASON_DISABLE
);
261 EXPECT_TRUE(observer
.loaded().empty());
262 EXPECT_TRUE(HasSingleExtension(observer
.unloaded(), extension
.get()));
265 registry
.TriggerOnUninstalled(extension
.get(),
266 extensions::UNINSTALL_REASON_FOR_TESTING
);
267 EXPECT_TRUE(observer
.installed().empty());
268 EXPECT_TRUE(HasSingleExtension(observer
.uninstalled(), extension
.get()));
270 registry
.RemoveObserver(&observer
);
274 } // namespace extensions