Skip to content

Commit

Permalink
[camerax] Mock InstanceManager in unit tests (flutter#4121)
Browse files Browse the repository at this point in the history
Ensures that `InstanceManager` is mocked (once) for all `camera_android_camerax` tests.

Fixes flutter/flutter#127661.
  • Loading branch information
camsim99 committed Jun 2, 2023
1 parent b7bf851 commit 75085ed
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import 'test_camerax_library.g.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('Analyzer', () {
setUp(() {});

tearDown(() {
TestAnalyzerHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
});

test('HostApi create', () {
final MockTestAnalyzerHostApi mockApi = MockTestAnalyzerHostApi();
TestAnalyzerHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ void main() {
return cameraClosingEventSent && cameraErrorSent;
}

// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

test('Should fetch CameraDescription instances for available cameras',
() async {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import 'package:camera_android_camerax/src/instance_manager.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';

import 'camera_state_error_test.mocks.dart';
import 'test_camerax_library.g.dart';

@GenerateMocks(<Type>[TestInstanceManagerHostApi])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('CameraStateError', () {
tearDown(() {
TestInstanceManagerHostApi.setup(null);
});
// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('CameraStateError', () {
test(
'FlutterAPI create makes call to create CameraStateError instance with expected identifier',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('CameraState', () {
tearDown(() {
TestInstanceManagerHostApi.setup(null);
});

test(
'FlutterAPI create makes call to create CameraState instance with expected identifier',
() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ void main() {

tearDown(() {
TestImageAnalysisHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
});

test('HostApi create', () {
final MockTestImageAnalysisHostApi mockApi =
MockTestImageAnalysisHostApi();
TestImageAnalysisHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import 'package:mockito/mockito.dart';
import 'image_capture_test.mocks.dart';
import 'test_camerax_library.g.dart';

@GenerateMocks(<Type>[TestImageCaptureHostApi])
@GenerateMocks(<Type>[TestImageCaptureHostApi, TestInstanceManagerHostApi])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('ImageCapture', () {
tearDown(() => TestImageCaptureHostApi.setup(null));

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

Expand Down Expand Up @@ -71,3 +73,22 @@ class MockTestImageCaptureHostApi extends _i1.Mock
returnValue: _i4.Future<String>.value(''),
) 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,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('ImageProxy', () {
setUp(() {});

tearDown(() {
TestImageProxyHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
});

test('getPlanes', () async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void main() {
group('LiveData', () {
tearDown(() {
TestLiveDataHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
});

test('observe makes call to add observer to LiveData instance', () async {
Expand Down Expand Up @@ -103,7 +102,6 @@ void main() {
test('getValue returns expected value', () async {
final MockTestLiveDataHostApi mockApi = MockTestLiveDataHostApi();
TestLiveDataHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import 'test_camerax_library.g.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

// Mocks the call to clear the native InstanceManager.
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('Observer', () {
tearDown(() {
TestObserverHostApi.setup(null);
TestInstanceManagerHostApi.setup(null);
});

test('HostApi create makes call to create Observer instance', () {
final MockTestObserverHostApi mockApi = MockTestObserverHostApi();
TestObserverHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand All @@ -47,7 +48,6 @@ void main() {
() {
final MockTestObserverHostApi mockApi = MockTestObserverHostApi();
TestObserverHostApi.setup(mockApi);
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

final Observer<String> cameraStateObserver =
Observer<String>.detached(onChanged: (Object value) {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('PlaneProxy', () {
setUp(() {});

tearDown(() {
TestInstanceManagerHostApi.setup(null);
});

test('FlutterAPI create', () {
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ void main() {
TestInstanceManagerHostApi.setup(MockTestInstanceManagerHostApi());

group('ZoomState', () {
tearDown(() => TestInstanceManagerHostApi.setup(null));

test('flutterApi create makes call to create expected ZoomState', () {
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down

0 comments on commit 75085ed

Please sign in to comment.