1 // Copyright (c) 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 #import "ui/message_center/cocoa/notification_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #import "ui/base/cocoa/hover_image_button.h"
13 #import "ui/base/test/ui_cocoa_test_helper.h"
14 #include "ui/message_center/fake_message_center.h"
15 #include "ui/message_center/message_center_style.h"
16 #include "ui/message_center/notification.h"
17 #include "ui/message_center/notification_types.h"
21 class MockMessageCenter : public message_center::FakeMessageCenter {
24 : last_removed_by_user_(false),
26 last_clicked_index_(-1) {}
28 virtual void RemoveNotification(const std::string& id,
29 bool by_user) OVERRIDE {
30 last_removed_id_ = id;
31 last_removed_by_user_ = by_user;
35 virtual void ClickOnNotificationButton(const std::string& id,
36 int button_index) OVERRIDE {
37 last_clicked_id_ = id;
38 last_clicked_index_ = button_index;
41 const std::string& last_removed_id() const { return last_removed_id_; }
42 bool last_removed_by_user() const { return last_removed_by_user_; }
43 int remove_count() const { return remove_count_; }
44 const std::string& last_clicked_id() const { return last_clicked_id_; }
45 int last_clicked_index() const { return last_clicked_index_; }
48 std::string last_removed_id_;
49 bool last_removed_by_user_;
52 std::string last_clicked_id_;
53 int last_clicked_index_;
55 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
60 @implementation MCNotificationController (TestingInterface)
61 - (NSButton*)closeButton {
62 return closeButton_.get();
65 - (NSButton*)secondButton {
66 // The buttons are in Cocoa-y-order, so the 2nd button is first.
67 NSView* view = [[bottomView_ subviews] objectAtIndex:0];
68 return base::mac::ObjCCastStrict<NSButton>(view);
71 - (NSArray*)bottomSubviews {
72 return [bottomView_ subviews];
75 - (NSImageView*)iconView {
79 - (NSTextView*)titleView {
83 - (NSTextView*)messageView {
84 return message_.get();
87 - (NSTextView*)contextMessageView {
88 return contextMessage_.get();
92 return listView_.get();
96 namespace message_center {
98 class NotificationControllerTest : public ui::CocoaTest {
100 NSImage* TestIcon() {
101 return [NSImage imageNamed:NSImageNameUser];
105 message_center::NotifierId DummyNotifierId() {
106 return message_center::NotifierId();
110 TEST_F(NotificationControllerTest, BasicLayout) {
111 scoped_ptr<message_center::Notification> notification(
112 new message_center::Notification(
113 message_center::NOTIFICATION_TYPE_SIMPLE,
115 ASCIIToUTF16("Added to circles"),
116 ASCIIToUTF16("Jonathan and 5 others"),
120 message_center::RichNotificationData(),
122 notification->set_icon(gfx::Image([TestIcon() retain]));
124 base::scoped_nsobject<MCNotificationController> controller(
125 [[MCNotificationController alloc] initWithNotification:notification.get()
126 messageCenter:NULL]);
129 EXPECT_EQ(TestIcon(), [[controller iconView] image]);
130 EXPECT_EQ(base::SysNSStringToUTF16([[controller titleView] string]),
131 notification->title());
132 EXPECT_EQ(base::SysNSStringToUTF16([[controller messageView] string]),
133 notification->message());
134 EXPECT_EQ(controller.get(), [[controller closeButton] target]);
137 TEST_F(NotificationControllerTest, OverflowText) {
138 scoped_ptr<message_center::Notification> notification(
139 new message_center::Notification(
140 message_center::NOTIFICATION_TYPE_SIMPLE,
142 ASCIIToUTF16("This is a much longer title that should wrap "
144 ASCIIToUTF16("And even the message is long. This sure is a wordy "
145 "notification. Are you really going to read this "
150 message_center::RichNotificationData(),
152 base::scoped_nsobject<MCNotificationController> controller(
153 [[MCNotificationController alloc] initWithNotification:notification.get()
154 messageCenter:NULL]);
157 EXPECT_GT(NSHeight([[controller view] frame]),
158 message_center::kNotificationIconSize);
161 TEST_F(NotificationControllerTest, Close) {
162 scoped_ptr<message_center::Notification> notification(
163 new message_center::Notification(
164 message_center::NOTIFICATION_TYPE_SIMPLE,
171 message_center::RichNotificationData(),
173 MockMessageCenter message_center;
175 base::scoped_nsobject<MCNotificationController> controller(
176 [[MCNotificationController alloc] initWithNotification:notification.get()
177 messageCenter:&message_center]);
180 [[controller closeButton] performClick:nil];
182 EXPECT_EQ(1, message_center.remove_count());
183 EXPECT_EQ("an_id", message_center.last_removed_id());
184 EXPECT_TRUE(message_center.last_removed_by_user());
187 TEST_F(NotificationControllerTest, Update) {
188 scoped_ptr<message_center::Notification> notification(
189 new message_center::Notification(
190 message_center::NOTIFICATION_TYPE_SIMPLE,
192 ASCIIToUTF16("A simple title"),
193 ASCIIToUTF16("This message isn't too long and should fit in the"
198 message_center::RichNotificationData(),
200 base::scoped_nsobject<MCNotificationController> controller(
201 [[MCNotificationController alloc] initWithNotification:notification.get()
202 messageCenter:NULL]);
204 // Set up the default layout.
206 EXPECT_EQ(NSHeight([[controller view] frame]),
207 message_center::kNotificationIconSize);
208 EXPECT_FALSE([[controller iconView] image]);
211 notification->set_icon(gfx::Image([TestIcon() retain]));
212 [controller updateNotification:notification.get()];
213 EXPECT_EQ(TestIcon(), [[controller iconView] image]);
214 EXPECT_EQ(NSHeight([[controller view] frame]),
215 message_center::kNotificationIconSize);
218 TEST_F(NotificationControllerTest, Buttons) {
219 message_center::RichNotificationData optional;
220 message_center::ButtonInfo button1(UTF8ToUTF16("button1"));
221 optional.buttons.push_back(button1);
222 message_center::ButtonInfo button2(UTF8ToUTF16("button2"));
223 optional.buttons.push_back(button2);
225 scoped_ptr<message_center::Notification> notification(
226 new message_center::Notification(
227 message_center::NOTIFICATION_TYPE_BASE_FORMAT,
236 MockMessageCenter message_center;
238 base::scoped_nsobject<MCNotificationController> controller(
239 [[MCNotificationController alloc] initWithNotification:notification.get()
240 messageCenter:&message_center]);
243 [[controller secondButton] performClick:nil];
245 EXPECT_EQ("an_id", message_center.last_clicked_id());
246 EXPECT_EQ(1, message_center.last_clicked_index());
249 TEST_F(NotificationControllerTest, Image) {
250 scoped_ptr<message_center::Notification> notification(
251 new message_center::Notification(
252 message_center::NOTIFICATION_TYPE_BASE_FORMAT,
259 message_center::RichNotificationData(),
261 NSImage* image = [NSImage imageNamed:NSImageNameFolder];
262 notification->set_image(gfx::Image([image retain]));
264 MockMessageCenter message_center;
266 base::scoped_nsobject<MCNotificationController> controller(
267 [[MCNotificationController alloc] initWithNotification:notification.get()
268 messageCenter:&message_center]);
271 ASSERT_EQ(1u, [[controller bottomSubviews] count]);
272 ASSERT_TRUE([[[[controller bottomSubviews] lastObject] contentView]
273 isKindOfClass:[NSImageView class]]);
275 [[[[controller bottomSubviews] lastObject] contentView] image]);
278 TEST_F(NotificationControllerTest, List) {
279 message_center::RichNotificationData optional;
280 message_center::NotificationItem item1(
281 UTF8ToUTF16("First title"), UTF8ToUTF16("first message"));
282 optional.items.push_back(item1);
283 message_center::NotificationItem item2(
284 UTF8ToUTF16("Second title"),
285 UTF8ToUTF16("second slightly longer message"));
286 optional.items.push_back(item2);
287 message_center::NotificationItem item3(
288 UTF8ToUTF16(""), // Test for empty string.
289 UTF8ToUTF16(" ")); // Test for string containing only spaces.
290 optional.items.push_back(item3);
291 optional.context_message = UTF8ToUTF16("Context Message");
293 scoped_ptr<message_center::Notification> notification(
294 new message_center::Notification(
295 message_center::NOTIFICATION_TYPE_BASE_FORMAT,
297 UTF8ToUTF16("Notification Title"),
298 UTF8ToUTF16("Notification Message - should be hidden"),
305 MockMessageCenter message_center;
306 base::scoped_nsobject<MCNotificationController> controller(
307 [[MCNotificationController alloc] initWithNotification:notification.get()
308 messageCenter:&message_center]);
311 EXPECT_FALSE([[controller titleView] isHidden]);
312 EXPECT_TRUE([[controller messageView] isHidden]);
313 EXPECT_FALSE([[controller contextMessageView] isHidden]);
315 EXPECT_EQ(3u, [[[controller listView] subviews] count]);
316 EXPECT_LT(NSMaxY([[controller listView] frame]),
317 NSMinY([[controller titleView] frame]));
320 } // namespace message_center