Unverified Commit d4e48a15 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks from android emulator tests (#81548)

parent 3a0abac7
......@@ -149,7 +149,7 @@ class AndroidEmulator extends Emulator {
String _prop(String name) => _properties != null ? _properties[name] : null;
@override
Future<void> launch() async {
Future<void> launch({@visibleForTesting Duration startupDuration}) async {
final Process process = await _processUtils.start(
<String>[_androidSdk.emulatorPath, '-avd', id],
);
......@@ -200,7 +200,7 @@ class AndroidEmulator extends Emulator {
}));
// Wait a few seconds for the emulator to start.
await Future<void>.delayed(const Duration(seconds: 3));
await Future<void>.delayed(startupDuration ?? const Duration(seconds: 3));
earlyFailure = false;
return;
}
......
......@@ -6,16 +6,14 @@
import 'dart:async';
import 'package:fake_async/fake_async.dart';
import 'package:flutter_tools/src/android/android_emulator.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:mockito/mockito.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/fake_process_manager.dart';
import '../../src/mocks.dart' show MockAndroidSdk;
const String emulatorID = 'i1234';
const String errorText = '[Android emulator test error]';
......@@ -32,7 +30,7 @@ void main() {
emulatorID,
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
androidSdk: MockAndroidSdk(),
androidSdk: FakeAndroidSdk(),
);
expect(emulator.id, emulatorID);
expect(emulator.hasConfig, false);
......@@ -45,7 +43,7 @@ void main() {
properties: const <String, String>{'name': 'test'},
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
androidSdk: MockAndroidSdk(),
androidSdk: FakeAndroidSdk(),
);
expect(emulator.id, emulatorID);
......@@ -65,7 +63,7 @@ void main() {
properties: properties,
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
androidSdk: MockAndroidSdk(),
androidSdk: FakeAndroidSdk(),
);
expect(emulator.id, emulatorID);
......@@ -86,7 +84,7 @@ void main() {
properties: properties,
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
androidSdk: MockAndroidSdk(),
androidSdk: FakeAndroidSdk(),
);
expect(emulator.name, displayName);
......@@ -104,7 +102,7 @@ void main() {
properties: properties,
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
androidSdk: MockAndroidSdk(),
androidSdk: FakeAndroidSdk(),
);
expect(emulator.name, 'This is my ID');
......@@ -127,11 +125,11 @@ void main() {
});
group('Android emulator launch ', () {
MockAndroidSdk mockSdk;
FakeAndroidSdk mockSdk;
setUp(() {
mockSdk = MockAndroidSdk();
when(mockSdk.emulatorPath).thenReturn('emulator');
mockSdk = FakeAndroidSdk();
mockSdk.emulatorPath = 'emulator';
});
testWithoutContext('succeeds', () async {
......@@ -143,13 +141,7 @@ void main() {
logger: BufferLogger.test(),
);
final Completer<void> completer = Completer<void>();
FakeAsync().run((FakeAsync time) {
unawaited(emulator.launch().whenComplete(completer.complete));
time.elapse(const Duration(seconds: 5));
time.flushMicrotasks();
});
await completer.future;
await emulator.launch(startupDuration: Duration.zero);
});
testWithoutContext('prints error on failure', () async {
......@@ -161,20 +153,13 @@ void main() {
exitCode: 1,
stderr: errorText,
stdout: 'dummy text',
duration: Duration(seconds: 1),
),
]),
androidSdk: mockSdk,
logger: logger,
);
final Completer<void> completer = Completer<void>();
FakeAsync().run((FakeAsync time) {
unawaited(emulator.launch().whenComplete(completer.complete));
time.elapse(const Duration(seconds: 5));
time.flushMicrotasks();
});
await completer.future;
await emulator.launch(startupDuration: Duration.zero);
expect(logger.errorText, contains(errorText));
});
......@@ -183,26 +168,25 @@ void main() {
final BufferLogger logger = BufferLogger.test();
final AndroidEmulator emulator = AndroidEmulator(emulatorID,
processManager: FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
FakeCommand(
command: kEmulatorLaunchCommand,
exitCode: 1,
stderr: '',
stdout: 'dummy text',
duration: Duration(seconds: 4),
completer: Completer<void>(),
),
]),
androidSdk: mockSdk,
logger: logger,
);
final Completer<void> completer = Completer<void>();
await FakeAsync().run((FakeAsync time) async {
unawaited(emulator.launch().whenComplete(completer.complete));
time.elapse(const Duration(seconds: 5));
time.flushMicrotasks();
});
await completer.future;
await emulator.launch(startupDuration: Duration.zero);
expect(logger.errorText, isEmpty);
}, skip: true); // TODO(jonahwilliams): clean up with https://github.com/flutter/flutter/issues/60675
});
});
}
class FakeAndroidSdk extends Fake implements AndroidSdk {
@override
String emulatorPath;
}
......@@ -6,18 +6,18 @@
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/android/android_workflow.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/emulator.dart';
import 'package:flutter_tools/src/ios/ios_emulators.dart';
import 'package:flutter_tools/src/macos/xcode.dart';
import 'package:mockito/mockito.dart';
import 'package:test/fake.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/fakes.dart';
import '../src/mocks.dart';
const FakeEmulator emulator1 = FakeEmulator('Nexus_5', 'Nexus 5', 'Google');
const FakeEmulator emulator2 = FakeEmulator('Nexus_5X_API_27_x86', 'Nexus 5X', 'Google');
......@@ -29,7 +29,7 @@ const List<Emulator> emulators = <Emulator>[
];
// We have to send a command that fails in order to get the list of valid
// system images paths. This is an example of the output to use in the mock.
// system images paths. This is an example of the output to use in the fake.
const String fakeCreateFailureOutput =
'Error: Package path (-k) not specified. Valid system image paths are:\n'
'system-images;android-27;google_apis;x86\n'
......@@ -45,20 +45,20 @@ const FakeCommand kListEmulatorsCommand = FakeCommand(
void main() {
FakeProcessManager fakeProcessManager;
MockAndroidSdk mockSdk;
FakeAndroidSdk sdk;
FileSystem fileSystem;
Xcode xcode;
setUp(() {
fileSystem = MemoryFileSystem.test();
fakeProcessManager = FakeProcessManager.empty();
mockSdk = MockAndroidSdk();
sdk = FakeAndroidSdk();
xcode = Xcode.test(processManager: fakeProcessManager, fileSystem: fileSystem);
when(mockSdk.avdManagerPath).thenReturn('avdmanager');
when(mockSdk.getAvdManagerPath()).thenReturn('avdmanager');
when(mockSdk.emulatorPath).thenReturn('emulator');
when(mockSdk.adbPath).thenReturn('adb');
sdk
..avdManagerPath = 'avdmanager'
..emulatorPath = 'emulator'
..adbPath = 'adb';
});
group('EmulatorManager', () {
......@@ -74,9 +74,9 @@ void main() {
stdout: 'existing-avd-1',
),
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -121,8 +121,7 @@ void main() {
});
testUsingContext('create emulator with a missing avdmanager does not crash.', () async {
when(mockSdk.avdManagerPath).thenReturn(null);
when(mockSdk.getAvdManagerPath()).thenReturn(null);
sdk.avdManagerPath = null;
final EmulatorManager emulatorManager = EmulatorManager(
fileSystem: MemoryFileSystem.test(),
logger: BufferLogger.test(),
......@@ -132,9 +131,9 @@ void main() {
stdout: 'existing-avd-1',
),
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -174,9 +173,9 @@ void main() {
],
)
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -211,9 +210,9 @@ void main() {
],
)
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -250,9 +249,9 @@ void main() {
'Use --force if you want to replace it.'
)
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -292,9 +291,9 @@ void main() {
],
)
]),
androidSdk: mockSdk,
androidSdk: sdk,
androidWorkflow: AndroidWorkflow(
androidSdk: mockSdk,
androidSdk: sdk,
featureFlags: TestFeatureFlags(),
operatingSystemUtils: FakeOperatingSystemUtils(),
),
......@@ -372,3 +371,23 @@ class FakeEmulator extends Emulator {
throw UnimplementedError('Not implemented in Mock');
}
}
class FakeAndroidSdk extends Fake implements AndroidSdk {
@override
String avdManagerPath;
@override
String emulatorPath;
@override
String adbPath;
@override
String getAvdManagerPath() => avdManagerPath;
@override
String getAvdPath() => 'avd';
@override
Map<String, String> get sdkManagerEnv => <String, String>{};
}
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