1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/foundation.dart'; |
6 | import 'package:flutter/gestures.dart'; |
7 | import 'package:flutter_test/flutter_test.dart'; |
8 | |
9 | void main() { |
10 | testWidgets('debugPrintGestureArenaDiagnostics' , (WidgetTester tester) async { |
11 | PointerEvent event; |
12 | debugPrintGestureArenaDiagnostics = true; |
13 | final DebugPrintCallback oldCallback = debugPrint; |
14 | final List<String> log = <String>[]; |
15 | debugPrint = (String? s, {int? wrapWidth}) { |
16 | log.add(s ?? '' ); |
17 | }; |
18 | |
19 | final TapGestureRecognizer tap = |
20 | TapGestureRecognizer() |
21 | ..onTapDown = (TapDownDetails details) {} |
22 | ..onTapUp = (TapUpDetails details) {} |
23 | ..onTap = () {} |
24 | ..onTapCancel = () {}; |
25 | expect(log, isEmpty); |
26 | |
27 | event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0)); |
28 | tap.addPointer(event as PointerDownEvent); |
29 | expect(log, hasLength(2)); |
30 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ ★ Opening new gesture arena.' )); |
31 | expect( |
32 | log[1], |
33 | equalsIgnoringHashCodes( |
34 | 'Gesture arena 1 ❙ Adding: TapGestureRecognizer#00000(state: ready, button: 1)' , |
35 | ), |
36 | ); |
37 | log.clear(); |
38 | |
39 | GestureBinding.instance.gestureArena.close(1); |
40 | expect(log, hasLength(1)); |
41 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ Closing with 1 member.' )); |
42 | log.clear(); |
43 | |
44 | GestureBinding.instance.pointerRouter.route(event); |
45 | expect(log, isEmpty); |
46 | |
47 | event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0)); |
48 | GestureBinding.instance.pointerRouter.route(event); |
49 | expect(log, isEmpty); |
50 | |
51 | GestureBinding.instance.gestureArena.sweep(1); |
52 | expect(log, hasLength(2)); |
53 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ Sweeping with 1 member.' )); |
54 | expect( |
55 | log[1], |
56 | equalsIgnoringHashCodes( |
57 | 'Gesture arena 1 ❙ Winner: TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1)' , |
58 | ), |
59 | ); |
60 | log.clear(); |
61 | |
62 | tap.dispose(); |
63 | expect(log, isEmpty); |
64 | |
65 | debugPrintGestureArenaDiagnostics = false; |
66 | debugPrint = oldCallback; |
67 | }); |
68 | |
69 | testWidgets('debugPrintRecognizerCallbacksTrace' , (WidgetTester tester) async { |
70 | PointerEvent event; |
71 | debugPrintRecognizerCallbacksTrace = true; |
72 | final DebugPrintCallback oldCallback = debugPrint; |
73 | final List<String> log = <String>[]; |
74 | debugPrint = (String? s, {int? wrapWidth}) { |
75 | log.add(s ?? '' ); |
76 | }; |
77 | |
78 | final TapGestureRecognizer tap = |
79 | TapGestureRecognizer() |
80 | ..onTapDown = (TapDownDetails details) {} |
81 | ..onTapUp = (TapUpDetails details) {} |
82 | ..onTap = () {} |
83 | ..onTapCancel = () {}; |
84 | expect(log, isEmpty); |
85 | |
86 | event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0)); |
87 | tap.addPointer(event as PointerDownEvent); |
88 | expect(log, isEmpty); |
89 | |
90 | GestureBinding.instance.gestureArena.close(1); |
91 | expect(log, isEmpty); |
92 | |
93 | GestureBinding.instance.pointerRouter.route(event); |
94 | expect(log, isEmpty); |
95 | |
96 | event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0)); |
97 | GestureBinding.instance.pointerRouter.route(event); |
98 | expect(log, isEmpty); |
99 | |
100 | GestureBinding.instance.gestureArena.sweep(1); |
101 | expect(log, hasLength(3)); |
102 | expect( |
103 | log[0], |
104 | equalsIgnoringHashCodes( |
105 | 'TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1) calling onTapDown callback.' , |
106 | ), |
107 | ); |
108 | expect( |
109 | log[1], |
110 | equalsIgnoringHashCodes( |
111 | 'TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTapUp callback.' , |
112 | ), |
113 | ); |
114 | expect( |
115 | log[2], |
116 | equalsIgnoringHashCodes( |
117 | 'TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTap callback.' , |
118 | ), |
119 | ); |
120 | log.clear(); |
121 | |
122 | tap.dispose(); |
123 | expect(log, isEmpty); |
124 | |
125 | debugPrintRecognizerCallbacksTrace = false; |
126 | debugPrint = oldCallback; |
127 | }); |
128 | |
129 | testWidgets('debugPrintGestureArenaDiagnostics and debugPrintRecognizerCallbacksTrace' , ( |
130 | WidgetTester tester, |
131 | ) async { |
132 | PointerEvent event; |
133 | debugPrintGestureArenaDiagnostics = true; |
134 | debugPrintRecognizerCallbacksTrace = true; |
135 | final DebugPrintCallback oldCallback = debugPrint; |
136 | final List<String> log = <String>[]; |
137 | debugPrint = (String? s, {int? wrapWidth}) { |
138 | log.add(s ?? '' ); |
139 | }; |
140 | |
141 | final TapGestureRecognizer tap = |
142 | TapGestureRecognizer() |
143 | ..onTapDown = (TapDownDetails details) {} |
144 | ..onTapUp = (TapUpDetails details) {} |
145 | ..onTap = () {} |
146 | ..onTapCancel = () {}; |
147 | expect(log, isEmpty); |
148 | |
149 | event = const PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0)); |
150 | tap.addPointer(event as PointerDownEvent); |
151 | expect(log, hasLength(2)); |
152 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ ★ Opening new gesture arena.' )); |
153 | expect( |
154 | log[1], |
155 | equalsIgnoringHashCodes( |
156 | 'Gesture arena 1 ❙ Adding: TapGestureRecognizer#00000(state: ready, button: 1)' , |
157 | ), |
158 | ); |
159 | log.clear(); |
160 | |
161 | GestureBinding.instance.gestureArena.close(1); |
162 | expect(log, hasLength(1)); |
163 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ Closing with 1 member.' )); |
164 | log.clear(); |
165 | |
166 | GestureBinding.instance.pointerRouter.route(event); |
167 | expect(log, isEmpty); |
168 | |
169 | event = const PointerUpEvent(pointer: 1, position: Offset(12.0, 8.0)); |
170 | GestureBinding.instance.pointerRouter.route(event); |
171 | expect(log, isEmpty); |
172 | |
173 | GestureBinding.instance.gestureArena.sweep(1); |
174 | expect(log, hasLength(5)); |
175 | expect(log[0], equalsIgnoringHashCodes('Gesture arena 1 ❙ Sweeping with 1 member.' )); |
176 | expect( |
177 | log[1], |
178 | equalsIgnoringHashCodes( |
179 | 'Gesture arena 1 ❙ Winner: TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1)' , |
180 | ), |
181 | ); |
182 | expect( |
183 | log[2], |
184 | equalsIgnoringHashCodes( |
185 | ' ❙ TapGestureRecognizer#00000(state: ready, finalPosition: Offset(12.0, 8.0), button: 1) calling onTapDown callback.' , |
186 | ), |
187 | ); |
188 | expect( |
189 | log[3], |
190 | equalsIgnoringHashCodes( |
191 | ' ❙ TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTapUp callback.' , |
192 | ), |
193 | ); |
194 | expect( |
195 | log[4], |
196 | equalsIgnoringHashCodes( |
197 | ' ❙ TapGestureRecognizer#00000(state: ready, won arena, finalPosition: Offset(12.0, 8.0), button: 1, sent tap down) calling onTap callback.' , |
198 | ), |
199 | ); |
200 | log.clear(); |
201 | |
202 | tap.dispose(); |
203 | expect(log, isEmpty); |
204 | |
205 | debugPrintGestureArenaDiagnostics = false; |
206 | debugPrintRecognizerCallbacksTrace = false; |
207 | debugPrint = oldCallback; |
208 | }); |
209 | |
210 | test('TapGestureRecognizer _sentTapDown toString' , () { |
211 | final TapGestureRecognizer tap = |
212 | TapGestureRecognizer()..onTap = () {}; // Add a callback so that event can be added |
213 | expect(tap.toString(), equalsIgnoringHashCodes('TapGestureRecognizer#00000(state: ready)' )); |
214 | const PointerDownEvent event = PointerDownEvent(pointer: 1, position: Offset(10.0, 10.0)); |
215 | tap.addPointer(event); |
216 | tap.didExceedDeadline(); |
217 | expect( |
218 | tap.toString(), |
219 | equalsIgnoringHashCodes( |
220 | 'TapGestureRecognizer#00000(state: possible, button: 1, sent tap down)' , |
221 | ), |
222 | ); |
223 | GestureBinding.instance.gestureArena.close(1); |
224 | tap.dispose(); |
225 | }); |
226 | } |
227 | |