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

Side by Side Diff: ui/views/widget/widget_unittest.cc

Issue 2604793002: Allow Widget to pass accelerator handling to its parent.
Patch Set: Created 3 years, 12 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <algorithm> 5 #include <algorithm>
6 #include <memory> 6 #include <memory>
7 #include <set> 7 #include <set>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 3891 matching lines...) Expand 10 before | Expand all | Expand 10 after
3902 TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetOpaque) { 3902 TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetOpaque) {
3903 CheckAllWidgetsForOpacity(Widget::InitParams::OPAQUE_WINDOW); 3903 CheckAllWidgetsForOpacity(Widget::InitParams::OPAQUE_WINDOW);
3904 } 3904 }
3905 3905
3906 TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetTranslucent) { 3906 TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetTranslucent) {
3907 CheckAllWidgetsForOpacity(Widget::InitParams::TRANSLUCENT_WINDOW); 3907 CheckAllWidgetsForOpacity(Widget::InitParams::TRANSLUCENT_WINDOW);
3908 } 3908 }
3909 3909
3910 #endif // !defined(OS_CHROMEOS) 3910 #endif // !defined(OS_CHROMEOS)
3911 3911
3912 namespace {
3913
3914 class ViewWithAccelerator : public View {
3915 public:
3916 explicit ViewWithAccelerator(
3917 const std::vector<ui::Accelerator>& accelerators) {
3918 for (const auto& accelerator : accelerators) {
3919 AddAccelerator(accelerator);
3920 accelerators_hit_count_.insert(std::make_pair(accelerator, 0));
3921 }
3922 }
3923
3924 int GetAcceleratorHitCount(const ui::Accelerator& accelerator) const {
3925 auto it = accelerators_hit_count_.find(accelerator);
3926 DCHECK(it != accelerators_hit_count_.end());
3927 return it->second;
3928 }
3929
3930 bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
3931 auto it = accelerators_hit_count_.find(accelerator);
3932 if (it == accelerators_hit_count_.end())
3933 return View::AcceleratorPressed(accelerator);
3934 ++it->second;
3935 return true;
3936 }
3937
3938 private:
3939 std::map<ui::Accelerator, int> accelerators_hit_count_;
3940
3941 DISALLOW_COPY_AND_ASSIGN(ViewWithAccelerator);
3942 };
3943
3944 } // namespace
3945
3946 class WidgetAcceleratorTest : public WidgetTest {
3947 public:
3948 WidgetAcceleratorTest() = default;
3949
3950 void SendAcceleratorToWidget(Widget* widget,
3951 const ui::Accelerator& accelerator) {
3952 ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, accelerator.key_code(),
3953 accelerator.modifiers());
3954 widget->OnKeyEvent(&key_pressed);
3955 ui::KeyEvent key_released(ui::ET_KEY_RELEASED, accelerator.key_code(),
3956 accelerator.modifiers());
3957 widget->OnKeyEvent(&key_released);
3958 }
3959
3960 private:
3961 DISALLOW_COPY_AND_ASSIGN(WidgetAcceleratorTest);
3962 };
3963
3964 TEST_F(WidgetAcceleratorTest, AcceleratorPassing) {
3965 ui::Accelerator parent_accelerator(ui::VKEY_T, ui::EF_CONTROL_DOWN);
3966 ui::Accelerator child_accelerator(ui::VKEY_RETURN, 0);
3967 ui::Accelerator both_accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN);
3968
3969 Widget parent_widget;
3970 ViewWithAccelerator* parent_view =
3971 new ViewWithAccelerator({parent_accelerator, both_accelerator});
3972 {
3973 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3974 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3975 parent_widget.Init(params);
3976 parent_widget.SetContentsView(parent_view);
3977 parent_widget.Show();
3978 }
3979
3980 Widget child_widget;
3981 ViewWithAccelerator* child_view =
3982 new ViewWithAccelerator({child_accelerator, both_accelerator});
3983 {
3984 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
3985 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
3986 params.parent = parent_widget.GetNativeView();
3987 params.pass_accelerator_to_parent = true;
3988 child_widget.Init(params);
3989 child_widget.SetContentsView(child_view);
3990 child_widget.Show();
3991 }
3992
3993 child_widget.Activate();
3994 // Both can handle accelerators. Child because it's active, parent because
3995 // it's overriden from child.
3996 EXPECT_TRUE(parent_view->CanHandleAccelerators());
3997 EXPECT_TRUE(child_view->CanHandleAccelerators());
3998
3999 ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4000 ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4001 ASSERT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
4002 ASSERT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4003
4004 // |parent_accelerator| is forwarded from child to parent.
4005 SendAcceleratorToWidget(&child_widget, parent_accelerator);
4006 EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
4007 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4008 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
4009 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4010
4011 // |child_accelerator| is handled by child itself.
4012 SendAcceleratorToWidget(&child_widget, child_accelerator);
4013 EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
4014 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4015 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4016 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4017
4018 // |both_accelerator| is also handled by child itself.
4019 SendAcceleratorToWidget(&child_widget, both_accelerator);
4020 EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
4021 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4022 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4023 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
4024
4025 // |both_accelerator| sent directly to parent is handled by parent.
4026 SendAcceleratorToWidget(&parent_widget, both_accelerator);
4027 EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
4028 EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(both_accelerator));
4029 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4030 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
4031 }
4032
4033 TEST_F(WidgetAcceleratorTest, AcceleratorNotPassing) {
4034 ui::Accelerator parent_accelerator(ui::VKEY_T, ui::EF_CONTROL_DOWN);
4035 ui::Accelerator child_accelerator(ui::VKEY_RETURN, 0);
4036 ui::Accelerator both_accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN);
4037
4038 Widget parent_widget;
4039 ViewWithAccelerator* parent_view =
4040 new ViewWithAccelerator({parent_accelerator, both_accelerator});
4041 {
4042 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
4043 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
4044 parent_widget.Init(params);
4045 parent_widget.SetContentsView(parent_view);
4046 parent_widget.Show();
4047 }
4048
4049 Widget child_widget;
4050 ViewWithAccelerator* child_view =
4051 new ViewWithAccelerator({child_accelerator, both_accelerator});
4052 {
4053 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
4054 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
4055 params.parent = parent_widget.GetNativeView();
4056 params.pass_accelerator_to_parent = false;
4057 child_widget.Init(params);
4058 child_widget.SetContentsView(child_view);
4059 child_widget.Show();
4060 }
4061
4062 child_widget.Activate();
4063 // Only child can handle accelerators because it's active.
4064 EXPECT_FALSE(parent_view->CanHandleAccelerators());
4065 EXPECT_TRUE(child_view->CanHandleAccelerators());
4066
4067 ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4068 ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4069 ASSERT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
4070 ASSERT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4071
4072 // |parent_accelerator| is not forwarded from child.
4073 SendAcceleratorToWidget(&child_widget, parent_accelerator);
4074 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4075 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4076 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
4077 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4078
4079 // Child handles |child_accelerator|.
4080 SendAcceleratorToWidget(&child_widget, child_accelerator);
4081 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4082 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4083 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4084 EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
4085
4086 // Child also handles |both_accelerator|.
4087 SendAcceleratorToWidget(&child_widget, both_accelerator);
4088 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4089 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4090 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4091 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
4092
4093 // Parent does not handle |both_accelerator| because it's inactive.
4094 SendAcceleratorToWidget(&parent_widget, both_accelerator);
4095 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
4096 EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
4097 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
4098 EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
4099 }
4100
3912 } // namespace test 4101 } // namespace test
3913 } // namespace views 4102 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698