Commit 586ab30c authored by Yegor's avatar Yegor

Merge pull request #2126 from yjbanov/sample-driver-test-waits

wait for text to change in the sample driver test
parents ccc9a25a d2c94990
...@@ -19,7 +19,7 @@ Future<dynamic> retry(Action action, Duration timeout, ...@@ -19,7 +19,7 @@ Future<dynamic> retry(Action action, Duration timeout,
assert(timeout != null); assert(timeout != null);
assert(pauseBetweenRetries != null); assert(pauseBetweenRetries != null);
Stopwatch sw = new Stopwatch()..start(); Stopwatch sw = stopwatchFactory()..start();
dynamic result = null; dynamic result = null;
dynamic lastError = null; dynamic lastError = null;
dynamic lastStackTrace = null; dynamic lastStackTrace = null;
...@@ -43,3 +43,14 @@ Future<dynamic> retry(Action action, Duration timeout, ...@@ -43,3 +43,14 @@ Future<dynamic> retry(Action action, Duration timeout,
else else
return new Future.error(lastError, lastStackTrace); return new Future.error(lastError, lastStackTrace);
} }
/// A function that produces a [Stopwatch].
typedef Stopwatch StopwatchFactory();
/// Restores [stopwatchFactory] to the default implementation.
void restoreStopwatchFactory() {
stopwatchFactory = () => new Stopwatch();
}
/// Used by [retry] as a source of [Stopwatch] implementation.
StopwatchFactory stopwatchFactory = () => new Stopwatch();
...@@ -3,14 +3,29 @@ ...@@ -3,14 +3,29 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:quiver/time.dart';
import 'package:quiver/testing/async.dart'; import 'package:quiver/testing/async.dart';
import 'package:quiver/testing/time.dart';
import 'package:flutter_driver/src/retry.dart'; import 'package:flutter_driver/src/retry.dart';
main() { main() {
group('retry', () { group('retry', () {
FakeAsync fakeAsync;
setUp(() {
fakeAsync = new FakeAsync();
Clock fakeClock = fakeAsync.getClock(new DateTime.now());
stopwatchFactory = () {
return new FakeStopwatch(
() => fakeClock.now().millisecondsSinceEpoch,
1000
);
};
});
test('retries until succeeds', () { test('retries until succeeds', () {
new FakeAsync().run((FakeAsync fakeAsync) { fakeAsync.run((_) {
int retryCount = 0; int retryCount = 0;
expect( expect(
...@@ -37,17 +52,18 @@ main() { ...@@ -37,17 +52,18 @@ main() {
}); });
test('times out returning last error', () async { test('times out returning last error', () async {
fakeAsync.run((_) {
bool timedOut = false; bool timedOut = false;
int retryCount = 0; int retryCount = 0;
dynamic lastError; dynamic lastError;
dynamic lastStackTrace; dynamic lastStackTrace;
await retry( retry(
() { () {
retryCount++; retryCount++;
throw 'error'; throw 'error';
}, },
new Duration(milliseconds: 9), new Duration(milliseconds: 7),
new Duration(milliseconds: 2) new Duration(milliseconds: 2)
).catchError((error, stackTrace) { ).catchError((error, stackTrace) {
timedOut = true; timedOut = true;
...@@ -55,10 +71,15 @@ main() { ...@@ -55,10 +71,15 @@ main() {
lastStackTrace = stackTrace; lastStackTrace = stackTrace;
}); });
print('before elapse');
fakeAsync.elapse(new Duration(milliseconds: 10));
print('after elapse');
expect(timedOut, isTrue); expect(timedOut, isTrue);
expect(lastError, 'error'); expect(lastError, 'error');
expect(lastStackTrace, isNotNull); expect(lastStackTrace, isNotNull);
expect(retryCount, 4); expect(retryCount, 4);
}, skip: "Flaky. See https://github.com/flutter/flutter/issues/2133"); });
});
}); });
} }
...@@ -32,13 +32,18 @@ main() { ...@@ -32,13 +32,18 @@ main() {
}); });
test('tap on the floating action button; verify counter', () async { test('tap on the floating action button; verify counter', () async {
// Find floating action button (fab) to tap on
ObjectRef fab = await driver.findByValueKey('fab'); ObjectRef fab = await driver.findByValueKey('fab');
expect(fab, isNotNull); expect(fab, isNotNull);
// Tap on the fab
await driver.tap(fab); await driver.tap(fab);
// Wait for text to change to the desired value
await driver.waitFor(() async {
ObjectRef counter = await driver.findByValueKey('counter'); ObjectRef counter = await driver.findByValueKey('counter');
expect(counter, isNotNull); return await driver.getText(counter);
String text = await driver.getText(counter); }, contains("Button tapped 1 times."));
expect(text, contains("Button tapped 1 times."));
}); });
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment