1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
ash / accelerators / suspend_state_machine_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/accelerators/suspend_state_machine.h"
#include "ash/test/ash_test_base.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/devices/stylus_state.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/types/event_type.h"
#include "ui/ozone/public/stub_input_controller.h"
namespace ash::accelerators {
namespace {
class MockInputController : public ui::StubInputController {
public:
MOCK_METHOD(bool, AreAnyKeysPressed, (), (override));
};
// Key representation in test cases.
struct TestKeyEvent {
ui::EventType type;
ui::DomCode code;
ui::DomKey key;
ui::KeyboardCode keycode;
ui::EventFlags flags = ui::EF_NONE;
std::string ToString() const;
};
// Factory template of TestKeyEvents just to reduce a lot of code/data
// duplication.
template <ui::DomCode code,
ui::DomKey::Base key,
ui::KeyboardCode keycode,
ui::EventFlags modifier_flag = ui::EF_NONE,
ui::DomKey::Base shifted_key = key>
struct TestKey {
// Returns press key event.
static constexpr TestKeyEvent Pressed(ui::EventFlags flags = ui::EF_NONE) {
return {ui::EventType::kKeyPressed, code,
(flags & ui::EF_SHIFT_DOWN) ? shifted_key : key, keycode,
flags | modifier_flag};
}
// Returns release key event.
static constexpr TestKeyEvent Released(ui::EventFlags flags = ui::EF_NONE) {
// Note: modifier flag should not be present on release events.
return {ui::EventType::kKeyReleased, code,
(flags & ui::EF_SHIFT_DOWN) ? shifted_key : key, keycode, flags};
}
};
// Short cut of TestKey construction for Character keys.
template <ui::DomCode code,
char key,
ui::KeyboardCode keycode,
char shifted_key = key>
using TestCharKey = TestKey<code,
ui::DomKey::FromCharacter(key),
keycode,
ui::EF_NONE,
ui::DomKey::FromCharacter(shifted_key)>;
// Modifier keys.
using KeyLShift = TestKey<ui::DomCode::SHIFT_LEFT,
ui::DomKey::SHIFT,
ui::VKEY_SHIFT,
ui::EF_SHIFT_DOWN>;
using KeyRShift = TestKey<ui::DomCode::SHIFT_RIGHT,
ui::DomKey::SHIFT,
ui::VKEY_SHIFT,
ui::EF_SHIFT_DOWN>;
using KeyLMeta = TestKey<ui::DomCode::META_LEFT,
ui::DomKey::META,
ui::VKEY_LWIN,
ui::EF_COMMAND_DOWN>;
using KeyRMeta = TestKey<ui::DomCode::META_RIGHT,
ui::DomKey::META,
ui::VKEY_RWIN,
ui::EF_COMMAND_DOWN>;
using KeyLControl = TestKey<ui::DomCode::CONTROL_LEFT,
ui::DomKey::CONTROL,
ui::VKEY_CONTROL,
ui::EF_CONTROL_DOWN>;
using KeyRControl = TestKey<ui::DomCode::CONTROL_RIGHT,
ui::DomKey::CONTROL,
ui::VKEY_CONTROL,
ui::EF_CONTROL_DOWN>;
using KeyLAlt = TestKey<ui::DomCode::ALT_LEFT,
ui::DomKey::ALT,
ui::VKEY_MENU,
ui::EF_ALT_DOWN>;
using KeyRAlt = TestKey<ui::DomCode::ALT_RIGHT,
ui::DomKey::ALT,
ui::VKEY_MENU,
ui::EF_ALT_DOWN>;
// Character keys. Shift chars are based on US layout.
using KeyA = TestCharKey<ui::DomCode::US_A, 'a', ui::VKEY_A, 'A'>;
using KeyB = TestCharKey<ui::DomCode::US_B, 'b', ui::VKEY_B, 'B'>;
using KeyC = TestCharKey<ui::DomCode::US_C, 'c', ui::VKEY_C, 'C'>;
const bool kKeysPressed = true;
const bool kNoKeysPressed = false;
using EventTypeVariant = std::variant<TestKeyEvent, bool>;
using SuspendStateMachineEvent = SuspendStateMachine::SuspendStateMachineEvent;
} // namespace
class SuspendStateMachineTest
: public AshTestBase,
public testing::WithParamInterface<
std::tuple<ui::Accelerator, std::vector<EventTypeVariant>>> {
public:
void SetUp() override {
AshTestBase::SetUp();
std::tie(trigger_accelerator_, events_) = GetParam();
input_controller_ = std::make_unique<MockInputController>();
suspend_state_machine_ =
std::make_unique<SuspendStateMachine>(input_controller_.get());
}
void TearDown() override {
suspend_state_machine_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<SuspendStateMachine> suspend_state_machine_;
std::unique_ptr<MockInputController> input_controller_;
std::vector<EventTypeVariant> events_;
ui::Accelerator trigger_accelerator_;
};
class SuccessfulSuspendStateMachineTest : public SuspendStateMachineTest {};
INSTANTIATE_TEST_SUITE_P(
All,
SuccessfulSuspendStateMachineTest,
testing::ValuesIn(
std::vector<std::tuple<ui::Accelerator, std::vector<EventTypeVariant>>>{
// Standard activations of the suspend.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), kNoKeysPressed,
KeyA::Released()}},
{{ui::VKEY_B, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), kNoKeysPressed,
KeyB::Released()}},
{{ui::VKEY_C, ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN},
{kKeysPressed, KeyLControl::Released(), KeyLAlt::Released(),
kNoKeysPressed, KeyC::Released()}},
// Reversed ordering of releases, checks that ordering does not
// matter.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyA::Released(), kNoKeysPressed,
KeyLMeta::Released()}},
{{ui::VKEY_B, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyB::Released(), kNoKeysPressed,
KeyLMeta::Released()}},
{{ui::VKEY_C, ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN},
{kKeysPressed, KeyC::Released(), KeyLAlt::Released(),
kNoKeysPressed, KeyLControl::Released()}},
// Left vs right modifiers does not matter.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyRMeta::Released(), KeyLMeta::Released(),
kNoKeysPressed, KeyA::Released()}},
{{ui::VKEY_A, ui::EF_ALT_DOWN},
{kKeysPressed, KeyRAlt::Released(), KeyLAlt::Released(),
kNoKeysPressed, KeyA::Released()}},
{{ui::VKEY_A, ui::EF_CONTROL_DOWN},
{kKeysPressed, KeyLControl::Released(), KeyRControl::Released(),
kNoKeysPressed, KeyA::Released()}},
{{ui::VKEY_A, ui::EF_SHIFT_DOWN},
{kKeysPressed, KeyLShift::Released(), KeyRShift::Released(),
kNoKeysPressed, KeyA::Released()}},
// All modifiers in one accelerator.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN | ui::EF_ALT_DOWN |
ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN},
{kKeysPressed, KeyLControl::Released(), KeyLShift::Released(),
KeyLMeta::Released(), KeyRAlt::Released(), kNoKeysPressed,
KeyA::Released()}},
// No modifiers in accelerator.
{{ui::VKEY_A, ui::EF_NONE}, {kNoKeysPressed, KeyA::Released()}},
// Other keys currently held down, not triggered until all keys
// released.
{{ui::VKEY_A, ui::EF_ALT_DOWN},
{kKeysPressed, KeyLAlt::Released(), KeyA::Released(),
kNoKeysPressed, KeyRAlt::Released()}},
}));
TEST_P(SuccessfulSuspendStateMachineTest, SuspendTriggered) {
base::HistogramTester histogram_tester;
suspend_state_machine_->StartObservingToTriggerSuspend(trigger_accelerator_);
histogram_tester.ExpectBucketCount("ChromeOS.Inputs.SuspendStateMachine",
SuspendStateMachineEvent::kTriggered, 1);
for (const auto& event : events_) {
ASSERT_EQ(0, power_manager_client()->num_request_suspend_calls());
if (std::holds_alternative<bool>(event)) {
ON_CALL(*input_controller_, AreAnyKeysPressed())
.WillByDefault(testing::Return(std::get<bool>(event)));
continue;
} else {
const TestKeyEvent& test_event = std::get<TestKeyEvent>(event);
ui::KeyEvent key_event(test_event.type, test_event.keycode,
test_event.code, test_event.flags, test_event.key,
ui::EventTimeForNow());
suspend_state_machine_->OnEvent(&key_event);
}
}
EXPECT_EQ(1, power_manager_client()->num_request_suspend_calls());
histogram_tester.ExpectBucketCount("ChromeOS.Inputs.SuspendStateMachine",
SuspendStateMachineEvent::kSuspended, 1);
}
class CancelledSuspendStateMachineTest : public SuspendStateMachineTest {};
INSTANTIATE_TEST_SUITE_P(
All,
CancelledSuspendStateMachineTest,
testing::ValuesIn(
std::vector<std::tuple<ui::Accelerator, std::vector<EventTypeVariant>>>{
// Release a key not in the original accelerator.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), KeyB::Released(),
kNoKeysPressed, KeyA::Released()}},
// Release a modifier not in the original accelerator.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLControl::Released(), kNoKeysPressed,
KeyA::Released()}},
// Press a modifier again in the original accelerator.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), KeyLMeta::Pressed(),
KeyLMeta::Released(), kNoKeysPressed, KeyA::Released()}},
// Press a key not in the original accelerator.
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), KeyB::Pressed(),
KeyB::Released(), kNoKeysPressed, KeyA::Released()}},
{{ui::VKEY_A, ui::EF_COMMAND_DOWN},
{kKeysPressed, KeyLMeta::Released(), KeyB::Pressed(),
kNoKeysPressed, KeyA::Released()}},
}));
TEST_P(CancelledSuspendStateMachineTest, SuspendNotTriggered) {
base::HistogramTester histogram_tester;
suspend_state_machine_->StartObservingToTriggerSuspend(trigger_accelerator_);
histogram_tester.ExpectBucketCount("ChromeOS.Inputs.SuspendStateMachine",
SuspendStateMachineEvent::kTriggered, 1);
for (const auto& event : events_) {
ASSERT_EQ(0, power_manager_client()->num_request_suspend_calls());
if (std::holds_alternative<bool>(event)) {
ON_CALL(*input_controller_, AreAnyKeysPressed())
.WillByDefault(testing::Return(std::get<bool>(event)));
continue;
} else {
const TestKeyEvent& test_event = std::get<TestKeyEvent>(event);
ui::KeyEvent key_event(test_event.type, test_event.keycode,
test_event.code, test_event.flags, test_event.key,
ui::EventTimeForNow());
suspend_state_machine_->OnEvent(&key_event);
}
}
EXPECT_EQ(0, power_manager_client()->num_request_suspend_calls());
histogram_tester.ExpectBucketCount("ChromeOS.Inputs.SuspendStateMachine",
SuspendStateMachineEvent::kCancelled, 1);
}
} // namespace ash::accelerators