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