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 "chrome/browser/extensions/extension_notification_observer.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "extensions/common/extension.h"
13 namespace extensions
{
17 std::string
Str(const std::vector
<extensions::NotificationType
>& types
) {
18 std::string str
= "[";
19 bool needs_comma
= false;
20 for (std::vector
<extensions::NotificationType
>::const_iterator it
=
27 str
+= base::StringPrintf("%d", *it
);
35 ExtensionNotificationObserver::ExtensionNotificationObserver(
36 content::NotificationSource source
,
37 const std::set
<std::string
>& extension_ids
)
38 : extension_ids_(extension_ids
) {
40 this, extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
, source
);
43 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
,
46 this, extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
, source
);
49 ExtensionNotificationObserver::~ExtensionNotificationObserver() {}
51 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications() {
52 return CheckNotifications(std::vector
<extensions::NotificationType
>());
55 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
56 extensions::NotificationType type
) {
57 return CheckNotifications(std::vector
<extensions::NotificationType
>(1, type
));
60 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
61 extensions::NotificationType t1
,
62 extensions::NotificationType t2
) {
63 std::vector
<extensions::NotificationType
> types
;
66 return CheckNotifications(types
);
69 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
70 extensions::NotificationType t1
,
71 extensions::NotificationType t2
,
72 extensions::NotificationType t3
) {
73 std::vector
<extensions::NotificationType
> types
;
77 return CheckNotifications(types
);
80 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
81 extensions::NotificationType t1
,
82 extensions::NotificationType t2
,
83 extensions::NotificationType t3
,
84 extensions::NotificationType t4
,
85 extensions::NotificationType t5
,
86 extensions::NotificationType t6
) {
87 std::vector
<extensions::NotificationType
> types
;
94 return CheckNotifications(types
);
97 // content::NotificationObserver implementation.
98 void ExtensionNotificationObserver::Observe(
100 const content::NotificationSource
& source
,
101 const content::NotificationDetails
& details
) {
103 case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
: {
104 const Extension
* extension
=
105 content::Details
<const InstalledExtensionInfo
>(details
)->extension
;
106 if (extension_ids_
.count(extension
->id()))
107 notifications_
.push_back(
108 static_cast<extensions::NotificationType
>(type
));
112 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
113 const Extension
* extension
=
114 content::Details
<const Extension
>(details
).ptr();
115 if (extension_ids_
.count(extension
->id()))
116 notifications_
.push_back(
117 static_cast<extensions::NotificationType
>(type
));
121 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
122 UnloadedExtensionInfo
* reason
=
123 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
124 if (extension_ids_
.count(reason
->extension
->id())) {
125 notifications_
.push_back(
126 static_cast<extensions::NotificationType
>(type
));
127 // The only way that extensions are unloaded in these tests is
129 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST
,
141 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
142 const std::vector
<extensions::NotificationType
>& types
) {
143 testing::AssertionResult result
= (notifications_
== types
) ?
144 testing::AssertionSuccess() :
145 testing::AssertionFailure() << "Expected " << Str(types
) << ", " <<
146 "Got " << Str(notifications_
);
147 notifications_
.clear();
151 } // namespace extensions