Add geometry getters to Flutter Driver (#32302)

This commit is contained in:
Michael Goderbauer
2019-05-09 09:50:02 +02:00
committed by GitHub
parent 8b114482a4
commit ff1dbcdeb6
5 changed files with 301 additions and 0 deletions

View File

@@ -261,6 +261,111 @@ void main() {
});
});
group('getOffset', () {
test('requires a target reference', () async {
expect(driver.getCenter(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getTopLeft(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getTopRight(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getBottomLeft(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getBottomRight(null), throwsA(isInstanceOf<DriverError>()));
});
test('sends the getCenter command', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, dynamic>{
'command': 'get_offset',
'offsetType': 'center',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
});
});
final DriverOffset result = await driver.getCenter(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getTopLeft command', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, dynamic>{
'command': 'get_offset',
'offsetType': 'topLeft',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
});
});
final DriverOffset result = await driver.getTopLeft(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getTopRight command', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, dynamic>{
'command': 'get_offset',
'offsetType': 'topRight',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
});
});
final DriverOffset result = await driver.getTopRight(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getBottomLeft command', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, dynamic>{
'command': 'get_offset',
'offsetType': 'bottomLeft',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
});
});
final DriverOffset result = await driver.getBottomLeft(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getBottomRight command', () async {
when(mockIsolate.invokeExtension(any, any)).thenAnswer((Invocation i) {
expect(i.positionalArguments[1], <String, dynamic>{
'command': 'get_offset',
'offsetType': 'bottomRight',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
});
});
final DriverOffset result = await driver.getBottomRight(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
});
group('clearTimeline', () {
test('clears timeline', () async {
bool clearWasCalled = false;

View File

@@ -6,6 +6,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_driver/src/common/find.dart';
import 'package:flutter_driver/src/common/geometry.dart';
import 'package:flutter_driver/src/common/request_data.dart';
import 'package:flutter_driver/src/extension/extension.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -120,4 +121,34 @@ void main() {
semantics.dispose();
});
});
testWidgets('getOffset', (WidgetTester tester) async {
final FlutterDriverExtension extension = FlutterDriverExtension((String arg) async => '', true);
Future<Offset> getOffset(OffsetType offset) async {
final Map<String, Object> arguments = GetOffset(ByValueKey(1), offset).serialize();
final GetOffsetResult result = GetOffsetResult.fromJson((await extension.call(arguments))['response']);
return Offset(result.dx, result.dy);
}
await tester.pumpWidget(
Align(
alignment: Alignment.topLeft,
child: Transform.translate(
offset: const Offset(40, 30),
child: Container(
key: const ValueKey<int>(1),
width: 100,
height: 120,
),
),
),
);
expect(await getOffset(OffsetType.topLeft), const Offset(40, 30));
expect(await getOffset(OffsetType.topRight), const Offset(40 + 100.0, 30));
expect(await getOffset(OffsetType.bottomLeft), const Offset(40, 30 + 120.0));
expect(await getOffset(OffsetType.bottomRight), const Offset(40 + 100.0, 30 + 120.0));
expect(await getOffset(OffsetType.center), const Offset(40 + (100 / 2), 30 + (120 / 2)));
});
}