1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/message_center_tray.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/models/menu_model.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_types.h"
using base::ASCIIToUTF16;
namespace message_center {
namespace {
class MockDelegate : public MessageCenterTrayDelegate {
public:
MockDelegate()
: show_popups_success_(true),
show_message_center_success_(true),
enable_context_menu_(true) {}
~MockDelegate() override {}
void OnMessageCenterTrayChanged() override {}
bool ShowPopups() override { return show_message_center_success_; }
void HidePopups() override {}
bool ShowMessageCenter() override { return show_popups_success_; }
void HideMessageCenter() override {}
bool ShowNotifierSettings() override { return true; }
bool IsContextMenuEnabled() const override { return enable_context_menu_; }
MessageCenterTray* GetMessageCenterTray() override { return NULL; }
bool show_popups_success_;
bool show_message_center_success_;
bool enable_context_menu_;
private:
DISALLOW_COPY_AND_ASSIGN(MockDelegate);
};
} // namespace
class MessageCenterTrayTest : public testing::Test {
public:
MessageCenterTrayTest() {}
~MessageCenterTrayTest() override {}
void SetUp() override {
MessageCenter::Initialize();
delegate_.reset(new MockDelegate);
message_center_ = MessageCenter::Get();
message_center_tray_.reset(
new MessageCenterTray(delegate_.get(), message_center_));
}
void TearDown() override {
message_center_tray_.reset();
delegate_.reset();
message_center_ = NULL;
MessageCenter::Shutdown();
}
protected:
NotifierId DummyNotifierId() {
return NotifierId();
}
void AddNotification(const std::string& id) {
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id,
ASCIIToUTF16("Test Web Notification"),
ASCIIToUTF16("Notification message body."),
gfx::Image(),
ASCIIToUTF16("www.test.org"),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL /* delegate */));
message_center_->AddNotification(notification.Pass());
}
scoped_ptr<MockDelegate> delegate_;
scoped_ptr<MessageCenterTray> message_center_tray_;
MessageCenter* message_center_;
private:
DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest);
};
TEST_F(MessageCenterTrayTest, BasicMessageCenter) {
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
bool shown = message_center_tray_->ShowMessageCenterBubble();
EXPECT_TRUE(shown);
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_TRUE(message_center_tray_->message_center_visible());
message_center_tray_->HideMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->ToggleMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_TRUE(message_center_tray_->message_center_visible());
message_center_tray_->ToggleMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
}
TEST_F(MessageCenterTrayTest, BasicPopup) {
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->ShowPopupBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
AddNotification("BasicPopup");
ASSERT_TRUE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->HidePopupBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
}
TEST_F(MessageCenterTrayTest, MessageCenterClosesPopups) {
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
AddNotification("MessageCenterClosesPopups");
ASSERT_TRUE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
bool shown = message_center_tray_->ShowMessageCenterBubble();
EXPECT_TRUE(shown);
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_TRUE(message_center_tray_->message_center_visible());
// The notification is queued if it's added when message center is visible.
AddNotification("MessageCenterClosesPopups2");
message_center_tray_->ShowPopupBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_TRUE(message_center_tray_->message_center_visible());
message_center_tray_->HideMessageCenterBubble();
// The queued notification appears as a popup.
ASSERT_TRUE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->ShowMessageCenterBubble();
message_center_tray_->HideMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
}
TEST_F(MessageCenterTrayTest, MessageCenterReopenPopupsForSystemPriority) {
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
"MessageCenterReopnPopupsForSystemPriority",
ASCIIToUTF16("Test Web Notification"),
ASCIIToUTF16("Notification message body."),
gfx::Image(),
ASCIIToUTF16("www.test.org"),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL /* delegate */));
notification->SetSystemPriority();
message_center_->AddNotification(notification.Pass());
ASSERT_TRUE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
bool shown = message_center_tray_->ShowMessageCenterBubble();
EXPECT_TRUE(shown);
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_TRUE(message_center_tray_->message_center_visible());
message_center_tray_->HideMessageCenterBubble();
ASSERT_TRUE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
}
TEST_F(MessageCenterTrayTest, ShowBubbleFails) {
// Now the delegate will signal that it was unable to show a bubble.
delegate_->show_popups_success_ = false;
delegate_->show_message_center_success_ = false;
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
AddNotification("ShowBubbleFails");
message_center_tray_->ShowPopupBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
bool shown = message_center_tray_->ShowMessageCenterBubble();
EXPECT_FALSE(shown);
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->HideMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->ToggleMessageCenterBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
message_center_tray_->HidePopupBubble();
ASSERT_FALSE(message_center_tray_->popups_visible());
ASSERT_FALSE(message_center_tray_->message_center_visible());
}
TEST_F(MessageCenterTrayTest, ContextMenuTest) {
const std::string id1 = "id1";
const std::string id2 = "id2";
const std::string id3 = "id3";
AddNotification(id1);
base::string16 display_source = ASCIIToUTF16("www.test.org");
NotifierId notifier_id = DummyNotifierId();
NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
scoped_ptr<Notification> notification(
new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
id2,
ASCIIToUTF16("Test Web Notification"),
ASCIIToUTF16("Notification message body."),
gfx::Image(),
base::string16() /* empty display source */,
notifier_id2,
message_center::RichNotificationData(),
NULL /* delegate */));
message_center_->AddNotification(notification.Pass());
AddNotification(id3);
scoped_ptr<ui::MenuModel> model(
message_center_tray_->CreateNotificationMenuModel(
notifier_id, display_source));
EXPECT_EQ(2, model->GetItemCount());
const int second_command = model->GetCommandIdAt(1);
// The second item is to open the settings.
EXPECT_TRUE(model->IsEnabledAt(0));
EXPECT_TRUE(model->IsEnabledAt(1));
model->ActivatedAt(1);
EXPECT_TRUE(message_center_tray_->message_center_visible());
message_center_tray_->HideMessageCenterBubble();
// The first item is to disable notifications from the notifier id. It also
// removes all notifications from the same notifier, i.e. id1 and id3.
model->ActivatedAt(0);
NotificationList::Notifications notifications =
message_center_->GetVisibleNotifications();
EXPECT_EQ(1u, notifications.size());
EXPECT_EQ(id2, (*notifications.begin())->id());
// Disables the context menu.
delegate_->enable_context_menu_ = false;
// id2 doesn't have the display source, so it don't have the menu item for
// disabling notifications.
model = message_center_tray_->CreateNotificationMenuModel(
notifier_id2, base::string16());
EXPECT_EQ(1, model->GetItemCount());
EXPECT_EQ(second_command, model->GetCommandIdAt(0));
// The command itself is disabled because delegate disables context menu.
EXPECT_FALSE(model->IsEnabledAt(0));
}
} // namespace message_center
|