Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / ui / message_center / views / notification_view_unittest.cc
blob596e9ae792829e27cb78fddbb808464bcc9b7e70
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 {
27 public:
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;
50 protected:
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) {
56 SkBitmap bitmap;
57 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
58 bitmap.allocPixels();
59 bitmap.eraseRGB(0, 255, 0);
60 return bitmap;
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 private:
70 scoped_ptr<RichNotificationData> data_;
71 scoped_ptr<Notification> notification_;
72 scoped_ptr<NotificationView> notification_view_;
74 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest);
77 NotificationViewTest::NotificationViewTest() {
80 NotificationViewTest::~NotificationViewTest() {
83 void NotificationViewTest::SetUp() {
84 views::ViewsTestBase::SetUp();
85 // Create a dummy notification.
86 SkBitmap bitmap;
87 data_.reset(new RichNotificationData());
88 notification_.reset(
89 new Notification(NOTIFICATION_TYPE_BASE_FORMAT,
90 std::string("notification id"),
91 base::UTF8ToUTF16("title"),
92 base::UTF8ToUTF16("message"),
93 CreateTestImage(80, 80),
94 base::UTF8ToUTF16("display source"),
95 NotifierId(NotifierId::APPLICATION, "extension_id"),
96 *data_,
97 NULL));
98 notification_->set_small_image(CreateTestImage(16, 16));
99 notification_->set_image(CreateTestImage(320, 240));
101 // Then create a new NotificationView with that single notification.
102 notification_view_.reset(
103 NotificationView::Create(this, *notification_, true));
104 notification_view_->set_owned_by_client();
106 views::Widget::InitParams init_params(
107 CreateParams(views::Widget::InitParams::TYPE_POPUP));
108 views::Widget* widget = new views::Widget();
109 widget->Init(init_params);
110 widget->SetContentsView(notification_view_.get());
111 widget->SetSize(notification_view_->GetPreferredSize());
114 void NotificationViewTest::TearDown() {
115 widget()->Close();
116 notification_view_.reset();
117 views::ViewsTestBase::TearDown();
120 void NotificationViewTest::ClickOnNotification(
121 const std::string& notification_id) {
122 // For this test, this method should not be invoked.
123 NOTREACHED();
126 void NotificationViewTest::RemoveNotification(
127 const std::string& notification_id,
128 bool by_user) {
129 // For this test, this method should not be invoked.
130 NOTREACHED();
133 scoped_ptr<ui::MenuModel> NotificationViewTest::CreateMenuModel(
134 const NotifierId& notifier_id,
135 const base::string16& display_source) {
136 // For this test, this method should not be invoked.
137 NOTREACHED();
138 return scoped_ptr<ui::MenuModel>();
141 bool NotificationViewTest::HasClickedListener(
142 const std::string& notification_id) {
143 return true;
146 void NotificationViewTest::ClickOnNotificationButton(
147 const std::string& notification_id,
148 int button_index) {
149 // For this test, this method should not be invoked.
150 NOTREACHED();
153 /* Unit tests *****************************************************************/
155 TEST_F(NotificationViewTest, CreateOrUpdateTest) {
156 EXPECT_TRUE(NULL != notification_view()->title_view_);
157 EXPECT_TRUE(NULL != notification_view()->message_view_);
158 EXPECT_TRUE(NULL != notification_view()->icon_view_);
159 EXPECT_TRUE(NULL != notification_view()->image_view_);
161 notification()->set_image(gfx::Image());
162 notification()->set_title(base::ASCIIToUTF16(""));
163 notification()->set_message(base::ASCIIToUTF16(""));
164 notification()->set_icon(gfx::Image());
166 notification_view()->CreateOrUpdateViews(*notification());
167 EXPECT_TRUE(NULL == notification_view()->title_view_);
168 EXPECT_TRUE(NULL == notification_view()->message_view_);
169 EXPECT_TRUE(NULL == notification_view()->image_view_);
170 // We still expect an icon view for all layouts.
171 EXPECT_TRUE(NULL != notification_view()->icon_view_);
174 TEST_F(NotificationViewTest, TestLineLimits) {
175 notification()->set_image(CreateTestImage(0, 0));
176 notification()->set_context_message(base::ASCIIToUTF16(""));
177 notification_view()->CreateOrUpdateViews(*notification());
179 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(0, 360));
180 EXPECT_EQ(5, notification_view()->GetMessageLineLimit(1, 360));
181 EXPECT_EQ(3, notification_view()->GetMessageLineLimit(2, 360));
183 notification()->set_image(CreateTestImage(2, 2));
184 notification_view()->CreateOrUpdateViews(*notification());
186 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(0, 360));
187 EXPECT_EQ(2, notification_view()->GetMessageLineLimit(1, 360));
188 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(2, 360));
190 notification()->set_context_message(base::UTF8ToUTF16("foo"));
191 notification_view()->CreateOrUpdateViews(*notification());
193 EXPECT_TRUE(notification_view()->context_message_view_ != NULL);
195 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360));
196 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360));
197 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360));
200 TEST_F(NotificationViewTest, UpdateButtonsStateTest) {
201 notification()->set_buttons(CreateButtons(2));
202 notification_view()->CreateOrUpdateViews(*notification());
203 widget()->Show();
205 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background());
207 // Now construct a mouse move event 1 pixel inside the boundary of the action
208 // button.
209 gfx::Point cursor_location(1, 1);
210 views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
211 &cursor_location);
212 ui::MouseEvent move(ui::ET_MOUSE_MOVED,
213 cursor_location,
214 cursor_location,
215 ui::EF_NONE,
216 ui::EF_NONE);
217 widget()->OnMouseEvent(&move);
219 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background());
221 notification_view()->CreateOrUpdateViews(*notification());
223 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background());
225 // Now construct a mouse move event 1 pixel outside the boundary of the
226 // widget.
227 cursor_location = gfx::Point(-1, -1);
228 move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
229 cursor_location,
230 cursor_location,
231 ui::EF_NONE,
232 ui::EF_NONE);
233 widget()->OnMouseEvent(&move);
235 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background());
238 TEST_F(NotificationViewTest, UpdateButtonCountTest) {
239 notification()->set_buttons(CreateButtons(2));
240 notification_view()->CreateOrUpdateViews(*notification());
241 widget()->Show();
243 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background());
244 EXPECT_TRUE(NULL == notification_view()->action_buttons_[1]->background());
246 // Now construct a mouse move event 1 pixel inside the boundary of the action
247 // button.
248 gfx::Point cursor_location(1, 1);
249 views::View::ConvertPointToWidget(notification_view()->action_buttons_[0],
250 &cursor_location);
251 ui::MouseEvent move(ui::ET_MOUSE_MOVED,
252 cursor_location,
253 cursor_location,
254 ui::EF_NONE,
255 ui::EF_NONE);
256 widget()->OnMouseEvent(&move);
258 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background());
259 EXPECT_TRUE(NULL == notification_view()->action_buttons_[1]->background());
261 notification()->set_buttons(CreateButtons(1));
262 notification_view()->CreateOrUpdateViews(*notification());
264 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background());
265 EXPECT_EQ(1u, notification_view()->action_buttons_.size());
267 // Now construct a mouse move event 1 pixel outside the boundary of the
268 // widget.
269 cursor_location = gfx::Point(-1, -1);
270 move = ui::MouseEvent(ui::ET_MOUSE_MOVED,
271 cursor_location,
272 cursor_location,
273 ui::EF_NONE,
274 ui::EF_NONE);
275 widget()->OnMouseEvent(&move);
277 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background());
280 } // namespace message_center