1 // Copyright 2014 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 #include "ui/message_center/views/notification_view.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/gfx/image/image.h"
12 #include "ui/message_center/notification.h"
13 #include "ui/message_center/notification_list.h"
14 #include "ui/message_center/notification_types.h"
15 #include "ui/message_center/views/message_center_controller.h"
16 #include "ui/message_center/views/notification_button.h"
17 #include "ui/views/layout/fill_layout.h"
18 #include "ui/views/test/views_test_base.h"
19 #include "ui/views/widget/widget_delegate.h"
21 namespace message_center
{
23 /* Test fixture ***************************************************************/
25 class NotificationViewTest
: public views::ViewsTestBase
,
26 public MessageCenterController
{
28 NotificationViewTest();
29 virtual ~NotificationViewTest();
31 virtual void SetUp() OVERRIDE
;
32 virtual void TearDown() OVERRIDE
;
34 views::Widget
* widget() { return notification_view_
->GetWidget(); }
35 NotificationView
* notification_view() { return notification_view_
.get(); }
36 Notification
* notification() { return notification_
.get(); }
37 RichNotificationData
* data() { return data_
.get(); }
39 // Overridden from MessageCenterController:
40 virtual void ClickOnNotification(const std::string
& notification_id
) OVERRIDE
;
41 virtual void RemoveNotification(const std::string
& notification_id
,
42 bool by_user
) OVERRIDE
;
43 virtual scoped_ptr
<ui::MenuModel
> CreateMenuModel(
44 const NotifierId
& notifier_id
,
45 const base::string16
& display_source
) OVERRIDE
;
46 virtual bool HasClickedListener(const std::string
& notification_id
) OVERRIDE
;
47 virtual void ClickOnNotificationButton(const std::string
& notification_id
,
48 int button_index
) OVERRIDE
;
51 const gfx::Image
CreateTestImage(int width
, int height
) {
52 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width
, height
));
55 const SkBitmap
CreateBitmap(int width
, int height
) {
57 bitmap
.allocN32Pixels(width
, height
);
58 bitmap
.eraseRGB(0, 255, 0);
62 std::vector
<ButtonInfo
> CreateButtons(int number
) {
63 ButtonInfo
info(base::ASCIIToUTF16("Test button title."));
64 info
.icon
= CreateTestImage(4, 4);
65 return std::vector
<ButtonInfo
>(number
, info
);
68 void CheckVerticalOrderInNotification() {
69 std::vector
<views::View
*> vertical_order
;
70 vertical_order
.push_back(notification_view()->top_view_
);
71 vertical_order
.push_back(notification_view()->image_view_
);
72 std::copy(notification_view()->action_buttons_
.begin(),
73 notification_view()->action_buttons_
.end(),
74 std::back_inserter(vertical_order
));
75 std::vector
<views::View
*>::iterator current
= vertical_order
.begin();
76 std::vector
<views::View
*>::iterator last
= current
++;
77 while (current
!= vertical_order
.end()) {
78 gfx::Point last_point
= (*last
)->bounds().origin();
79 views::View::ConvertPointToTarget(
80 (*last
), notification_view(), &last_point
);
82 gfx::Point current_point
= (*current
)->bounds().origin();
83 views::View::ConvertPointToTarget(
84 (*current
), notification_view(), ¤t_point
);
86 EXPECT_LT(last_point
.y(), current_point
.y());
91 void UpdateNotificationViews() {
92 notification_view()->CreateOrUpdateViews(*notification());
93 notification_view()->Layout();
97 scoped_ptr
<RichNotificationData
> data_
;
98 scoped_ptr
<Notification
> notification_
;
99 scoped_ptr
<NotificationView
> notification_view_
;
101 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest
);
104 NotificationViewTest::NotificationViewTest() {
107 NotificationViewTest::~NotificationViewTest() {
110 void NotificationViewTest::SetUp() {
111 views::ViewsTestBase::SetUp();
112 // Create a dummy notification.
114 data_
.reset(new RichNotificationData());
116 new Notification(NOTIFICATION_TYPE_BASE_FORMAT
,
117 std::string("notification id"),
118 base::UTF8ToUTF16("title"),
119 base::UTF8ToUTF16("message"),
120 CreateTestImage(80, 80),
121 base::UTF8ToUTF16("display source"),
122 NotifierId(NotifierId::APPLICATION
, "extension_id"),
125 notification_
->set_small_image(CreateTestImage(16, 16));
126 notification_
->set_image(CreateTestImage(320, 240));
128 // Then create a new NotificationView with that single notification.
129 notification_view_
.reset(
130 NotificationView::Create(this, *notification_
, true));
131 notification_view_
->set_owned_by_client();
133 views::Widget::InitParams
init_params(
134 CreateParams(views::Widget::InitParams::TYPE_POPUP
));
135 views::Widget
* widget
= new views::Widget();
136 widget
->Init(init_params
);
137 widget
->SetContentsView(notification_view_
.get());
138 widget
->SetSize(notification_view_
->GetPreferredSize());
141 void NotificationViewTest::TearDown() {
143 notification_view_
.reset();
144 views::ViewsTestBase::TearDown();
147 void NotificationViewTest::ClickOnNotification(
148 const std::string
& notification_id
) {
149 // For this test, this method should not be invoked.
153 void NotificationViewTest::RemoveNotification(
154 const std::string
& notification_id
,
156 // For this test, this method should not be invoked.
160 scoped_ptr
<ui::MenuModel
> NotificationViewTest::CreateMenuModel(
161 const NotifierId
& notifier_id
,
162 const base::string16
& display_source
) {
163 // For this test, this method should not be invoked.
165 return scoped_ptr
<ui::MenuModel
>();
168 bool NotificationViewTest::HasClickedListener(
169 const std::string
& notification_id
) {
173 void NotificationViewTest::ClickOnNotificationButton(
174 const std::string
& notification_id
,
176 // For this test, this method should not be invoked.
180 /* Unit tests *****************************************************************/
182 TEST_F(NotificationViewTest
, CreateOrUpdateTest
) {
183 EXPECT_TRUE(NULL
!= notification_view()->title_view_
);
184 EXPECT_TRUE(NULL
!= notification_view()->message_view_
);
185 EXPECT_TRUE(NULL
!= notification_view()->icon_view_
);
186 EXPECT_TRUE(NULL
!= notification_view()->image_view_
);
188 notification()->set_image(gfx::Image());
189 notification()->set_title(base::ASCIIToUTF16(""));
190 notification()->set_message(base::ASCIIToUTF16(""));
191 notification()->set_icon(gfx::Image());
193 notification_view()->CreateOrUpdateViews(*notification());
194 EXPECT_TRUE(NULL
== notification_view()->title_view_
);
195 EXPECT_TRUE(NULL
== notification_view()->message_view_
);
196 EXPECT_TRUE(NULL
== notification_view()->image_view_
);
197 // We still expect an icon view for all layouts.
198 EXPECT_TRUE(NULL
!= notification_view()->icon_view_
);
201 TEST_F(NotificationViewTest
, TestLineLimits
) {
202 notification()->set_image(CreateTestImage(0, 0));
203 notification()->set_context_message(base::ASCIIToUTF16(""));
204 notification_view()->CreateOrUpdateViews(*notification());
206 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(0, 360));
207 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(1, 360));
208 EXPECT_EQ(3, notification_view()->GetMessageLineLimit(2, 360));
210 notification()->set_image(CreateTestImage(2, 2));
211 notification_view()->CreateOrUpdateViews(*notification());
213 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(0, 360));
214 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360));
215 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360));
217 notification()->set_context_message(base::UTF8ToUTF16("foo"));
218 notification_view()->CreateOrUpdateViews(*notification());
220 EXPECT_TRUE(notification_view()->context_message_view_
!= NULL
);
222 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360));
223 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360));
224 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360));
227 TEST_F(NotificationViewTest
, UpdateButtonsStateTest
) {
228 notification()->set_buttons(CreateButtons(2));
229 notification_view()->CreateOrUpdateViews(*notification());
232 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[0]->background());
234 // Now construct a mouse move event 1 pixel inside the boundary of the action
236 gfx::Point
cursor_location(1, 1);
237 views::View::ConvertPointToWidget(notification_view()->action_buttons_
[0],
239 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
244 widget()->OnMouseEvent(&move
);
246 EXPECT_TRUE(NULL
!= notification_view()->action_buttons_
[0]->background());
248 notification_view()->CreateOrUpdateViews(*notification());
250 EXPECT_TRUE(NULL
!= notification_view()->action_buttons_
[0]->background());
252 // Now construct a mouse move event 1 pixel outside the boundary of the
254 cursor_location
= gfx::Point(-1, -1);
255 move
= ui::MouseEvent(ui::ET_MOUSE_MOVED
,
260 widget()->OnMouseEvent(&move
);
262 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[0]->background());
265 TEST_F(NotificationViewTest
, UpdateButtonCountTest
) {
266 notification()->set_buttons(CreateButtons(2));
267 notification_view()->CreateOrUpdateViews(*notification());
270 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[0]->background());
271 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[1]->background());
273 // Now construct a mouse move event 1 pixel inside the boundary of the action
275 gfx::Point
cursor_location(1, 1);
276 views::View::ConvertPointToWidget(notification_view()->action_buttons_
[0],
278 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
,
283 widget()->OnMouseEvent(&move
);
285 EXPECT_TRUE(NULL
!= notification_view()->action_buttons_
[0]->background());
286 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[1]->background());
288 notification()->set_buttons(CreateButtons(1));
289 notification_view()->CreateOrUpdateViews(*notification());
291 EXPECT_TRUE(NULL
!= notification_view()->action_buttons_
[0]->background());
292 EXPECT_EQ(1u, notification_view()->action_buttons_
.size());
294 // Now construct a mouse move event 1 pixel outside the boundary of the
296 cursor_location
= gfx::Point(-1, -1);
297 move
= ui::MouseEvent(ui::ET_MOUSE_MOVED
,
302 widget()->OnMouseEvent(&move
);
304 EXPECT_TRUE(NULL
== notification_view()->action_buttons_
[0]->background());
307 TEST_F(NotificationViewTest
, ViewOrderingTest
) {
308 // Tests that views are created in the correct vertical order.
309 notification()->set_buttons(CreateButtons(2));
311 // Layout the initial views.
312 UpdateNotificationViews();
314 // Double-check that vertical order is correct.
315 CheckVerticalOrderInNotification();
317 // Tests that views remain in that order even after an update.
318 UpdateNotificationViews();
319 CheckVerticalOrderInNotification();
322 } // namespace message_center