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. 4 5 #include "chrome/browser/extensions/extension_notification_observer.h" 6 7 #include <vector> 8 9 #include "base/logging.h" 10 #include "base/strings/stringprintf.h" 11 #include "extensions/common/extension.h" 12 13 namespace extensions { 14 15 namespace { 16 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); 26 } 27 str += "]"; 28 return str; 29 } 30 31 } // namespace 32 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( 40 this, chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, source); 41 registrar_.Add( 42 this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, source); 43 } 44 45 ExtensionNotificationObserver::~ExtensionNotificationObserver() {} 46 47 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications() { 48 return CheckNotifications(std::vector<chrome::NotificationType>()); 49 } 50 51 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( 52 chrome::NotificationType type) { 53 return CheckNotifications(std::vector<chrome::NotificationType>(1, type)); 54 } 55 56 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( 57 chrome::NotificationType t1, 58 chrome::NotificationType t2) { 59 std::vector<chrome::NotificationType> types; 60 types.push_back(t1); 61 types.push_back(t2); 62 return CheckNotifications(types); 63 } 64 65 testing::AssertionResult ExtensionNotificationObserver::CheckNotifications( 66 chrome::NotificationType t1, 67 chrome::NotificationType t2, 68 chrome::NotificationType t3) { 69 std::vector<chrome::NotificationType> types; 70 types.push_back(t1); 71 types.push_back(t2); 72 types.push_back(t3); 73 return CheckNotifications(types); 74 } 75 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; 84 types.push_back(t1); 85 types.push_back(t2); 86 types.push_back(t3); 87 types.push_back(t4); 88 types.push_back(t5); 89 types.push_back(t6); 90 return CheckNotifications(types); 91 } 92 93 // content::NotificationObserver implementation. 94 void ExtensionNotificationObserver::Observe( 95 int type, 96 const content::NotificationSource& source, 97 const content::NotificationDetails& details) { 98 switch (type) { 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)); 104 break; 105 } 106 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)); 112 break; 113 } 114 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 121 // by blacklisting. 122 EXPECT_EQ(UnloadedExtensionInfo::REASON_BLACKLIST, 123 reason->reason); 124 } 125 break; 126 } 127 128 default: 129 NOTREACHED(); 130 break; 131 } 132 } 133 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(); 141 return result; 142 } 143 144 } // namespace extensions 145