| Index: ui/views/widget/widget_unittest.cc
|
| diff --git a/ui/views/widget/widget_unittest.cc b/ui/views/widget/widget_unittest.cc
|
| index 22c6bdc2f76aaabe38348f7da5ba2ca996febbb1..1f02dcd91f30fc79a186165868deed84cbac0f38 100644
|
| --- a/ui/views/widget/widget_unittest.cc
|
| +++ b/ui/views/widget/widget_unittest.cc
|
| @@ -3909,5 +3909,194 @@ TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetTranslucent) {
|
|
|
| #endif // !defined(OS_CHROMEOS)
|
|
|
| +namespace {
|
| +
|
| +class ViewWithAccelerator : public View {
|
| + public:
|
| + explicit ViewWithAccelerator(
|
| + const std::vector<ui::Accelerator>& accelerators) {
|
| + for (const auto& accelerator : accelerators) {
|
| + AddAccelerator(accelerator);
|
| + accelerators_hit_count_.insert(std::make_pair(accelerator, 0));
|
| + }
|
| + }
|
| +
|
| + int GetAcceleratorHitCount(const ui::Accelerator& accelerator) const {
|
| + auto it = accelerators_hit_count_.find(accelerator);
|
| + DCHECK(it != accelerators_hit_count_.end());
|
| + return it->second;
|
| + }
|
| +
|
| + bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
|
| + auto it = accelerators_hit_count_.find(accelerator);
|
| + if (it == accelerators_hit_count_.end())
|
| + return View::AcceleratorPressed(accelerator);
|
| + ++it->second;
|
| + return true;
|
| + }
|
| +
|
| + private:
|
| + std::map<ui::Accelerator, int> accelerators_hit_count_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ViewWithAccelerator);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class WidgetAcceleratorTest : public WidgetTest {
|
| + public:
|
| + WidgetAcceleratorTest() = default;
|
| +
|
| + void SendAcceleratorToWidget(Widget* widget,
|
| + const ui::Accelerator& accelerator) {
|
| + ui::KeyEvent key_pressed(ui::ET_KEY_PRESSED, accelerator.key_code(),
|
| + accelerator.modifiers());
|
| + widget->OnKeyEvent(&key_pressed);
|
| + ui::KeyEvent key_released(ui::ET_KEY_RELEASED, accelerator.key_code(),
|
| + accelerator.modifiers());
|
| + widget->OnKeyEvent(&key_released);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(WidgetAcceleratorTest);
|
| +};
|
| +
|
| +TEST_F(WidgetAcceleratorTest, AcceleratorPassing) {
|
| + ui::Accelerator parent_accelerator(ui::VKEY_T, ui::EF_CONTROL_DOWN);
|
| + ui::Accelerator child_accelerator(ui::VKEY_RETURN, 0);
|
| + ui::Accelerator both_accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN);
|
| +
|
| + Widget parent_widget;
|
| + ViewWithAccelerator* parent_view =
|
| + new ViewWithAccelerator({parent_accelerator, both_accelerator});
|
| + {
|
| + Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
|
| + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
|
| + parent_widget.Init(params);
|
| + parent_widget.SetContentsView(parent_view);
|
| + parent_widget.Show();
|
| + }
|
| +
|
| + Widget child_widget;
|
| + ViewWithAccelerator* child_view =
|
| + new ViewWithAccelerator({child_accelerator, both_accelerator});
|
| + {
|
| + Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
|
| + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
|
| + params.parent = parent_widget.GetNativeView();
|
| + params.pass_accelerator_to_parent = true;
|
| + child_widget.Init(params);
|
| + child_widget.SetContentsView(child_view);
|
| + child_widget.Show();
|
| + }
|
| +
|
| + child_widget.Activate();
|
| + // Both can handle accelerators. Child because it's active, parent because
|
| + // it's overriden from child.
|
| + EXPECT_TRUE(parent_view->CanHandleAccelerators());
|
| + EXPECT_TRUE(child_view->CanHandleAccelerators());
|
| +
|
| + ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + ASSERT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + ASSERT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // |parent_accelerator| is forwarded from child to parent.
|
| + SendAcceleratorToWidget(&child_widget, parent_accelerator);
|
| + EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // |child_accelerator| is handled by child itself.
|
| + SendAcceleratorToWidget(&child_widget, child_accelerator);
|
| + EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // |both_accelerator| is also handled by child itself.
|
| + SendAcceleratorToWidget(&child_widget, both_accelerator);
|
| + EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // |both_accelerator| sent directly to parent is handled by parent.
|
| + SendAcceleratorToWidget(&parent_widget, both_accelerator);
|
| + EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(1, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +}
|
| +
|
| +TEST_F(WidgetAcceleratorTest, AcceleratorNotPassing) {
|
| + ui::Accelerator parent_accelerator(ui::VKEY_T, ui::EF_CONTROL_DOWN);
|
| + ui::Accelerator child_accelerator(ui::VKEY_RETURN, 0);
|
| + ui::Accelerator both_accelerator(ui::VKEY_Z, ui::EF_CONTROL_DOWN);
|
| +
|
| + Widget parent_widget;
|
| + ViewWithAccelerator* parent_view =
|
| + new ViewWithAccelerator({parent_accelerator, both_accelerator});
|
| + {
|
| + Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
|
| + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
|
| + parent_widget.Init(params);
|
| + parent_widget.SetContentsView(parent_view);
|
| + parent_widget.Show();
|
| + }
|
| +
|
| + Widget child_widget;
|
| + ViewWithAccelerator* child_view =
|
| + new ViewWithAccelerator({child_accelerator, both_accelerator});
|
| + {
|
| + Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
|
| + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
|
| + params.parent = parent_widget.GetNativeView();
|
| + params.pass_accelerator_to_parent = false;
|
| + child_widget.Init(params);
|
| + child_widget.SetContentsView(child_view);
|
| + child_widget.Show();
|
| + }
|
| +
|
| + child_widget.Activate();
|
| + // Only child can handle accelerators because it's active.
|
| + EXPECT_FALSE(parent_view->CanHandleAccelerators());
|
| + EXPECT_TRUE(child_view->CanHandleAccelerators());
|
| +
|
| + ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + ASSERT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + ASSERT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + ASSERT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // |parent_accelerator| is not forwarded from child.
|
| + SendAcceleratorToWidget(&child_widget, parent_accelerator);
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // Child handles |child_accelerator|.
|
| + SendAcceleratorToWidget(&child_widget, child_accelerator);
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(0, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // Child also handles |both_accelerator|.
|
| + SendAcceleratorToWidget(&child_widget, both_accelerator);
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +
|
| + // Parent does not handle |both_accelerator| because it's inactive.
|
| + SendAcceleratorToWidget(&parent_widget, both_accelerator);
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(parent_accelerator));
|
| + EXPECT_EQ(0, parent_view->GetAcceleratorHitCount(both_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(child_accelerator));
|
| + EXPECT_EQ(1, child_view->GetAcceleratorHitCount(both_accelerator));
|
| +}
|
| +
|
| } // namespace test
|
| } // namespace views
|
|
|