| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include <memory> |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "third_party/skia/include/core/SkColor.h" |
| 12 #include "ui/events/event.h" |
| 13 #include "ui/events/event_utils.h" |
| 14 #include "ui/message_center/notification.h" |
| 15 #include "ui/message_center/notification_delegate.h" |
| 16 #include "ui/message_center/views/custom_notification_view.h" |
| 17 #include "ui/message_center/views/message_center_controller.h" |
| 18 #include "ui/message_center/views/message_view_factory.h" |
| 19 #include "ui/views/background.h" |
| 20 #include "ui/views/controls/button/image_button.h" |
| 21 #include "ui/views/test/views_test_base.h" |
| 22 |
| 23 namespace message_center { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const SkColor kBackgroundColor = SK_ColorGREEN; |
| 28 |
| 29 class TestCustomView : public views::View { |
| 30 public: |
| 31 TestCustomView() { |
| 32 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 33 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); |
| 34 } |
| 35 ~TestCustomView() override {} |
| 36 |
| 37 void Reset() { |
| 38 mouse_event_count_ = 0; |
| 39 keyboard_event_count_ = 0; |
| 40 } |
| 41 |
| 42 // views::View |
| 43 gfx::Size GetPreferredSize() const override { return gfx::Size(100, 100); } |
| 44 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 45 ++mouse_event_count_; |
| 46 return true; |
| 47 } |
| 48 void OnMouseMoved(const ui::MouseEvent& event) override { |
| 49 ++mouse_event_count_; |
| 50 } |
| 51 void OnMouseReleased(const ui::MouseEvent& event) override { |
| 52 ++mouse_event_count_; |
| 53 } |
| 54 bool OnKeyPressed(const ui::KeyEvent& event) override { |
| 55 ++keyboard_event_count_; |
| 56 return true; |
| 57 } |
| 58 |
| 59 int mouse_event_count() const { return mouse_event_count_; } |
| 60 int keyboard_event_count() const { return keyboard_event_count_; } |
| 61 |
| 62 private: |
| 63 int mouse_event_count_ = 0; |
| 64 int keyboard_event_count_ = 0; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(TestCustomView); |
| 67 }; |
| 68 |
| 69 class TestNotificationDelegate : public NotificationDelegate { |
| 70 public: |
| 71 TestNotificationDelegate() {} |
| 72 |
| 73 // NotificateDelegate |
| 74 std::unique_ptr<views::View> CreateCustomContent() override { |
| 75 return base::WrapUnique(new TestCustomView); |
| 76 } |
| 77 |
| 78 private: |
| 79 ~TestNotificationDelegate() override {} |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate); |
| 82 }; |
| 83 |
| 84 class TestMessageCenterController : public MessageCenterController { |
| 85 public: |
| 86 TestMessageCenterController() {} |
| 87 |
| 88 // MessageCenterController |
| 89 void ClickOnNotification(const std::string& notification_id) override { |
| 90 // For this test, this method should not be invoked. |
| 91 NOTREACHED(); |
| 92 } |
| 93 |
| 94 void RemoveNotification(const std::string& notification_id, |
| 95 bool by_user) override { |
| 96 removed_ids_.insert(notification_id); |
| 97 } |
| 98 |
| 99 std::unique_ptr<ui::MenuModel> CreateMenuModel( |
| 100 const NotifierId& notifier_id, |
| 101 const base::string16& display_source) override { |
| 102 // For this test, this method should not be invoked. |
| 103 NOTREACHED(); |
| 104 return nullptr; |
| 105 } |
| 106 |
| 107 bool HasClickedListener(const std::string& notification_id) override { |
| 108 return false; |
| 109 } |
| 110 |
| 111 void ClickOnNotificationButton(const std::string& notification_id, |
| 112 int button_index) override { |
| 113 // For this test, this method should not be invoked. |
| 114 NOTREACHED(); |
| 115 } |
| 116 |
| 117 void ClickOnSettingsButton(const std::string& notification_id) override { |
| 118 // For this test, this method should not be invoked. |
| 119 NOTREACHED(); |
| 120 } |
| 121 |
| 122 bool IsRemoved(const std::string& notification_id) const { |
| 123 return (removed_ids_.find(notification_id) != removed_ids_.end()); |
| 124 } |
| 125 |
| 126 private: |
| 127 std::set<std::string> removed_ids_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController); |
| 130 }; |
| 131 |
| 132 } // namespace |
| 133 |
| 134 class CustomNotificationViewTest : public views::ViewsTestBase { |
| 135 public: |
| 136 CustomNotificationViewTest() {} |
| 137 ~CustomNotificationViewTest() override {} |
| 138 |
| 139 // views::ViewsTestBase |
| 140 void SetUp() override { |
| 141 views::ViewsTestBase::SetUp(); |
| 142 |
| 143 notification_delegate_ = new TestNotificationDelegate; |
| 144 |
| 145 notification_.reset(new Notification( |
| 146 NOTIFICATION_TYPE_CUSTOM, std::string("notification id"), |
| 147 base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(), |
| 148 base::UTF8ToUTF16("display source"), GURL(), |
| 149 NotifierId(NotifierId::APPLICATION, "extension_id"), |
| 150 message_center::RichNotificationData(), notification_delegate_.get())); |
| 151 |
| 152 notification_view_.reset(static_cast<CustomNotificationView*>( |
| 153 MessageViewFactory::Create(controller(), *notification_, true))); |
| 154 notification_view_->set_owned_by_client(); |
| 155 |
| 156 views::Widget::InitParams init_params( |
| 157 CreateParams(views::Widget::InitParams::TYPE_POPUP)); |
| 158 views::Widget* widget = new views::Widget(); |
| 159 widget->Init(init_params); |
| 160 widget->SetContentsView(notification_view_.get()); |
| 161 widget->SetSize(notification_view_->GetPreferredSize()); |
| 162 } |
| 163 |
| 164 void TearDown() override { |
| 165 widget()->Close(); |
| 166 notification_view_.reset(); |
| 167 views::ViewsTestBase::TearDown(); |
| 168 } |
| 169 |
| 170 SkColor GetBackgroundColor() const { |
| 171 return notification_view_->background_view()->background()->get_color(); |
| 172 } |
| 173 |
| 174 void PerformClick(const gfx::Point& point) { |
| 175 ui::MouseEvent pressed_event = ui::MouseEvent( |
| 176 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(), |
| 177 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 178 widget()->OnMouseEvent(&pressed_event); |
| 179 ui::MouseEvent released_event = ui::MouseEvent( |
| 180 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(), |
| 181 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 182 widget()->OnMouseEvent(&released_event); |
| 183 } |
| 184 |
| 185 void KeyPress(ui::KeyboardCode key_code) { |
| 186 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE); |
| 187 widget()->OnKeyEvent(&event); |
| 188 } |
| 189 |
| 190 views::ImageButton* close_button() { |
| 191 return notification_view_->close_button(); |
| 192 } |
| 193 TestMessageCenterController* controller() { return &controller_; } |
| 194 Notification* notification() { return notification_.get(); } |
| 195 TestCustomView* custom_view() { |
| 196 return static_cast<TestCustomView*>(notification_view_->contents_view_); |
| 197 } |
| 198 views::Widget* widget() { return notification_view_->GetWidget(); } |
| 199 |
| 200 private: |
| 201 TestMessageCenterController controller_; |
| 202 scoped_refptr<TestNotificationDelegate> notification_delegate_; |
| 203 std::unique_ptr<Notification> notification_; |
| 204 std::unique_ptr<CustomNotificationView> notification_view_; |
| 205 |
| 206 DISALLOW_COPY_AND_ASSIGN(CustomNotificationViewTest); |
| 207 }; |
| 208 |
| 209 TEST_F(CustomNotificationViewTest, Background) { |
| 210 EXPECT_EQ(kBackgroundColor, GetBackgroundColor()); |
| 211 } |
| 212 |
| 213 TEST_F(CustomNotificationViewTest, ClickCloseButton) { |
| 214 widget()->Show(); |
| 215 |
| 216 gfx::Point cursor_location(1, 1); |
| 217 views::View::ConvertPointToWidget(close_button(), &cursor_location); |
| 218 PerformClick(cursor_location); |
| 219 EXPECT_TRUE(controller()->IsRemoved(notification()->id())); |
| 220 } |
| 221 |
| 222 TEST_F(CustomNotificationViewTest, Events) { |
| 223 widget()->Show(); |
| 224 custom_view()->RequestFocus(); |
| 225 |
| 226 EXPECT_EQ(0, custom_view()->mouse_event_count()); |
| 227 gfx::Point cursor_location(1, 1); |
| 228 views::View::ConvertPointToWidget(custom_view(), &cursor_location); |
| 229 PerformClick(cursor_location); |
| 230 EXPECT_EQ(2, custom_view()->mouse_event_count()); |
| 231 |
| 232 ui::MouseEvent move(ui::ET_MOUSE_MOVED, cursor_location, cursor_location, |
| 233 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE); |
| 234 widget()->OnMouseEvent(&move); |
| 235 EXPECT_EQ(3, custom_view()->mouse_event_count()); |
| 236 |
| 237 EXPECT_EQ(0, custom_view()->keyboard_event_count()); |
| 238 KeyPress(ui::VKEY_A); |
| 239 EXPECT_EQ(1, custom_view()->keyboard_event_count()); |
| 240 } |
| 241 |
| 242 } // namespace message_center |
| OLD | NEW |