Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_notification_observer.cc
blobe7bb65a606f07f5548c545bc302e261f26efd0e2
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"
7 #include <vector>
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "extensions/common/extension.h"
13 namespace extensions {
15 namespace {
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) {
22 if (needs_comma)
23 str += ",";
24 needs_comma = true;
25 str += base::StringPrintf("%d", *it);
27 str += "]";
28 return str;
31 } // namespace
33 ExtensionNotificationObserver::ExtensionNotificationObserver(
34 content::NotificationSource source,
35 const std::set<std::string>& extension_ids)
36 : extension_ids_(extension_ids) {
37 registrar_.Add(
38 this, chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, source);
39 registrar_.Add(this,
40 chrome::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
41 source);
42 registrar_.Add(
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;
61 types.push_back(t1);
62 types.push_back(t2);
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;
71 types.push_back(t1);
72 types.push_back(t2);
73 types.push_back(t3);
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;
85 types.push_back(t1);
86 types.push_back(t2);
87 types.push_back(t3);
88 types.push_back(t4);
89 types.push_back(t5);
90 types.push_back(t6);
91 return CheckNotifications(types);
94 // content::NotificationObserver implementation.
95 void ExtensionNotificationObserver::Observe(
96 int type,
97 const content::NotificationSource& source,
98 const content::NotificationDetails& details) {
99 switch (type) {
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));
105 break;
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));
113 break;
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
122 // by blacklisting.
123 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST,
124 reason->reason);
126 break;
129 default:
130 NOTREACHED();
131 break;
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();
142 return result;
145 } // namespace extensions