resampler_test.dart 33.3 KB
Newer Older
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui';

import 'package:flutter/gestures.dart';

import '../flutter_test_alternative.dart';

void main() {
  PointerEvent _createSimulatedPointerAddedEvent(
      int timeStampUs,
      double x,
      double y,
  ) {
    return PointerAddedEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
    );
  }

  PointerEvent _createSimulatedPointerRemovedEvent(
      int timeStampUs,
      double x,
      double y,
  ) {
    return PointerRemovedEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
    );
  }

  PointerEvent _createSimulatedPointerDownEvent(
      int timeStampUs,
      double x,
      double y,
  ) {
    return PointerDownEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
    );
  }

  PointerEvent _createSimulatedPointerMoveEvent(
      int timeStampUs,
      double x,
      double y,
      double deltaX,
      double deltaY,
  ) {
    return PointerMoveEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
        delta: Offset(deltaX, deltaY),
    );
  }

59 60 61 62 63 64 65 66 67 68 69 70 71 72
  PointerEvent _createSimulatedPointerHoverEvent(
      int timeStampUs,
      double x,
      double y,
      double deltaX,
      double deltaY,
  ) {
    return PointerHoverEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
        delta: Offset(deltaX, deltaY),
    );
  }

73 74 75 76 77 78 79 80 81 82 83 84 85 86
  PointerEvent _createSimulatedPointerUpEvent(
      int timeStampUs,
      double x,
      double y,
  ) {
    return PointerUpEvent(
        timeStamp: Duration(microseconds: timeStampUs),
        position: Offset(x, y),
    );
  }

  test('basic', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 50.0);
87 88 89 90 91 92 93 94
    final PointerEvent event1 = _createSimulatedPointerHoverEvent(2000, 10.0, 40.0, 10.0, -10.0);
    final PointerEvent event2 = _createSimulatedPointerDownEvent(2000, 10.0, 40.0);
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(3000, 20.0, 30.0, 10.0, -10.0);
    final PointerEvent event4 = _createSimulatedPointerMoveEvent(4000, 30.0, 20.0, 10.0, -10.0);
    final PointerEvent event5 = _createSimulatedPointerUpEvent(4000, 30.0, 20.0);
    final PointerEvent event6 = _createSimulatedPointerHoverEvent(5000, 40.0, 10.0, 10.0, -10.0);
    final PointerEvent event7 = _createSimulatedPointerHoverEvent(6000, 50.0, 0.0, 10.0, -10.0);
    final PointerEvent event8 = _createSimulatedPointerRemovedEvent(6000, 50.0, 0.0);
95 96 97 98 99 100 101

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
      ..addEvent(event4)
102 103 104 105
      ..addEvent(event5)
      ..addEvent(event6)
      ..addEvent(event7)
      ..addEvent(event8);
106 107 108

    final List<PointerEvent> result = <PointerEvent>[];

109
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
110 111 112 113

    // No pointer event should have been returned yet.
    expect(result.isEmpty, true);

114
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
115 116 117 118 119 120 121 122

    // Add pointer event should have been returned.
    expect(result.length, 1);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 5.0);
    expect(result[0].position.dy, 45.0);

123
    resampler.sample(const Duration(microseconds: 2500), Duration.zero, result.add);
124

125 126
    // Hover and down pointer events should have been returned.
    expect(result.length, 3);
127
    expect(result[1].timeStamp, const Duration(microseconds: 2500));
128
    expect(result[1] is PointerHoverEvent, true);
129 130
    expect(result[1].position.dx, 15.0);
    expect(result[1].position.dy, 35.0);
131 132 133 134 135 136
    expect(result[1].delta.dx, 10.0);
    expect(result[1].delta.dy, -10.0);
    expect(result[2].timeStamp, const Duration(microseconds: 2500));
    expect(result[2] is PointerDownEvent, true);
    expect(result[2].position.dx, 15.0);
    expect(result[2].position.dy, 35.0);
137

