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