debug_test.dart 7.08 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('debugPrintGestureArenaDiagnostics', (WidgetTester tester) async {
11 12 13 14
    PointerEvent event;
    debugPrintGestureArenaDiagnostics = true;
    final DebugPrintCallback oldCallback = debugPrint;
    final List<String> log = <String>[];
15
    debugPrint = (String? s, { int? wrapWidth }) { log.add(s ?? ''); };
16

17
    final TapGestureRecognizer tap = TapGestureRecognizer()
18 19 20 21 22 23
      ..onTapDown = (TapDownDetails details) { }
      ..onTapUp = (TapUpDetails details) { }
      ..onTap = () { }
      ..onTapCancel = () { };
    expect(log, isEmpty);

24
    event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0));
25
    tap.addPointer(event as PointerDownEvent);
26 27
    expect(log, hasLength(2));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ ★ Opening new gesture arena.'));
28
    expect(log[1], equalsIgnoringHashCodes('Gesture arena 1    ❙ Adding: TapGestureRecognizer#00000(state: ready, button: 1)'));
29 30
    log.clear();

31
    GestureBinding.instance.gestureArena.close(1);
32 33 34 35
    expect(log, hasLength(1));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ Closing with 1 member.'));
    log.clear();

36
    GestureBinding.instance.pointerRouter.route(event);
37 38
    expect(log, isEmpty);

39
    event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0));
40
    GestureBinding.instance.pointerRouter.route(event);
41 42
    expect(log, isEmpty);

43
    GestureBinding.instance.gestureArena.sweep(1);
44 45
    expect(log, hasLength(2));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ Sweeping with 1 member.'));
46
    expect(log[1], equalsIgnoringHashCodes('Gesture arena 1    ❙ Winner: TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1)'));
47 48 49 50 51 52 53 54 55
    log.clear();

    tap.dispose();
    expect(log, isEmpty);

    debugPrintGestureArenaDiagnostics = false;
    debugPrint = oldCallback;
  });

56
  testWidgets('debugPrintRecognizerCallbacksTrace', (WidgetTester tester) async {
57 58 59 60
    PointerEvent event;
    debugPrintRecognizerCallbacksTrace = true;
    final DebugPrintCallback oldCallback = debugPrint;
    final List<String> log = <String>[];
61
    debugPrint = (String? s, { int? wrapWidth }) { log.add(s ?? ''); };
62

63
    final TapGestureRecognizer tap = TapGestureRecognizer()
64 65 66 67 68 69
      ..onTapDown = (TapDownDetails details) { }
      ..onTapUp = (TapUpDetails details) { }
      ..onTap = () { }
      ..onTapCancel = () { };
    expect(log, isEmpty);

70
    event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0));
71
    tap.addPointer(event as PointerDownEvent);
72 73
    expect(log, isEmpty);

74
    GestureBinding.instance.gestureArena.close(1);
75 76
    expect(log, isEmpty);

77
    GestureBinding.instance.pointerRouter.route(event);
78 79
    expect(log, isEmpty);

80
    event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0));
81
    GestureBinding.instance.pointerRouter.route(event);
82 83
    expect(log, isEmpty);

84
    GestureBinding.instance.gestureArena.sweep(1);
85
    expect(log, hasLength(3));
86 87 88
    expect(log[0], equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1) calling onTapDown callback.'));
    expect(log[1], equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTapUp callback.'));
    expect(log[2], equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTap callback.'));
89 90 91 92 93 94 95 96 97
    log.clear();

    tap.dispose();
    expect(log, isEmpty);

    debugPrintRecognizerCallbacksTrace = false;
    debugPrint = oldCallback;
  });

98
  testWidgets('debugPrintGestureArenaDiagnostics and debugPrintRecognizerCallbacksTrace', (WidgetTester tester) async {
99 100 101 102 103
    PointerEvent event;
    debugPrintGestureArenaDiagnostics = true;
    debugPrintRecognizerCallbacksTrace = true;
    final DebugPrintCallback oldCallback = debugPrint;
    final List<String> log = <String>[];
104
    debugPrint = (String? s, { int? wrapWidth }) { log.add(s ?? ''); };
105

106
    final TapGestureRecognizer tap = TapGestureRecognizer()
107 108 109 110 111 112
      ..onTapDown = (TapDownDetails details) { }
      ..onTapUp = (TapUpDetails details) { }
      ..onTap = () { }
      ..onTapCancel = () { };
    expect(log, isEmpty);

113
    event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0));
114
    tap.addPointer(event as PointerDownEvent);
115 116
    expect(log, hasLength(2));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ ★ Opening new gesture arena.'));
117
    expect(log[1], equalsIgnoringHashCodes('Gesture arena 1    ❙ Adding: TapGestureRecognizer#00000(state: ready, button: 1)'));
118 119
    log.clear();

120
    GestureBinding.instance.gestureArena.close(1);
121 122 123 124
    expect(log, hasLength(1));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ Closing with 1 member.'));
    log.clear();

125
    GestureBinding.instance.pointerRouter.route(event);
126 127
    expect(log, isEmpty);

128
    event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0));
129
    GestureBinding.instance.pointerRouter.route(event);
130 131
    expect(log, isEmpty);

132
    GestureBinding.instance.gestureArena.sweep(1);
133 134
    expect(log, hasLength(5));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ Sweeping with 1 member.'));
135 136 137 138
    expect(log[1], equalsIgnoringHashCodes('Gesture arena 1    ❙ Winner: TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1)'));
    expect(log[2], equalsIgnoringHashCodes('                   ❙ TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1) calling onTapDown callback.'));
    expect(log[3], equalsIgnoringHashCodes('                   ❙ TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTapUp callback.'));
    expect(log[4], equalsIgnoringHashCodes('                   ❙ TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTap callback.'));
139 140 141 142 143 144 145 146 147
    log.clear();

    tap.dispose();
    expect(log, isEmpty);

    debugPrintGestureArenaDiagnostics = false;
    debugPrintRecognizerCallbacksTrace = false;
    debugPrint = oldCallback;
  });
148 149

  test('TapGestureRecognizer _sentTapDown toString', () {
150 151
    final TapGestureRecognizer tap = TapGestureRecognizer()
      ..onTap = () {}; // Add a callback so that event can be added
152
    expect(tap.toString(), equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: ready)'));
153
    const PointerDownEvent event = PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0));
154
    tap.addPointer(event);
155 156
    tap.didExceedDeadline();
    expect(tap.toString(), equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: possible, button: 1, sent tap down)'));
157
    GestureBinding.instance.gestureArena.close(1);
158
    tap.dispose();
159
  });
160
}