| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "components/mus/common/event_matcher_util.h" | 12 #include "components/mus/common/event_matcher_util.h" |
| 13 #include "components/mus/public/cpp/window.h" | 13 #include "components/mus/public/cpp/window.h" |
| 14 #include "components/mus/public/interfaces/accelerator_registrar.mojom.h" | 14 #include "components/mus/public/interfaces/accelerator_registrar.mojom.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
| 16 #include "services/shell/public/cpp/shell_test.h" | 16 #include "services/shell/public/cpp/shell_test.h" |
| 17 | 17 |
| 18 using mus::mojom::AcceleratorHandler; | 18 using ::mus::mojom::AcceleratorHandler; |
| 19 using mus::mojom::AcceleratorHandlerPtr; | 19 using ::mus::mojom::AcceleratorHandlerPtr; |
| 20 using mus::mojom::AcceleratorRegistrar; | 20 using ::mus::mojom::AcceleratorRegistrar; |
| 21 using mus::mojom::AcceleratorRegistrarPtr; | 21 using ::mus::mojom::AcceleratorRegistrarPtr; |
| 22 | 22 |
| 23 namespace mash { | 23 namespace ash { |
| 24 namespace wm { | 24 namespace mus { |
| 25 | 25 |
| 26 class TestAcceleratorHandler : public AcceleratorHandler { | 26 class TestAcceleratorHandler : public AcceleratorHandler { |
| 27 public: | 27 public: |
| 28 explicit TestAcceleratorHandler(AcceleratorRegistrarPtr registrar) | 28 explicit TestAcceleratorHandler(AcceleratorRegistrarPtr registrar) |
| 29 : binding_(this), | 29 : binding_(this), |
| 30 registrar_(std::move(registrar)), | 30 registrar_(std::move(registrar)), |
| 31 add_accelerator_result_(false) { | 31 add_accelerator_result_(false) { |
| 32 registrar_->SetHandler(binding_.CreateInterfacePtrAndBind()); | 32 registrar_->SetHandler(binding_.CreateInterfacePtrAndBind()); |
| 33 } | 33 } |
| 34 ~TestAcceleratorHandler() override {} | 34 ~TestAcceleratorHandler() override {} |
| 35 | 35 |
| 36 // Attempts to install an accelerator with the specified id and event matcher. | 36 // Attempts to install an accelerator with the specified id and event matcher. |
| 37 // Returns whether the accelerator could be successfully added or not. | 37 // Returns whether the accelerator could be successfully added or not. |
| 38 bool AttemptToInstallAccelerator(uint32_t accelerator_id, | 38 bool AttemptToInstallAccelerator(uint32_t accelerator_id, |
| 39 mus::mojom::EventMatcherPtr matcher) { | 39 ::mus::mojom::EventMatcherPtr matcher) { |
| 40 DCHECK(!run_loop_); | 40 DCHECK(!run_loop_); |
| 41 registrar_->AddAccelerator( | 41 registrar_->AddAccelerator( |
| 42 accelerator_id, std::move(matcher), | 42 accelerator_id, std::move(matcher), |
| 43 base::Bind(&TestAcceleratorHandler::AddAcceleratorCallback, | 43 base::Bind(&TestAcceleratorHandler::AddAcceleratorCallback, |
| 44 base::Unretained(this))); | 44 base::Unretained(this))); |
| 45 run_loop_.reset(new base::RunLoop); | 45 run_loop_.reset(new base::RunLoop); |
| 46 run_loop_->Run(); | 46 run_loop_->Run(); |
| 47 run_loop_.reset(); | 47 run_loop_.reset(); |
| 48 return add_accelerator_result_; | 48 return add_accelerator_result_; |
| 49 } | 49 } |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 void AddAcceleratorCallback(bool success) { | 52 void AddAcceleratorCallback(bool success) { |
| 53 DCHECK(run_loop_ && run_loop_->running()); | 53 DCHECK(run_loop_ && run_loop_->running()); |
| 54 add_accelerator_result_ = success; | 54 add_accelerator_result_ = success; |
| 55 run_loop_->Quit(); | 55 run_loop_->Quit(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // AcceleratorHandler: | 58 // AcceleratorHandler: |
| 59 void OnAccelerator(uint32_t id, mus::mojom::EventPtr event) override {} | 59 void OnAccelerator(uint32_t id, ::mus::mojom::EventPtr event) override {} |
| 60 | 60 |
| 61 std::set<uint32_t> installed_accelerators_; | 61 std::set<uint32_t> installed_accelerators_; |
| 62 std::unique_ptr<base::RunLoop> run_loop_; | 62 std::unique_ptr<base::RunLoop> run_loop_; |
| 63 mojo::Binding<AcceleratorHandler> binding_; | 63 mojo::Binding<AcceleratorHandler> binding_; |
| 64 AcceleratorRegistrarPtr registrar_; | 64 AcceleratorRegistrarPtr registrar_; |
| 65 bool add_accelerator_result_; | 65 bool add_accelerator_result_; |
| 66 | 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorHandler); | 67 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorHandler); |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 class AcceleratorRegistrarTest : public shell::test::ShellTest { | 70 class AcceleratorRegistrarTest : public shell::test::ShellTest { |
| 71 public: | 71 public: |
| 72 AcceleratorRegistrarTest() : shell::test::ShellTest("exe:mash_unittests") {} | 72 AcceleratorRegistrarTest() : shell::test::ShellTest("exe:mash_unittests") {} |
| 73 ~AcceleratorRegistrarTest() override {} | 73 ~AcceleratorRegistrarTest() override {} |
| 74 | 74 |
| 75 protected: | 75 protected: |
| 76 void ConnectToRegistrar(AcceleratorRegistrarPtr* registrar) { | 76 void ConnectToRegistrar(AcceleratorRegistrarPtr* registrar) { |
| 77 connector()->ConnectToInterface("mojo:desktop_wm", registrar); | 77 connector()->ConnectToInterface("mojo:desktop_wm", registrar); |
| 78 } | 78 } |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(AcceleratorRegistrarTest); | 81 DISALLOW_COPY_AND_ASSIGN(AcceleratorRegistrarTest); |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 TEST_F(AcceleratorRegistrarTest, AcceleratorRegistrarBasic) { | 84 TEST_F(AcceleratorRegistrarTest, AcceleratorRegistrarBasic) { |
| 85 AcceleratorRegistrarPtr registrar_first; | 85 AcceleratorRegistrarPtr registrar_first; |
| 86 ConnectToRegistrar(®istrar_first); | 86 ConnectToRegistrar(®istrar_first); |
| 87 TestAcceleratorHandler handler_first(std::move(registrar_first)); | 87 TestAcceleratorHandler handler_first(std::move(registrar_first)); |
| 88 EXPECT_TRUE(handler_first.AttemptToInstallAccelerator( | 88 EXPECT_TRUE(handler_first.AttemptToInstallAccelerator( |
| 89 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::T, | 89 1, ::mus::CreateKeyMatcher(::mus::mojom::KeyboardCode::T, |
| 90 mus::mojom::kEventFlagShiftDown))); | 90 ::mus::mojom::kEventFlagShiftDown))); |
| 91 // Attempting to add an accelerator with the same accelerator id from the same | 91 // Attempting to add an accelerator with the same accelerator id from the same |
| 92 // registrar should fail. | 92 // registrar should fail. |
| 93 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | 93 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( |
| 94 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | 94 1, ::mus::CreateKeyMatcher(::mus::mojom::KeyboardCode::N, |
| 95 mus::mojom::kEventFlagShiftDown))); | 95 ::mus::mojom::kEventFlagShiftDown))); |
| 96 | 96 |
| 97 // Attempting to add an accelerator with the same id from a different | 97 // Attempting to add an accelerator with the same id from a different |
| 98 // registrar should be OK. | 98 // registrar should be OK. |
| 99 AcceleratorRegistrarPtr registrar_second; | 99 AcceleratorRegistrarPtr registrar_second; |
| 100 ConnectToRegistrar(®istrar_second); | 100 ConnectToRegistrar(®istrar_second); |
| 101 TestAcceleratorHandler handler_second(std::move(registrar_second)); | 101 TestAcceleratorHandler handler_second(std::move(registrar_second)); |
| 102 EXPECT_TRUE(handler_second.AttemptToInstallAccelerator( | 102 EXPECT_TRUE(handler_second.AttemptToInstallAccelerator( |
| 103 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | 103 1, ::mus::CreateKeyMatcher(::mus::mojom::KeyboardCode::N, |
| 104 mus::mojom::kEventFlagShiftDown))); | 104 ::mus::mojom::kEventFlagShiftDown))); |
| 105 | 105 |
| 106 // But attempting to add an accelerator with the same matcher should fail. | 106 // But attempting to add an accelerator with the same matcher should fail. |
| 107 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | 107 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( |
| 108 3, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | 108 3, ::mus::CreateKeyMatcher(::mus::mojom::KeyboardCode::N, |
| 109 mus::mojom::kEventFlagShiftDown))); | 109 ::mus::mojom::kEventFlagShiftDown))); |
| 110 EXPECT_FALSE(handler_second.AttemptToInstallAccelerator( | 110 EXPECT_FALSE(handler_second.AttemptToInstallAccelerator( |
| 111 3, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | 111 3, ::mus::CreateKeyMatcher(::mus::mojom::KeyboardCode::N, |
| 112 mus::mojom::kEventFlagShiftDown))); | 112 ::mus::mojom::kEventFlagShiftDown))); |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace wm | 115 } // namespace mus |
| 116 } // namespace mash | 116 } // namespace ash |
| OLD | NEW |