forked from bdlukaa/fluent_ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip_test.dart
More file actions
341 lines (294 loc) · 9.63 KB
/
tooltip_test.dart
File metadata and controls
341 lines (294 loc) · 9.63 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import 'dart:ui';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'app_test.dart';
void main() {
testWidgets('Tooltip shows message on long press', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Center(
child: Tooltip(
message: 'Test Tooltip',
triggerMode: TooltipTriggerMode.longPress,
child: Button(
child: const Text('Long Press Me'),
onPressed: () {},
),
),
),
),
),
);
expect(find.text('Test Tooltip'), findsNothing);
await tester.longPress(find.byType(Button));
await tester.pumpAndSettle();
expect(find.text('Test Tooltip'), findsOneWidget);
});
testWidgets('Tooltip shows message on hover', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Center(
child: Tooltip(
message: 'Test Tooltip',
child: Button(
key: const Key('Button'),
child: const Text('Hover Me'),
onPressed: () {},
),
),
),
),
),
);
expect(find.text('Test Tooltip'), findsNothing);
// Create a mouse gesture and hover over the button
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await tester.pump();
await gesture.moveTo(tester.getCenter(find.byKey(const Key('Button'))));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Test Tooltip'), findsOneWidget);
await gesture.moveTo(Offset.zero);
await tester.pumpAndSettle();
expect(find.text('Test Tooltip'), findsNothing);
});
testWidgets('Tooltip honors style waitDuration on hover', (tester) async {
await tester.pumpWidget(
wrapApp(
child: const ScaffoldPage(
content: Center(
child: Tooltip(
message: 'Styled delay tooltip',
style: TooltipThemeData(waitDuration: Duration(seconds: 10)),
child: Button(
key: Key('StyledDelayButton'),
onPressed: null,
child: Text('Hover Me'),
),
),
),
),
),
);
expect(find.text('Styled delay tooltip'), findsNothing);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await tester.pump();
await gesture.moveTo(
tester.getCenter(find.byKey(const Key('StyledDelayButton'))),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 9999));
expect(find.text('Styled delay tooltip'), findsNothing);
await tester.pump(const Duration(milliseconds: 1));
expect(find.text('Styled delay tooltip'), findsOneWidget);
});
testWidgets('Tooltip dismisses when clicking outside', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Column(
children: [
Tooltip(
message: 'Tooltip 1',
child: Button(child: const Text('Button 1'), onPressed: () {}),
),
Button(child: const Text('Button 2'), onPressed: () {}),
],
),
),
),
);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await tester.pumpAndSettle();
await gesture.moveTo(tester.getCenter(find.text('Button 1')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Tooltip 1'), findsOneWidget);
await tester.tap(find.text('Button 2'));
await tester.pumpAndSettle();
expect(find.text('Tooltip 1'), findsNothing);
});
testWidgets('Tooltip with richMessage displays correctly', (tester) async {
const richMessage = TextSpan(
text: 'Rich ',
children: [
TextSpan(
text: 'Tooltip',
style: TextStyle(fontWeight: FontWeight.bold),
),
],
);
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Center(
child: Tooltip(
richMessage: richMessage,
child: Button(child: const Text('Tap Me'), onPressed: () {}),
),
),
),
),
);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await gesture.moveTo(tester.getCenter(find.text('Tap Me')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Rich Tooltip'), findsOneWidget);
});
testWidgets('Tooltip.dismissAllToolTips works correctly', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Column(
children: [
Tooltip(
message: 'Tooltip 1',
child: Button(child: const Text('Button 1'), onPressed: () {}),
),
Tooltip(
message: 'Tooltip 2',
child: Button(child: const Text('Button 2'), onPressed: () {}),
),
],
),
),
),
);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await gesture.moveTo(tester.getCenter(find.text('Button 1')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Tooltip 1'), findsOneWidget);
await gesture.moveTo(tester.getCenter(find.text('Button 2')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Tooltip 2'), findsOneWidget);
final dismissed = Tooltip.dismissAllToolTips();
await tester.pumpAndSettle();
expect(dismissed, isTrue);
expect(find.text('Tooltip 1'), findsNothing);
expect(find.text('Tooltip 2'), findsNothing);
});
testWidgets('Tooltip respects visibility', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: TooltipVisibility(
visible: false,
child: Center(
child: Tooltip(
message: 'Should not show',
child: Button(child: const Text('Tap Me'), onPressed: () {}),
),
),
),
),
),
);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await gesture.moveTo(tester.getCenter(find.text('Tap Me')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text('Should not show'), findsNothing);
});
testWidgets('Tooltip with empty message does not show', (tester) async {
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Center(
child: Tooltip(
message: '',
child: Button(child: const Text('Tap Me'), onPressed: () {}),
),
),
),
),
);
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await gesture.moveTo(tester.getCenter(find.text('Tap Me')));
await tester.pumpAndSettle(const Duration(milliseconds: 1500));
expect(find.text(''), findsNothing);
});
testWidgets('Tooltip does not rebuild child when mouse tracker changes', (
tester,
) async {
// Regression test: when the mouse enters or leaves the window,
// _mouseIsConnected used to change via setState, which would add/remove
// the MouseRegion wrapper and cause the child widget to lose its state.
var initStateCount = 0;
await tester.pumpWidget(
wrapApp(
child: ScaffoldPage(
content: Center(
child: Tooltip(
message: 'tooltip message',
child: _InitStateCounter(onInitState: () => initStateCount++),
),
),
),
),
);
expect(initStateCount, 1);
// Simulate a mouse being added (hover over the widget)
final gesture = await tester.createGesture(
kind: PointerDeviceKind.mouse,
pointer: 1,
);
await gesture.addPointer(location: Offset.zero);
await tester.pump();
// The child's initState should NOT be called again
expect(initStateCount, 1);
// Move the mouse over the tooltip child
await gesture.moveTo(tester.getCenter(find.byType(_InitStateCounter)));
await tester.pump();
expect(initStateCount, 1);
// Remove the mouse pointer (simulate cursor leaving window)
await gesture.removePointer();
await tester.pump();
// The child should still preserve its state
expect(initStateCount, 1);
});
}
/// A helper widget that counts how many times initState is called.
class _InitStateCounter extends StatefulWidget {
const _InitStateCounter({required this.onInitState});
final VoidCallback onInitState;
@override
State<_InitStateCounter> createState() => _InitStateCounterState();
}
class _InitStateCounterState extends State<_InitStateCounter> {
@override
void initState() {
super.initState();
widget.onInitState();
}
@override
Widget build(BuildContext context) {
return const Text('Stateful child');
}
}