support waiting for things to happen in Flutter Driver

This commit is contained in:
yjbanov
2016-02-23 17:44:09 -08:00
parent 81df765d21
commit 7f39703759
7 changed files with 281 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import 'package:flutter_driver/src/health.dart';
import 'package:flutter_driver/src/message.dart';
import 'package:json_rpc_2/json_rpc_2.dart' as rpc;
import 'package:mockito/mockito.dart';
import 'package:quiver/testing/async.dart';
import 'package:vm_service_client/vm_service_client.dart';
main() {
@@ -181,6 +182,58 @@ main() {
expect(result, 'hello');
});
});
group('waitFor', () {
test('waits for a condition', () {
expect(
driver.waitFor(() {
return new Future.delayed(
new Duration(milliseconds: 50),
() => 123
);
}, equals(123)),
completion(123)
);
});
test('retries a correct number of times', () {
new FakeAsync().run((FakeAsync fakeAsync) {
int retryCount = 0;
expect(
driver.waitFor(
() {
retryCount++;
return retryCount;
},
equals(2),
timeout: new Duration(milliseconds: 30),
pauseBetweenRetries: new Duration(milliseconds: 10)
),
completion(2)
);
fakeAsync.elapse(new Duration(milliseconds: 50));
// Check that we didn't retry more times than necessary
expect(retryCount, 2);
});
});
test('times out', () async {
bool timedOut = false;
await driver.waitFor(
() => 1,
equals(2),
timeout: new Duration(milliseconds: 10),
pauseBetweenRetries: new Duration(milliseconds: 2)
).catchError((err, stack) {
timedOut = true;
});
expect(timedOut, isTrue);
});
});
});
}

View File

@@ -0,0 +1,64 @@
// Copyright 2016 The Chromium 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:test/test.dart';
import 'package:quiver/testing/async.dart';
import 'package:flutter_driver/src/retry.dart';
main() {
group('retry', () {
test('retries until succeeds', () {
new FakeAsync().run((FakeAsync fakeAsync) {
int retryCount = 0;
expect(
retry(
() async {
retryCount++;
if (retryCount < 2) {
throw 'error';
} else {
return retryCount;
}
},
new Duration(milliseconds: 30),
new Duration(milliseconds: 10)
),
completion(2)
);
fakeAsync.elapse(new Duration(milliseconds: 50));
// Check that we didn't retry more times than necessary
expect(retryCount, 2);
});
});
test('times out returning last error', () async {
bool timedOut = false;
int retryCount = 0;
dynamic lastError;
dynamic lastStackTrace;
await retry(
() {
retryCount++;
throw 'error';
},
new Duration(milliseconds: 9),
new Duration(milliseconds: 2)
).catchError((error, stackTrace) {
timedOut = true;
lastError = error;
lastStackTrace = stackTrace;
});
expect(timedOut, isTrue);
expect(lastError, 'error');
expect(lastStackTrace, isNotNull);
expect(retryCount, 4);
});
});
}