waitFor method
Waits until test is true for screen, and returns that screen.
Throws TuiTestFailure with the last screen when timeout passes or
the run ends first, and rethrows an error the run ended with.
Implementation
Future<String> waitFor(
bool Function(String screen) test, {
Duration timeout = const Duration(seconds: 2),
}) async {
final clock = Stopwatch()..start();
while (true) {
final current = screen;
if (test(current)) return current;
if (!_running) {
await _run;
throw TuiTestFailure(
'the program ended before the screen matched.\n${_frame(current)}');
}
final left = timeout - clock.elapsed;
if (left <= Duration.zero) {
throw TuiTestFailure('waitFor timed out after '
'${timeout.inMilliseconds} ms.\n${_frame(current)}');
}
await _changed.future.timeout(left, onTimeout: () {});
}
}