Rename native_driver to android_{driver_extensions|engine_test} (#161263)
Towards https://github.com/flutter/flutter/issues/161261 (before I add more tests). This is a 1:1 move, with massaging of `run_flutter_driver_android_tests.dart` to be more automated as tests are added. /cc @reidbaker
This commit is contained in:
125
dev/tools/android_driver_extensions/test/adb_fake_io_test.dart
Normal file
125
dev/tools/android_driver_extensions/test/adb_fake_io_test.dart
Normal file
@@ -0,0 +1,125 @@
|
||||
// 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 'dart:typed_data';
|
||||
|
||||
import 'package:android_driver_extensions/src/backend/android/adb.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'src/fake_process_manager.dart';
|
||||
|
||||
// These tests are fake IO tests, so they don't actually run any commands.
|
||||
void main() {
|
||||
group('AndroidDeviceTarget', () {
|
||||
late List<String> caughtArgs;
|
||||
late FakeProcessManager processManager;
|
||||
|
||||
setUp(() {
|
||||
caughtArgs = <String>[];
|
||||
processManager = FakeProcessManager((String exec, List<String> args) async {
|
||||
assert(caughtArgs.isEmpty, 'Should only catch one call to run');
|
||||
caughtArgs = args;
|
||||
return FakeProcessManager.ok();
|
||||
});
|
||||
});
|
||||
|
||||
test('bySerial emits "-s <number>"', () async {
|
||||
await Adb.create(
|
||||
target: const AndroidDeviceTarget.bySerial('1234'),
|
||||
processManager: processManager,
|
||||
);
|
||||
|
||||
expect(caughtArgs.take(2), <String>['-s', '1234']);
|
||||
});
|
||||
|
||||
test('onlyEmulatorOrDevice emits no arguments', () async {
|
||||
await Adb.create(
|
||||
target: const AndroidDeviceTarget.onlyEmulatorOrDevice(),
|
||||
processManager: processManager,
|
||||
);
|
||||
|
||||
expect(caughtArgs, <String>['shell', 'echo', 'connected']);
|
||||
});
|
||||
|
||||
test('onlyEmulator emits "-e"', () async {
|
||||
await Adb.create(
|
||||
target: const AndroidDeviceTarget.onlyEmulator(),
|
||||
processManager: processManager,
|
||||
);
|
||||
|
||||
expect(caughtArgs.take(1), <String>['-e']);
|
||||
});
|
||||
|
||||
test('onlyDevice emits "-d"', () async {
|
||||
await Adb.create(
|
||||
target: const AndroidDeviceTarget.onlyDevice(),
|
||||
processManager: processManager,
|
||||
);
|
||||
|
||||
expect(caughtArgs.take(1), <String>['-d']);
|
||||
});
|
||||
});
|
||||
|
||||
test('if isDeviceConnected fails, bootstrap fails', () async {
|
||||
final ProcessManager processManager = FakeProcessManager((
|
||||
String exec,
|
||||
List<String> args,
|
||||
) async {
|
||||
return FakeProcessManager.error('error');
|
||||
});
|
||||
|
||||
expect(
|
||||
Adb.create(processManager: processManager),
|
||||
throwsA(
|
||||
isA<StateError>().having(
|
||||
(StateError e) => e.message,
|
||||
'message',
|
||||
'No device connected: error',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('screencap invokes "exec-out screencap -p"', () async {
|
||||
final FakeProcessManager processManager = FakeProcessManager((
|
||||
String exec,
|
||||
List<String> args,
|
||||
) async {
|
||||
switch (args) {
|
||||
case ['shell', 'echo', 'connected']:
|
||||
return FakeProcessManager.ok('connected');
|
||||
case ['exec-out', 'screencap', '-p']:
|
||||
return FakeProcessManager.okBinary(<int>[0, 1, 2, 3]);
|
||||
default:
|
||||
throw UnsupportedError('Unknown command: $args');
|
||||
}
|
||||
});
|
||||
|
||||
final Adb adb = await Adb.create(processManager: processManager);
|
||||
|
||||
final Uint8List result = await adb.screencap();
|
||||
expect(result, <int>[0, 1, 2, 3]);
|
||||
});
|
||||
|
||||
test('tap invokes "shell input tap"', () async {
|
||||
final FakeProcessManager processManager = FakeProcessManager((
|
||||
String exec,
|
||||
List<String> args,
|
||||
) async {
|
||||
switch (args) {
|
||||
case ['shell', 'echo', 'connected']:
|
||||
return FakeProcessManager.ok('connected');
|
||||
case ['shell', 'input', 'tap', '1', '2']:
|
||||
return FakeProcessManager.ok();
|
||||
default:
|
||||
throw UnsupportedError('Unknown command: $args');
|
||||
}
|
||||
});
|
||||
|
||||
final Adb adb = await Adb.create(processManager: processManager);
|
||||
|
||||
await adb.tap(1, 2);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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 'dart:io' as io;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:android_driver_extensions/native_driver.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'src/fake_adb.dart';
|
||||
import 'src/fake_driver.dart';
|
||||
|
||||
void main() async {
|
||||
test('screenshot calls ADB screencap', () async {
|
||||
final FakeAdb adb = FakeAdb(screencap: () async => Uint8List.fromList(<int>[1, 2, 3, 4]));
|
||||
final io.Directory tmpDir = io.Directory.systemTemp.createTempSync();
|
||||
final AndroidNativeDriver driver = AndroidNativeDriver.forTesting(
|
||||
adb: adb,
|
||||
driver: const NullFlutterDriver(),
|
||||
tempDirectory: tmpDir,
|
||||
);
|
||||
|
||||
final NativeScreenshot screenshot = await driver.screenshot();
|
||||
await expectLater(screenshot.readAsBytes(), completion(<int>[1, 2, 3, 4]));
|
||||
|
||||
final String path = await screenshot.saveAs();
|
||||
final io.File file = io.File(path);
|
||||
expect(file.readAsBytes(), completion(<int>[1, 2, 3, 4]));
|
||||
|
||||
await driver.close();
|
||||
});
|
||||
|
||||
test('closes deletes the temporary directory', () async {
|
||||
final FakeAdb adb = FakeAdb();
|
||||
final io.Directory tmpDir = io.Directory.systemTemp.createTempSync();
|
||||
final AndroidNativeDriver driver = AndroidNativeDriver.forTesting(
|
||||
adb: adb,
|
||||
driver: const NullFlutterDriver(),
|
||||
tempDirectory: tmpDir,
|
||||
);
|
||||
|
||||
expect(tmpDir.existsSync(), isTrue);
|
||||
await driver.close();
|
||||
expect(tmpDir.existsSync(), isFalse);
|
||||
});
|
||||
}
|
||||
86
dev/tools/android_driver_extensions/test/src/fake_adb.dart
Normal file
86
dev/tools/android_driver_extensions/test/src/fake_adb.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
// 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 'dart:typed_data';
|
||||
|
||||
import 'package:android_driver_extensions/src/backend/android.dart';
|
||||
|
||||
/// A stub of [Adb] that allows overriding its methods.
|
||||
///
|
||||
/// The default implementation of each method is a simple success case.
|
||||
final class FakeAdb implements Adb {
|
||||
FakeAdb({
|
||||
Future<(bool, String?)> Function()? isDeviceConnected,
|
||||
Future<Uint8List> Function()? screencap,
|
||||
Future<void> Function(int x, int y)? tap,
|
||||
Future<void> Function()? disableImmersiveModeConfirmations,
|
||||
Future<void> Function()? disableAnimations,
|
||||
Future<void> Function()? sendToHome,
|
||||
Future<void> Function(String appName, [String? activityName])? resumeApp,
|
||||
Future<void> Function(String appName)? trimMemory,
|
||||
}) : _isDeviceConnected = isDeviceConnected,
|
||||
_screencap = screencap,
|
||||
_tap = tap,
|
||||
_disableImmersiveModeConfirmations = disableImmersiveModeConfirmations,
|
||||
_disableAnimations = disableAnimations,
|
||||
_sendToHome = sendToHome,
|
||||
_resumeApp = resumeApp,
|
||||
_trimMemory = trimMemory;
|
||||
|
||||
@override
|
||||
Future<(bool, String?)> isDeviceConnected() async {
|
||||
return _isDeviceConnected?.call() ?? (true, null);
|
||||
}
|
||||
|
||||
final Future<(bool, String?)> Function()? _isDeviceConnected;
|
||||
|
||||
@override
|
||||
Future<Uint8List> screencap() {
|
||||
return _screencap?.call() ?? Future<Uint8List>.value(Uint8List(0));
|
||||
}
|
||||
|
||||
final Future<Uint8List> Function()? _screencap;
|
||||
|
||||
@override
|
||||
Future<void> tap(int x, int y) async {
|
||||
return _tap?.call(x, y) ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function(int x, int y)? _tap;
|
||||
|
||||
@override
|
||||
Future<void> disableImmersiveModeConfirmations() {
|
||||
return _disableImmersiveModeConfirmations?.call() ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function()? _disableImmersiveModeConfirmations;
|
||||
|
||||
@override
|
||||
Future<void> disableAnimations() {
|
||||
return _disableAnimations?.call() ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function()? _disableAnimations;
|
||||
|
||||
@override
|
||||
Future<void> sendToHome() {
|
||||
return _sendToHome?.call() ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function()? _sendToHome;
|
||||
|
||||
@override
|
||||
Future<void> resumeApp({required String appName, String? activityName}) {
|
||||
return _resumeApp?.call(appName, activityName) ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function(String appName, [String? activityName])? _resumeApp;
|
||||
|
||||
@override
|
||||
Future<void> trimMemory({required String appName}) {
|
||||
return _trimMemory?.call(appName) ?? Future<void>.value();
|
||||
}
|
||||
|
||||
final Future<void> Function(String appName)? _trimMemory;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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/flutter_driver.dart';
|
||||
|
||||
/// A fake [FlutterDriver] that throws [UnimplementedError] for all methods.
|
||||
final class NullFlutterDriver implements FlutterDriver {
|
||||
const NullFlutterDriver();
|
||||
|
||||
@override
|
||||
Never noSuchMethod(Invocation invocation) => throw UnimplementedError();
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// 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 'dart:convert';
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:process/process.dart';
|
||||
|
||||
/// A stub of [ProcessManager] that allows mocking the [run] method only.
|
||||
final class FakeProcessManager extends ProcessManager {
|
||||
/// Creates a new [FakeProcessManager] with the given delegate callback.
|
||||
FakeProcessManager(this._runDelegate);
|
||||
final Future<ProcessResult> Function(String executable, List<String> arguments) _runDelegate;
|
||||
|
||||
@override
|
||||
Never noSuchMethod(_) => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
Future<ProcessResult> run(
|
||||
List<Object> command, {
|
||||
String? workingDirectory,
|
||||
Map<String, String>? environment,
|
||||
bool includeParentEnvironment = true,
|
||||
bool runInShell = false,
|
||||
Encoding? stdoutEncoding = systemEncoding,
|
||||
Encoding? stderrEncoding = systemEncoding,
|
||||
}) {
|
||||
final List<String> strings = command.map((Object e) => '$e').toList();
|
||||
final [String executable, ...List<String> arguments] = strings;
|
||||
return _runDelegate(executable, arguments);
|
||||
}
|
||||
|
||||
/// Returns a [ProcessResult] with the given [stdout].
|
||||
static ProcessResult ok([String stdout = '']) {
|
||||
return ProcessResult(0, 0, stdout, '');
|
||||
}
|
||||
|
||||
/// Returns a [ProcessResult] with the given [stderr].
|
||||
static ProcessResult error([String stderr = '']) {
|
||||
return ProcessResult(0, 1, '', stderr);
|
||||
}
|
||||
|
||||
/// Returns a [ProcessResult] with the given [stdout] binary data.
|
||||
static ProcessResult okBinary(List<int> stdout) {
|
||||
return ProcessResult(0, 0, Uint8List.fromList(stdout), '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user