[camerax] Mock InstanceManager in unit tests (#4121)

Ensures that `InstanceManager` is mocked (once) for all `camera_android_camerax` tests.

Fixes https://github.com/flutter/flutter/issues/127661.
This commit is contained in:
Camille Simon
2023-06-02 09:02:14 -07:00
committed by GitHub
parent b7bf8516f0
commit 75085edb54
12 changed files with 36 additions and 33 deletions

View File

@ -16,18 +16,19 @@ import 'test_camerax_library.g.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('Analyzer', () { group('Analyzer', () {
setUp(() {}); setUp(() {});
tearDown(() { tearDown(() {
TestAnalyzerHostApi.setup(null); TestAnalyzerHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
}); });
test('HostApi create', () { test('HostApi create', () {
final MockTestAnalyzerHostApi mockApi = MockTestAnalyzerHostApi(); final MockTestAnalyzerHostApi mockApi = MockTestAnalyzerHostApi();
TestAnalyzerHostApi.setup(mockApi); TestAnalyzerHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},

View File

@ -98,9 +98,6 @@ void main() {
return cameraClosingEventSent && cameraErrorSent; return cameraClosingEventSent && cameraErrorSent;
} }
// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
test('Should fetch CameraDescription instances for available cameras', test('Should fetch CameraDescription instances for available cameras',
() async { () async {
// Arrange // Arrange

View File

@ -7,17 +7,17 @@ import 'package:camera_android_camerax/src/instance_manager.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart'; import 'package:mockito/annotations.dart';
import 'camera_state_error_test.mocks.dart';
import 'test_camerax_library.g.dart'; import 'test_camerax_library.g.dart';
@GenerateMocks(<Type>[TestInstanceManagerHostApi]) @GenerateMocks(<Type>[TestInstanceManagerHostApi])
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
group('CameraStateError', () { // Mocks the call to clear the native InstanceManager.
tearDown(() { TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
TestInstanceManagerHostApi.setup(null);
});
group('CameraStateError', () {
test( test(
'FlutterAPI create makes call to create CameraStateError instance with expected identifier', 'FlutterAPI create makes call to create CameraStateError instance with expected identifier',
() { () {

View File

@ -20,10 +20,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi()); TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('CameraState', () { group('CameraState', () {
tearDown(() {
TestInstanceManagerHostApi.setup(null);
});
test( test(
'FlutterAPI create makes call to create CameraState instance with expected identifier', 'FlutterAPI create makes call to create CameraState instance with expected identifier',
() { () {

View File

@ -26,14 +26,12 @@ void main() {
tearDown(() { tearDown(() {
TestImageAnalysisHostApi.setup(null); TestImageAnalysisHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
}); });
test('HostApi create', () { test('HostApi create', () {
final MockTestImageAnalysisHostApi mockApi = final MockTestImageAnalysisHostApi mockApi =
MockTestImageAnalysisHostApi(); MockTestImageAnalysisHostApi();
TestImageAnalysisHostApi.setup(mockApi); TestImageAnalysisHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},

View File

@ -12,10 +12,13 @@ import 'package:mockito/mockito.dart';
import 'image_capture_test.mocks.dart'; import 'image_capture_test.mocks.dart';
import 'test_camerax_library.g.dart'; import 'test_camerax_library.g.dart';
@GenerateMocks(<Type>[TestImageCaptureHostApi]) @GenerateMocks(<Type>[TestImageCaptureHostApi, TestInstanceManagerHostApi])
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('ImageCapture', () { group('ImageCapture', () {
tearDown(() => TestImageCaptureHostApi.setup(null)); tearDown(() => TestImageCaptureHostApi.setup(null));

View File

@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations // Mocks generated by Mockito 5.4.1 from annotations
// in camera_android_camerax/test/image_capture_test.dart. // in camera_android_camerax/test/image_capture_test.dart.
// Do not manually edit this file. // Do not manually edit this file.
// @dart=2.19
// ignore_for_file: no_leading_underscores_for_library_prefixes // ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4; import 'dart:async' as _i4;
@ -71,3 +73,22 @@ class MockTestImageCaptureHostApi extends _i1.Mock
returnValue: _i4.Future<String>.value(''), returnValue: _i4.Future<String>.value(''),
) as _i4.Future<String>); ) as _i4.Future<String>);
} }
/// A class which mocks [TestInstanceManagerHostApi].
///
/// See the documentation for Mockito's code generation for more information.
class MockTestInstanceManagerHostApi extends _i1.Mock
implements _i2.TestInstanceManagerHostApi {
MockTestInstanceManagerHostApi() {
_i1.throwOnMissingStub(this);
}
@override
void clear() => super.noSuchMethod(
Invocation.method(
#clear,
[],
),
returnValueForMissingStub: null,
);
}

View File

@ -22,11 +22,8 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi()); TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('ImageProxy', () { group('ImageProxy', () {
setUp(() {});
tearDown(() { tearDown(() {
TestImageProxyHostApi.setup(null); TestImageProxyHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
}); });
test('getPlanes', () async { test('getPlanes', () async {

View File

@ -24,7 +24,6 @@ void main() {
group('LiveData', () { group('LiveData', () {
tearDown(() { tearDown(() {
TestLiveDataHostApi.setup(null); TestLiveDataHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
}); });
test('observe makes call to add observer to LiveData instance', () async { test('observe makes call to add observer to LiveData instance', () async {
@ -103,7 +102,6 @@ void main() {
test('getValue returns expected value', () async { test('getValue returns expected value', () async {
final MockTestLiveDataHostApi mockApi = MockTestLiveDataHostApi(); final MockTestLiveDataHostApi mockApi = MockTestLiveDataHostApi();
TestLiveDataHostApi.setup(mockApi); TestLiveDataHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},

View File

@ -17,16 +17,17 @@ import 'test_camerax_library.g.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('Observer', () { group('Observer', () {
tearDown(() { tearDown(() {
TestObserverHostApi.setup(null); TestObserverHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
}); });
test('HostApi create makes call to create Observer instance', () { test('HostApi create makes call to create Observer instance', () {
final MockTestObserverHostApi mockApi = MockTestObserverHostApi(); final MockTestObserverHostApi mockApi = MockTestObserverHostApi();
TestObserverHostApi.setup(mockApi); TestObserverHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},
@ -47,7 +48,6 @@ void main() {
() { () {
final MockTestObserverHostApi mockApi = MockTestObserverHostApi(); final MockTestObserverHostApi mockApi = MockTestObserverHostApi();
TestObserverHostApi.setup(mockApi); TestObserverHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
final Observer<String> cameraStateObserver = final Observer<String> cameraStateObserver =
Observer<String>.detached(onChanged: (Object value) {}); Observer<String>.detached(onChanged: (Object value) {});

View File

@ -20,12 +20,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi()); TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('PlaneProxy', () { group('PlaneProxy', () {
setUp(() {});
tearDown(() {
TestInstanceManagerHostApi.setup(null);
});
test('FlutterAPI create', () { test('FlutterAPI create', () {
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},

View File

@ -18,8 +18,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi()); TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());
group('ZoomState', () { group('ZoomState', () {
tearDown(() => TestInstanceManagerHostApi.setup(null));
test('flutterApi create makes call to create expected ZoomState', () { test('flutterApi create makes call to create expected ZoomState', () {
final InstanceManager instanceManager = InstanceManager( final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {}, onWeakReferenceRemoved: (_) {},