Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: mash/wm/accelerator_registrar_apptest.cc

Issue 1510713002: mash: Add a couple of apptests for AcceleratorRegistrar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove-failing-test Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mash/wm/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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(&registrar_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(&registrar_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 } // namespace wm
115 } // namespace mash
OLDNEW
« no previous file with comments | « mash/wm/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698