138
    resampler.sample(const Duration(microseconds: 3500), Duration.zero, result.add);
139 140

    // Move pointer event should have been returned.
141 142 143 144 145 146 147
    expect(result.length, 4);
    expect(result[3].timeStamp, const Duration(microseconds: 3500));
    expect(result[3] is PointerMoveEvent, true);
    expect(result[3].position.dx, 25.0);
    expect(result[3].position.dy, 25.0);
    expect(result[3].delta.dx, 10.0);
    expect(result[3].delta.dy, -10.0);
148

149
    resampler.sample(const Duration(microseconds: 4500), Duration.zero, result.add);
150

151 152 153 154 155 156 157 158 159 160 161 162
    // Move and up pointer events should have been returned.
    expect(result.length, 6);
    expect(result[4].timeStamp, const Duration(microseconds: 4500));
    expect(result[4] is PointerMoveEvent, true);
    expect(result[4].position.dx, 35.0);
    expect(result[4].position.dy, 15.0);
    expect(result[4].delta.dx, 10.0);
    expect(result[4].delta.dy, -10.0);
    expect(result[5].timeStamp, const Duration(microseconds: 4500));
    expect(result[5] is PointerUpEvent, true);
    expect(result[5].position.dx, 35.0);
    expect(result[5].position.dy, 15.0);
163

164
    resampler.sample(const Duration(microseconds: 5500), Duration.zero, result.add);
165

166 167
    // Hover pointer event should have been returned.
    expect(result.length, 7);
168 169 170 171 172 173
    expect(result[6].timeStamp, const Duration(microseconds: 5500));
    expect(result[6] is PointerHoverEvent, true);
    expect(result[6].position.dx, 45.0);
    expect(result[6].position.dy, 5.0);
    expect(result[6].delta.dx, 10.0);
    expect(result[6].delta.dy, -10.0);
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    resampler.sample(const Duration(microseconds: 6500), Duration.zero, result.add);

    // Hover and removed pointer events should have been returned.
    expect(result.length, 9);
    expect(result[7].timeStamp, const Duration(microseconds: 6500));
    expect(result[7] is PointerHoverEvent, true);
    expect(result[7].position.dx, 50.0);
    expect(result[7].position.dy, 0.0);
    expect(result[7].delta.dx, 5.0);
    expect(result[7].delta.dy, -5.0);
    expect(result[8].timeStamp, const Duration(microseconds: 6500));
    expect(result[8] is PointerRemovedEvent, true);
    expect(result[8].position.dx, 50.0);
    expect(result[8].position.dy, 0.0);

    resampler.sample(const Duration(microseconds: 7500), Duration.zero, result.add);
191 192

    // No pointer event should have been returned.
193
    expect(result.length, 9);
194 195 196 197 198
  });

  test('stream', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 50.0);
199 200 201 202 203 204 205 206
    final PointerEvent event1 = _createSimulatedPointerHoverEvent(2000, 10.0, 40.0, 10.0, -10.0);
    final PointerEvent event2 = _createSimulatedPointerDownEvent(2000, 10.0, 40.0);
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(3000, 20.0, 30.0, 10.0, -10.0);
    final PointerEvent event4 = _createSimulatedPointerMoveEvent(4000, 30.0, 20.0, 10.0, -10.0);
    final PointerEvent event5 = _createSimulatedPointerUpEvent(4000, 30.0, 20.0);
    final PointerEvent event6 = _createSimulatedPointerHoverEvent(5000, 40.0, 10.0, 10.0, -10.0);
    final PointerEvent event7 = _createSimulatedPointerHoverEvent(6000, 50.0, 0.0, 10.0, -10.0);
    final PointerEvent event8 = _createSimulatedPointerRemovedEvent(6000, 50.0, 0.0);
207 208 209 210 211 212 213 214 215

    resampler.addEvent(event0);

    //
    // Initial sample time a 0.5 ms.
    //

    final List<PointerEvent> result = <PointerEvent>[];

