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/extension_registry.h" | 5 #include "extensions/browser/extension_registry.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/strings/string_util.h" |
10 #include "extensions/browser/extension_registry_observer.h" | 11 #include "extensions/browser/extension_registry_observer.h" |
11 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
12 #include "extensions/common/test_util.h" | 13 #include "extensions/common/test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace extensions { | 16 namespace extensions { |
16 namespace { | 17 namespace { |
17 | 18 |
18 typedef testing::Test ExtensionRegistryTest; | 19 typedef testing::Test ExtensionRegistryTest; |
19 | 20 |
(...skipping 10 matching lines...) Expand all Loading... |
30 return testing::AssertionFailure() << "Expected " << extension->id() | 31 return testing::AssertionFailure() << "Expected " << extension->id() |
31 << " found " << did_load->id(); | 32 << " found " << did_load->id(); |
32 return testing::AssertionSuccess(); | 33 return testing::AssertionSuccess(); |
33 } | 34 } |
34 | 35 |
35 class TestObserver : public ExtensionRegistryObserver { | 36 class TestObserver : public ExtensionRegistryObserver { |
36 public: | 37 public: |
37 void Reset() { | 38 void Reset() { |
38 loaded_.clear(); | 39 loaded_.clear(); |
39 unloaded_.clear(); | 40 unloaded_.clear(); |
| 41 installed_.clear(); |
40 } | 42 } |
41 | 43 |
42 const ExtensionList& loaded() { return loaded_; } | 44 const ExtensionList& loaded() { return loaded_; } |
43 const ExtensionList& unloaded() { return unloaded_; } | 45 const ExtensionList& unloaded() { return unloaded_; } |
| 46 const ExtensionList& installed() { return installed_; } |
44 | 47 |
45 private: | 48 private: |
46 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | 49 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
47 const Extension* extension) OVERRIDE { | 50 const Extension* extension) OVERRIDE { |
48 loaded_.push_back(extension); | 51 loaded_.push_back(extension); |
49 } | 52 } |
50 | 53 |
51 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context, | 54 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context, |
52 const Extension* extension, | 55 const Extension* extension, |
53 UnloadedExtensionInfo::Reason reason) | 56 UnloadedExtensionInfo::Reason reason) |
54 OVERRIDE { | 57 OVERRIDE { |
55 unloaded_.push_back(extension); | 58 unloaded_.push_back(extension); |
56 } | 59 } |
57 | 60 |
| 61 virtual void OnExtensionWillBeInstalled( |
| 62 content::BrowserContext* browser_context, |
| 63 const Extension* extension, |
| 64 bool is_update, |
| 65 const std::string& old_name) OVERRIDE { |
| 66 installed_.push_back(extension); |
| 67 } |
| 68 |
58 ExtensionList loaded_; | 69 ExtensionList loaded_; |
59 ExtensionList unloaded_; | 70 ExtensionList unloaded_; |
| 71 ExtensionList installed_; |
60 }; | 72 }; |
61 | 73 |
62 TEST_F(ExtensionRegistryTest, FillAndClearRegistry) { | 74 TEST_F(ExtensionRegistryTest, FillAndClearRegistry) { |
63 ExtensionRegistry registry(NULL); | 75 ExtensionRegistry registry(NULL); |
64 scoped_refptr<Extension> extension1 = test_util::CreateExtensionWithID("id1"); | 76 scoped_refptr<Extension> extension1 = test_util::CreateExtensionWithID("id1"); |
65 scoped_refptr<Extension> extension2 = test_util::CreateExtensionWithID("id2"); | 77 scoped_refptr<Extension> extension2 = test_util::CreateExtensionWithID("id2"); |
66 scoped_refptr<Extension> extension3 = test_util::CreateExtensionWithID("id3"); | 78 scoped_refptr<Extension> extension3 = test_util::CreateExtensionWithID("id3"); |
67 scoped_refptr<Extension> extension4 = test_util::CreateExtensionWithID("id4"); | 79 scoped_refptr<Extension> extension4 = test_util::CreateExtensionWithID("id4"); |
68 | 80 |
69 // All the sets start empty. | 81 // All the sets start empty. |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 "enabled", ExtensionRegistry::DISABLED | ExtensionRegistry::BLACKLISTED)); | 220 "enabled", ExtensionRegistry::DISABLED | ExtensionRegistry::BLACKLISTED)); |
209 } | 221 } |
210 | 222 |
211 TEST_F(ExtensionRegistryTest, Observer) { | 223 TEST_F(ExtensionRegistryTest, Observer) { |
212 ExtensionRegistry registry(NULL); | 224 ExtensionRegistry registry(NULL); |
213 TestObserver observer; | 225 TestObserver observer; |
214 registry.AddObserver(&observer); | 226 registry.AddObserver(&observer); |
215 | 227 |
216 EXPECT_TRUE(observer.loaded().empty()); | 228 EXPECT_TRUE(observer.loaded().empty()); |
217 EXPECT_TRUE(observer.unloaded().empty()); | 229 EXPECT_TRUE(observer.unloaded().empty()); |
| 230 EXPECT_TRUE(observer.installed().empty()); |
218 | 231 |
219 scoped_refptr<const Extension> extension = | 232 scoped_refptr<const Extension> extension = |
220 test_util::CreateExtensionWithID("id"); | 233 test_util::CreateExtensionWithID("id"); |
221 | 234 |
| 235 registry.TriggerOnWillBeInstalled(extension, false, base::EmptyString()); |
| 236 EXPECT_TRUE(HasSingleExtension(observer.installed(), extension.get())); |
| 237 |
222 registry.AddEnabled(extension); | 238 registry.AddEnabled(extension); |
223 registry.TriggerOnLoaded(extension); | 239 registry.TriggerOnLoaded(extension); |
224 | 240 |
| 241 registry.TriggerOnWillBeInstalled(extension, true, "foo"); |
| 242 |
225 EXPECT_TRUE(HasSingleExtension(observer.loaded(), extension.get())); | 243 EXPECT_TRUE(HasSingleExtension(observer.loaded(), extension.get())); |
226 EXPECT_TRUE(observer.unloaded().empty()); | 244 EXPECT_TRUE(observer.unloaded().empty()); |
227 observer.Reset(); | 245 observer.Reset(); |
228 | 246 |
229 registry.RemoveEnabled(extension->id()); | 247 registry.RemoveEnabled(extension->id()); |
230 registry.TriggerOnUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE); | 248 registry.TriggerOnUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE); |
231 | 249 |
232 EXPECT_TRUE(observer.loaded().empty()); | 250 EXPECT_TRUE(observer.loaded().empty()); |
233 EXPECT_TRUE(HasSingleExtension(observer.unloaded(), extension.get())); | 251 EXPECT_TRUE(HasSingleExtension(observer.unloaded(), extension.get())); |
234 observer.Reset(); | 252 observer.Reset(); |
235 | 253 |
236 registry.RemoveObserver(&observer); | 254 registry.RemoveObserver(&observer); |
237 } | 255 } |
238 | 256 |
239 } // namespace | 257 } // namespace |
240 } // namespace extensions | 258 } // namespace extensions |
OLD | NEW |