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
<chrome::NotificationType
>& types
) {
18 std::string str
= "[";
19 bool needs_comma
= false;
20 for (std::vector
<chrome::NotificationType
>::const_iterator it
=
21 types
.begin(); it
!= types
.end(); ++it
) {
25 str
+= base::StringPrintf("%d", *it
);
33 ExtensionNotificationObserver::ExtensionNotificationObserver(
34 content::NotificationSource source
,
35 const std::set
<std::string
>& extension_ids
)
36 : extension_ids_(extension_ids
) {
37 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED
, source
);
38 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED
, source
);
39 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
, source
);
42 ExtensionNotificationObserver::~ExtensionNotificationObserver() {}
44 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications() {
45 return CheckNotifications(std::vector
<chrome::NotificationType
>());
48 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
49 chrome::NotificationType type
) {
50 return CheckNotifications(std::vector
<chrome::NotificationType
>(1, type
));
53 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
54 chrome::NotificationType t1
,
55 chrome::NotificationType t2
) {
56 std::vector
<chrome::NotificationType
> types
;
59 return CheckNotifications(types
);
62 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
63 chrome::NotificationType t1
,
64 chrome::NotificationType t2
,
65 chrome::NotificationType t3
) {
66 std::vector
<chrome::NotificationType
> types
;
70 return CheckNotifications(types
);
73 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
74 chrome::NotificationType t1
,
75 chrome::NotificationType t2
,
76 chrome::NotificationType t3
,
77 chrome::NotificationType t4
,
78 chrome::NotificationType t5
,
79 chrome::NotificationType t6
) {
80 std::vector
<chrome::NotificationType
> types
;
87 return CheckNotifications(types
);
90 // content::NotificationObserver implementation.
91 void ExtensionNotificationObserver::Observe(
93 const content::NotificationSource
& source
,
94 const content::NotificationDetails
& details
) {
96 case chrome::NOTIFICATION_EXTENSION_INSTALLED
: {
97 const Extension
* extension
=
98 content::Details
<const InstalledExtensionInfo
>(details
)->extension
;
99 if (extension_ids_
.count(extension
->id()))
100 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
104 case chrome::NOTIFICATION_EXTENSION_LOADED
: {
105 const Extension
* extension
=
106 content::Details
<const Extension
>(details
).ptr();
107 if (extension_ids_
.count(extension
->id()))
108 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
112 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
113 UnloadedExtensionInfo
* reason
=
114 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
115 if (extension_ids_
.count(reason
->extension
->id())) {
116 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
117 // The only way that extensions are unloaded in these tests is
119 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST
,
131 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
132 const std::vector
<chrome::NotificationType
>& types
) {
133 testing::AssertionResult result
= (notifications_
== types
) ?
134 testing::AssertionSuccess() :
135 testing::AssertionFailure() << "Expected " << Str(types
) << ", " <<
136 "Got " << Str(notifications_
);
137 notifications_
.clear();
141 } // namespace extensions