From 59f8267391b2b2bb155e8e29c6b204d8ba92348b Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 9 Jul 2023 12:41:01 +0200 Subject: [PATCH] tests --- .../bloc_container_light_sensor.dart | 22 +++---- .../event_container_light_sensor.dart | 4 ++ test/screens/metering/bloc_metering_test.dart | 21 +++++++ .../bloc_communication_metering_test.dart | 26 ++++++++ .../bloc_container_light_sensor_test.dart | 62 +++++++++++++++++++ 5 files changed, 123 insertions(+), 12 deletions(-) diff --git a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart index 9441fe2..23cd796 100644 --- a/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/bloc_container_light_sensor.dart @@ -25,6 +25,7 @@ class LightSensorContainerBloc communicationBloc, const LightSensorContainerState(null), ) { + on(_onStartLuxMeteringEvent); on(_onLuxMeteringEvent); on(_onCancelLuxMeteringEvent); } @@ -34,22 +35,27 @@ class LightSensorContainerBloc switch (communicationState) { case communication_states.MeasureState(): if (_luxSubscriptions == null) { - _startMetering(); + add(const StartLuxMeteringEvent()); } else { - _cancelMetering(); + add(const CancelLuxMeteringEvent()); } case communication_states.SettingsOpenedState(): - _cancelMetering(); + add(const CancelLuxMeteringEvent()); default: } } @override Future close() async { - _cancelMetering(); + communicationBloc.add(communication_event.MeteringEndedEvent(state.ev100)); + _luxSubscriptions?.cancel().then((_) => _luxSubscriptions = null); return super.close(); } + void _onStartLuxMeteringEvent(StartLuxMeteringEvent event, _) { + _luxSubscriptions = _meteringInteractor.luxStream().listen((lux) => add(LuxMeteringEvent(lux))); + } + void _onLuxMeteringEvent(LuxMeteringEvent event, Emitter emit) { final ev100 = log2(event.lux.toDouble() / 2.5) + _meteringInteractor.lightSensorEvCalibration; emit(LightSensorContainerState(ev100)); @@ -57,14 +63,6 @@ class LightSensorContainerBloc } void _onCancelLuxMeteringEvent(CancelLuxMeteringEvent event, _) { - _cancelMetering(); - } - - void _startMetering() { - _luxSubscriptions = _meteringInteractor.luxStream().listen((lux) => add(LuxMeteringEvent(lux))); - } - - void _cancelMetering() { communicationBloc.add(communication_event.MeteringEndedEvent(state.ev100)); _luxSubscriptions?.cancel().then((_) => _luxSubscriptions = null); } diff --git a/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart b/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart index 5fd69da..bffcadd 100644 --- a/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart +++ b/lib/screens/metering/components/light_sensor_container/event_container_light_sensor.dart @@ -2,6 +2,10 @@ abstract class LightSensorContainerEvent { const LightSensorContainerEvent(); } +class StartLuxMeteringEvent extends LightSensorContainerEvent { + const StartLuxMeteringEvent(); +} + class LuxMeteringEvent extends LightSensorContainerEvent { final int lux; diff --git a/test/screens/metering/bloc_metering_test.dart b/test/screens/metering/bloc_metering_test.dart index 60c6f34..121d103 100644 --- a/test/screens/metering/bloc_metering_test.dart +++ b/test/screens/metering/bloc_metering_test.dart @@ -668,4 +668,25 @@ void main() { ); }, ); + + group( + '`SettingOpenedEvent`/`SettingsClosedEvent`', + () { + blocTest( + 'Settings opened & closed', + build: () => bloc, + act: (bloc) async { + bloc.add(const SettingsOpenedEvent()); + bloc.add(const SettingsClosedEvent()); + }, + verify: (_) { + verify(() => communicationBloc.add(const communication_events.SettingsOpenedEvent())) + .called(1); + verify(() => communicationBloc.add(const communication_events.SettingsClosedEvent())) + .called(1); + }, + expect: () => [], + ); + }, + ); } diff --git a/test/screens/metering/communication/bloc_communication_metering_test.dart b/test/screens/metering/communication/bloc_communication_metering_test.dart index 604de75..a04aab7 100644 --- a/test/screens/metering/communication/bloc_communication_metering_test.dart +++ b/test/screens/metering/communication/bloc_communication_metering_test.dart @@ -98,4 +98,30 @@ void main() { ); }, ); + + group( + '`SettingsOpenedEvent`/`SettingsClosedEvent`', + () { + blocTest( + 'Multiple consequtive settings events', + build: () => bloc, + act: (bloc) async { + bloc.add(const SettingsOpenedEvent()); + bloc.add(const SettingsOpenedEvent()); + bloc.add(const SettingsOpenedEvent()); + bloc.add(const SettingsClosedEvent()); + bloc.add(const SettingsClosedEvent()); + bloc.add(const SettingsClosedEvent()); + bloc.add(const SettingsOpenedEvent()); + bloc.add(const SettingsClosedEvent()); + }, + expect: () => [ + isA(), + isA(), + isA(), + isA(), + ], + ); + }, + ); } diff --git a/test/screens/metering/components/light_sensor/bloc_container_light_sensor_test.dart b/test/screens/metering/components/light_sensor/bloc_container_light_sensor_test.dart index f60a79e..dc1e470 100644 --- a/test/screens/metering/components/light_sensor/bloc_container_light_sensor_test.dart +++ b/test/screens/metering/components/light_sensor/bloc_container_light_sensor_test.dart @@ -78,4 +78,66 @@ void main() { ); }, ); + + group( + '`communication_states.SettingsOpenedState()`', + () { + const List luxIterable = [1, 2, 2, 2, 3]; + final List resultList = luxIterable.map((lux) => log2(lux / 2.5)).toList(); + blocTest( + 'Metering is already canceled', + build: () => bloc, + setUp: () { + when(() => meteringInteractor.luxStream()) + .thenAnswer((_) => Stream.fromIterable(luxIterable)); + when(() => meteringInteractor.lightSensorEvCalibration).thenReturn(0.0); + }, + act: (bloc) async { + bloc.onCommunicationState(const communication_states.SettingsOpenedState()); + }, + verify: (_) { + verifyNever(() => meteringInteractor.luxStream().listen((_) {})); + verifyNever(() => meteringInteractor.lightSensorEvCalibration); + verify(() { + communicationBloc.add(const communication_events.MeteringEndedEvent(null)); + }).called(2); // +1 from dispose + }, + expect: () => [], + ); + + blocTest( + 'Metering is in progress', + build: () => bloc, + setUp: () { + when(() => meteringInteractor.luxStream()) + .thenAnswer((_) => Stream.fromIterable(luxIterable)); + when(() => meteringInteractor.lightSensorEvCalibration).thenReturn(0.0); + }, + act: (bloc) async { + bloc.onCommunicationState(const communication_states.MeasureState()); + await Future.delayed(Duration.zero); + bloc.onCommunicationState(const communication_states.SettingsOpenedState()); + }, + verify: (_) { + verify(() => meteringInteractor.luxStream().listen((_) {})).called(1); + verify(() => meteringInteractor.lightSensorEvCalibration).called(5); + verify(() { + communicationBloc.add(communication_events.MeteringInProgressEvent(resultList.first)); + }).called(1); + verify(() { + communicationBloc.add(communication_events.MeteringInProgressEvent(resultList[1])); + }).called(3); + verify(() { + communicationBloc.add(communication_events.MeteringInProgressEvent(resultList.last)); + }).called(1); + verify(() { + communicationBloc.add(communication_events.MeteringEndedEvent(resultList.last)); + }).called(2); // +1 from dispose + }, + expect: () => resultList.map( + (e) => isA().having((state) => state.ev100, 'ev100', e), + ), + ); + }, + ); }