forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlighter_controller_unittest.cc
464 lines (389 loc) · 18.8 KB
/
highlighter_controller_unittest.cc
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/highlighter/highlighter_controller.h"
#include <memory>
#include "ash/components/fast_ink/fast_ink_points.h"
#include "ash/highlighter/highlighter_controller_test_api.h"
#include "ash/public/cpp/config.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/strings/stringprintf.h"
#include "ui/aura/window_tree_host.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/events/test/event_generator.h"
namespace ash {
namespace {
class HighlighterControllerTest : public AshTestBase {
public:
HighlighterControllerTest() = default;
~HighlighterControllerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
controller_test_api_ = std::make_unique<HighlighterControllerTestApi>(
Shell::Get()->highlighter_controller());
}
void TearDown() override {
// This needs to be called first to reset the controller state before the
// shell instance gets torn down.
controller_test_api_.reset();
AshTestBase::TearDown();
}
void UpdateDisplayAndWaitForCompositingEnded(
const std::string& display_specs) {
UpdateDisplay(display_specs);
ui::DrawWaiterForTest::WaitForCompositingEnded(
Shell::GetPrimaryRootWindow()->GetHost()->compositor());
}
protected:
void TraceRect(const gfx::Rect& rect) {
GetEventGenerator().MoveTouch(gfx::Point(rect.x(), rect.y()));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(rect.right(), rect.y()));
GetEventGenerator().MoveTouch(gfx::Point(rect.right(), rect.bottom()));
GetEventGenerator().MoveTouch(gfx::Point(rect.x(), rect.bottom()));
GetEventGenerator().MoveTouch(gfx::Point(rect.x(), rect.y()));
GetEventGenerator().ReleaseTouch();
// The the events above will trigger a frame, so wait until a new
// CompositorFrame is generated before terminating.
ui::DrawWaiterForTest::WaitForCompositingEnded(
Shell::GetPrimaryRootWindow()->GetHost()->compositor());
}
std::unique_ptr<HighlighterControllerTestApi> controller_test_api_;
private:
DISALLOW_COPY_AND_ASSIGN(HighlighterControllerTest);
};
} // namespace
// Test to ensure the class responsible for drawing the highlighter pointer
// receives points from stylus movements as expected.
TEST_F(HighlighterControllerTest, HighlighterRenderer) {
// The highlighter pointer mode only works with stylus.
GetEventGenerator().EnterPenPointerMode();
// When disabled the highlighter pointer should not be showing.
GetEventGenerator().MoveTouch(gfx::Point(1, 1));
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
// Verify that by enabling the mode, the highlighter pointer should still not
// be showing.
controller_test_api_->SetEnabled(true);
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
// Verify moving the stylus 4 times will not display the highlighter pointer.
GetEventGenerator().MoveTouch(gfx::Point(2, 2));
GetEventGenerator().MoveTouch(gfx::Point(3, 3));
GetEventGenerator().MoveTouch(gfx::Point(4, 4));
GetEventGenerator().MoveTouch(gfx::Point(5, 5));
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
// Verify pressing the stylus will show the highlighter pointer and add a
// point but will not activate fading out.
GetEventGenerator().PressTouch();
EXPECT_TRUE(controller_test_api_->IsShowingHighlighter());
EXPECT_FALSE(controller_test_api_->IsFadingAway());
EXPECT_EQ(1, controller_test_api_->points().GetNumberOfPoints());
// Verify dragging the stylus 2 times will add 2 more points.
GetEventGenerator().MoveTouch(gfx::Point(6, 6));
GetEventGenerator().MoveTouch(gfx::Point(7, 7));
EXPECT_EQ(3, controller_test_api_->points().GetNumberOfPoints());
// Verify releasing the stylus still shows the highlighter pointer, which is
// fading away.
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->IsShowingHighlighter());
EXPECT_TRUE(controller_test_api_->IsFadingAway());
// Verify that disabling the mode right after the gesture completion does not
// hide the highlighter pointer immediately but lets it play out the
// animation.
controller_test_api_->SetEnabled(false);
EXPECT_TRUE(controller_test_api_->IsShowingHighlighter());
EXPECT_TRUE(controller_test_api_->IsFadingAway());
// Verify that disabling the mode mid-gesture hides the highlighter pointer
// immediately.
controller_test_api_->DestroyPointerView();
controller_test_api_->SetEnabled(true);
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(6, 6));
EXPECT_TRUE(controller_test_api_->IsShowingHighlighter());
controller_test_api_->SetEnabled(false);
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
// Verify that the highlighter pointer does not add points while disabled.
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(8, 8));
GetEventGenerator().ReleaseTouch();
GetEventGenerator().MoveTouch(gfx::Point(9, 9));
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
// Verify that the highlighter pointer does not get shown if points are not
// coming from the stylus, even when enabled.
GetEventGenerator().ExitPenPointerMode();
controller_test_api_->SetEnabled(true);
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(10, 10));
GetEventGenerator().MoveTouch(gfx::Point(11, 11));
EXPECT_FALSE(controller_test_api_->IsShowingHighlighter());
GetEventGenerator().ReleaseTouch();
}
// Test to ensure the class responsible for drawing the highlighter pointer
// handles prediction as expected when it receives points from stylus movements.
TEST_F(HighlighterControllerTest, HighlighterPrediction) {
controller_test_api_->SetEnabled(true);
// The highlighter pointer mode only works with stylus.
GetEventGenerator().EnterPenPointerMode();
GetEventGenerator().PressTouch();
EXPECT_TRUE(controller_test_api_->IsShowingHighlighter());
EXPECT_EQ(1, controller_test_api_->points().GetNumberOfPoints());
// Initial press event shouldn't generate any predicted points as there's no
// history to use for prediction.
EXPECT_EQ(0, controller_test_api_->predicted_points().GetNumberOfPoints());
// Verify dragging the stylus 3 times will add some predicted points.
GetEventGenerator().MoveTouch(gfx::Point(10, 10));
GetEventGenerator().MoveTouch(gfx::Point(20, 20));
GetEventGenerator().MoveTouch(gfx::Point(30, 30));
EXPECT_NE(0, controller_test_api_->predicted_points().GetNumberOfPoints());
// Verify predicted points are in the right direction.
for (const auto& point : controller_test_api_->predicted_points().points()) {
EXPECT_LT(30, point.location.x());
EXPECT_LT(30, point.location.y());
}
}
// Test that stylus gestures are correctly recognized by HighlighterController.
TEST_F(HighlighterControllerTest, HighlighterGestures) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
// A non-horizontal stroke is not recognized
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(100, 100));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(200, 200));
GetEventGenerator().ReleaseTouch();
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
// An almost horizontal stroke is recognized
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(100, 100));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(300, 102));
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
// Horizontal stroke selection rectangle should:
// have the same horizontal center line as the stroke bounding box,
// be 4dp wider than the stroke bounding box,
// be exactly 14dp high.
EXPECT_EQ("98,94 204x14", controller_test_api_->selection().ToString());
// An insufficiently closed C-like shape is not recognized
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(100, 0));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(0, 0));
GetEventGenerator().MoveTouch(gfx::Point(0, 100));
GetEventGenerator().MoveTouch(gfx::Point(100, 100));
GetEventGenerator().ReleaseTouch();
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
// An almost closed G-like shape is recognized
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(200, 0));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(0, 0));
GetEventGenerator().MoveTouch(gfx::Point(0, 100));
GetEventGenerator().MoveTouch(gfx::Point(200, 100));
GetEventGenerator().MoveTouch(gfx::Point(200, 20));
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("0,0 200x100", controller_test_api_->selection().ToString());
// A closed diamond shape is recognized
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(100, 50));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(200, 150));
GetEventGenerator().MoveTouch(gfx::Point(100, 250));
GetEventGenerator().MoveTouch(gfx::Point(0, 150));
GetEventGenerator().MoveTouch(gfx::Point(100, 50));
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("0,50 200x200", controller_test_api_->selection().ToString());
}
TEST_F(HighlighterControllerTest, HighlighterGesturesScaled) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
const gfx::Rect original_rect(200, 100, 400, 300);
// Allow for rounding errors.
gfx::Rect inflated(original_rect);
inflated.Inset(-1, -1);
constexpr float display_scales[] = {1.f, 1.5f, 2.0f};
constexpr float ui_scales[] = {0.5f, 0.67f, 1.0f, 1.25f,
1.33f, 1.5f, 1.67f, 2.0f};
for (size_t i = 0; i < sizeof(display_scales) / sizeof(float); ++i) {
const float display_scale = display_scales[i];
for (size_t j = 0; j < sizeof(ui_scales) / sizeof(float); ++j) {
const float ui_scale = ui_scales[j];
std::string display_spec =
base::StringPrintf("1500x1000*%.2f@%.2f", display_scale, ui_scale);
SCOPED_TRACE(display_spec);
UpdateDisplayAndWaitForCompositingEnded(display_spec);
controller_test_api_->ResetSelection();
TraceRect(original_rect);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
const gfx::Rect selection = controller_test_api_->selection();
EXPECT_TRUE(inflated.Contains(selection));
EXPECT_TRUE(selection.Contains(original_rect));
}
}
}
// Test that stylus gesture recognition correctly handles display rotation
TEST_F(HighlighterControllerTest, HighlighterGesturesRotated) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
const gfx::Rect trace(200, 100, 400, 300);
// No rotation
UpdateDisplayAndWaitForCompositingEnded("1500x1000");
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("200,100 400x300", controller_test_api_->selection().ToString());
// Rotate to 90 degrees
UpdateDisplayAndWaitForCompositingEnded("1500x1000/r");
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("100,900 300x400", controller_test_api_->selection().ToString());
// Rotate to 180 degrees
UpdateDisplayAndWaitForCompositingEnded("1500x1000/u");
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("900,600 400x300", controller_test_api_->selection().ToString());
// Rotate to 270 degrees
UpdateDisplayAndWaitForCompositingEnded("1500x1000/l");
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("600,200 300x400", controller_test_api_->selection().ToString());
}
// Test that a stroke interrupted close to the screen edge is treated as
// contiguous.
TEST_F(HighlighterControllerTest, InterruptedStroke) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
UpdateDisplayAndWaitForCompositingEnded("1500x1000");
// An interrupted stroke close to the screen edge should be recognized as a
// contiguous stroke.
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(300, 100));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(0, 100));
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->IsWaitingToResumeStroke());
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
EXPECT_FALSE(controller_test_api_->IsFadingAway());
GetEventGenerator().MoveTouch(gfx::Point(0, 200));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(300, 200));
GetEventGenerator().ReleaseTouch();
EXPECT_FALSE(controller_test_api_->IsWaitingToResumeStroke());
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_EQ("0,100 300x100", controller_test_api_->selection().ToString());
// Repeat the same gesture, but simulate a timeout after the gap. This should
// force the gesture completion.
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(300, 100));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(0, 100));
GetEventGenerator().ReleaseTouch();
EXPECT_TRUE(controller_test_api_->IsWaitingToResumeStroke());
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
EXPECT_FALSE(controller_test_api_->IsFadingAway());
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_FALSE(controller_test_api_->IsWaitingToResumeStroke());
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(controller_test_api_->IsFadingAway());
}
// Test that the selection is never crossing the screen bounds.
TEST_F(HighlighterControllerTest, SelectionInsideScreen) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
constexpr float display_scales[] = {1.f, 1.5f, 2.0f};
for (size_t i = 0; i < sizeof(display_scales) / sizeof(float); ++i) {
std::string display_spec =
base::StringPrintf("1000x1000*%.2f", display_scales[i]);
SCOPED_TRACE(display_spec);
UpdateDisplayAndWaitForCompositingEnded(display_spec);
const gfx::Rect screen(0, 0, 1000, 1000);
// Rectangle completely offscreen.
controller_test_api_->ResetSelection();
TraceRect(gfx::Rect(-100, -100, 10, 10));
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
// Rectangle crossing the left edge.
controller_test_api_->ResetSelection();
TraceRect(gfx::Rect(-100, 100, 200, 200));
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
// Rectangle crossing the top edge.
controller_test_api_->ResetSelection();
TraceRect(gfx::Rect(100, -100, 200, 200));
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
// Rectangle crossing the right edge.
controller_test_api_->ResetSelection();
TraceRect(gfx::Rect(900, 100, 200, 200));
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
// Rectangle crossing the bottom edge.
controller_test_api_->ResetSelection();
TraceRect(gfx::Rect(100, 900, 200, 200));
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
// Horizontal stroke completely offscreen.
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(0, -100));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(1000, -100));
GetEventGenerator().ReleaseTouch();
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
// Horizontal stroke along the top edge of the screen.
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(0, 0));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(1000, 0));
GetEventGenerator().ReleaseTouch();
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
// Horizontal stroke along the bottom edge of the screen.
controller_test_api_->ResetSelection();
GetEventGenerator().MoveTouch(gfx::Point(0, 999));
GetEventGenerator().PressTouch();
GetEventGenerator().MoveTouch(gfx::Point(1000, 999));
GetEventGenerator().ReleaseTouch();
controller_test_api_->SimulateInterruptedStrokeTimeout();
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
EXPECT_TRUE(screen.Contains(controller_test_api_->selection()));
}
}
// Test that a detached client does not receive notifications.
TEST_F(HighlighterControllerTest, DetachedClient) {
controller_test_api_->SetEnabled(true);
GetEventGenerator().EnterPenPointerMode();
UpdateDisplayAndWaitForCompositingEnded("1500x1000");
const gfx::Rect trace(200, 100, 400, 300);
// Detach the client, no notifications should reach it.
controller_test_api_->DetachClient();
controller_test_api_->ResetEnabledState();
controller_test_api_->SetEnabled(false);
EXPECT_FALSE(controller_test_api_->HandleEnabledStateChangedCalled());
controller_test_api_->SetEnabled(true);
EXPECT_FALSE(controller_test_api_->HandleEnabledStateChangedCalled());
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_FALSE(controller_test_api_->HandleSelectionCalled());
// Attach the client again, notifications should be delivered normally.
controller_test_api_->AttachClient();
controller_test_api_->ResetEnabledState();
controller_test_api_->SetEnabled(false);
EXPECT_TRUE(controller_test_api_->HandleEnabledStateChangedCalled());
controller_test_api_->SetEnabled(true);
EXPECT_TRUE(controller_test_api_->HandleEnabledStateChangedCalled());
controller_test_api_->ResetSelection();
TraceRect(trace);
EXPECT_TRUE(controller_test_api_->HandleSelectionCalled());
}
} // namespace ash