216
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
217 218 219 220

    // No pointer event should have been returned yet.
    expect(result.isEmpty, true);

221 222 223
    resampler
      ..addEvent(event1)
      ..addEvent(event2);
224

225
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
226 227 228 229 230 231 232 233

    // No pointer event should have been returned yet.
    expect(result.isEmpty, true);

    //
    // Advance sample time to 1.5 ms.
    //

234
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
235

236
    // Added pointer event should have been returned.
237 238 239 240 241 242
    expect(result.length, 1);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 5.0);
    expect(result[0].position.dy, 45.0);

243
    resampler.addEvent(event3);
244

245
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
246 247 248 249 250 251 252 253

    // No more pointer events should have been returned.
    expect(result.length, 1);

    //
    // Advance sample time to 2.5 ms.
    //

254
    resampler.sample(const Duration(microseconds: 2500), Duration.zero, result.add);
255

256 257
    // Hover and down pointer events should have been returned.
    expect(result.length, 3);
258
    expect(result[1].timeStamp, const Duration(microseconds: 2500));
259
    expect(result[1] is PointerHoverEvent, true);
260 261
    expect(result[1].position.dx, 15.0);
    expect(result[1].position.dy, 35.0);
262 263 264 265 266 267
    expect(result[1].delta.dx, 10.0);
    expect(result[1].delta.dy, -10.0);
    expect(result[2].timeStamp, const Duration(microseconds: 2500));
    expect(result[2] is PointerDownEvent, true);
    expect(result[2].position.dx, 15.0);
    expect(result[2].position.dy, 35.0);
268

269 270 271
    resampler
      ..addEvent(event4)
      ..addEvent(event5);
272

273
    resampler.sample(const Duration(microseconds: 2500), Duration.zero, result.add);
274 275

    // No more pointer events should have been returned.
276
    expect(result.length, 3);
277 278 279 280 281

    //
    // Advance sample time to 3.5 ms.
    //

282
    resampler.sample(const Duration(microseconds: 3500), Duration.zero, result.add);
283 284

    // Move pointer event should have been returned.
285 286 287 288 289 290 291
    expect(result.length, 4);
    expect(result[3].timeStamp, const Duration(microseconds: 3500));
    expect(result[3] is PointerMoveEvent, true);
    expect(result[3].position.dx, 25.0);
    expect(result[3].position.dy, 25.0);
    expect(result[3].delta.dx, 10.0);
    expect(result[3].delta.dy, -10.0);
292

293
    resampler.addEvent(event6);
294

295
    resampler.sample(const Duration(microseconds: 3500), Duration.zero, result.add);
296 297

    // No more pointer events should have been returned.
298
    expect(result.length, 4);
299 300 301 302 303

    //
    // Advance sample time to 4.5 ms.
    //

304
    resampler.sample(const Duration(microseconds: 4500), Duration.zero, result.add);
305

306 307 308 309 310 311 312 313 314 315 316 317
    // Move and up pointer events should have been returned.
    expect(result.length, 6);
    expect(result[4].timeStamp, const Duration(microseconds: 4500));
    expect(result[4] is PointerMoveEvent, true);
    expect(result[4].position.dx, 35.0);
    expect(result[4].position.dy, 15.0);
    expect(result[4].delta.dx, 10.0);
    expect(result[4].delta.dy, -10.0);
    expect(result[5].timeStamp, const Duration(microseconds: 4500));
    expect(result[5] is PointerUpEvent, true);
    expect(result[5].position.dx, 35.0);
    expect(result[5].position.dy, 15.0);
318

319 320 321
    resampler
      ..addEvent(event7)
      ..addEvent(event8);
322

323
    resampler.sample(const Duration(microseconds: 4500), Duration.zero, result.add);
324 325

    // No more pointer events should have been returned.
326
    expect(result.length, 6);
327 328 329 330 331

    //
    // Advance sample time to 5.5 ms.
    //

332
    resampler.sample(const Duration(microseconds: 5500), Duration.zero, result.add);
333

