This commit is contained in:
Vadim 2023-11-08 18:42:13 +01:00
parent e75eaa74c3
commit a7e75025dd
2 changed files with 83 additions and 23 deletions

View file

@ -215,6 +215,53 @@ void main() {
}, },
); );
testWidgets(
'Set camera features config',
(tester) async {
await pumpTestWidget(
tester,
builder: (context) {
final config = UserPreferencesProvider.cameraConfigOf(context);
return Column(
children: [
...List.generate(
config.length,
(index) => Text('${config.keys.toList()[index]}: ${config.values.toList()[index]}'),
),
...List.generate(
CameraFeature.values.length,
(index) => Text(
'${CameraFeature.values[index]}: ${UserPreferencesProvider.cameraFeatureOf(context, CameraFeature.values[index])}',
),
),
ElevatedButton(
onPressed: () => UserPreferencesProvider.of(context).setCameraFeature({
CameraFeature.spotMetering: true,
CameraFeature.histogram: false,
}),
child: const Text(''),
),
],
);
},
);
// Match `findsNWidgets(2)` to verify that `cameraFeatureOf` specific results are the same as the whole config
expect(find.text("${CameraFeature.spotMetering}: true"), findsNWidgets(2));
expect(find.text("${CameraFeature.histogram}: true"), findsNWidgets(2));
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
expect(find.text("${CameraFeature.spotMetering}: true"), findsNWidgets(2));
expect(find.text("${CameraFeature.histogram}: false"), findsNWidgets(2));
verify(
() => mockUserPreferencesService.cameraFeatures = {
CameraFeature.spotMetering: true,
CameraFeature.histogram: false,
},
).called(1);
},
);
testWidgets( testWidgets(
'Set different locale', 'Set different locale',
(tester) async { (tester) async {

View file

@ -4,10 +4,8 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:lightmeter/interactors/metering_interactor.dart'; import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart'; import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart' import 'package:lightmeter/screens/metering/communication/event_communication_metering.dart' as communication_events;
as communication_events; import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart' as communication_states;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'package:lightmeter/screens/metering/components/camera_container/bloc_container_camera.dart'; import 'package:lightmeter/screens/metering/components/camera_container/bloc_container_camera.dart';
import 'package:lightmeter/screens/metering/components/camera_container/event_container_camera.dart'; import 'package:lightmeter/screens/metering/components/camera_container/event_container_camera.dart';
import 'package:lightmeter/screens/metering/components/camera_container/models/camera_error_type.dart'; import 'package:lightmeter/screens/metering/components/camera_container/models/camera_error_type.dart';
@ -16,9 +14,9 @@ import 'package:mocktail/mocktail.dart';
class _MockMeteringInteractor extends Mock implements MeteringInteractor {} class _MockMeteringInteractor extends Mock implements MeteringInteractor {}
class _MockMeteringCommunicationBloc extends MockBloc< class _MockMeteringCommunicationBloc
communication_events.MeteringCommunicationEvent, extends MockBloc<communication_events.MeteringCommunicationEvent, communication_states.MeteringCommunicationState>
communication_states.MeteringCommunicationState> implements MeteringCommunicationBloc {} implements MeteringCommunicationBloc {}
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
@ -147,8 +145,7 @@ void main() {
verify(() => meteringInteractor.requestCameraPermission()).called(1); verify(() => meteringInteractor.requestCameraPermission()).called(1);
}, },
expect: () => [ expect: () => [
isA<CameraErrorState>() isA<CameraErrorState>().having((state) => state.error, "error", CameraErrorType.permissionNotGranted),
.having((state) => state.error, "error", CameraErrorType.permissionNotGranted),
], ],
); );
@ -166,8 +163,7 @@ void main() {
}, },
expect: () => [ expect: () => [
isA<CameraLoadingState>(), isA<CameraLoadingState>(),
isA<CameraErrorState>() isA<CameraErrorState>().having((state) => state.error, "error", CameraErrorType.permissionNotGranted),
.having((state) => state.error, "error", CameraErrorType.permissionNotGranted),
], ],
); );
@ -215,8 +211,7 @@ void main() {
'No cameras detected error', 'No cameras detected error',
setUp: () { setUp: () {
when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true); when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
.setMockMethodCallHandler(
cameraMethodChannel, cameraMethodChannel,
(methodCall) async => cameraMethodCallSuccessHandler(methodCall, cameras: const []), (methodCall) async => cameraMethodCallSuccessHandler(methodCall, cameras: const []),
); );
@ -232,8 +227,7 @@ void main() {
}, },
expect: () => [ expect: () => [
isA<CameraLoadingState>(), isA<CameraLoadingState>(),
isA<CameraErrorState>() isA<CameraErrorState>().having((state) => state.error, "error", CameraErrorType.noCamerasDetected),
.having((state) => state.error, "error", CameraErrorType.noCamerasDetected),
], ],
); );
@ -241,8 +235,7 @@ void main() {
'No back facing cameras available', 'No back facing cameras available',
setUp: () { setUp: () {
when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true); when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
.setMockMethodCallHandler(
cameraMethodChannel, cameraMethodChannel,
(methodCall) async => cameraMethodCallSuccessHandler(methodCall, cameras: frontCameras), (methodCall) async => cameraMethodCallSuccessHandler(methodCall, cameras: frontCameras),
); );
@ -263,8 +256,7 @@ void main() {
'Catch other initialization errors', 'Catch other initialization errors',
setUp: () { setUp: () {
when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true); when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
.setMockMethodCallHandler(
cameraMethodChannel, cameraMethodChannel,
(methodCall) async { (methodCall) async {
switch (methodCall.method) { switch (methodCall.method) {
@ -300,10 +292,8 @@ void main() {
act: (bloc) async { act: (bloc) async {
bloc.add(const InitializeEvent()); bloc.add(const InitializeEvent());
await Future.delayed(Duration.zero); await Future.delayed(Duration.zero);
TestWidgetsFlutterBinding.instance TestWidgetsFlutterBinding.instance.handleAppLifecycleStateChanged(AppLifecycleState.detached);
.handleAppLifecycleStateChanged(AppLifecycleState.detached); TestWidgetsFlutterBinding.instance.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
TestWidgetsFlutterBinding.instance
.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
}, },
verify: (_) { verify: (_) {
verify(() => meteringInteractor.checkCameraPermission()).called(2); verify(() => meteringInteractor.checkCameraPermission()).called(2);
@ -500,6 +490,29 @@ void main() {
); );
}, },
); );
group(
'`ExposureSpotChangedEvent`',
() {
blocTest<CameraContainerBloc, CameraContainerState>(
'Set exposure spot multiple times',
setUp: () {
when(() => meteringInteractor.checkCameraPermission()).thenAnswer((_) async => true);
},
build: () => bloc,
act: (bloc) async {
bloc.add(const InitializeEvent());
await Future.delayed(Duration.zero);
bloc.add(const ExposureSpotChangedEvent(Offset(0.1, 0.1)));
bloc.add(const ExposureSpotChangedEvent(Offset(1.0, 0.5)));
},
verify: (_) {
verify(() => meteringInteractor.checkCameraPermission()).called(1);
},
expect: () => [...initializedStateSequence],
);
},
);
} }
extension _MethodChannelMock on MethodChannel { extension _MethodChannelMock on MethodChannel {