Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: ui/message_center/views/notification_view_unittest.cc

Issue 2855763003: reland "CrOS: Fix appearance of notification toasts when sliding out via gesture" (Closed)
Patch Set: cleaned up Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 ui::GestureEvent event(0, 0, 0, ui::EventTimeForNow(), details);
188 generator.Dispatch(&event);
189 }
190
191 void BeginScroll() {
192 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN));
193 }
194
195 void EndScroll() {
196 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END));
197 }
198
199 void ScrollBy(int dx) {
200 DispatchGesture(
201 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, dx, 0));
202 }
203
199 private: 204 private:
200 std::set<std::string> removed_ids_; 205 std::set<std::string> removed_ids_;
201 206
202 std::unique_ptr<RichNotificationData> data_; 207 std::unique_ptr<RichNotificationData> data_;
203 std::unique_ptr<Notification> notification_; 208 std::unique_ptr<Notification> notification_;
204 std::unique_ptr<NotificationView> notification_view_; 209 std::unique_ptr<NotificationView> notification_view_;
205 210
206 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest); 211 DISALLOW_COPY_AND_ASSIGN(NotificationViewTest);
207 }; 212 };
208 213
(...skipping 21 matching lines...) Expand all
230 notification_view_.reset(static_cast<NotificationView*>( 235 notification_view_.reset(static_cast<NotificationView*>(
231 MessageViewFactory::Create(this, *notification_, true))); 236 MessageViewFactory::Create(this, *notification_, true)));
232 notification_view_->set_owned_by_client(); 237 notification_view_->set_owned_by_client();
233 238
234 views::Widget::InitParams init_params( 239 views::Widget::InitParams init_params(
235 CreateParams(views::Widget::InitParams::TYPE_POPUP)); 240 CreateParams(views::Widget::InitParams::TYPE_POPUP));
236 views::Widget* widget = new views::Widget(); 241 views::Widget* widget = new views::Widget();
237 widget->Init(init_params); 242 widget->Init(init_params);
238 widget->SetContentsView(notification_view_.get()); 243 widget->SetContentsView(notification_view_.get());
239 widget->SetSize(notification_view_->GetPreferredSize()); 244 widget->SetSize(notification_view_->GetPreferredSize());
245 widget->Show();
240 } 246 }
241 247
242 void NotificationViewTest::TearDown() { 248 void NotificationViewTest::TearDown() {
243 widget()->Close(); 249 widget()->Close();
244 notification_view_.reset(); 250 notification_view_.reset();
245 views::ViewsTestBase::TearDown(); 251 views::ViewsTestBase::TearDown();
246 } 252 }
247 253
248 void NotificationViewTest::ClickOnNotification( 254 void NotificationViewTest::ClickOnNotification(
249 const std::string& notification_id) { 255 const std::string& notification_id) {
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 EXPECT_TRUE(result_utf8.find("hello") == std::string::npos); 658 EXPECT_TRUE(result_utf8.find("hello") == std::string::npos);
653 } 659 }
654 660
655 TEST_F(NotificationViewTest, SlideOut) { 661 TEST_F(NotificationViewTest, SlideOut) {
656 ui::ScopedAnimationDurationScaleMode zero_duration_scope( 662 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
657 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); 663 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
658 664
659 UpdateNotificationViews(); 665 UpdateNotificationViews();
660 std::string notification_id = notification()->id(); 666 std::string notification_id = notification()->id();
661 667
662 auto event_begin = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN); 668 BeginScroll();
663 auto event_scroll10 = GenerateGestureVerticalScrollUpdateEvent(-10); 669 ScrollBy(-10);
664 auto event_scroll500 = GenerateGestureVerticalScrollUpdateEvent(-500); 670 EXPECT_FALSE(IsRemoved(notification_id));
665 auto event_end = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_END); 671 EXPECT_EQ(-10.f, GetNotificationSlideAmount());
672 EndScroll();
673 EXPECT_FALSE(IsRemoved(notification_id));
674 EXPECT_EQ(0.f, GetNotificationSlideAmount());
666 675
667 notification_view()->OnGestureEvent(event_begin.get()); 676 BeginScroll();
668 notification_view()->OnGestureEvent(event_scroll10.get()); 677 ScrollBy(-200);
669 EXPECT_FALSE(IsRemoved(notification_id)); 678 EXPECT_FALSE(IsRemoved(notification_id));
670 EXPECT_EQ(-10.f, GetNotificationScrollAmount()); 679 EXPECT_EQ(-200.f, GetNotificationSlideAmount());
671 notification_view()->OnGestureEvent(event_end.get()); 680 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)); 681 EXPECT_TRUE(IsRemoved(notification_id));
681 } 682 }
682 683
684 TEST_F(NotificationViewTest, SlideOutNested) {
685 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
686 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
687
688 UpdateNotificationViews();
689 notification_view()->SetIsNested();
690 std::string notification_id = notification()->id();
691
692 BeginScroll();
693 ScrollBy(-10);
694 EXPECT_FALSE(IsRemoved(notification_id));
695 EXPECT_EQ(-10.f, GetNotificationSlideAmount());
696 EndScroll();
697 EXPECT_FALSE(IsRemoved(notification_id));
698 EXPECT_EQ(0.f, GetNotificationSlideAmount());
699
700 BeginScroll();
701 ScrollBy(-200);
702 EXPECT_FALSE(IsRemoved(notification_id));
703 EXPECT_EQ(-200.f, GetNotificationSlideAmount());
704 EndScroll();
705 EXPECT_TRUE(IsRemoved(notification_id));
706 }
707
683 // Pinning notification is ChromeOS only feature. 708 // Pinning notification is ChromeOS only feature.
684 #if defined(OS_CHROMEOS) 709 #if defined(OS_CHROMEOS)
685 710
686 TEST_F(NotificationViewTest, SlideOutPinned) { 711 TEST_F(NotificationViewTest, SlideOutPinned) {
687 ui::ScopedAnimationDurationScaleMode zero_duration_scope( 712 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
688 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION); 713 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
689 714
690 notification()->set_pinned(true); 715 notification()->set_pinned(true);
691 UpdateNotificationViews(); 716 UpdateNotificationViews();
692 std::string notification_id = notification()->id(); 717 std::string notification_id = notification()->id();
693 718
694 auto event_begin = GenerateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN); 719 BeginScroll();
695 auto event_scroll500 = GenerateGestureVerticalScrollUpdateEvent(-500); 720 ScrollBy(-200);
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)); 721 EXPECT_FALSE(IsRemoved(notification_id));
701 EXPECT_LT(-500.f, GetNotificationScrollAmount()); 722 EXPECT_LT(-200.f, GetNotificationSlideAmount());
702 notification_view()->OnGestureEvent(event_end.get()); 723 EndScroll();
703 EXPECT_FALSE(IsRemoved(notification_id)); 724 EXPECT_FALSE(IsRemoved(notification_id));
704 } 725 }
705 726
706 TEST_F(NotificationViewTest, Pinned) { 727 TEST_F(NotificationViewTest, Pinned) {
707 notification()->set_pinned(true); 728 notification()->set_pinned(true);
708 729
709 UpdateNotificationViews(); 730 UpdateNotificationViews();
710 EXPECT_EQ(NULL, GetCloseButton()); 731 EXPECT_EQ(NULL, GetCloseButton());
711 } 732 }
712 733
713 #endif // defined(OS_CHROMEOS) 734 #endif // defined(OS_CHROMEOS)
714 735
715 } // namespace message_center 736 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/message_view_factory.cc ('k') | ui/message_center/views/slide_out_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698