334 335
    // Hover pointer event should have been returned.
    expect(result.length, 7);
336 337 338 339 340 341
    expect(result[6].timeStamp, const Duration(microseconds: 5500));
    expect(result[6] is PointerHoverEvent, true);
    expect(result[6].position.dx, 45.0);
    expect(result[6].position.dy, 5.0);
    expect(result[6].delta.dx, 10.0);
    expect(result[6].delta.dy, -10.0);
342 343 344 345 346

    //
    // Advance sample time to 6.5 ms.
    //

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    resampler.sample(const Duration(microseconds: 6500), Duration.zero, result.add);

    // Hover and removed pointer event should have been returned.
    expect(result.length, 9);
    expect(result[7].timeStamp, const Duration(microseconds: 6500));
    expect(result[7] is PointerHoverEvent, true);
    expect(result[7].position.dx, 50.0);
    expect(result[7].position.dy, 0.0);
    expect(result[7].delta.dx, 5.0);
    expect(result[7].delta.dy, -5.0);
    expect(result[8].timeStamp, const Duration(microseconds: 6500));
    expect(result[8] is PointerRemovedEvent, true);
    expect(result[8].position.dx, 50.0);
    expect(result[8].position.dy, 0.0);

    //
    // Advance sample time to 7.5 ms.
    //

    resampler.sample(const Duration(microseconds: 7500), Duration.zero, result.add);
367 368

    // No pointer events should have been returned.
