Flutter Driver: command extensions and extension feature cleanup (#67916)
This commit is contained in:
@@ -20,6 +20,8 @@ import 'package:flutter_driver/src/common/wait.dart';
|
||||
import 'package:flutter_driver/src/extension/extension.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'stubs/stub_command.dart';
|
||||
import 'stubs/stub_command_extension.dart';
|
||||
import 'stubs/stub_finder.dart';
|
||||
import 'stubs/stub_finder_extension.dart';
|
||||
|
||||
@@ -984,6 +986,100 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('extension commands', () {
|
||||
int invokes = 0;
|
||||
final VoidCallback stubCallback = () => invokes++;
|
||||
|
||||
final Widget debugTree = Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
FlatButton(
|
||||
child: const Text('Whatever'),
|
||||
key: const ValueKey<String>('Button'),
|
||||
onPressed: stubCallback,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
invokes = 0;
|
||||
});
|
||||
|
||||
testWidgets('unknown extension command', (WidgetTester tester) async {
|
||||
final FlutterDriverExtension driverExtension = FlutterDriverExtension(
|
||||
(String arg) async => '',
|
||||
true,
|
||||
commands: <CommandExtension>[],
|
||||
);
|
||||
|
||||
Future<Map<String, dynamic>> invokeCommand(SerializableFinder finder, int times) async {
|
||||
final Map<String, String> arguments = StubNestedCommand(finder, times).serialize();
|
||||
return await driverExtension.call(arguments);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(debugTree);
|
||||
|
||||
final Map<String, dynamic> result = await invokeCommand(ByValueKey('Button'), 10);
|
||||
expect(result['isError'], true);
|
||||
expect(result['response'] is String, true);
|
||||
expect(result['response'] as String, contains('Unsupported command kind StubNestedCommand'));
|
||||
});
|
||||
|
||||
testWidgets('nested command', (WidgetTester tester) async {
|
||||
final FlutterDriverExtension driverExtension = FlutterDriverExtension(
|
||||
(String arg) async => '',
|
||||
true,
|
||||
commands: <CommandExtension>[
|
||||
StubNestedCommandExtension(),
|
||||
],
|
||||
);
|
||||
|
||||
Future<StubCommandResult> invokeCommand(SerializableFinder finder, int times) async {
|
||||
await driverExtension.call(const SetFrameSync(false).serialize()); // disable frame sync for test to avoid lock
|
||||
final Map<String, String> arguments = StubNestedCommand(finder, times, timeout: const Duration(seconds: 1)).serialize();
|
||||
final Map<String, dynamic> response = await driverExtension.call(arguments);
|
||||
final Map<String, dynamic> commandResponse = response['response'] as Map<String, dynamic>;
|
||||
return StubCommandResult(commandResponse['resultParam'] as String);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(debugTree);
|
||||
|
||||
const int times = 10;
|
||||
final StubCommandResult result = await invokeCommand(ByValueKey('Button'), times);
|
||||
expect(result.resultParam, 'stub response');
|
||||
expect(invokes, times);
|
||||
});
|
||||
|
||||
testWidgets('prober command', (WidgetTester tester) async {
|
||||
final FlutterDriverExtension driverExtension = FlutterDriverExtension(
|
||||
(String arg) async => '',
|
||||
true,
|
||||
commands: <CommandExtension>[
|
||||
StubProberCommandExtension(),
|
||||
],
|
||||
);
|
||||
|
||||
Future<StubCommandResult> invokeCommand(SerializableFinder finder, int times) async {
|
||||
await driverExtension.call(const SetFrameSync(false).serialize()); // disable frame sync for test to avoid lock
|
||||
final Map<String, String> arguments = StubProberCommand(finder, times, timeout: const Duration(seconds: 1)).serialize();
|
||||
final Map<String, dynamic> response = await driverExtension.call(arguments);
|
||||
final Map<String, dynamic> commandResponse = response['response'] as Map<String, dynamic>;
|
||||
return StubCommandResult(commandResponse['resultParam'] as String);
|
||||
}
|
||||
|
||||
await tester.pumpWidget(debugTree);
|
||||
|
||||
const int times = 10;
|
||||
final StubCommandResult result = await invokeCommand(ByValueKey('Button'), times);
|
||||
expect(result.resultParam, 'stub response');
|
||||
expect(invokes, times);
|
||||
});
|
||||
});
|
||||
|
||||
group('waitUntilFrameSync', () {
|
||||
FlutterDriverExtension driverExtension;
|
||||
Map<String, dynamic> result;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:flutter_driver/src/common/find.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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_driver/driver_extension.dart';
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
|
||||
class StubNestedCommand extends CommandWithTarget {
|
||||
StubNestedCommand(SerializableFinder finder, this.times, {Duration? timeout})
|
||||
: super(finder, timeout: timeout);
|
||||
|
||||
StubNestedCommand.deserialize(
|
||||
Map<String, String> json, DeserializeFinderFactory finderFactory)
|
||||
: times = int.parse(json['times']!),
|
||||
super.deserialize(json, finderFactory);
|
||||
|
||||
@override
|
||||
Map<String, String> serialize() {
|
||||
return super.serialize()..addAll(<String, String>{'times': '$times'});
|
||||
}
|
||||
|
||||
@override
|
||||
String get kind => 'StubNestedCommand';
|
||||
|
||||
final int times;
|
||||
}
|
||||
|
||||
class StubProberCommand extends CommandWithTarget {
|
||||
StubProberCommand(SerializableFinder finder, this.times, {Duration? timeout})
|
||||
: super(finder, timeout: timeout);
|
||||
|
||||
StubProberCommand.deserialize(Map<String, String> json, DeserializeFinderFactory finderFactory)
|
||||
: times = int.parse(json['times']!),
|
||||
super.deserialize(json, finderFactory);
|
||||
|
||||
@override
|
||||
Map<String, String> serialize() {
|
||||
return super.serialize()..addAll(<String, String>{'times': '$times'});
|
||||
}
|
||||
|
||||
@override
|
||||
String get kind => 'StubProberCommand';
|
||||
|
||||
final int times;
|
||||
}
|
||||
|
||||
class StubCommandResult extends Result {
|
||||
const StubCommandResult(this.resultParam);
|
||||
|
||||
final String resultParam;
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return <String, dynamic>{
|
||||
'resultParam': resultParam,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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_driver/driver_extension.dart';
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:flutter_driver/src/common/message.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'stub_command.dart';
|
||||
|
||||
class StubNestedCommandExtension extends CommandExtension {
|
||||
@override
|
||||
String get commandKind => 'StubNestedCommand';
|
||||
|
||||
@override
|
||||
Future<Result> call(Command command, WidgetController prober, CreateFinderFactory finderFactory, CommandHandlerFactory handlerFactory) async {
|
||||
final StubNestedCommand stubCommand = command as StubNestedCommand;
|
||||
handlerFactory.waitForElement(finderFactory.createFinder(stubCommand.finder));
|
||||
for (int index = 0; index < stubCommand.times; index++) {
|
||||
await handlerFactory.handleCommand(Tap(stubCommand.finder), prober, finderFactory);
|
||||
}
|
||||
return const StubCommandResult('stub response');
|
||||
}
|
||||
|
||||
@override
|
||||
Command deserialize(Map<String, String> params, DeserializeFinderFactory finderFactory, DeserializeCommandFactory commandFactory) {
|
||||
return StubNestedCommand.deserialize(params, finderFactory);
|
||||
}
|
||||
}
|
||||
|
||||
class StubProberCommandExtension extends CommandExtension {
|
||||
@override
|
||||
String get commandKind => 'StubProberCommand';
|
||||
|
||||
@override
|
||||
Future<Result> call(Command command, WidgetController prober, CreateFinderFactory finderFactory, CommandHandlerFactory handlerFactory) async {
|
||||
final StubProberCommand stubCommand = command as StubProberCommand;
|
||||
final Finder finder = finderFactory.createFinder(stubCommand.finder);
|
||||
handlerFactory.waitForElement(finder);
|
||||
for (int index = 0; index < stubCommand.times; index++) {
|
||||
await prober.tap(finder);
|
||||
}
|
||||
return const StubCommandResult('stub response');
|
||||
}
|
||||
|
||||
@override
|
||||
Command deserialize(Map<String, String> params, DeserializeFinderFactory finderFactory, DeserializeCommandFactory commandFactory) {
|
||||
return StubProberCommand.deserialize(params, finderFactory);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/src/widgets/framework.dart';
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
import 'package:flutter_driver/src/common/create_finder_factory.dart';
|
||||
import 'package:flutter_test/src/finders.dart';
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
import 'package:flutter_driver/src/common/handler_factory.dart';
|
||||
import 'package:flutter_driver/src/common/find.dart';
|
||||
|
||||
import 'stub_finder.dart';
|
||||
|
||||
Reference in New Issue
Block a user