debug_test.dart 7.2 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
// 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';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9

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

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

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

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

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

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

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

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

    debugPrintGestureArenaDiagnostics = false;
    debugPrint = oldCallback;
  });

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

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

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

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

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

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

85
    GestureBinding.instance.gestureArena.sweep(1);
86
    expect(log, hasLength(3));
87 88 89
    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.'));
90 91 92 93 94 95 96 97 98
    log.clear();

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

    debugPrintRecognizerCallbacksTrace = false;
    debugPrint = oldCallback;
  });

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

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

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

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

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

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

133
    GestureBinding.instance.gestureArena.sweep(1);
134 135
    expect(log, hasLength(5));
    expect(log[0], equalsIgnoringHashCodes('Gesture arena 1    ❙ Sweeping with 1 member.'));
136 137 138 139
    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.'));
140 141 142 143 144 145 146 147 148
    log.clear();

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

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

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