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
|
// 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.
#import "ui/message_center/cocoa/tray_view_controller.h"
#include "base/mac/scoped_nsobject.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#import "ui/gfx/test/ui_cocoa_test_helper.h"
#include "ui/message_center/fake_notifier_settings_provider.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_impl.h"
#include "ui/message_center/message_center_style.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notifier_settings.h"
using base::ASCIIToUTF16;
namespace message_center {
class TrayViewControllerTest : public ui::CocoaTest {
public:
TrayViewControllerTest()
: center_(NULL) {
}
void SetUp() override {
ui::CocoaTest::SetUp();
message_center::MessageCenter::Initialize();
center_ = message_center::MessageCenter::Get();
center_->DisableTimersForTest();
tray_.reset([[MCTrayViewController alloc] initWithMessageCenter:center_]);
[tray_ setAnimationDuration:0.002];
[tray_ setAnimateClearingNextNotificationDelay:0.001];
[tray_ setAnimationEndedCallback:^{
if (nested_run_loop_.get())
nested_run_loop_->Quit();
}];
[tray_ view]; // Create the view.
}
void TearDown() override {
tray_.reset();
message_center::MessageCenter::Shutdown();
ui::CocoaTest::TearDown();
}
void WaitForAnimationEnded() {
if (![tray_ isAnimating])
return;
nested_run_loop_.reset(new base::RunLoop());
nested_run_loop_->Run();
nested_run_loop_.reset();
}
protected:
message_center::NotifierId DummyNotifierId() {
return message_center::NotifierId();
}
message_center::MessageCenter* center_; // Weak, global.
base::MessageLoopForUI message_loop_;
scoped_ptr<base::RunLoop> nested_run_loop_;
base::scoped_nsobject<MCTrayViewController> tray_;
};
TEST_F(TrayViewControllerTest, AddRemoveOne) {
NSScrollView* view = [[tray_ scrollView] documentView];
EXPECT_EQ(0u, [[view subviews] count]);
scoped_ptr<message_center::Notification> notification_data;
notification_data.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"1",
ASCIIToUTF16("First notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification_data.Pass());
[tray_ onMessageCenterTrayChanged];
ASSERT_EQ(1u, [[view subviews] count]);
// The view should have padding around it.
NSView* notification = [[view subviews] objectAtIndex:0];
NSRect notification_frame = [notification frame];
EXPECT_CGFLOAT_EQ(2 * message_center::kMarginBetweenItems,
NSHeight([view frame]) - NSHeight(notification_frame));
EXPECT_CGFLOAT_EQ(2 * message_center::kMarginBetweenItems,
NSWidth([view frame]) - NSWidth(notification_frame));
EXPECT_GT(NSHeight([[tray_ view] frame]),
NSHeight([[tray_ scrollView] frame]));
center_->RemoveNotification("1", true);
[tray_ onMessageCenterTrayChanged];
EXPECT_EQ(0u, [[view subviews] count]);
// The empty tray is now 100px tall to accommodate
// the empty message.
EXPECT_CGFLOAT_EQ(message_center::kMinScrollViewHeight,
NSHeight([view frame]));
}
TEST_F(TrayViewControllerTest, AddThreeClearAll) {
NSScrollView* view = [[tray_ scrollView] documentView];
EXPECT_EQ(0u, [[view subviews] count]);
scoped_ptr<message_center::Notification> notification;
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"1",
ASCIIToUTF16("First notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"2",
ASCIIToUTF16("Second notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"3",
ASCIIToUTF16("Third notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
[tray_ onMessageCenterTrayChanged];
ASSERT_EQ(3u, [[view subviews] count]);
[tray_ clearAllNotifications:nil];
WaitForAnimationEnded();
[tray_ onMessageCenterTrayChanged];
EXPECT_EQ(0u, [[view subviews] count]);
// The empty tray is now 100px tall to accommodate
// the empty message.
EXPECT_CGFLOAT_EQ(message_center::kMinScrollViewHeight,
NSHeight([view frame]));
}
TEST_F(TrayViewControllerTest, NoClearAllWhenNoNotifications) {
EXPECT_TRUE([tray_ pauseButton]);
EXPECT_TRUE([tray_ clearAllButton]);
// With no notifications, the clear all button should be hidden.
EXPECT_TRUE([[tray_ clearAllButton] isHidden]);
EXPECT_LT(NSMinX([[tray_ clearAllButton] frame]),
NSMinX([[tray_ pauseButton] frame]));
// Add a notification.
scoped_ptr<message_center::Notification> notification;
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"1",
ASCIIToUTF16("First notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
[tray_ onMessageCenterTrayChanged];
// Clear all should now be visible.
EXPECT_FALSE([[tray_ clearAllButton] isHidden]);
EXPECT_GT(NSMinX([[tray_ clearAllButton] frame]),
NSMinX([[tray_ pauseButton] frame]));
// Adding a second notification should keep things still visible.
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"2",
ASCIIToUTF16("Second notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
[tray_ onMessageCenterTrayChanged];
EXPECT_FALSE([[tray_ clearAllButton] isHidden]);
EXPECT_GT(NSMinX([[tray_ clearAllButton] frame]),
NSMinX([[tray_ pauseButton] frame]));
// Clear all notifications.
[tray_ clearAllNotifications:nil];
WaitForAnimationEnded();
[tray_ onMessageCenterTrayChanged];
// The button should be hidden again.
EXPECT_TRUE([[tray_ clearAllButton] isHidden]);
EXPECT_LT(NSMinX([[tray_ clearAllButton] frame]),
NSMinX([[tray_ pauseButton] frame]));
}
namespace {
Notifier* NewNotifier(const std::string& id,
const std::string& title,
bool enabled) {
NotifierId notifier_id(NotifierId::APPLICATION, id);
return new Notifier(notifier_id, base::UTF8ToUTF16(title), enabled);
}
} // namespace
TEST_F(TrayViewControllerTest, Settings) {
std::vector<Notifier*> notifiers;
notifiers.push_back(NewNotifier("id", "title", /*enabled=*/true));
notifiers.push_back(NewNotifier("id2", "other title", /*enabled=*/false));
FakeNotifierSettingsProvider provider(notifiers);
center_->SetNotifierSettingsProvider(&provider);
CGFloat trayHeight = NSHeight([[tray_ view] frame]);
EXPECT_EQ(0, provider.closed_called_count());
[tray_ showSettings:nil];
EXPECT_FALSE(center_->IsMessageCenterVisible());
// There are 0 notifications, but 2 notifiers. The settings pane should be
// higher than the empty tray bubble.
EXPECT_LT(trayHeight, NSHeight([[tray_ view] frame]));
[tray_ showMessages:nil];
EXPECT_EQ(1, provider.closed_called_count());
EXPECT_TRUE(center_->IsMessageCenterVisible());
// The tray should be back at its previous height now.
EXPECT_EQ(trayHeight, NSHeight([[tray_ view] frame]));
// Clean up since this frame owns FakeNotifierSettingsProvider.
center_->SetNotifierSettingsProvider(NULL);
}
TEST_F(TrayViewControllerTest, EmptyCenter) {
EXPECT_FALSE([[tray_ emptyDescription] isHidden]);
// With no notifications, the divider should be hidden.
EXPECT_TRUE([[tray_ divider] isHidden]);
EXPECT_TRUE([[tray_ scrollView] isHidden]);
scoped_ptr<message_center::Notification> notification;
notification.reset(new message_center::Notification(
message_center::NOTIFICATION_TYPE_SIMPLE,
"1",
ASCIIToUTF16("First notification"),
ASCIIToUTF16("This is a simple test."),
gfx::Image(),
base::string16(),
DummyNotifierId(),
message_center::RichNotificationData(),
NULL));
center_->AddNotification(notification.Pass());
[tray_ onMessageCenterTrayChanged];
EXPECT_FALSE([[tray_ divider] isHidden]);
EXPECT_FALSE([[tray_ scrollView] isHidden]);
EXPECT_TRUE([[tray_ emptyDescription] isHidden]);
}
} // namespace message_center
|