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
) {
38 this, chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
, source
);
40 chrome::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
,
43 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
, source
);
46 ExtensionNotificationObserver::~ExtensionNotificationObserver() {}
48 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications() {
49 return CheckNotifications(std::vector
<chrome::NotificationType
>());
52 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
53 chrome::NotificationType type
) {
54 return CheckNotifications(std::vector
<chrome::NotificationType
>(1, type
));
57 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
58 chrome::NotificationType t1
,
59 chrome::NotificationType t2
) {
60 std::vector
<chrome::NotificationType
> types
;
63 return CheckNotifications(types
);
66 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
67 chrome::NotificationType t1
,
68 chrome::NotificationType t2
,
69 chrome::NotificationType t3
) {
70 std::vector
<chrome::NotificationType
> types
;
74 return CheckNotifications(types
);
77 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
78 chrome::NotificationType t1
,
79 chrome::NotificationType t2
,
80 chrome::NotificationType t3
,
81 chrome::NotificationType t4
,
82 chrome::NotificationType t5
,
83 chrome::NotificationType t6
) {
84 std::vector
<chrome::NotificationType
> types
;
91 return CheckNotifications(types
);
94 // content::NotificationObserver implementation.
95 void ExtensionNotificationObserver::Observe(
97 const content::NotificationSource
& source
,
98 const content::NotificationDetails
& details
) {
100 case chrome::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED
: {
101 const Extension
* extension
=
102 content::Details
<const InstalledExtensionInfo
>(details
)->extension
;
103 if (extension_ids_
.count(extension
->id()))
104 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
108 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
109 const Extension
* extension
=
110 content::Details
<const Extension
>(details
).ptr();
111 if (extension_ids_
.count(extension
->id()))
112 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
116 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
117 UnloadedExtensionInfo
* reason
=
118 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
119 if (extension_ids_
.count(reason
->extension
->id())) {
120 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
121 // The only way that extensions are unloaded in these tests is
123 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST
,
135 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
136 const std::vector
<chrome::NotificationType
>& types
) {
137 testing::AssertionResult result
= (notifications_
== types
) ?
138 testing::AssertionSuccess() :
139 testing::AssertionFailure() << "Expected " << Str(types
) << ", " <<
140 "Got " << Str(notifications_
);
141 notifications_
.clear();
145 } // namespace extensions