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 this, chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED
, source
);
42 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
, source
);
45 ExtensionNotificationObserver::~ExtensionNotificationObserver() {}
47 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications() {
48 return CheckNotifications(std::vector
<chrome::NotificationType
>());
51 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
52 chrome::NotificationType type
) {
53 return CheckNotifications(std::vector
<chrome::NotificationType
>(1, type
));
56 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
57 chrome::NotificationType t1
,
58 chrome::NotificationType t2
) {
59 std::vector
<chrome::NotificationType
> types
;
62 return CheckNotifications(types
);
65 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
66 chrome::NotificationType t1
,
67 chrome::NotificationType t2
,
68 chrome::NotificationType t3
) {
69 std::vector
<chrome::NotificationType
> types
;
73 return CheckNotifications(types
);
76 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
77 chrome::NotificationType t1
,
78 chrome::NotificationType t2
,
79 chrome::NotificationType t3
,
80 chrome::NotificationType t4
,
81 chrome::NotificationType t5
,
82 chrome::NotificationType t6
) {
83 std::vector
<chrome::NotificationType
> types
;
90 return CheckNotifications(types
);
93 // content::NotificationObserver implementation.
94 void ExtensionNotificationObserver::Observe(
96 const content::NotificationSource
& source
,
97 const content::NotificationDetails
& details
) {
99 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED
: {
100 const Extension
* extension
=
101 content::Details
<const InstalledExtensionInfo
>(details
)->extension
;
102 if (extension_ids_
.count(extension
->id()))
103 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
107 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
108 const Extension
* extension
=
109 content::Details
<const Extension
>(details
).ptr();
110 if (extension_ids_
.count(extension
->id()))
111 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
115 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
116 UnloadedExtensionInfo
* reason
=
117 content::Details
<UnloadedExtensionInfo
>(details
).ptr();
118 if (extension_ids_
.count(reason
->extension
->id())) {
119 notifications_
.push_back(static_cast<chrome::NotificationType
>(type
));
120 // The only way that extensions are unloaded in these tests is
122 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST
,
134 testing::AssertionResult
ExtensionNotificationObserver::CheckNotifications(
135 const std::vector
<chrome::NotificationType
>& types
) {
136 testing::AssertionResult result
= (notifications_
== types
) ?
137 testing::AssertionSuccess() :
138 testing::AssertionFailure() << "Expected " << Str(types
) << ", " <<
139 "Got " << Str(notifications_
);
140 notifications_
.clear();
144 } // namespace extensions