File size: 6,029 Bytes
88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 | 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 | // Copyright 2014 The Flutter 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 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';
// This test is very fragile and bypasses some zone-related checks.
// It is written this way to verify some invariants that would otherwise
// be difficult to check.
// Do not use this test as a guide for writing good Flutter code.
class TestBinding extends WidgetsFlutterBinding {
@override
void initInstances() {
super.initInstances();
_instance = this;
}
@override
bool debugCheckZone(String entryPoint) {
return true;
}
static TestBinding get instance => BindingBase.checkInstance(_instance);
static TestBinding? _instance;
static TestBinding ensureInitialized() {
if (TestBinding._instance == null) {
TestBinding();
}
return TestBinding.instance;
}
}
class CountButton extends StatefulWidget {
const CountButton({super.key});
@override
State<CountButton> createState() => _CountButtonState();
}
class _CountButtonState extends State<CountButton> {
int counter = 0;
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: Text('Counter $counter'),
onPressed: () {
setState(() {
counter += 1;
});
},
);
}
}
class AnimateSample extends StatefulWidget {
const AnimateSample({super.key});
@override
State<AnimateSample> createState() => _AnimateSampleState();
}
class _AnimateSampleState extends State<AnimateSample> with SingleTickerProviderStateMixin {
late AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: const Duration(seconds: 1))..forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, _) => Text('Value: ${_controller.value}'),
);
}
}
void main() {
TestBinding.ensureInitialized();
test('Test pump on LiveWidgetController', () async {
runApp(const MaterialApp(home: Center(child: CountButton())));
await SchedulerBinding.instance.endOfFrame;
final WidgetController controller = LiveWidgetController(WidgetsBinding.instance);
await controller.tap(find.text('Counter 0'));
expect(find.text('Counter 0'), findsOneWidget);
expect(find.text('Counter 1'), findsNothing);
await controller.pump();
expect(find.text('Counter 0'), findsNothing);
expect(find.text('Counter 1'), findsOneWidget);
});
test('Test pumpAndSettle on LiveWidgetController', () async {
runApp(const MaterialApp(home: Center(child: AnimateSample())));
await SchedulerBinding.instance.endOfFrame;
final WidgetController controller = LiveWidgetController(WidgetsBinding.instance);
expect(find.text('Value: 1.0'), findsNothing);
await controller.pumpAndSettle();
expect(find.text('Value: 1.0'), findsOneWidget);
});
test('Input event array on LiveWidgetController', () async {
final List<String> logs = <String>[];
runApp(
MaterialApp(
home: Listener(
onPointerDown: (PointerDownEvent event) => logs.add('down ${event.buttons}'),
onPointerMove: (PointerMoveEvent event) => logs.add('move ${event.buttons}'),
onPointerUp: (PointerUpEvent event) => logs.add('up ${event.buttons}'),
child: const Text('test'),
),
),
);
await SchedulerBinding.instance.endOfFrame;
final WidgetController controller = LiveWidgetController(WidgetsBinding.instance);
final Offset location = controller.getCenter(find.text('test'));
final List<PointerEventRecord> records = <PointerEventRecord>[
PointerEventRecord(Duration.zero, <PointerEvent>[
// Typically PointerAddedEvent is not used in testers, but for records
// captured on a device it is usually what starts a gesture.
PointerAddedEvent(position: location),
PointerDownEvent(position: location, buttons: kSecondaryMouseButton, pointer: 1),
]),
...<PointerEventRecord>[
for (
Duration t = const Duration(milliseconds: 5);
t < const Duration(milliseconds: 80);
t += const Duration(milliseconds: 16)
)
PointerEventRecord(t, <PointerEvent>[
PointerMoveEvent(
timeStamp: t - const Duration(milliseconds: 1),
position: location,
buttons: kSecondaryMouseButton,
pointer: 1,
),
]),
],
PointerEventRecord(const Duration(milliseconds: 80), <PointerEvent>[
PointerUpEvent(
timeStamp: const Duration(milliseconds: 79),
position: location,
buttons: kSecondaryMouseButton,
pointer: 1,
),
]),
];
final List<Duration> timeDiffs = await controller.handlePointerEventRecord(records);
expect(timeDiffs.length, records.length);
for (final Duration diff in timeDiffs) {
// Allow some freedom of time delay in real world.
// TODO(pdblasi-google): The expected wiggle room should be -1, but occasional
// results were reaching -6. This assert has been adjusted to reduce flakiness,
// but the root cause is still unknown. (https://github.com/flutter/flutter/issues/109638)
assert(
diff.inMilliseconds > -7,
'timeDiffs were: $timeDiffs (offending time was ${diff.inMilliseconds}ms)',
);
}
const String b = '$kSecondaryMouseButton';
expect(logs.first, 'down $b');
for (int i = 1; i < logs.length - 1; i++) {
expect(logs[i], 'move $b');
}
expect(logs.last, 'up $b');
});
}
|