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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
ash / accelerators / rapid_key_sequence_recorder_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/rapid_key_sequence_recorder.h"
#include "ash/public/cpp/accelerators_util.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/types/event_type.h"
namespace ash {
class RapidKeySequenceRecorderTest : public AshTestBase {
public:
RapidKeySequenceRecorderTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
void SetUp() override {
AshTestBase::SetUp();
rapid_key_sequence_recorder_ = std::make_unique<RapidKeySequenceRecorder>();
histogram_tester_ = std::make_unique<base::HistogramTester>();
}
void TearDown() override {
rapid_key_sequence_recorder_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<RapidKeySequenceRecorder> rapid_key_sequence_recorder_;
std::unique_ptr<base::HistogramTester> histogram_tester_;
void AdvanceClock(base::TimeDelta time) {
task_environment()->AdvanceClock(time);
}
base::TimeTicks GetNowTimestamp() { return task_environment()->NowTicks(); }
const ui::KeyEvent LeftShiftKeyEvent() {
return ui::KeyEvent(ui::EventType::kKeyPressed, ui::VKEY_LSHIFT,
ui::DomCode::SHIFT_LEFT, ui::EF_NONE,
GetNowTimestamp());
}
const ui::KeyEvent RightShiftKeyEvent() {
return ui::KeyEvent(ui::EventType::kKeyPressed, ui::VKEY_RSHIFT,
ui::DomCode::SHIFT_RIGHT, ui::EF_NONE,
GetNowTimestamp());
}
};
TEST_F(RapidKeySequenceRecorderTest, SingleShiftPress) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// No samples should have been recorded for any of the above events.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, LeftShiftTappedTwice) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(100));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
histogram_tester_->ExpectUniqueTimeSample(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(100), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 1);
// Right shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, RightShiftTappedTwice) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(100));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", base::Milliseconds(100),
1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 1);
// Left shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, RightShiftOtherKeyPressedTogether) {
const auto press_other_key_event =
ui::KeyEvent(ui::EventType::kKeyPressed, ui::VKEY_A, ui::DomCode::US_A,
ui::EF_NONE, GetNowTimestamp());
const auto release_other_key_event =
ui::KeyEvent(ui::EventType::kKeyReleased, ui::VKEY_A, ui::DomCode::US_A,
ui::EF_NONE, GetNowTimestamp());
const auto release_shift_key_event =
ui::KeyEvent(ui::EventType::kKeyReleased, ui::VKEY_RSHIFT,
ui::DomCode::SHIFT_RIGHT, ui::EF_NONE, GetNowTimestamp());
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
press_other_key_event);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
release_other_key_event);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
release_shift_key_event);
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
release_shift_key_event);
// Right shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
// Left shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, AlternatingShiftLocations) {
// RightShift then LeftShift:
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(100));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
// LeftShift then RightShift:
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(100));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// No metrics should have been recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, ThreeRapidPressesLeftShift) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(73));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// When three keys are consecutively pressed, only one metric for the first
// two presses should be emitted.
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 1);
// Right shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, ThreeRapidPressesRightShift) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(73));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// When three keys are consecutively pressed, only one metric for the first
// two presses should be emitted.
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 1);
// Left shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, FiveRapidPressesLeftShift) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(60));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(70));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(80));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// When five keys are consecutively pressed, two metrics (for the first
// four presses) should be emitted.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 2);
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(70), 1);
// Right shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, FiveRapidPressesRightShift) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(60));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(70));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(80));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// When five keys are consecutively pressed, two metrics (for the first
// four presses) should be emitted.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 2);
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTimeBucketCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", base::Milliseconds(70), 1);
// Left shift metric should not be recorded.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, TimingWindowCloseToLimit) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(499));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// Only record one sample (for the 499ms delay double-tap).
histogram_tester_->ExpectUniqueTimeSample(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(499), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 1);
}
TEST_F(RapidKeySequenceRecorderTest, TimingWindowAtLimit) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(500));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, TimingWindowExceedsLimit) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(2000));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, StartWithShiftThenWait) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(3000));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
histogram_tester_->ExpectUniqueTimeSample(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 1);
}
TEST_F(RapidKeySequenceRecorderTest, StartWithShiftThenWaitThenThreeShifts) {
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(3000));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(75));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// Only one sample should be recorded.
histogram_tester_->ExpectUniqueTimeSample(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", base::Milliseconds(50), 1);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 1);
}
TEST_F(RapidKeySequenceRecorderTest, OtherKeys) {
ui::KeyEvent alpha_key_no_modifier(ui::EventType::kKeyPressed, ui::VKEY_C,
ui::EF_NONE, GetNowTimestamp());
// Other key, then left shift
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
alpha_key_no_modifier);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
// Left shift, then other key
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(LeftShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
alpha_key_no_modifier);
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
// Other key, then right shift
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
alpha_key_no_modifier);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
// Right shift, then other key
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
alpha_key_no_modifier);
// Wait a few seconds to reset the double-tap window.
AdvanceClock(base::Milliseconds(3000));
// Right shift, then other key, then right shift again
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
alpha_key_no_modifier);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(RightShiftKeyEvent());
// No samples should have been recorded for any of the above events.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, ShiftAndOtherModifiers) {
ui::KeyEvent ctrl_shift_t(ui::EventType::kKeyPressed, ui::VKEY_T,
ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
GetNowTimestamp());
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(ctrl_shift_t);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(ctrl_shift_t);
// No samples should have been recorded for any of the above events.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, ShiftAndAlpha) {
ui::KeyEvent shift_t(ui::EventType::kKeyPressed, ui::VKEY_T,
ui::EF_SHIFT_DOWN, GetNowTimestamp());
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(shift_t);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(shift_t);
// No samples should have been recorded for any of the above events.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
TEST_F(RapidKeySequenceRecorderTest, UnknownKeyAndShift) {
ui::KeyEvent non_keypress_with_shift(ui::EventType::kKeyPressed,
ui::VKEY_UNKNOWN, ui::DomCode::NONE,
ui::EF_SHIFT_DOWN, GetNowTimestamp());
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
non_keypress_with_shift);
AdvanceClock(base::Milliseconds(50));
rapid_key_sequence_recorder_->OnPrerewriteKeyInputEvent(
non_keypress_with_shift);
// No samples should have been recorded for any of the above events.
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapLeftShiftDuration", 0);
histogram_tester_->ExpectTotalCount(
"ChromeOS.Inputs.DoubleTapRightShiftDuration", 0);
}
} // namespace ash