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 "base/bind.h" | |
6 #include "base/run_loop.h" | |
7 #include "components/mus/public/cpp/event_matcher.h" | |
8 #include "components/mus/public/cpp/window.h" | |
9 #include "components/mus/public/interfaces/accelerator_registrar.mojom.h" | |
10 #include "components/mus/public/interfaces/window_manager.mojom.h" | |
11 #include "mojo/application/public/cpp/application_impl.h" | |
12 #include "mojo/application/public/cpp/application_test_base.h" | |
13 #include "mojo/public/cpp/bindings/binding.h" | |
14 | |
15 using mus::mojom::AcceleratorHandler; | |
16 using mus::mojom::AcceleratorHandlerPtr; | |
17 using mus::mojom::AcceleratorRegistrar; | |
18 using mus::mojom::AcceleratorRegistrarPtr; | |
19 | |
20 namespace mash { | |
21 namespace wm { | |
22 | |
23 class TestAcceleratorHandler : public AcceleratorHandler { | |
24 public: | |
25 explicit TestAcceleratorHandler(AcceleratorRegistrarPtr registrar) | |
26 : binding_(this), | |
27 registrar_(std::move(registrar)), | |
28 add_accelerator_result_(false) { | |
29 AcceleratorHandlerPtr handler; | |
30 binding_.Bind(GetProxy(&handler)); | |
31 registrar_->SetHandler(std::move(handler)); | |
32 } | |
33 ~TestAcceleratorHandler() override {} | |
34 | |
35 // Attempts to install an accelerator with the specified id and event matcher. | |
36 // Returns whether the accelerator could be successfully added or not. | |
37 bool AttemptToInstallAccelerator(uint32_t accelerator_id, | |
38 mus::mojom::EventMatcherPtr matcher) { | |
39 DCHECK(!run_loop_); | |
40 registrar_->AddAccelerator( | |
41 accelerator_id, std::move(matcher), | |
42 base::Bind(&TestAcceleratorHandler::AddAcceleratorCallback, | |
43 base::Unretained(this))); | |
44 run_loop_.reset(new base::RunLoop); | |
45 run_loop_->Run(); | |
46 run_loop_.reset(); | |
47 return add_accelerator_result_; | |
48 } | |
49 | |
50 private: | |
51 void AddAcceleratorCallback(bool success) { | |
52 DCHECK(run_loop_ && run_loop_->running()); | |
53 add_accelerator_result_ = success; | |
54 run_loop_->Quit(); | |
55 } | |
56 | |
57 // AcceleratorHandler: | |
58 void OnAccelerator(uint32_t id, mus::mojom::EventPtr event) override {} | |
59 | |
60 std::set<uint32_t> installed_accelerators_; | |
61 scoped_ptr<base::RunLoop> run_loop_; | |
62 mojo::Binding<AcceleratorHandler> binding_; | |
63 AcceleratorRegistrarPtr registrar_; | |
64 bool add_accelerator_result_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorHandler); | |
67 }; | |
68 | |
69 class AcceleratorRegistrarTest : public mojo::test::ApplicationTestBase { | |
70 public: | |
71 AcceleratorRegistrarTest() {} | |
72 ~AcceleratorRegistrarTest() override {} | |
73 | |
74 protected: | |
75 void ConnectToRegistrar(AcceleratorRegistrarPtr* registrar) { | |
76 application_impl()->ConnectToService("mojo:desktop_wm", registrar); | |
77 } | |
78 | |
79 private: | |
80 DISALLOW_COPY_AND_ASSIGN(AcceleratorRegistrarTest); | |
81 }; | |
82 | |
83 TEST_F(AcceleratorRegistrarTest, AcceleratorRegistrarBasic) { | |
84 AcceleratorRegistrarPtr registrar_first; | |
85 ConnectToRegistrar(®istrar_first); | |
86 TestAcceleratorHandler handler_first(std::move(registrar_first)); | |
87 EXPECT_TRUE(handler_first.AttemptToInstallAccelerator( | |
88 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_T, | |
89 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
90 // Attempting to add an accelerator with the same accelerator id from the same | |
91 // registrar should fail. | |
92 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | |
93 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_N, | |
94 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
95 | |
96 // Attempting to add an accelerator with the same id from a different | |
97 // registrar should be OK. | |
98 AcceleratorRegistrarPtr registrar_second; | |
99 ConnectToRegistrar(®istrar_second); | |
100 TestAcceleratorHandler handler_second(std::move(registrar_second)); | |
101 EXPECT_TRUE(handler_second.AttemptToInstallAccelerator( | |
102 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_N, | |
103 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
104 | |
105 // But attempting to add an accelerator with the same matcher should fail. | |
106 EXPECT_FALSE(handler_first.AttemptToInstallAccelerator( | |
107 3, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_N, | |
108 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
109 EXPECT_FALSE(handler_second.AttemptToInstallAccelerator( | |
110 3, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_N, | |
111 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
112 } | |
113 | |
114 // Tests that accelerators installed for a handler are automatically removed | |
115 // when the handler is destroyed. | |
116 TEST_F(AcceleratorRegistrarTest, AcceleratorsRemovedOnHandlerDestroy) { | |
117 AcceleratorRegistrarPtr registrar_first; | |
118 ConnectToRegistrar(®istrar_first); | |
119 scoped_ptr<TestAcceleratorHandler> handler_first( | |
120 new TestAcceleratorHandler(std::move(registrar_first))); | |
121 EXPECT_TRUE(handler_first->AttemptToInstallAccelerator( | |
122 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_T, | |
123 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
124 | |
125 AcceleratorRegistrarPtr registrar_second; | |
126 ConnectToRegistrar(®istrar_second); | |
127 TestAcceleratorHandler handler_second(std::move(registrar_second)); | |
128 // The attempt to install an accelerator with the same matcher will now fail. | |
129 EXPECT_FALSE(handler_second.AttemptToInstallAccelerator( | |
130 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_T, | |
131 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
132 | |
133 handler_first.reset(); | |
134 // Now that the first handler is destroyed, that should unregister the | |
135 // accelerator. So another attempt to install the accelerator with the same | |
136 // matcher should now pass. | |
137 EXPECT_TRUE(handler_second.AttemptToInstallAccelerator( | |
138 1, mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_T, | |
139 mus::mojom::EVENT_FLAGS_SHIFT_DOWN))); | |
140 } | |
141 | |
142 } // namespace wm | |
143 } // namespace mash | |
OLD | NEW |