This commit is contained in:
Vadim 2023-07-09 12:41:01 +02:00
parent c50baa4802
commit 59f8267391
5 changed files with 123 additions and 12 deletions

View file

@ -25,6 +25,7 @@ class LightSensorContainerBloc
communicationBloc,
const LightSensorContainerState(null),
) {
on<StartLuxMeteringEvent>(_onStartLuxMeteringEvent);
on<LuxMeteringEvent>(_onLuxMeteringEvent);
on<CancelLuxMeteringEvent>(_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<void> 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<LightSensorContainerState> 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);
}

View file

@ -2,6 +2,10 @@ abstract class LightSensorContainerEvent {
const LightSensorContainerEvent();
}
class StartLuxMeteringEvent extends LightSensorContainerEvent {
const StartLuxMeteringEvent();
}
class LuxMeteringEvent extends LightSensorContainerEvent {
final int lux;

View file

@ -668,4 +668,25 @@ void main() {
);
},
);
group(
'`SettingOpenedEvent`/`SettingsClosedEvent`',
() {
blocTest<MeteringBloc, MeteringState>(
'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: () => [],
);
},
);
}

View file

@ -98,4 +98,30 @@ void main() {
);
},
);
group(
'`SettingsOpenedEvent`/`SettingsClosedEvent`',
() {
blocTest<MeteringCommunicationBloc, MeteringCommunicationState>(
'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<SettingsOpenedState>(),
isA<SettingsClosedState>(),
isA<SettingsOpenedState>(),
isA<SettingsClosedState>(),
],
);
},
);
}

View file

@ -78,4 +78,66 @@ void main() {
);
},
);
group(
'`communication_states.SettingsOpenedState()`',
() {
const List<int> luxIterable = [1, 2, 2, 2, 3];
final List<double> resultList = luxIterable.map((lux) => log2(lux / 2.5)).toList();
blocTest<LightSensorContainerBloc, LightSensorContainerState>(
'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<LightSensorContainerBloc, LightSensorContainerState>(
'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<LightSensorContainerState>().having((state) => state.ev100, 'ev100', e),
),
);
},
);
}