Prefix string16 with base:: in ui/.
[chromium-blink-merge.git] / ui / message_center / cocoa / notification_controller_unittest.mm
blob57726abdb65763e08be4a98752e5d22bdeef881c
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"
19 namespace {
21 class MockMessageCenter : public message_center::FakeMessageCenter {
22  public:
23   MockMessageCenter()
24       : last_removed_by_user_(false),
25         remove_count_(0),
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;
32     ++remove_count_;
33   }
35   virtual void ClickOnNotificationButton(const std::string& id,
36                                          int button_index) OVERRIDE {
37     last_clicked_id_ = id;
38     last_clicked_index_ = button_index;
39   }
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_; }
47  private:
48   std::string last_removed_id_;
49   bool last_removed_by_user_;
50   int remove_count_;
52   std::string last_clicked_id_;
53   int last_clicked_index_;
55   DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
58 }  // namespace
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 {
76   return icon_.get();
79 - (NSTextView*)titleView {
80   return title_.get();
83 - (NSTextView*)messageView {
84   return message_.get();
87 - (NSTextView*)contextMessageView {
88   return contextMessage_.get();
91 - (NSView*)listView {
92   return listView_.get();
94 @end
96 namespace message_center {
98 class NotificationControllerTest : public ui::CocoaTest {
99  public:
100   NSImage* TestIcon() {
101     return [NSImage imageNamed:NSImageNameUser];
102   }
104  protected:
105   message_center::NotifierId DummyNotifierId() {
106     return message_center::NotifierId();
107   }
110 TEST_F(NotificationControllerTest, BasicLayout) {
111   scoped_ptr<message_center::Notification> notification(
112       new message_center::Notification(
113           message_center::NOTIFICATION_TYPE_SIMPLE,
114           "",
115           ASCIIToUTF16("Added to circles"),
116           ASCIIToUTF16("Jonathan and 5 others"),
117           gfx::Image(),
118           base::string16(),
119           DummyNotifierId(),
120           message_center::RichNotificationData(),
121           NULL));
122   notification->set_icon(gfx::Image([TestIcon() retain]));
124   base::scoped_nsobject<MCNotificationController> controller(
125       [[MCNotificationController alloc] initWithNotification:notification.get()
126                                                messageCenter:NULL]);
127   [controller view];
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,
141           "",
142           ASCIIToUTF16("This is a much longer title that should wrap "
143                        "multiple lines."),
144           ASCIIToUTF16("And even the message is long. This sure is a wordy "
145                        "notification. Are you really going to read this "
146                        "entire thing?"),
147           gfx::Image(),
148           base::string16(),
149           DummyNotifierId(),
150           message_center::RichNotificationData(),
151           NULL));
152   base::scoped_nsobject<MCNotificationController> controller(
153       [[MCNotificationController alloc] initWithNotification:notification.get()
154                                                messageCenter:NULL]);
155   [controller view];
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,
165           "an_id",
166           base::string16(),
167           base::string16(),
168           gfx::Image(),
169           base::string16(),
170           DummyNotifierId(),
171           message_center::RichNotificationData(),
172           NULL));
173   MockMessageCenter message_center;
175   base::scoped_nsobject<MCNotificationController> controller(
176       [[MCNotificationController alloc] initWithNotification:notification.get()
177                                                messageCenter:&message_center]);
178   [controller view];
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,
191           "",
192           ASCIIToUTF16("A simple title"),
193           ASCIIToUTF16("This message isn't too long and should fit in the"
194                        "default bounds."),
195           gfx::Image(),
196           base::string16(),
197           DummyNotifierId(),
198           message_center::RichNotificationData(),
199           NULL));
200   base::scoped_nsobject<MCNotificationController> controller(
201       [[MCNotificationController alloc] initWithNotification:notification.get()
202                                                messageCenter:NULL]);
204   // Set up the default layout.
205   [controller view];
206   EXPECT_EQ(NSHeight([[controller view] frame]),
207             message_center::kNotificationIconSize);
208   EXPECT_FALSE([[controller iconView] image]);
210   // Update the icon.
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,
228           "an_id",
229           base::string16(),
230           base::string16(),
231           gfx::Image(),
232           base::string16(),
233           DummyNotifierId(),
234           optional,
235           NULL));
236   MockMessageCenter message_center;
238   base::scoped_nsobject<MCNotificationController> controller(
239       [[MCNotificationController alloc] initWithNotification:notification.get()
240                                                messageCenter:&message_center]);
241   [controller view];
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,
253           "an_id",
254           base::string16(),
255           base::string16(),
256           gfx::Image(),
257           base::string16(),
258           DummyNotifierId(),
259           message_center::RichNotificationData(),
260           NULL));
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]);
269   [controller view];
271   ASSERT_EQ(1u, [[controller bottomSubviews] count]);
272   ASSERT_TRUE([[[[controller bottomSubviews] lastObject] contentView]
273       isKindOfClass:[NSImageView class]]);
274   EXPECT_EQ(image,
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,
296           "an_id",
297           UTF8ToUTF16("Notification Title"),
298           UTF8ToUTF16("Notification Message - should be hidden"),
299           gfx::Image(),
300           base::string16(),
301           DummyNotifierId(),
302           optional,
303           NULL));
305   MockMessageCenter message_center;
306   base::scoped_nsobject<MCNotificationController> controller(
307       [[MCNotificationController alloc] initWithNotification:notification.get()
308                                                messageCenter:&message_center]);
309   [controller view];
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