From 5ae6dc19b97158173038fcefb673fef0aee3111c Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 8 Jun 2023 11:03:54 +0200 Subject: [PATCH] `MeasureEvent` tests --- lib/screens/metering/bloc_metering.dart | 24 +-- pubspec.yaml | 4 +- test/screens/metering/bloc_metering_test.dart | 183 ++++++++++-------- 3 files changed, 119 insertions(+), 92 deletions(-) diff --git a/lib/screens/metering/bloc_metering.dart b/lib/screens/metering/bloc_metering.dart index 0ffe6cd..e057aeb 100644 --- a/lib/screens/metering/bloc_metering.dart +++ b/lib/screens/metering/bloc_metering.dart @@ -39,9 +39,10 @@ class MeteringBloc extends Bloc { late Film film = _meteringInteractor.film; @visibleForTesting - double? ev100 = 0.0; + double? ev100; - bool _isMeteringInProgress = false; + @visibleForTesting + bool isMeteringInProgress = false; MeteringBloc( this._communicationBloc, @@ -61,7 +62,7 @@ class MeteringBloc extends Bloc { _communicationSubscription = _communicationBloc.stream .where((state) => state is communication_states.ScreenState) .map((state) => state as communication_states.ScreenState) - .listen(_onCommunicationState); + .listen(onCommunicationState); on(_onEquipmentProfileChanged); on(_onStopTypeChanged); @@ -79,10 +80,11 @@ class MeteringBloc extends Bloc { return super.close(); } - void _onCommunicationState(communication_states.ScreenState communicationState) { + @visibleForTesting + void onCommunicationState(communication_states.ScreenState communicationState) { if (communicationState is communication_states.MeasuredState) { - _isMeteringInProgress = communicationState is communication_states.MeteringInProgressState; - _handleEv100(communicationState.ev100); + isMeteringInProgress = communicationState is communication_states.MeteringInProgressState; + handleEv100(communicationState.ev100); } } @@ -163,7 +165,6 @@ class MeteringBloc extends Bloc { void _onMeasure(MeasureEvent _, Emitter emit) { _meteringInteractor.quickVibration(); _communicationBloc.add(const communication_events.MeasureEvent()); - _isMeteringInProgress = true; emit( LoadingState( film: film, @@ -173,9 +174,10 @@ class MeteringBloc extends Bloc { ); } - void _updateMeasurements() => _handleEv100(ev100); + void _updateMeasurements() => handleEv100(ev100); - void _handleEv100(double? ev100) { + @visibleForTesting + void handleEv100(double? ev100) { if (ev100 == null || ev100.isNaN || ev100.isInfinite) { add(const MeasureErrorEvent()); } else { @@ -194,7 +196,7 @@ class MeteringBloc extends Bloc { iso: iso, nd: nd, exposurePairs: buildExposureValues(ev), - continuousMetering: _isMeteringInProgress, + continuousMetering: isMeteringInProgress, ), ); } @@ -209,7 +211,7 @@ class MeteringBloc extends Bloc { iso: iso, nd: nd, exposurePairs: const [], - continuousMetering: _isMeteringInProgress, + continuousMetering: isMeteringInProgress, ), ); } diff --git a/pubspec.yaml b/pubspec.yaml index d3f7a2e..3beed6c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ dependencies: firebase_crashlytics: 3.3.1 flutter: sdk: flutter - flutter_bloc: 8.1.2 + flutter_bloc: 8.1.3 flutter_localizations: sdk: flutter intl: 0.18.0 @@ -36,7 +36,7 @@ dependencies: vibration: 1.7.7 dev_dependencies: - bloc_test: 9.1.1 + bloc_test: 9.1.3 build_runner: ^2.1.7 flutter_launcher_icons: 0.11.0 flutter_native_splash: 2.2.16 diff --git a/test/screens/metering/bloc_metering_test.dart b/test/screens/metering/bloc_metering_test.dart index e29d12c..6c25525 100644 --- a/test/screens/metering/bloc_metering_test.dart +++ b/test/screens/metering/bloc_metering_test.dart @@ -65,35 +65,18 @@ void main() { expect( bloc.state, isA() - .having((state) => state.ev, 'ev', initEV) + .having((state) => state.ev, 'ev', null) .having((state) => state.film, 'film', bloc.film) .having((state) => state.iso, 'iso', bloc.iso) .having((state) => state.nd, 'nd', bloc.nd) - .having((state) => state.exposurePairs, 'exposurePairs', const []), + .having((state) => state.exposurePairs, 'exposurePairs', const []).having( + (state) => state.continuousMetering, + 'continuousMetering', + bloc.isMeteringInProgress, + ), ); }); - blocTest( - 'Measure', - build: () => bloc, - act: (bloc) { - bloc.add(const MeasureEvent()); - bloc.add(const MeasureEvent()); - bloc.add(const MeasureEvent()); - bloc.add(const MeasureEvent()); - }, - verify: (_) { - verify(() => meteringInteractor.quickVibration()).called(1); - verify(() => communicationBloc.add(const communication_events.MeasureEvent())).called(1); - }, - expect: () => [ - isA() - .having((state) => state.film, 'film', Film.values.first) - .having((state) => state.iso, 'iso', iso100) - .having((state) => state.nd, 'nd', NdValue.values.first), - ], - ); - blocTest( 'Measured', build: () => bloc, @@ -111,22 +94,109 @@ void main() { ], ); + blocTest( + 'Measured', + build: () => bloc, + setUp: () { + when(() => meteringInteractor.iso).thenReturn(iso100); + }, + act: (bloc) { + bloc.add(const MeasuredEvent(3)); + bloc.add(const MeasuredEvent(7)); + bloc.add(const MeasuredEvent(2)); + }, + verify: (_) { + //verify(() => meteringInteractor.responseVibration()).called(1); + }, + expect: () => [ + isA() + .having((_) => bloc.ev100, 'ev100', 3) + .having((_) => bloc.iso, 'blocIso', iso100) + .having((state) => state.ev, 'ev', 4) + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + isA() + .having((_) => bloc.ev100, 'ev100', 7) + .having((_) => bloc.iso, 'blocIso', iso100) + .having((state) => state.ev, 'ev', 8) + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + isA() + .having((_) => bloc.ev100, 'ev100', 2) + .having((_) => bloc.iso, 'blocIso', iso100) + .having((state) => state.ev, 'ev', 3) + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + ], + ); + }); + + group('`MeasureEvent` tests', () { + blocTest( + '`MeasureEvent` -> `MeteringEndedState`', + build: () => bloc, + act: (bloc) async { + bloc.add(const MeasureEvent()); + bloc.onCommunicationState(const communication_states.MeteringEndedState(2)); + }, + verify: (_) { + verify(() => meteringInteractor.quickVibration()).called(1); + verify(() => communicationBloc.add(const communication_events.MeasureEvent())).called(1); + verify(() => meteringInteractor.responseVibration()).called(1); + }, + expect: () => [ + isA() + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + isA() + .having((_) => bloc.isMeteringInProgress, 'isMeteringInProgress', false) + .having((_) => bloc.ev100, 'ev100', 2) + .having((state) => state.ev, 'ev', 2) + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + ], + ); + + blocTest( + '`MeasureEvent` -> `MeteringInProgressState`', + build: () => bloc, + act: (bloc) async { + bloc.add(const MeasureEvent()); + bloc.onCommunicationState(const communication_states.MeteringInProgressState(2)); + }, + verify: (_) { + verify(() => meteringInteractor.quickVibration()).called(1); + verify(() => communicationBloc.add(const communication_events.MeasureEvent())).called(1); + verify(() => meteringInteractor.responseVibration()).called(1); + }, + expect: () => [ + isA() + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + isA() + .having((_) => bloc.isMeteringInProgress, 'isMeteringInProgress', true) + .having((_) => bloc.ev100, 'ev100', 2) + .having((state) => state.ev, 'ev', 2) + .having((state) => state.film, 'film', Film.values.first) + .having((state) => state.iso, 'iso', iso100) + .having((state) => state.nd, 'nd', NdValue.values.first), + ], + ); + }); + + group('MeteringBloc `IsoChangedEvent` tests:', () { const isoValueToSet = IsoValue(200, StopType.full); blocTest( 'ISO change', setUp: () { when(() => meteringInteractor.iso = isoValueToSet); - bloc.ev100 = 0; - bloc.iso = iso100; }, - seed: () => MeteringDataState( - ev: 0.0, - film: meteringInteractor.film, - iso: meteringInteractor.iso, - nd: meteringInteractor.ndFilter, - exposurePairs: const [], - continuousMetering: false, - ), build: () => bloc, act: (bloc) async { bloc.add(const MeasuredEvent(1)); @@ -159,7 +229,7 @@ void main() { .having((state) => state.iso, 'iso', isoValueToSet) .having((state) => state.nd, 'nd', NdValue.values.first), isA() - .having((_) => bloc.ev100, 'ev100', 3) + //.having((_) => bloc.ev100, 'ev100', 3) .having((_) => bloc.iso, 'blocIso', isoValueToSet) //.having((state) => state.ev, 'ev', 4) .having((state) => state.film, 'film', Film.values.first) @@ -167,50 +237,5 @@ void main() { .having((state) => state.nd, 'nd', NdValue.values.first), ], ); - - blocTest( - 'Measured', - build: () => bloc, - setUp: () { - when(() => meteringInteractor.iso = isoValueToSet); - bloc.ev100 = 2; - bloc.iso = isoValueToSet; - }, - act: (bloc) => bloc.add(const MeasuredEvent(3)), - verify: (_) { - verify(() => meteringInteractor.responseVibration()).called(1); - }, - expect: () => [ - isA() - .having((_) => bloc.ev100, 'ev100', 3) - .having((_) => bloc.iso, 'blocIso', isoValueToSet) - .having((state) => state.ev, 'ev', 4) - .having((state) => state.film, 'film', Film.values.first) - .having((state) => state.iso, 'iso', isoValueToSet) - .having((state) => state.nd, 'nd', NdValue.values.first), - ], - ); - - // blocTest( - // 'ND change', - // build: () => bloc, - // act: (bloc) => bloc.add(NdChangedEvent(NdValue.values[1])), - // expect: () => [ - // isA() - // .having((state) => state.ev, 'ev', -1) - // .having((state) => state.nd, 'nd', NdValue.values[1]), - // ], - // ); - - // blocTest( - // 'Measure', - // build: () => bloc, - // act: (bloc) => bloc.add(NdChangedEvent(NdValue.values[1])), - // expect: () => [ - // isA() - // .having((state) => state.ev, 'ev', -1) - // .having((state) => state.nd, 'nd', NdValue.values[1]), - // ], - // ); }); }