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/events/event_utils.h"
12 #include "ui/gfx/image/image.h"
13 #include "ui/message_center/notification.h"
14 #include "ui/message_center/notification_list.h"
15 #include "ui/message_center/notification_types.h"
16 #include "ui/message_center/views/message_center_controller.h"
17 #include "ui/message_center/views/notification_button.h"
18 #include "ui/views/layout/fill_layout.h"
19 #include "ui/views/test/views_test_base.h"
20 #include "ui/views/widget/widget_delegate.h"
22 namespace message_center
{
24 /* Test fixture ***************************************************************/
26 class NotificationViewTest
: public views::ViewsTestBase
,
27 public MessageCenterController
{
29 NotificationViewTest();
30 ~NotificationViewTest() override
;
32 void SetUp() override
;
33 void TearDown() override
;
35 views::Widget
* widget() { return notification_view_
->GetWidget(); }
36 NotificationView
* notification_view() { return notification_view_
.get(); }
37 Notification
* notification() { return notification_
.get(); }
38 RichNotificationData
* data() { return data_
.get(); }
40 // Overridden from MessageCenterController:
41 void ClickOnNotification(const std::string
& notification_id
) override
;
42 void RemoveNotification(const std::string
& notification_id
,
43 bool by_user
) override
;
44 scoped_ptr
<ui::MenuModel
> CreateMenuModel(
45 const NotifierId
& notifier_id
,
46 const base::string16
& display_source
) override
;
47 bool HasClickedListener(const std::string
& notification_id
) override
;
48 void ClickOnNotificationButton(const std::string
& notification_id
,
49 int button_index
) override
;
52 const gfx::Image
CreateTestImage(int width
, int height
) {
53 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width
, height
));
56 const SkBitmap
CreateBitmap(int width
, int height
) {
58 bitmap
.allocN32Pixels(width
, height
);
59 bitmap
.eraseRGB(0, 255, 0);
63 std::vector
<ButtonInfo
> CreateButtons(int number
) {
64 ButtonInfo
info(base::ASCIIToUTF16("Test button title."));
65 info
.icon
= CreateTestImage(4, 4);
66 return std::vector
<ButtonInfo
>(number
, info
);
69 void CheckVerticalOrderInNotification() {
70 std::vector
<views::View
*> vertical_order
;
71 vertical_order
.push_back(notification_view()->top_view_
);
72 vertical_order
.push_back(notification_view()->image_view_
);
73 std::copy(notification_view()->action_buttons_
.begin(),
74 notification_view()->action_buttons_
.end(),
75 std::back_inserter(vertical_order
));
76 std::vector
<views::View
*>::iterator current
= vertical_order
.begin();
77 std::vector
<views::View
*>::iterator last
= current
++;
78 while (current
!= vertical_order
.end()) {
79 gfx::Point last_point
= (*last
)->bounds().origin();
80 views::View::ConvertPointToTarget(
81 (*last
), notification_view(), &last_point
);
83 gfx::Point current_point
= (*current
)->bounds().origin();
84 views::View::ConvertPointToTarget(
85 (*current
), notification_view(), ¤t_point
);
87 EXPECT_LT(last_point
.y(), current_point
.y());
92 void UpdateNotificationViews() {
93 notification_view()->CreateOrUpdateViews(*notification());
94 notification_view()->Layout();
98 scoped_ptr
<RichNotificationData
> data_
;
99 scoped_ptr
<Notification
> notification_
;
100 scoped_ptr
<NotificationView
> notification_view_
;
102 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest
);
105 NotificationViewTest::NotificationViewTest() {
108 NotificationViewTest::~NotificationViewTest() {
111 void NotificationViewTest::SetUp() {
112 views::ViewsTestBase::SetUp();
113 // Create a dummy notification.
115 data_
.reset(new RichNotificationData());
117 new Notification(NOTIFICATION_TYPE_BASE_FORMAT
,
118 std::string("notification id"),
119 base::UTF8ToUTF16("title"),
120 base::UTF8ToUTF16("message"),
121 CreateTestImage(80, 80),
122 base::UTF8ToUTF16("display source"),
123 NotifierId(NotifierId::APPLICATION
, "extension_id"),
126 notification_
->set_small_image(CreateTestImage(16, 16));
127 notification_
->set_image(CreateTestImage(320, 240));
129 // Then create a new NotificationView with that single notification.
130 notification_view_
.reset(
131 NotificationView::Create(this, *notification_
, true));
132 notification_view_
->set_owned_by_client();
134 views::Widget::InitParams
init_params(
135 CreateParams(views::Widget::InitParams::TYPE_POPUP
));
136 views::Widget
* widget
= new views::Widget();
137 widget
->Init(init_params
);
138 widget
->SetContentsView(notification_view_
.get());
139 widget
->SetSize(notification_view_
->GetPreferredSize());
142 void NotificationViewTest::TearDown() {
144 notification_view_
.reset();
145 views::ViewsTestBase::TearDown();
148 void NotificationViewTest::ClickOnNotification(
149 const std::string
& notification_id
) {
150 // For this test, this method should not be invoked.
154 void NotificationViewTest::RemoveNotification(
155 const std::string
& notification_id
,
157 // For this test, this method should not be invoked.
161 scoped_ptr
<ui::MenuModel
> NotificationViewTest::CreateMenuModel(
162 const NotifierId
& notifier_id
,
163 const base::string16
& display_source
) {
164 // For this test, this method should not be invoked.
169 bool NotificationViewTest::HasClickedListener(
170 const std::string
& notification_id
) {
174 void NotificationViewTest::ClickOnNotificationButton(
175 const std::string
& notification_id
,
177 // For this test, this method should not be invoked.
181 /* Unit tests *****************************************************************/
183 TEST_F(NotificationViewTest
, CreateOrUpdateTest
) {
184 EXPECT_TRUE(NULL
!= notification_view()->title_view_
);
185 EXPECT_TRUE(NULL
!= notification_view()->message_view_
);
186 EXPECT_TRUE(NULL
!= notification_view()->icon_view_
);
187 EXPECT_TRUE(NULL
!= notification_view()->image_view_
);
189 notification()->set_image(gfx::Image());
190 notification()->set_title(base::ASCIIToUTF16(""));
191 notification()->set_message(base::ASCIIToUTF16(""));
192 notification()->set_icon(gfx::Image());
194 notification_view()->CreateOrUpdateViews(*notification());
195 EXPECT_TRUE(NULL
== notification_view()->title_view_
);
196 EXPECT_TRUE(NULL
== notification_view()->message_view_
);
197 EXPECT_TRUE(NULL
== notification_view()->image_view_
);
198 // We still expect an icon view for all layouts.
199 EXPECT_TRUE(NULL
!= notification_view()->icon_view_
);
202 TEST_F(NotificationViewTest
, TestLineLimits
) {
203 notification()->set_image(CreateTestImage(0, 0));
204 notification()->set_context_message(base::ASCIIToUTF16(""));
205 notification_view()->CreateOrUpdateViews(*notification());
207 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(0, 360));
208 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(1, 360));
209 EXPECT_EQ(3, notification_view()->GetMessageLineLimit(2, 360));
211 notification()->set_image(CreateTestImage(2, 2));
212 notification_view()->CreateOrUpdateViews(*notification());
214 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(0, 360));
215 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360));
216 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360));
218 notification()->set_context_message(base::UTF8ToUTF16("foo"));
219 notification_view()->CreateOrUpdateViews(*notification());
221 EXPECT_TRUE(notification_view()->context_message_view_
!= NULL
);
223 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360));
224 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360));
225 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360));
228 TEST_F(NotificationViewTest
, UpdateButtonsStateTest
) {
229 notification()->set_buttons(CreateButtons(2));
230 notification_view()->CreateOrUpdateViews(*notification());
233 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
234 notification_view()->action_buttons_
[0]->state());
236 // Now construct a mouse move event 1 pixel inside the boundary of the action
238 gfx::Point
cursor_location(1, 1);
239 views::View::ConvertPointToWidget(notification_view()->action_buttons_
[0],
241 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, cursor_location
, cursor_location
,
242 ui::EventTimeForNow(), ui::EF_NONE
, ui::EF_NONE
);
243 widget()->OnMouseEvent(&move
);
245 EXPECT_EQ(views::CustomButton::STATE_HOVERED
,
246 notification_view()->action_buttons_
[0]->state());
248 notification_view()->CreateOrUpdateViews(*notification());
250 EXPECT_EQ(views::CustomButton::STATE_HOVERED
,
251 notification_view()->action_buttons_
[0]->state());
253 // Now construct a mouse move event 1 pixel outside the boundary of the
255 cursor_location
= gfx::Point(-1, -1);
256 move
= ui::MouseEvent(ui::ET_MOUSE_MOVED
, cursor_location
, cursor_location
,
257 ui::EventTimeForNow(), ui::EF_NONE
, ui::EF_NONE
);
258 widget()->OnMouseEvent(&move
);
260 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
261 notification_view()->action_buttons_
[0]->state());
264 TEST_F(NotificationViewTest
, UpdateButtonCountTest
) {
265 notification()->set_buttons(CreateButtons(2));
266 notification_view()->CreateOrUpdateViews(*notification());
269 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
270 notification_view()->action_buttons_
[0]->state());
271 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
272 notification_view()->action_buttons_
[1]->state());
274 // Now construct a mouse move event 1 pixel inside the boundary of the action
276 gfx::Point
cursor_location(1, 1);
277 views::View::ConvertPointToWidget(notification_view()->action_buttons_
[0],
279 ui::MouseEvent
move(ui::ET_MOUSE_MOVED
, cursor_location
, cursor_location
,
280 ui::EventTimeForNow(), ui::EF_NONE
, ui::EF_NONE
);
281 widget()->OnMouseEvent(&move
);
283 EXPECT_EQ(views::CustomButton::STATE_HOVERED
,
284 notification_view()->action_buttons_
[0]->state());
285 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
286 notification_view()->action_buttons_
[1]->state());
288 notification()->set_buttons(CreateButtons(1));
289 notification_view()->CreateOrUpdateViews(*notification());
291 EXPECT_EQ(views::CustomButton::STATE_HOVERED
,
292 notification_view()->action_buttons_
[0]->state());
293 EXPECT_EQ(1u, notification_view()->action_buttons_
.size());
295 // Now construct a mouse move event 1 pixel outside the boundary of the
297 cursor_location
= gfx::Point(-1, -1);
298 move
= ui::MouseEvent(ui::ET_MOUSE_MOVED
, cursor_location
, cursor_location
,
299 ui::EventTimeForNow(), ui::EF_NONE
, ui::EF_NONE
);
300 widget()->OnMouseEvent(&move
);
302 EXPECT_EQ(views::CustomButton::STATE_NORMAL
,
303 notification_view()->action_buttons_
[0]->state());
306 TEST_F(NotificationViewTest
, ViewOrderingTest
) {
307 // Tests that views are created in the correct vertical order.
308 notification()->set_buttons(CreateButtons(2));
310 // Layout the initial views.
311 UpdateNotificationViews();
313 // Double-check that vertical order is correct.
314 CheckVerticalOrderInNotification();
316 // Tests that views remain in that order even after an update.
317 UpdateNotificationViews();
318 CheckVerticalOrderInNotification();
321 } // namespace message_center