| 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/wm/core/nested_accelerator_controller.h" | 5 #include "ui/wm/core/nested_accelerator_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/event_types.h" | 8 #include "base/event_types.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "ui/aura/test/aura_test_base.h" | 10 #include "ui/aura/test/aura_test_base.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 class MockDispatcher : public ui::PlatformEventDispatcher { | 35 class MockDispatcher : public ui::PlatformEventDispatcher { |
| 36 public: | 36 public: |
| 37 MockDispatcher() : num_key_events_dispatched_(0) {} | 37 MockDispatcher() : num_key_events_dispatched_(0) {} |
| 38 | 38 |
| 39 int num_key_events_dispatched() { return num_key_events_dispatched_; } | 39 int num_key_events_dispatched() { return num_key_events_dispatched_; } |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 // ui::PlatformEventDispatcher: | 42 // ui::PlatformEventDispatcher: |
| 43 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) override { | 43 bool CanDispatchEvent(const ui::PlatformEvent& event) override { |
| 44 return true; | 44 return true; |
| 45 } | 45 } |
| 46 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) override { | 46 uint32_t DispatchEvent(const ui::PlatformEvent& event) override { |
| 47 if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED) | 47 if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED) |
| 48 num_key_events_dispatched_++; | 48 num_key_events_dispatched_++; |
| 49 return ui::POST_DISPATCH_NONE; | 49 return ui::POST_DISPATCH_NONE; |
| 50 } | 50 } |
| 51 | 51 |
| 52 int num_key_events_dispatched_; | 52 int num_key_events_dispatched_; |
| 53 | 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); | 54 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 class TestTarget : public ui::AcceleratorTarget { | 57 class TestTarget : public ui::AcceleratorTarget { |
| 58 public: | 58 public: |
| 59 TestTarget() : accelerator_pressed_count_(0) {} | 59 TestTarget() : accelerator_pressed_count_(0) {} |
| 60 virtual ~TestTarget() {} | 60 ~TestTarget() override {} |
| 61 | 61 |
| 62 int accelerator_pressed_count() const { return accelerator_pressed_count_; } | 62 int accelerator_pressed_count() const { return accelerator_pressed_count_; } |
| 63 | 63 |
| 64 // Overridden from ui::AcceleratorTarget: | 64 // Overridden from ui::AcceleratorTarget: |
| 65 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 65 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 66 accelerator_pressed_count_++; | 66 accelerator_pressed_count_++; |
| 67 return true; | 67 return true; |
| 68 } | 68 } |
| 69 virtual bool CanHandleAccelerators() const override { return true; } | 69 bool CanHandleAccelerators() const override { return true; } |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 int accelerator_pressed_count_; | 72 int accelerator_pressed_count_; |
| 73 | 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(TestTarget); | 74 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 void DispatchKeyReleaseA(aura::Window* root_window) { | 77 void DispatchKeyReleaseA(aura::Window* root_window) { |
| 78 // Sending both keydown and keyup is necessary here because the accelerator | 78 // Sending both keydown and keyup is necessary here because the accelerator |
| 79 // manager only checks a keyup event following a keydown event. See | 79 // manager only checks a keyup event following a keydown event. See |
| (...skipping 14 matching lines...) Expand all Loading... |
| 94 #endif | 94 #endif |
| 95 // Make sure the inner message-loop terminates after dispatching the events. | 95 // Make sure the inner message-loop terminates after dispatching the events. |
| 96 base::MessageLoop::current()->PostTask( | 96 base::MessageLoop::current()->PostTask( |
| 97 FROM_HERE, base::MessageLoop::current()->QuitClosure()); | 97 FROM_HERE, base::MessageLoop::current()->QuitClosure()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 class MockNestedAcceleratorDelegate : public NestedAcceleratorDelegate { | 100 class MockNestedAcceleratorDelegate : public NestedAcceleratorDelegate { |
| 101 public: | 101 public: |
| 102 MockNestedAcceleratorDelegate() | 102 MockNestedAcceleratorDelegate() |
| 103 : accelerator_manager_(new ui::AcceleratorManager) {} | 103 : accelerator_manager_(new ui::AcceleratorManager) {} |
| 104 virtual ~MockNestedAcceleratorDelegate() {} | 104 ~MockNestedAcceleratorDelegate() override {} |
| 105 | 105 |
| 106 // NestedAcceleratorDelegate: | 106 // NestedAcceleratorDelegate: |
| 107 virtual Result ProcessAccelerator( | 107 Result ProcessAccelerator(const ui::Accelerator& accelerator) override { |
| 108 const ui::Accelerator& accelerator) override { | |
| 109 return accelerator_manager_->Process(accelerator) ? | 108 return accelerator_manager_->Process(accelerator) ? |
| 110 RESULT_PROCESSED : RESULT_NOT_PROCESSED; | 109 RESULT_PROCESSED : RESULT_NOT_PROCESSED; |
| 111 } | 110 } |
| 112 | 111 |
| 113 void Register(const ui::Accelerator& accelerator, | 112 void Register(const ui::Accelerator& accelerator, |
| 114 ui::AcceleratorTarget* target) { | 113 ui::AcceleratorTarget* target) { |
| 115 accelerator_manager_->Register( | 114 accelerator_manager_->Register( |
| 116 accelerator, ui::AcceleratorManager::kNormalPriority, target); | 115 accelerator, ui::AcceleratorManager::kNormalPriority, target); |
| 117 } | 116 } |
| 118 | 117 |
| 119 private: | 118 private: |
| 120 scoped_ptr<ui::AcceleratorManager> accelerator_manager_; | 119 scoped_ptr<ui::AcceleratorManager> accelerator_manager_; |
| 121 | 120 |
| 122 DISALLOW_COPY_AND_ASSIGN(MockNestedAcceleratorDelegate); | 121 DISALLOW_COPY_AND_ASSIGN(MockNestedAcceleratorDelegate); |
| 123 }; | 122 }; |
| 124 | 123 |
| 125 class NestedAcceleratorTest : public aura::test::AuraTestBase { | 124 class NestedAcceleratorTest : public aura::test::AuraTestBase { |
| 126 public: | 125 public: |
| 127 NestedAcceleratorTest() {} | 126 NestedAcceleratorTest() {} |
| 128 virtual ~NestedAcceleratorTest() {} | 127 ~NestedAcceleratorTest() override {} |
| 129 | 128 |
| 130 virtual void SetUp() override { | 129 void SetUp() override { |
| 131 AuraTestBase::SetUp(); | 130 AuraTestBase::SetUp(); |
| 132 delegate_ = new MockNestedAcceleratorDelegate(); | 131 delegate_ = new MockNestedAcceleratorDelegate(); |
| 133 nested_accelerator_controller_.reset( | 132 nested_accelerator_controller_.reset( |
| 134 new NestedAcceleratorController(delegate_)); | 133 new NestedAcceleratorController(delegate_)); |
| 135 aura::client::SetDispatcherClient(root_window(), | 134 aura::client::SetDispatcherClient(root_window(), |
| 136 nested_accelerator_controller_.get()); | 135 nested_accelerator_controller_.get()); |
| 137 } | 136 } |
| 138 | 137 |
| 139 virtual void TearDown() override { | 138 void TearDown() override { |
| 140 aura::client::SetDispatcherClient(root_window(), NULL); | 139 aura::client::SetDispatcherClient(root_window(), NULL); |
| 141 AuraTestBase::TearDown(); | 140 AuraTestBase::TearDown(); |
| 142 delegate_ = NULL; | 141 delegate_ = NULL; |
| 143 nested_accelerator_controller_.reset(); | 142 nested_accelerator_controller_.reset(); |
| 144 } | 143 } |
| 145 | 144 |
| 146 MockNestedAcceleratorDelegate* delegate() { return delegate_; } | 145 MockNestedAcceleratorDelegate* delegate() { return delegate_; } |
| 147 | 146 |
| 148 private: | 147 private: |
| 149 scoped_ptr<NestedAcceleratorController> nested_accelerator_controller_; | 148 scoped_ptr<NestedAcceleratorController> nested_accelerator_controller_; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 &inner_dispatcher); | 197 &inner_dispatcher); |
| 199 aura::client::DispatcherRunLoop run_loop( | 198 aura::client::DispatcherRunLoop run_loop( |
| 200 aura::client::GetDispatcherClient(root_window()), NULL); | 199 aura::client::GetDispatcherClient(root_window()), NULL); |
| 201 run_loop.Run(); | 200 run_loop.Run(); |
| 202 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); | 201 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); |
| 203 EXPECT_EQ(1, target.accelerator_pressed_count()); | 202 EXPECT_EQ(1, target.accelerator_pressed_count()); |
| 204 } | 203 } |
| 205 | 204 |
| 206 } // namespace test | 205 } // namespace test |
| 207 } // namespace wm | 206 } // namespace wm |
| OLD | NEW |