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 "components/mus/common/event_matcher_util.h" | |
13 #include "components/mus/public/cpp/window.h" | |
14 #include "components/mus/public/interfaces/accelerator_registrar.mojom.h" | |
15 #include "mojo/public/cpp/bindings/binding.h" | |
16 #include "services/shell/public/cpp/shell_test.h" | |
17 | |
18 using mus::mojom::AcceleratorHandler; | |
19 using mus::mojom::AcceleratorHandlerPtr; | |
20 using mus::mojom::AcceleratorRegistrar; | |
21 using mus::mojom::AcceleratorRegistrarPtr; | |
22 | |
23 namespace mash { | |
24 namespace wm { | |
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 mus::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, mus::mojom::EventPtr 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 shell::test::ShellTest { | |
71 public: | |
72 AcceleratorRegistrarTest() : shell::test::ShellTest("exe:mash_unittests") {} | |
73 ~AcceleratorRegistrarTest() override {} | |
74 | |
75 protected: | |
76 void ConnectToRegistrar(AcceleratorRegistrarPtr* registrar) { | |
77 connector()->ConnectToInterface("mojo:desktop_wm", registrar); | |
78 } | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(AcceleratorRegistrarTest); | |
82 }; | |
83 | |
84 TEST_F(AcceleratorRegistrarTest, AcceleratorRegistrarBasic) { | |
85 AcceleratorRegistrarPtr registrar_first; | |
86 ConnectToRegistrar(®istrar_first); | |
87 TestAcceleratorHandler handler_first(std::move(registrar_first)); | |
88 EXPECT_TRUE(handler_first.AttemptToInstallAccelerator( | |
89 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::T, | |
90 mus::mojom::kEventFlagShiftDown))); | |
91 // Attempting to add an accelerator with the same accelerator id from the same | |
92 // registrar should fail. | |
93 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | |
94 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | |
95 mus::mojom::kEventFlagShiftDown))); | |
96 | |
97 // Attempting to add an accelerator with the same id from a different | |
98 // registrar should be OK. | |
99 AcceleratorRegistrarPtr registrar_second; | |
100 ConnectToRegistrar(®istrar_second); | |
101 TestAcceleratorHandler handler_second(std::move(registrar_second)); | |
102 EXPECT_TRUE(handler_second.AttemptToInstallAccelerator( | |
103 1, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | |
104 mus::mojom::kEventFlagShiftDown))); | |
105 | |
106 // But attempting to add an accelerator with the same matcher should fail. | |
107 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | |
108 3, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | |
109 mus::mojom::kEventFlagShiftDown))); | |
110 EXPECT_FALSE(handler_second.AttemptToInstallAccelerator( | |
111 3, mus::CreateKeyMatcher(mus::mojom::KeyboardCode::N, | |
112 mus::mojom::kEventFlagShiftDown))); | |
113 } | |
114 | |
115 } // namespace wm | |
116 } // namespace mash | |
OLD | NEW |