Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / message_center / cocoa / notification_controller_unittest.mm
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.
4
5 #import "ui/message_center/cocoa/notification_controller.h"
6
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/gfx/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"
18
19 using base::ASCIIToUTF16;
20 using base::UTF8ToUTF16;
21
22 namespace {
23
24 class MockMessageCenter : public message_center::FakeMessageCenter {
25  public:
26   MockMessageCenter()
27       : last_removed_by_user_(false),
28         remove_count_(0),
29         last_clicked_index_(-1) {}
30
31   virtual void RemoveNotification(const std::string& id,
32                                   bool by_user) OVERRIDE {
33     last_removed_id_ = id;
34     last_removed_by_user_ = by_user;
35     ++remove_count_;
36   }
37
38   virtual void ClickOnNotificationButton(const std::string& id,
39                                          int button_index) OVERRIDE {
40     last_clicked_id_ = id;
41     last_clicked_index_ = button_index;
42   }
43
44   const std::string& last_removed_id() const { return last_removed_id_; }
45   bool last_removed_by_user() const { return last_removed_by_user_; }
46   int remove_count() const { return remove_count_; }
47   const std::string& last_clicked_id() const { return last_clicked_id_; }
48   int last_clicked_index() const { return last_clicked_index_; }
49
50  private:
51   std::string last_removed_id_;
52   bool last_removed_by_user_;
53   int remove_count_;
54
55   std::string last_clicked_id_;
56   int last_clicked_index_;
57
58   DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
59 };
60
61 }  // namespace
62
63 @implementation MCNotificationController (TestingInterface)
64 - (NSButton*)closeButton {
65   return closeButton_.get();
66 }
67
68 - (NSImageView*)smallImageView {
69   return smallImage_.get();
70 }
71
72 - (NSButton*)secondButton {
73   // The buttons are in Cocoa-y-order, so the 2nd button is first.
74   NSView* view = [[bottomView_ subviews] objectAtIndex:0];
75   return base::mac::ObjCCastStrict<NSButton>(view);
76 }
77
78 - (NSArray*)bottomSubviews {
79   return [bottomView_ subviews];
80 }
81
82 - (NSImageView*)iconView {
83   return icon_.get();
84 }
85
86 - (NSTextView*)titleView {
87   return title_.get();
88 }
89
90 - (NSTextView*)messageView {
91   return message_.get();
92 }
93
94 - (NSTextView*)contextMessageView {
95   return contextMessage_.get();
96 }
97
98 - (NSView*)listView {
99   return listView_.get();
100 }
101 @end
102
103 namespace message_center {
104
105 class NotificationControllerTest : public ui::CocoaTest {
106  public:
107   NSImage* TestIcon() {
108     return [NSImage imageNamed:NSImageNameUser];
109   }
110
111  protected:
112   message_center::NotifierId DummyNotifierId() {
113     return message_center::NotifierId();
114   }
115 };
116
117 TEST_F(NotificationControllerTest, BasicLayout) {
118   scoped_ptr<message_center::Notification> notification(
119       new message_center::Notification(
120           message_center::NOTIFICATION_TYPE_SIMPLE,
121           "",
122           ASCIIToUTF16("Added to circles"),
123           ASCIIToUTF16("Jonathan and 5 others"),
124           gfx::Image(),
125           base::string16(),
126           DummyNotifierId(),
127           message_center::RichNotificationData(),
128           NULL));
129   gfx::Image testIcon([TestIcon() retain]);
130   notification->set_icon(testIcon);
131   notification->set_small_image(testIcon);
132
133   base::scoped_nsobject<MCNotificationController> controller(
134       [[MCNotificationController alloc] initWithNotification:notification.get()
135                                                messageCenter:NULL]);
136   [controller view];
137
138   EXPECT_EQ(TestIcon(), [[controller iconView] image]);
139   EXPECT_EQ(TestIcon(), [[controller smallImageView] image]);
140   EXPECT_EQ(base::SysNSStringToUTF16([[controller titleView] string]),
141             notification->title());
142   EXPECT_EQ(base::SysNSStringToUTF16([[controller messageView] string]),
143             notification->message());
144   EXPECT_EQ(controller.get(), [[controller closeButton] target]);
145 }
146
147 TEST_F(NotificationControllerTest, OverflowText) {
148   scoped_ptr<message_center::Notification> notification(
149       new message_center::Notification(
150           message_center::NOTIFICATION_TYPE_SIMPLE,
151           "",
152           ASCIIToUTF16("This is a much longer title that should wrap "
153                        "multiple lines."),
154           ASCIIToUTF16("And even the message is long. This sure is a wordy "
155                        "notification. Are you really going to read this "
156                        "entire thing?"),
157           gfx::Image(),
158           base::string16(),
159           DummyNotifierId(),
160           message_center::RichNotificationData(),
161           NULL));
162   base::scoped_nsobject<MCNotificationController> controller(
163       [[MCNotificationController alloc] initWithNotification:notification.get()
164                                                messageCenter:NULL]);
165   [controller view];
166
167   EXPECT_GT(NSHeight([[controller view] frame]),
168             message_center::kNotificationIconSize);
169 }
170
171 TEST_F(NotificationControllerTest, Close) {
172   scoped_ptr<message_center::Notification> notification(
173       new message_center::Notification(
174           message_center::NOTIFICATION_TYPE_SIMPLE,
175           "an_id",
176           base::string16(),
177           base::string16(),
178           gfx::Image(),
179           base::string16(),
180           DummyNotifierId(),
181           message_center::RichNotificationData(),
182           NULL));
183   MockMessageCenter message_center;
184
185   base::scoped_nsobject<MCNotificationController> controller(
186       [[MCNotificationController alloc] initWithNotification:notification.get()
187                                                messageCenter:&message_center]);
188   [controller view];
189
190   [[controller closeButton] performClick:nil];
191
192   EXPECT_EQ(1, message_center.remove_count());
193   EXPECT_EQ("an_id", message_center.last_removed_id());
194   EXPECT_TRUE(message_center.last_removed_by_user());
195 }
196
197 TEST_F(NotificationControllerTest, Update) {
198   scoped_ptr<message_center::Notification> notification(
199       new message_center::Notification(
200           message_center::NOTIFICATION_TYPE_SIMPLE,
201           "",
202           ASCIIToUTF16("A simple title"),
203           ASCIIToUTF16("This message isn't too long and should fit in the"
204                        "default bounds."),
205           gfx::Image(),
206           base::string16(),
207           DummyNotifierId(),
208           message_center::RichNotificationData(),
209           NULL));
210   base::scoped_nsobject<MCNotificationController> controller(
211       [[MCNotificationController alloc] initWithNotification:notification.get()
212                                                messageCenter:NULL]);
213
214   // Set up the default layout.
215   [controller view];
216   EXPECT_EQ(NSHeight([[controller view] frame]),
217             message_center::kNotificationIconSize);
218   EXPECT_FALSE([[controller iconView] image]);
219   EXPECT_FALSE([[controller smallImageView] image]);
220
221   // Update the icon.
222   gfx::Image testIcon([TestIcon() retain]);
223   notification->set_icon(testIcon);
224   notification->set_small_image(testIcon);
225   [controller updateNotification:notification.get()];
226   EXPECT_EQ(TestIcon(), [[controller iconView] image]);
227   EXPECT_EQ(TestIcon(), [[controller smallImageView] image]);
228   EXPECT_EQ(NSHeight([[controller view] frame]),
229             message_center::kNotificationIconSize);
230 }
231
232 TEST_F(NotificationControllerTest, Buttons) {
233   message_center::RichNotificationData optional;
234   message_center::ButtonInfo button1(UTF8ToUTF16("button1"));
235   optional.buttons.push_back(button1);
236   message_center::ButtonInfo button2(UTF8ToUTF16("button2"));
237   optional.buttons.push_back(button2);
238
239   scoped_ptr<message_center::Notification> notification(
240       new message_center::Notification(
241           message_center::NOTIFICATION_TYPE_BASE_FORMAT,
242           "an_id",
243           base::string16(),
244           base::string16(),
245           gfx::Image(),
246           base::string16(),
247           DummyNotifierId(),
248           optional,
249           NULL));
250   MockMessageCenter message_center;
251
252   base::scoped_nsobject<MCNotificationController> controller(
253       [[MCNotificationController alloc] initWithNotification:notification.get()
254                                                messageCenter:&message_center]);
255   [controller view];
256
257   [[controller secondButton] performClick:nil];
258
259   EXPECT_EQ("an_id", message_center.last_clicked_id());
260   EXPECT_EQ(1, message_center.last_clicked_index());
261 }
262
263 TEST_F(NotificationControllerTest, Image) {
264   scoped_ptr<message_center::Notification> notification(
265       new message_center::Notification(
266           message_center::NOTIFICATION_TYPE_BASE_FORMAT,
267           "an_id",
268           base::string16(),
269           base::string16(),
270           gfx::Image(),
271           base::string16(),
272           DummyNotifierId(),
273           message_center::RichNotificationData(),
274           NULL));
275   NSImage* image = [NSImage imageNamed:NSImageNameFolder];
276   notification->set_image(gfx::Image([image retain]));
277
278   MockMessageCenter message_center;
279
280   base::scoped_nsobject<MCNotificationController> controller(
281       [[MCNotificationController alloc] initWithNotification:notification.get()
282                                                messageCenter:&message_center]);
283   [controller view];
284
285   ASSERT_EQ(1u, [[controller bottomSubviews] count]);
286   ASSERT_TRUE([[[[controller bottomSubviews] lastObject] contentView]
287       isKindOfClass:[NSImageView class]]);
288   EXPECT_EQ(image,
289       [[[[controller bottomSubviews] lastObject] contentView] image]);
290 }
291
292 TEST_F(NotificationControllerTest, List) {
293   message_center::RichNotificationData optional;
294   message_center::NotificationItem item1(
295       UTF8ToUTF16("First title"), UTF8ToUTF16("first message"));
296   optional.items.push_back(item1);
297   message_center::NotificationItem item2(
298       UTF8ToUTF16("Second title"),
299       UTF8ToUTF16("second slightly longer message"));
300   optional.items.push_back(item2);
301   message_center::NotificationItem item3(
302       UTF8ToUTF16(""),    // Test for empty string.
303       UTF8ToUTF16(" "));  // Test for string containing only spaces.
304   optional.items.push_back(item3);
305   optional.context_message = UTF8ToUTF16("Context Message");
306
307   scoped_ptr<message_center::Notification> notification(
308       new message_center::Notification(
309           message_center::NOTIFICATION_TYPE_BASE_FORMAT,
310           "an_id",
311           UTF8ToUTF16("Notification Title"),
312           UTF8ToUTF16("Notification Message - should be hidden"),
313           gfx::Image(),
314           base::string16(),
315           DummyNotifierId(),
316           optional,
317           NULL));
318
319   MockMessageCenter message_center;
320   base::scoped_nsobject<MCNotificationController> controller(
321       [[MCNotificationController alloc] initWithNotification:notification.get()
322                                                messageCenter:&message_center]);
323   [controller view];
324
325   EXPECT_FALSE([[controller titleView] isHidden]);
326   EXPECT_TRUE([[controller messageView] isHidden]);
327   EXPECT_FALSE([[controller contextMessageView] isHidden]);
328
329   EXPECT_EQ(3u, [[[controller listView] subviews] count]);
330   EXPECT_LT(NSMaxY([[controller listView] frame]),
331             NSMinY([[controller titleView] frame]));
332 }
333
334 }  // namespace message_center