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 ~NotificationViewTest() override;
31 void SetUp() override;
32 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 void ClickOnNotification(const std::string& notification_id) override;
41 void RemoveNotification(const std::string& notification_id,
42 bool by_user) override;
43 scoped_ptr<ui::MenuModel> CreateMenuModel(
44 const NotifierId& notifier_id,
45 const base::string16& display_source) override;
46 bool HasClickedListener(const std::string& notification_id) override;
47 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_EQ(views::CustomButton::STATE_NORMAL,
233 notification_view()->action_buttons_[0]->state());
235 // Now construct a mouse move event 1 pixel inside the boundary of the action
237 gfx::Point cursor_location(1, 1);
238 views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
240 ui::MouseEvent move(ui::ET_MOUSE_MOVED,
245 widget()->OnMouseEvent(&move);
247 EXPECT_EQ(views::CustomButton::STATE_HOVERED,
248 notification_view()->action_buttons_[0]->state());
250 notification_view()->CreateOrUpdateViews(*notification());
252 EXPECT_EQ(views::CustomButton::STATE_HOVERED,
253 notification_view()->action_buttons_[0]->state());
255 // Now construct a mouse move event 1 pixel outside the boundary of the
257 cursor_location = gfx::Point(-1, -1);
258 move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
263 widget()->OnMouseEvent(&move);
265 EXPECT_EQ(views::CustomButton::STATE_NORMAL,
266 notification_view()->action_buttons_[0]->state());
269 TEST_F(NotificationViewTest, UpdateButtonCountTest) {
270 notification()->set_buttons(CreateButtons(2));
271 notification_view()->CreateOrUpdateViews(*notification());
274 EXPECT_EQ(views::CustomButton::STATE_NORMAL,
275 notification_view()->action_buttons_[0]->state());
276 EXPECT_EQ(views::CustomButton::STATE_NORMAL,
277 notification_view()->action_buttons_[1]->state());
279 // Now construct a mouse move event 1 pixel inside the boundary of the action
281 gfx::Point cursor_location(1, 1);
282 views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
284 ui::MouseEvent move(ui::ET_MOUSE_MOVED,
289 widget()->OnMouseEvent(&move);
291 EXPECT_EQ(views::CustomButton::STATE_HOVERED,
292 notification_view()->action_buttons_[0]->state());
293 EXPECT_EQ(views::CustomButton::STATE_NORMAL,
294 notification_view()->action_buttons_[1]->state());
296 notification()->set_buttons(CreateButtons(1));
297 notification_view()->CreateOrUpdateViews(*notification());
299 EXPECT_EQ(views::CustomButton::STATE_HOVERED,
300 notification_view()->action_buttons_[0]->state());
301 EXPECT_EQ(1u, notification_view()->action_buttons_.size());
303 // Now construct a mouse move event 1 pixel outside the boundary of the
305 cursor_location = gfx::Point(-1, -1);
306 move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
311 widget()->OnMouseEvent(&move);
313 EXPECT_EQ(views::CustomButton::STATE_NORMAL,
314 notification_view()->action_buttons_[0]->state());
317 TEST_F(NotificationViewTest, ViewOrderingTest) {
318 // Tests that views are created in the correct vertical order.
319 notification()->set_buttons(CreateButtons(2));
321 // Layout the initial views.
322 UpdateNotificationViews();
324 // Double-check that vertical order is correct.
325 CheckVerticalOrderInNotification();
327 // Tests that views remain in that order even after an update.
328 UpdateNotificationViews();
329 CheckVerticalOrderInNotification();
332 } // namespace message_center