| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/message_center/views/notification_view.h" | 5 #include "ui/message_center/views/notification_view.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "ui/gfx/image/image.h" | 11 #include "ui/gfx/image/image.h" |
| 12 #include "ui/message_center/notification.h" | 12 #include "ui/message_center/notification.h" |
| 13 #include "ui/message_center/notification_list.h" | 13 #include "ui/message_center/notification_list.h" |
| 14 #include "ui/message_center/notification_types.h" | 14 #include "ui/message_center/notification_types.h" |
| 15 #include "ui/message_center/views/message_center_controller.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" |
| 16 | 20 |
| 17 namespace message_center { | 21 namespace message_center { |
| 18 | 22 |
| 19 /* Test fixture ***************************************************************/ | 23 /* Test fixture ***************************************************************/ |
| 20 | 24 |
| 21 class NotificationViewTest : public testing::Test, | 25 class NotificationViewTest : public views::ViewsTestBase, |
| 22 public MessageCenterController { | 26 public MessageCenterController { |
| 23 public: | 27 public: |
| 24 NotificationViewTest(); | 28 NotificationViewTest(); |
| 25 virtual ~NotificationViewTest(); | 29 virtual ~NotificationViewTest(); |
| 26 | 30 |
| 27 virtual void SetUp() OVERRIDE; | 31 virtual void SetUp() OVERRIDE; |
| 28 virtual void TearDown() OVERRIDE; | 32 virtual void TearDown() OVERRIDE; |
| 29 | 33 |
| 34 views::Widget* widget() { return notification_view_->GetWidget(); } |
| 30 NotificationView* notification_view() { return notification_view_.get(); } | 35 NotificationView* notification_view() { return notification_view_.get(); } |
| 31 Notification* notification() { return notification_.get(); } | 36 Notification* notification() { return notification_.get(); } |
| 32 RichNotificationData* data() { return data_.get(); } | 37 RichNotificationData* data() { return data_.get(); } |
| 33 | 38 |
| 34 // Overridden from MessageCenterController: | 39 // Overridden from MessageCenterController: |
| 35 virtual void ClickOnNotification(const std::string& notification_id) OVERRIDE; | 40 virtual void ClickOnNotification(const std::string& notification_id) OVERRIDE; |
| 36 virtual void RemoveNotification(const std::string& notification_id, | 41 virtual void RemoveNotification(const std::string& notification_id, |
| 37 bool by_user) OVERRIDE; | 42 bool by_user) OVERRIDE; |
| 38 virtual scoped_ptr<ui::MenuModel> CreateMenuModel( | 43 virtual scoped_ptr<ui::MenuModel> CreateMenuModel( |
| 39 const NotifierId& notifier_id, | 44 const NotifierId& notifier_id, |
| 40 const base::string16& display_source) OVERRIDE; | 45 const base::string16& display_source) OVERRIDE; |
| 41 virtual bool HasClickedListener(const std::string& notification_id) OVERRIDE; | 46 virtual bool HasClickedListener(const std::string& notification_id) OVERRIDE; |
| 42 virtual void ClickOnNotificationButton(const std::string& notification_id, | 47 virtual void ClickOnNotificationButton(const std::string& notification_id, |
| 43 int button_index) OVERRIDE; | 48 int button_index) OVERRIDE; |
| 44 | 49 |
| 45 protected: | 50 protected: |
| 46 const gfx::Image CreateTestImage(int width, int height) { | 51 const gfx::Image CreateTestImage(int width, int height) { |
| 47 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height)); | 52 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(width, height)); |
| 48 } | 53 } |
| 49 | 54 |
| 50 const SkBitmap CreateBitmap(int width, int height) { | 55 const SkBitmap CreateBitmap(int width, int height) { |
| 51 SkBitmap bitmap; | 56 SkBitmap bitmap; |
| 52 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 57 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 53 bitmap.allocPixels(); | 58 bitmap.allocPixels(); |
| 54 bitmap.eraseRGB(0, 255, 0); | 59 bitmap.eraseRGB(0, 255, 0); |
| 55 return bitmap; | 60 return bitmap; |
| 56 } | 61 } |
| 57 | 62 |
| 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); |
| 67 } |
| 68 |
| 58 private: | 69 private: |
| 59 scoped_ptr<RichNotificationData> data_; | 70 scoped_ptr<RichNotificationData> data_; |
| 60 scoped_ptr<Notification> notification_; | 71 scoped_ptr<Notification> notification_; |
| 61 scoped_ptr<NotificationView> notification_view_; | 72 scoped_ptr<NotificationView> notification_view_; |
| 62 | 73 |
| 63 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest); | 74 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest); |
| 64 }; | 75 }; |
| 65 | 76 |
| 66 NotificationViewTest::NotificationViewTest() { | 77 NotificationViewTest::NotificationViewTest() { |
| 67 } | 78 } |
| 68 | 79 |
| 69 NotificationViewTest::~NotificationViewTest() { | 80 NotificationViewTest::~NotificationViewTest() { |
| 70 } | 81 } |
| 71 | 82 |
| 72 void NotificationViewTest::SetUp() { | 83 void NotificationViewTest::SetUp() { |
| 84 views::ViewsTestBase::SetUp(); |
| 73 // Create a dummy notification. | 85 // Create a dummy notification. |
| 74 SkBitmap bitmap; | 86 SkBitmap bitmap; |
| 75 data_.reset(new RichNotificationData()); | 87 data_.reset(new RichNotificationData()); |
| 76 notification_.reset( | 88 notification_.reset( |
| 77 new Notification(NOTIFICATION_TYPE_BASE_FORMAT, | 89 new Notification(NOTIFICATION_TYPE_BASE_FORMAT, |
| 78 std::string("notification id"), | 90 std::string("notification id"), |
| 79 base::UTF8ToUTF16("title"), | 91 base::UTF8ToUTF16("title"), |
| 80 base::UTF8ToUTF16("message"), | 92 base::UTF8ToUTF16("message"), |
| 81 CreateTestImage(80, 80), | 93 CreateTestImage(80, 80), |
| 82 base::UTF8ToUTF16("display source"), | 94 base::UTF8ToUTF16("display source"), |
| 83 NotifierId(NotifierId::APPLICATION, "extension_id"), | 95 NotifierId(NotifierId::APPLICATION, "extension_id"), |
| 84 *data_, | 96 *data_, |
| 85 NULL)); | 97 NULL)); |
| 86 notification_->set_small_image(CreateTestImage(16, 16)); | 98 notification_->set_small_image(CreateTestImage(16, 16)); |
| 87 notification_->set_image(CreateTestImage(320, 240)); | 99 notification_->set_image(CreateTestImage(320, 240)); |
| 88 | 100 |
| 89 // Then create a new NotificationView with that single notification. | 101 // Then create a new NotificationView with that single notification. |
| 90 notification_view_.reset( | 102 notification_view_.reset( |
| 91 NotificationView::Create(this, *notification_, true)); | 103 NotificationView::Create(this, *notification_, true)); |
| 104 notification_view_->set_owned_by_client(); |
| 105 |
| 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()); |
| 92 } | 112 } |
| 93 | 113 |
| 94 void NotificationViewTest::TearDown() { | 114 void NotificationViewTest::TearDown() { |
| 115 widget()->Close(); |
| 95 notification_view_.reset(); | 116 notification_view_.reset(); |
| 117 views::ViewsTestBase::TearDown(); |
| 96 } | 118 } |
| 97 | 119 |
| 98 void NotificationViewTest::ClickOnNotification( | 120 void NotificationViewTest::ClickOnNotification( |
| 99 const std::string& notification_id) { | 121 const std::string& notification_id) { |
| 100 // For this test, this method should not be invoked. | 122 // For this test, this method should not be invoked. |
| 101 NOTREACHED(); | 123 NOTREACHED(); |
| 102 } | 124 } |
| 103 | 125 |
| 104 void NotificationViewTest::RemoveNotification( | 126 void NotificationViewTest::RemoveNotification( |
| 105 const std::string& notification_id, | 127 const std::string& notification_id, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 notification()->set_context_message(base::UTF8ToUTF16("foo")); | 190 notification()->set_context_message(base::UTF8ToUTF16("foo")); |
| 169 notification_view()->CreateOrUpdateViews(*notification()); | 191 notification_view()->CreateOrUpdateViews(*notification()); |
| 170 | 192 |
| 171 EXPECT_TRUE(notification_view()->context_message_view_ != NULL); | 193 EXPECT_TRUE(notification_view()->context_message_view_ != NULL); |
| 172 | 194 |
| 173 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360)); | 195 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(0, 360)); |
| 174 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360)); | 196 EXPECT_EQ(1, notification_view()->GetMessageLineLimit(1, 360)); |
| 175 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360)); | 197 EXPECT_EQ(0, notification_view()->GetMessageLineLimit(2, 360)); |
| 176 } | 198 } |
| 177 | 199 |
| 200 TEST_F(NotificationViewTest, UpdateButtonsStateTest) { |
| 201 notification()->set_buttons(CreateButtons(2)); |
| 202 notification_view()->CreateOrUpdateViews(*notification()); |
| 203 widget()->Show(); |
| 204 |
| 205 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background()); |
| 206 |
| 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); |
| 218 |
| 219 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background()); |
| 220 |
| 221 notification_view()->CreateOrUpdateViews(*notification()); |
| 222 |
| 223 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background()); |
| 224 |
| 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); |
| 234 |
| 235 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background()); |
| 236 } |
| 237 |
| 238 TEST_F(NotificationViewTest, UpdateButtonCountTest) { |
| 239 notification()->set_buttons(CreateButtons(2)); |
| 240 notification_view()->CreateOrUpdateViews(*notification()); |
| 241 widget()->Show(); |
| 242 |
| 243 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background()); |
| 244 EXPECT_TRUE(NULL == notification_view()->action_buttons_[1]->background()); |
| 245 |
| 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); |
| 257 |
| 258 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background()); |
| 259 EXPECT_TRUE(NULL == notification_view()->action_buttons_[1]->background()); |
| 260 |
| 261 notification()->set_buttons(CreateButtons(1)); |
| 262 notification_view()->CreateOrUpdateViews(*notification()); |
| 263 |
| 264 EXPECT_TRUE(NULL != notification_view()->action_buttons_[0]->background()); |
| 265 EXPECT_EQ(1u, notification_view()->action_buttons_.size()); |
| 266 |
| 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); |
| 276 |
| 277 EXPECT_TRUE(NULL == notification_view()->action_buttons_[0]->background()); |
| 278 } |
| 279 |
| 178 } // namespace message_center | 280 } // namespace message_center |
| OLD | NEW |