gesture_binding_test.dart 6.13 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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.

import 'dart:ui' as ui;

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/gestures.dart';
9 10

import '../flutter_test_alternative.dart';
11

12
typedef HandleEventCallback = void Function(PointerEvent event);
13

14
class TestGestureFlutterBinding extends BindingBase with GestureBinding {
15 16 17 18 19 20 21
  HandleEventCallback callback;

  @override
  void handleEvent(PointerEvent event, HitTestEntry entry) {
    if (callback != null)
      callback(event);
    super.handleEvent(event, entry);
22
  }
23 24
}

25
TestGestureFlutterBinding _binding = TestGestureFlutterBinding();
26

27
void ensureTestGestureBinding() {
28
  _binding ??= TestGestureFlutterBinding();
29
  assert(GestureBinding.instance != null);
30 31 32
}

void main() {
33
  setUp(ensureTestGestureBinding);
34 35

  test('Pointer tap events', () {
36 37 38 39
    const ui.PointerDataPacket packet = ui.PointerDataPacket(
      data: <ui.PointerData>[
        ui.PointerData(change: ui.PointerChange.down),
        ui.PointerData(change: ui.PointerChange.up),
40 41
      ]
    );
42

43
    final List<PointerEvent> events = <PointerEvent>[];
44
    _binding.callback = events.add;
45

46
    ui.window.onPointerDataPacket(packet);
47
    expect(events.length, 2);
48 49 50 51
    expect(events[0].runtimeType, equals(PointerDownEvent));
    expect(events[1].runtimeType, equals(PointerUpEvent));
  });

52
  test('Pointer move events', () {
53 54 55 56 57
    const ui.PointerDataPacket packet = ui.PointerDataPacket(
      data: <ui.PointerData>[
        ui.PointerData(change: ui.PointerChange.down),
        ui.PointerData(change: ui.PointerChange.move),
        ui.PointerData(change: ui.PointerChange.up),
58 59
      ]
    );
60

61
    final List<PointerEvent> events = <PointerEvent>[];
62
    _binding.callback = events.add;
63

64
    ui.window.onPointerDataPacket(packet);
65 66 67 68 69 70
    expect(events.length, 3);
    expect(events[0].runtimeType, equals(PointerDownEvent));
    expect(events[1].runtimeType, equals(PointerMoveEvent));
    expect(events[2].runtimeType, equals(PointerUpEvent));
  });

71
  test('Synthetic move events', () {
72
    final ui.PointerDataPacket packet = ui.PointerDataPacket(
73
      data: <ui.PointerData>[
74
        ui.PointerData(
75
          change: ui.PointerChange.down,
76 77
          physicalX: 1.0 * ui.window.devicePixelRatio,
          physicalY: 3.0 * ui.window.devicePixelRatio,
78
        ),
79
        ui.PointerData(
80
          change: ui.PointerChange.up,
81 82
          physicalX: 10.0 * ui.window.devicePixelRatio,
          physicalY: 15.0 * ui.window.devicePixelRatio,
83 84 85
        ),
      ]
    );
86

87
    final List<PointerEvent> events = <PointerEvent>[];
88
    _binding.callback = events.add;
89

90
    ui.window.onPointerDataPacket(packet);
91 92 93 94 95 96 97
    expect(events.length, 3);
    expect(events[0].runtimeType, equals(PointerDownEvent));
    expect(events[1].runtimeType, equals(PointerMoveEvent));
    expect(events[1].delta, equals(const Offset(9.0, 12.0)));
    expect(events[2].runtimeType, equals(PointerUpEvent));
  });

98
  test('Pointer cancel events', () {
99 100 101 102
    const ui.PointerDataPacket packet = ui.PointerDataPacket(
      data: <ui.PointerData>[
        ui.PointerData(change: ui.PointerChange.down),
        ui.PointerData(change: ui.PointerChange.cancel),
103 104
      ]
    );
105

106
    final List<PointerEvent> events = <PointerEvent>[];
107
    _binding.callback = events.add;
108

109
    ui.window.onPointerDataPacket(packet);
110 111 112 113 114 115
    expect(events.length, 2);
    expect(events[0].runtimeType, equals(PointerDownEvent));
    expect(events[1].runtimeType, equals(PointerCancelEvent));
  });

  test('Can cancel pointers', () {
116 117 118 119
    const ui.PointerDataPacket packet = ui.PointerDataPacket(
      data: <ui.PointerData>[
        ui.PointerData(change: ui.PointerChange.down),
        ui.PointerData(change: ui.PointerChange.up),
120 121
      ]
    );
122

123
    final List<PointerEvent> events = <PointerEvent>[];
124 125 126 127 128 129
    _binding.callback = (PointerEvent event) {
      events.add(event);
      if (event is PointerDownEvent)
        _binding.cancelPointer(event.pointer);
    };

130
    ui.window.onPointerDataPacket(packet);
131
    expect(events.length, 2);
132 133 134
    expect(events[0].runtimeType, equals(PointerDownEvent));
    expect(events[1].runtimeType, equals(PointerCancelEvent));
  });
135 136

  test('Can expand add and hover pointers', () {
137 138 139 140 141 142
    const ui.PointerDataPacket packet = ui.PointerDataPacket(
      data: <ui.PointerData>[
        ui.PointerData(change: ui.PointerChange.add, device: 24),
        ui.PointerData(change: ui.PointerChange.hover, device: 24),
        ui.PointerData(change: ui.PointerChange.remove, device: 24),
        ui.PointerData(change: ui.PointerChange.hover, device: 24),
143 144 145
      ]
    );

146
    final List<PointerEvent> events = PointerEventConverter.expand(
147 148 149 150 151 152 153 154 155 156 157
      packet.data, ui.window.devicePixelRatio).toList();

    expect(events.length, 5);
    expect(events[0].runtimeType, equals(PointerAddedEvent));
    expect(events[1].runtimeType, equals(PointerHoverEvent));
    expect(events[2].runtimeType, equals(PointerRemovedEvent));
    expect(events[3].runtimeType, equals(PointerAddedEvent));
    expect(events[4].runtimeType, equals(PointerHoverEvent));
  });

  test('Synthetic hover and cancel for misplaced down and remove', () {
158
    final ui.PointerDataPacket packet = ui.PointerDataPacket(
159
      data: <ui.PointerData>[
160 161
        ui.PointerData(change: ui.PointerChange.add, device: 25, physicalX: 10.0 * ui.window.devicePixelRatio, physicalY: 10.0 * ui.window.devicePixelRatio),
        ui.PointerData(change: ui.PointerChange.down, device: 25, physicalX: 15.0 * ui.window.devicePixelRatio, physicalY: 17.0 * ui.window.devicePixelRatio),
162 163 164 165
        const ui.PointerData(change: ui.PointerChange.remove, device: 25),
      ]
    );

166
    final List<PointerEvent> events = PointerEventConverter.expand(
167 168 169 170 171
      packet.data, ui.window.devicePixelRatio).toList();

    expect(events.length, 5);
    expect(events[0].runtimeType, equals(PointerAddedEvent));
    expect(events[1].runtimeType, equals(PointerHoverEvent));
172
    expect(events[1].delta, equals(const Offset(5.0, 7.0)));
173 174 175 176
    expect(events[2].runtimeType, equals(PointerDownEvent));
    expect(events[3].runtimeType, equals(PointerCancelEvent));
    expect(events[4].runtimeType, equals(PointerRemovedEvent));
  });
177
}