369
    expect(result.length, 9);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  });

  test('quick tap', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerUpEvent(1000, 0.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerRemovedEvent(1000, 0.0, 0.0);

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3);

    final List<PointerEvent> result = <PointerEvent>[];

387
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
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

    // All pointer events should have been returned.
    expect(result.length, 4);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 0.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 1500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 0.0);
    expect(result[1].position.dy, 0.0);
    expect(result[2].timeStamp, const Duration(microseconds: 1500));
    expect(result[2] is PointerUpEvent, true);
    expect(result[2].position.dx, 0.0);
    expect(result[2].position.dy, 0.0);
    expect(result[3].timeStamp, const Duration(microseconds: 1500));
    expect(result[3] is PointerRemovedEvent, true);
    expect(result[3].position.dx, 0.0);
    expect(result[3].position.dy, 0.0);
  });

  test('advance slowly', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(2000, 10.0, 0.0, 10.0, 0.0);
414 415 416
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(3000, 20.0, 0.0, 10.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerUpEvent(3000, 20.0, 0.0);
    final PointerEvent event5 = _createSimulatedPointerRemovedEvent(3000, 20.0, 0.0);
417 418 419 420 421 422

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
423 424
      ..addEvent(event4)
      ..addEvent(event5);
425 426 427

    final List<PointerEvent> result = <PointerEvent>[];

428
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
429 430 431 432 433 434 435 436 437 438 439 440

    // Added and down pointer events should have been returned.
    expect(result.length, 2);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 5.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 1500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 5.0);
    expect(result[1].position.dy, 0.0);

441
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
442 443 444 445

    // No pointer events should have been returned.
    expect(result.length, 2);

446
    resampler.sample(const Duration(microseconds: 1750), Duration.zero, result.add);
447 448 449 450 451 452 453 454 455 456

    // Move pointer event should have been returned.
    expect(result.length, 3);
    expect(result[2].timeStamp, const Duration(microseconds: 1750));
    expect(result[2] is PointerMoveEvent, true);
    expect(result[2].position.dx, 7.5);
    expect(result[2].position.dy, 0.0);
    expect(result[2].delta.dx, 2.5);
    expect(result[2].delta.dy, 0.0);

457
    resampler.sample(const Duration(microseconds: 2000), Duration.zero, result.add);
458 459 460 461 462 463 464 465 466 467

    // Another move pointer event should have been returned.
    expect(result.length, 4);
    expect(result[3].timeStamp, const Duration(microseconds: 2000));
    expect(result[3] is PointerMoveEvent, true);
    expect(result[3].position.dx, 10.0);
    expect(result[3].position.dy, 0.0);
    expect(result[3].delta.dx, 2.5);
    expect(result[3].delta.dy, 0.0);

468
    resampler.sample(const Duration(microseconds: 3000), Duration.zero, result.add);
469

470 471
    // Move, up and removed pointer events should have been returned.
    expect(result.length, 7);
472
    expect(result[4].timeStamp, const Duration(microseconds: 3000));
473
    expect(result[4] is PointerMoveEvent, true);
474
    expect(result[4].position.dx, 20.0);
475
    expect(result[4].position.dy, 0.0);
476
    expect(result[4].delta.dx, 10.0);
477
    expect(result[4].delta.dy, 0.0);
478
    expect(result[5].timeStamp, const Duration(microseconds: 3000));
479
    expect(result[5] is PointerUpEvent, true);
480
    expect(result[5].position.dx, 20.0);
481
    expect(result[5].position.dy, 0.0);
482
    expect(result[6].timeStamp, const Duration(microseconds: 3000));
483
    expect(result[6] is PointerRemovedEvent, true);
484
    expect(result[6].position.dx, 20.0);
485
    expect(result[6].position.dy, 0.0);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  });

  test('advance fast', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(2000, 5.0, 0.0, 5.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(3000, 20.0, 0.0, 15.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerUpEvent(4000, 30.0, 0.0);
    final PointerEvent event5 = _createSimulatedPointerRemovedEvent(4000, 30.0, 0.0);

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
      ..addEvent(event4)
      ..addEvent(event5);

    final List<PointerEvent> result = <PointerEvent>[];

507
    resampler.sample(const Duration(microseconds: 2500), Duration.zero, result.add);
508

509
    // Addeds and down pointer events should have been returned.
510 511 512 513 514 515 516 517 518 519
    expect(result.length, 2);
    expect(result[0].timeStamp, const Duration(microseconds: 2500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 12.5);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 2500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 12.5);
    expect(result[1].position.dy, 0.0);

520
    resampler.sample(const Duration(microseconds: 5500), Duration.zero, result.add);
521

522 523
    // Move, up and removed pointer events should have been returned.
    expect(result.length, 5);
524
    expect(result[2].timeStamp, const Duration(microseconds: 5500));
525
    expect(result[2] is PointerMoveEvent, true);
526 527
    expect(result[2].position.dx, 30.0);
    expect(result[2].position.dy, 0.0);
528 529
    expect(result[2].delta.dx, 17.5);
    expect(result[2].delta.dy, 0.0);
530
    expect(result[3].timeStamp, const Duration(microseconds: 5500));
531
    expect(result[3] is PointerUpEvent, true);
532 533
    expect(result[3].position.dx, 30.0);
    expect(result[3].position.dy, 0.0);
534 535 536 537
    expect(result[4].timeStamp, const Duration(microseconds: 5500));
    expect(result[4] is PointerRemovedEvent, true);
    expect(result[4].position.dx, 30.0);
    expect(result[4].position.dy, 0.0);
538

539
    resampler.sample(const Duration(microseconds: 6500), Duration.zero, result.add);
540 541

    // No pointer events should have been returned.
542
    expect(result.length, 5);
543 544 545 546 547 548 549 550
  });

  test('skip', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(2000, 10.0, 0.0, 10.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerUpEvent(3000, 10.0, 0.0);
551 552 553 554 555
    final PointerEvent event4 = _createSimulatedPointerHoverEvent(4000, 20.0, 0.0, 10.0, 0.0);
    final PointerEvent event5 = _createSimulatedPointerDownEvent(4000, 20.0, 0.0);
    final PointerEvent event6 = _createSimulatedPointerMoveEvent(5000, 30.0, 0.0, 10.0, 0.0);
    final PointerEvent event7 = _createSimulatedPointerUpEvent(5000, 30.0, 0.0);
    final PointerEvent event8 = _createSimulatedPointerRemovedEvent(5000, 30.0, 0.0);
556 557 558 559 560 561 562 563

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
      ..addEvent(event4)
      ..addEvent(event5)
564 565 566
      ..addEvent(event6)
      ..addEvent(event7)
      ..addEvent(event8);
567 568 569

    final List<PointerEvent> result = <PointerEvent>[];

570
    resampler.sample(const Duration(microseconds: 1500), Duration.zero, result.add);
571 572 573 574 575 576 577 578 579 580 581 582

    // Added and down pointer events should have been returned.
    expect(result.length, 2);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 5.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 1500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 5.0);
    expect(result[1].position.dy, 0.0);

583
    resampler.sample(const Duration(microseconds: 5500), Duration.zero, result.add);
584 585

    // All remaining pointer events should have been returned.
586
    expect(result.length, 7);
587
    expect(result[2].timeStamp, const Duration(microseconds: 5500));
588
    expect(result[2] is PointerMoveEvent, true);
589
    expect(result[2].position.dx, 30.0);
590
    expect(result[2].position.dy, 0.0);
591
    expect(result[2].delta.dx, 25.0);
592
    expect(result[2].delta.dy, 0.0);
593
    expect(result[3].timeStamp, const Duration(microseconds: 5500));
594
    expect(result[3] is PointerUpEvent, true);
595
    expect(result[3].position.dx, 30.0);
596
    expect(result[3].position.dy, 0.0);
597
    expect(result[4].timeStamp, const Duration(microseconds: 5500));
598
    expect(result[4] is PointerDownEvent, true);
599
    expect(result[4].position.dx, 30.0);
600
    expect(result[4].position.dy, 0.0);
601
    expect(result[5].timeStamp, const Duration(microseconds: 5500));
602
    expect(result[5] is PointerUpEvent, true);
603
    expect(result[5].position.dx, 30.0);
604
    expect(result[5].position.dy, 0.0);
605
    expect(result[6].timeStamp, const Duration(microseconds: 5500));
606
    expect(result[6] is PointerRemovedEvent, true);
607
    expect(result[6].position.dx, 30.0);
608
    expect(result[6].position.dy, 0.0);
609

610
    resampler.sample(const Duration(microseconds: 6500), Duration.zero, result.add);
611 612

    // No pointer events should have been returned.
613
    expect(result.length, 7);
614 615 616 617 618 619
  });

  test('skip all', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
620 621 622
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(4000, 30.0, 0.0, 30.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerUpEvent(4000, 30.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerRemovedEvent(4000, 30.0, 0.0);
623 624 625 626 627

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
628 629
      ..addEvent(event3)
      ..addEvent(event4);
630 631 632

    final List<PointerEvent> result = <PointerEvent>[];

633
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
634 635 636 637

    // No pointer events should have been returned.
    expect(result.isEmpty, true);

638
    resampler.sample(const Duration(microseconds: 5500), Duration.zero, result.add);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

    // All remaining pointer events should have been returned.
    expect(result.length, 4);
    expect(result[0].timeStamp, const Duration(microseconds: 5500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 30.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 5500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 30.0);
    expect(result[1].position.dy, 0.0);
    expect(result[2].timeStamp, const Duration(microseconds: 5500));
    expect(result[2] is PointerUpEvent, true);
    expect(result[2].position.dx, 30.0);
    expect(result[2].position.dy, 0.0);
    expect(result[3].timeStamp, const Duration(microseconds: 5500));
    expect(result[3] is PointerRemovedEvent, true);
    expect(result[3].position.dx, 30.0);
    expect(result[3].position.dy, 0.0);

659
    resampler.sample(const Duration(microseconds: 6500), Duration.zero, result.add);
660 661 662 663 664 665 666 667 668 669

    // No pointer events should have been returned.
    expect(result.length, 4);
  });

  test('stop', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(2000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(3000, 10.0, 0.0, 10.0, 0.0);
670 671 672
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(4000, 20.0, 0.0, 10.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerUpEvent(4000, 20.0, 0.0);
    final PointerEvent event5 = _createSimulatedPointerRemovedEvent(5000, 20.0, 0.0);
673 674 675 676 677 678

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
679 680
      ..addEvent(event4)
      ..addEvent(event5);
681 682 683

    final List<PointerEvent> result = <PointerEvent>[];

684
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
685 686 687 688 689 690 691 692

    // No pointer events should have been returned.
    expect(result.isEmpty, true);

    resampler.stop(result.add);

    // All pointer events should have been returned with orignal
    // time stamps and positions.
693
    expect(result.length, 6);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    expect(result[0].timeStamp, const Duration(microseconds: 1000));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 0.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 2000));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 0.0);
    expect(result[1].position.dy, 0.0);
    expect(result[2].timeStamp, const Duration(microseconds: 3000));
    expect(result[2] is PointerMoveEvent, true);
    expect(result[2].position.dx, 10.0);
    expect(result[2].position.dy, 0.0);
    expect(result[2].delta.dx, 10.0);
    expect(result[2].delta.dy, 0.0);
    expect(result[3].timeStamp, const Duration(microseconds: 4000));
709
    expect(result[3] is PointerMoveEvent, true);
710 711
    expect(result[3].position.dx, 20.0);
    expect(result[3].position.dy, 0.0);
712 713 714 715
    expect(result[3].delta.dx, 10.0);
    expect(result[3].delta.dy, 0.0);
    expect(result[4].timeStamp, const Duration(microseconds: 4000));
    expect(result[4] is PointerUpEvent, true);
716 717
    expect(result[4].position.dx, 20.0);
    expect(result[4].position.dy, 0.0);
718 719 720 721
    expect(result[5].timeStamp, const Duration(microseconds: 5000));
    expect(result[5] is PointerRemovedEvent, true);
    expect(result[5].position.dx, 20.0);
    expect(result[5].position.dy, 0.0);
722

723
    resampler.sample(const Duration(microseconds: 10000), Duration.zero, result.add);
724 725

    // No pointer events should have been returned.
726
    expect(result.length, 6);
727
  });
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

  test('synthetic move', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(2000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(3000, 10.0, 0.0, 10.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerUpEvent(4000, 10.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerRemovedEvent(5000, 10.0, 0.0);

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
      ..addEvent(event4);

    final List<PointerEvent> result = <PointerEvent>[];

746
    resampler.sample(const Duration(microseconds: 500), Duration.zero, result.add);
747 748 749 750

    // No pointer events should have been returned.
    expect(result.isEmpty, true);

751
    resampler.sample(const Duration(microseconds: 2000), Duration.zero, result.add);
752 753 754 755 756 757 758 759 760 761 762 763

    // Added and down pointer events should have been returned.
    expect(result.length, 2);
    expect(result[0].timeStamp, const Duration(microseconds: 2000));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 0.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 2000));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 0.0);
    expect(result[1].position.dy, 0.0);

764
    resampler.sample(const Duration(microseconds: 5000), Duration.zero, result.add);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

    // All remaining pointer events and a synthetic move event should
    // have been returned.
    expect(result.length, 5);
    expect(result[2].timeStamp, const Duration(microseconds: 5000));
    expect(result[2] is PointerMoveEvent, true);
    expect(result[2].position.dx, 10.0);
    expect(result[2].position.dy, 0.0);
    expect(result[2].delta.dx, 10.0);
    expect(result[2].delta.dy, 0.0);
    expect(result[3].timeStamp, const Duration(microseconds: 5000));
    expect(result[3] is PointerUpEvent, true);
    expect(result[3].position.dx, 10.0);
    expect(result[3].position.dy, 0.0);
    expect(result[4].timeStamp, const Duration(microseconds: 5000));
    expect(result[4] is PointerRemovedEvent, true);
    expect(result[4].position.dx, 10.0);
    expect(result[4].position.dy, 0.0);

784
    resampler.sample(const Duration(microseconds: 10000), Duration.zero, result.add);
785 786 787 788

    // No pointer events should have been returned.
    expect(result.length, 5);
  });
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

  test('next sample time', () {
    final PointerEventResampler resampler = PointerEventResampler();
    final PointerEvent event0 = _createSimulatedPointerAddedEvent(1000, 0.0, 0.0);
    final PointerEvent event1 = _createSimulatedPointerDownEvent(1000, 0.0, 0.0);
    final PointerEvent event2 = _createSimulatedPointerMoveEvent(2000, 10.0, 0.0, 10.0, 0.0);
    final PointerEvent event3 = _createSimulatedPointerMoveEvent(3000, 20.0, 0.0, 10.0, 0.0);
    final PointerEvent event4 = _createSimulatedPointerUpEvent(3000, 20.0, 0.0);
    final PointerEvent event5 = _createSimulatedPointerHoverEvent(4000, 30.0, 0.0, 10.0, 0.0);
    final PointerEvent event6 = _createSimulatedPointerRemovedEvent(4000, 30.0, 0.0);

    resampler
      ..addEvent(event0)
      ..addEvent(event1)
      ..addEvent(event2)
      ..addEvent(event3)
      ..addEvent(event4)
      ..addEvent(event5)
      ..addEvent(event6);

    final List<PointerEvent> result = <PointerEvent>[];

    Duration sampleTime = const Duration(microseconds: 500);
    Duration nextSampleTime = const Duration(microseconds: 1500);
    resampler.sample(sampleTime, nextSampleTime, result.add);

    // No pointer events should have been returned.
    expect(result.isEmpty, true);

    sampleTime = nextSampleTime;
    nextSampleTime = const Duration(microseconds: 2500);
    resampler.sample(sampleTime, nextSampleTime, result.add);

    // Added and down pointer events should have been returned.
    expect(result.length, 2);
    expect(result[0].timeStamp, const Duration(microseconds: 1500));
    expect(result[0] is PointerAddedEvent, true);
    expect(result[0].position.dx, 5.0);
    expect(result[0].position.dy, 0.0);
    expect(result[1].timeStamp, const Duration(microseconds: 1500));
    expect(result[1] is PointerDownEvent, true);
    expect(result[1].position.dx, 5.0);
    expect(result[1].position.dy, 0.0);

    sampleTime = nextSampleTime;
    nextSampleTime = const Duration(microseconds: 3500);
    resampler.sample(sampleTime, nextSampleTime, result.add);

    // Move and up pointer events should have been returned.
    expect(result.length, 4);
    expect(result[2].timeStamp, const Duration(microseconds: 2500));
    expect(result[2] is PointerMoveEvent, true);
    expect(result[2].position.dx, 15.0);
    expect(result[2].position.dy, 0.0);
    expect(result[2].delta.dx, 10.0);
    expect(result[2].delta.dy, 0.0);
    expect(result[3].timeStamp, const Duration(microseconds: 2500));
    expect(result[3] is PointerUpEvent, true);
    expect(result[3].position.dx, 15.0);
    expect(result[3].position.dy, 0.0);

    sampleTime = nextSampleTime;
    nextSampleTime = const Duration(microseconds: 4500);
    resampler.sample(sampleTime, nextSampleTime, result.add);

    // All remaining pointer events should have been returned.
    expect(result.length, 6);
    expect(result[4].timeStamp, const Duration(microseconds: 3500));
    expect(result[4] is PointerHoverEvent, true);
    expect(result[4].position.dx, 25.0);
    expect(result[4].position.dy, 0.0);
    expect(result[4].delta.dx, 10.0);
    expect(result[4].delta.dy, 0.0);
    expect(result[5].timeStamp, const Duration(microseconds: 3500));
    expect(result[5] is PointerRemovedEvent, true);
    expect(result[5].position.dx, 25.0);
    expect(result[5].position.dy, 0.0);

    resampler.sample(const Duration(microseconds: 10000), Duration.zero, result.add);

    // No pointer events should have been returned.
    expect(result.length, 6);
  });
872
}