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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "third_party/skia/include/core/SkCanvas.h" | 14 #include "third_party/skia/include/core/SkCanvas.h" |
15 #include "third_party/skia/include/core/SkColor.h" | 15 #include "third_party/skia/include/core/SkColor.h" |
16 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 16 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
17 #include "ui/events/event_processor.h" | 17 #include "ui/events/event_processor.h" |
18 #include "ui/events/event_utils.h" | 18 #include "ui/events/event_utils.h" |
| 19 #include "ui/events/test/event_generator.h" |
19 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/geometry/rect.h" | 21 #include "ui/gfx/geometry/rect.h" |
21 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
22 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
23 #include "ui/message_center/message_center_style.h" | 24 #include "ui/message_center/message_center_style.h" |
24 #include "ui/message_center/notification.h" | 25 #include "ui/message_center/notification.h" |
25 #include "ui/message_center/notification_list.h" | 26 #include "ui/message_center/notification_list.h" |
26 #include "ui/message_center/notification_types.h" | 27 #include "ui/message_center/notification_types.h" |
27 #include "ui/message_center/views/constants.h" | 28 #include "ui/message_center/views/constants.h" |
28 #include "ui/message_center/views/message_center_controller.h" | 29 #include "ui/message_center/views/message_center_controller.h" |
29 #include "ui/message_center/views/message_view_factory.h" | 30 #include "ui/message_center/views/message_view_factory.h" |
30 #include "ui/message_center/views/notification_button.h" | 31 #include "ui/message_center/views/notification_button.h" |
31 #include "ui/message_center/views/proportional_image_view.h" | 32 #include "ui/message_center/views/proportional_image_view.h" |
32 #include "ui/views/controls/button/image_button.h" | 33 #include "ui/views/controls/button/image_button.h" |
33 #include "ui/views/layout/fill_layout.h" | 34 #include "ui/views/layout/fill_layout.h" |
34 #include "ui/views/test/views_test_base.h" | 35 #include "ui/views/test/views_test_base.h" |
35 #include "ui/views/test/widget_test.h" | 36 #include "ui/views/test/widget_test.h" |
36 #include "ui/views/widget/widget_delegate.h" | 37 #include "ui/views/widget/widget_delegate.h" |
37 | 38 |
38 namespace { | |
39 | |
40 std::unique_ptr<ui::GestureEvent> GenerateGestureEvent(ui::EventType type) { | |
41 ui::GestureEventDetails detail(type); | |
42 std::unique_ptr<ui::GestureEvent> event( | |
43 new ui::GestureEvent(0, 0, 0, base::TimeTicks(), detail)); | |
44 return event; | |
45 } | |
46 | |
47 std::unique_ptr<ui::GestureEvent> GenerateGestureVerticalScrollUpdateEvent( | |
48 int dx) { | |
49 ui::GestureEventDetails detail(ui::ET_GESTURE_SCROLL_UPDATE, dx, 0); | |
50 std::unique_ptr<ui::GestureEvent> event( | |
51 new ui::GestureEvent(0, 0, 0, base::TimeTicks(), detail)); | |
52 return event; | |
53 } | |
54 | |
55 } // anonymouse namespace | |
56 | |
57 namespace message_center { | 39 namespace message_center { |
58 | 40 |
59 // A test delegate used for tests that deal with the notification settings | 41 // A test delegate used for tests that deal with the notification settings |
60 // button. | 42 // button. |
61 class NotificationSettingsDelegate : public NotificationDelegate { | 43 class NotificationSettingsDelegate : public NotificationDelegate { |
62 bool ShouldDisplaySettingsButton() override { return true; } | 44 bool ShouldDisplaySettingsButton() override { return true; } |
63 | 45 |
64 private: | 46 private: |
65 ~NotificationSettingsDelegate() override {} | 47 ~NotificationSettingsDelegate() override {} |
66 }; | 48 }; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 161 } |
180 | 162 |
181 views::ImageButton* GetCloseButton() { | 163 views::ImageButton* GetCloseButton() { |
182 return notification_view()->close_button(); | 164 return notification_view()->close_button(); |
183 } | 165 } |
184 | 166 |
185 void UpdateNotificationViews() { | 167 void UpdateNotificationViews() { |
186 notification_view()->UpdateWithNotification(*notification()); | 168 notification_view()->UpdateWithNotification(*notification()); |
187 } | 169 } |
188 | 170 |
189 float GetNotificationScrollAmount() const { | 171 float GetNotificationSlideAmount() const { |
190 return notification_view()->GetTransform().To2dTranslation().x(); | 172 return notification_view_->GetSlideOutLayer() |
| 173 ->transform() |
| 174 .To2dTranslation() |
| 175 .x(); |
191 } | 176 } |
192 | 177 |
193 bool IsRemoved(const std::string& notification_id) const { | 178 bool IsRemoved(const std::string& notification_id) const { |
194 return (removed_ids_.find(notification_id) != removed_ids_.end()); | 179 return (removed_ids_.find(notification_id) != removed_ids_.end()); |
195 } | 180 } |
196 | 181 |
197 void RemoveNotificationView() { notification_view_.reset(); } | 182 void RemoveNotificationView() { notification_view_.reset(); } |
198 | 183 |
| 184 void DispatchGesture(const ui::GestureEventDetails& details) { |
| 185 ui::test::EventGenerator generator( |
| 186 notification_view()->GetWidget()->GetNativeWindow()); |
| 187 gfx::Point center = notification_view()->GetBoundsInScreen().CenterPoint(); |
| 188 ui::GestureEvent event(center.x(), center.y(), 0, ui::EventTimeForNow(), |
| 189 details); |
| 190 generator.Dispatch(&event); |
| 191 } |
| 192 |
| 193 void BeginScroll() { |
| 194 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN)); |
| 195 } |
| 196 |
| 197 void EndScroll() { |
| 198 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END)); |
| 199 } |
| 200 |
| 201 void ScrollBy(int dx) { |
| 202 DispatchGesture( |
| 203 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, dx, 0)); |
| 204 } |
| 205 |
199 private: | 206 private: |
200 std::set<std::string> removed_ids_; | 207 std::set<std::string> removed_ids_; |
201 | 208 |
202 std::unique_ptr<RichNotificationData> data_; | 209 std::unique_ptr<RichNotificationData> data_; |
203 std::unique_ptr<Notification> notification_; | 210 std::unique_ptr<Notification> notification_; |
204 std::unique_ptr<NotificationView> notification_view_; | 211 std::unique_ptr<NotificationView> notification_view_; |
205 | 212 |
206 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest); | 213 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest); |
207 }; | 214 }; |
208 | 215 |
(...skipping 21 matching lines...) Expand all Loading... |
230 notification_view_.reset(static_cast<NotificationView*>( | 237 notification_view_.reset(static_cast<NotificationView*>( |
231 MessageViewFactory::Create(this, *notification_, true))); | 238 MessageViewFactory::Create(this, *notification_, true))); |
232 notification_view_->set_owned_by_client(); | 239 notification_view_->set_owned_by_client(); |
233 | 240 |
234 views::Widget::InitParams init_params( | 241 views::Widget::InitParams init_params( |
235 CreateParams(views::Widget::InitParams::TYPE_POPUP)); | 242 CreateParams(views::Widget::InitParams::TYPE_POPUP)); |
236 views::Widget* widget = new views::Widget(); | 243 views::Widget* widget = new views::Widget(); |
237 widget->Init(init_params); | 244 widget->Init(init_params); |
238 widget->SetContentsView(notification_view_.get()); | 245 widget->SetContentsView(notification_view_.get()); |
239 widget->SetSize(notification_view_->GetPreferredSize()); | 246 widget->SetSize(notification_view_->GetPreferredSize()); |
| 247 widget->Show(); |
240 } | 248 } |
241 | 249 |
242 void NotificationViewTest::TearDown() { | 250 void NotificationViewTest::TearDown() { |
243 widget()->Close(); | 251 widget()->Close(); |
244 notification_view_.reset(); | 252 notification_view_.reset(); |
245 views::ViewsTestBase::TearDown(); | 253 views::ViewsTestBase::TearDown(); |
246 } | 254 } |
247 | 255 |
248 void NotificationViewTest::ClickOnNotification( | 256 void NotificationViewTest::ClickOnNotification( |
249 const std::string& notification_id) { | 257 const std::string& notification_id) { |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 EXPECT_TRUE(result_utf8.find("hello") == std::string::npos); | 660 EXPECT_TRUE(result_utf8.find("hello") == std::string::npos); |
653 } | 661 } |
654 | 662 |
655 TEST_F(NotificationViewTest, SlideOut) { | 663 TEST_F(NotificationViewTest, SlideOut) { |
656 ui::ScopedAnimationDurationScaleMode zero_duration_scope( | 664 ui::ScopedAnimationDurationScaleMode zero_duration_scope( |
657 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | 665 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); |
658 | 666 |
659 UpdateNotificationViews(); | 667 UpdateNotificationViews(); |
660 std::string notification_id = notification()->id(); | 668 std::string notification_id = notification()->id(); |
661 | 669 |
662 auto event_begin = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN); | 670 BeginScroll(); |
663 auto event_scroll10 = GenerateGestureVerticalScrollUpdateEvent(-10); | 671 ScrollBy(-10); |
664 auto event_scroll500 = GenerateGestureVerticalScrollUpdateEvent(-500); | 672 EXPECT_FALSE(IsRemoved(notification_id)); |
665 auto event_end = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_END); | 673 EXPECT_EQ(-10.f, GetNotificationSlideAmount()); |
| 674 EndScroll(); |
| 675 EXPECT_FALSE(IsRemoved(notification_id)); |
| 676 EXPECT_EQ(0.f, GetNotificationSlideAmount()); |
666 | 677 |
667 notification_view()->OnGestureEvent(event_begin.get()); | 678 BeginScroll(); |
668 notification_view()->OnGestureEvent(event_scroll10.get()); | 679 ScrollBy(-180); |
669 EXPECT_FALSE(IsRemoved(notification_id)); | 680 EXPECT_FALSE(IsRemoved(notification_id)); |
670 EXPECT_EQ(-10.f, GetNotificationScrollAmount()); | 681 EXPECT_EQ(-180.f, GetNotificationSlideAmount()); |
671 notification_view()->OnGestureEvent(event_end.get()); | 682 EndScroll(); |
672 EXPECT_FALSE(IsRemoved(notification_id)); | |
673 EXPECT_EQ(0.f, GetNotificationScrollAmount()); | |
674 | |
675 notification_view()->OnGestureEvent(event_begin.get()); | |
676 notification_view()->OnGestureEvent(event_scroll500.get()); | |
677 EXPECT_FALSE(IsRemoved(notification_id)); | |
678 EXPECT_EQ(-500.f, GetNotificationScrollAmount()); | |
679 notification_view()->OnGestureEvent(event_end.get()); | |
680 EXPECT_TRUE(IsRemoved(notification_id)); | 683 EXPECT_TRUE(IsRemoved(notification_id)); |
681 } | 684 } |
682 | 685 |
| 686 TEST_F(NotificationViewTest, SlideOutNested) { |
| 687 ui::ScopedAnimationDurationScaleMode zero_duration_scope( |
| 688 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); |
| 689 |
| 690 UpdateNotificationViews(); |
| 691 notification_view()->SetIsNested(); |
| 692 std::string notification_id = notification()->id(); |
| 693 |
| 694 BeginScroll(); |
| 695 ScrollBy(-10); |
| 696 EXPECT_FALSE(IsRemoved(notification_id)); |
| 697 EXPECT_EQ(-10.f, GetNotificationSlideAmount()); |
| 698 EndScroll(); |
| 699 EXPECT_FALSE(IsRemoved(notification_id)); |
| 700 EXPECT_EQ(0.f, GetNotificationSlideAmount()); |
| 701 |
| 702 BeginScroll(); |
| 703 ScrollBy(-180); |
| 704 EXPECT_FALSE(IsRemoved(notification_id)); |
| 705 EXPECT_EQ(-180.f, GetNotificationSlideAmount()); |
| 706 EndScroll(); |
| 707 EXPECT_TRUE(IsRemoved(notification_id)); |
| 708 } |
| 709 |
683 // Pinning notification is ChromeOS only feature. | 710 // Pinning notification is ChromeOS only feature. |
684 #if defined(OS_CHROMEOS) | 711 #if defined(OS_CHROMEOS) |
685 | 712 |
686 TEST_F(NotificationViewTest, SlideOutPinned) { | 713 TEST_F(NotificationViewTest, SlideOutPinned) { |
687 ui::ScopedAnimationDurationScaleMode zero_duration_scope( | 714 ui::ScopedAnimationDurationScaleMode zero_duration_scope( |
688 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); | 715 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); |
689 | 716 |
690 notification()->set_pinned(true); | 717 notification()->set_pinned(true); |
691 UpdateNotificationViews(); | 718 UpdateNotificationViews(); |
692 std::string notification_id = notification()->id(); | 719 std::string notification_id = notification()->id(); |
693 | 720 |
694 auto event_begin = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN); | 721 BeginScroll(); |
695 auto event_scroll500 = GenerateGestureVerticalScrollUpdateEvent(-500); | 722 ScrollBy(-180); |
696 auto event_end = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_END); | |
697 | |
698 notification_view()->OnGestureEvent(event_begin.get()); | |
699 notification_view()->OnGestureEvent(event_scroll500.get()); | |
700 EXPECT_FALSE(IsRemoved(notification_id)); | 723 EXPECT_FALSE(IsRemoved(notification_id)); |
701 EXPECT_LT(-500.f, GetNotificationScrollAmount()); | 724 EXPECT_LT(-180.f, GetNotificationSlideAmount()); |
702 notification_view()->OnGestureEvent(event_end.get()); | 725 EndScroll(); |
703 EXPECT_FALSE(IsRemoved(notification_id)); | 726 EXPECT_FALSE(IsRemoved(notification_id)); |
704 } | 727 } |
705 | 728 |
706 TEST_F(NotificationViewTest, Pinned) { | 729 TEST_F(NotificationViewTest, Pinned) { |
707 notification()->set_pinned(true); | 730 notification()->set_pinned(true); |
708 | 731 |
709 UpdateNotificationViews(); | 732 UpdateNotificationViews(); |
710 EXPECT_EQ(NULL, GetCloseButton()); | 733 EXPECT_EQ(NULL, GetCloseButton()); |
711 } | 734 } |
712 | 735 |
713 #endif // defined(OS_CHROMEOS) | 736 #endif // defined(OS_CHROMEOS) |
714 | 737 |
715 } // namespace message_center | 738 } // namespace message_center |
OLD | NEW |