VolumeKeysNotifier tests

This commit is contained in:
Vadim 2023-11-01 20:45:48 +01:00
parent 3993abb68b
commit 002b7c985a
5 changed files with 61 additions and 9 deletions

View file

@ -3,7 +3,7 @@ import 'package:flutter/services.dart';
import 'package:platform/platform.dart';
class VolumeEventsService {
final LocalPlatform localPlatform;
final LocalPlatform _localPlatform;
@visibleForTesting
static const volumeHandlingChannel = MethodChannel("com.vodemn.lightmeter/volumeHandling");
@ -11,12 +11,12 @@ class VolumeEventsService {
@visibleForTesting
static const volumeEventsChannel = EventChannel("com.vodemn.lightmeter/volumeEvents");
const VolumeEventsService(this.localPlatform);
const VolumeEventsService(this._localPlatform);
/// If set to `false` we allow system to handle key events.
/// Returns current status of volume handling.
Future<bool> setVolumeHandling(bool enableHandling) async {
if (!localPlatform.isAndroid) {
if (!_localPlatform.isAndroid) {
return false;
}
return volumeHandlingChannel
@ -29,7 +29,7 @@ class VolumeEventsService {
/// KEYCODE_VOLUME_DOWN = 25;
/// pressed
Stream<int> volumeButtonsEventStream() {
if (!localPlatform.isAndroid) {
if (!_localPlatform.isAndroid) {
return const Stream.empty();
}
return volumeEventsChannel

View file

@ -10,9 +10,9 @@ import 'package:lightmeter/screens/metering/communication/event_communication_me
as communication_events;
import 'package:lightmeter/screens/metering/communication/state_communication_metering.dart'
as communication_states;
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
import 'package:lightmeter/screens/metering/event_metering.dart';
import 'package:lightmeter/screens/metering/state_metering.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class MeteringBloc extends Bloc<MeteringEvent, MeteringState> {

View file

@ -4,8 +4,8 @@ import 'package:lightmeter/interactors/metering_interactor.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/screens/metering/bloc_metering.dart';
import 'package:lightmeter/screens/metering/communication/bloc_communication_metering.dart';
import 'package:lightmeter/screens/metering/components/shared/volume_keys_notifier/notifier_volume_keys.dart';
import 'package:lightmeter/screens/metering/screen_metering.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
class MeteringFlow extends StatefulWidget {
const MeteringFlow({super.key});

View file

@ -5,12 +5,12 @@ import 'package:lightmeter/data/models/volume_action.dart';
import 'package:lightmeter/data/volume_events_service.dart';
class VolumeKeysNotifier extends ChangeNotifier with RouteAware {
final VolumeEventsService volumeEventsService;
final VolumeEventsService _volumeEventsService;
late final StreamSubscription<VolumeKey> _volumeKeysSubscription;
VolumeKey _value = VolumeKey.up;
VolumeKeysNotifier(this.volumeEventsService) {
_volumeKeysSubscription = volumeEventsService
VolumeKeysNotifier(this._volumeEventsService) {
_volumeKeysSubscription = _volumeEventsService
.volumeButtonsEventStream()
.map((event) => event == 24 ? VolumeKey.up : VolumeKey.down)
.listen((event) {
@ -19,6 +19,8 @@ class VolumeKeysNotifier extends ChangeNotifier with RouteAware {
}
VolumeKey get value => _value;
@protected
set value(VolumeKey newValue) {
_value = newValue;
notifyListeners();

View file

@ -0,0 +1,50 @@
import 'dart:async';
import 'package:lightmeter/data/models/volume_action.dart';
import 'package:lightmeter/data/volume_events_service.dart';
import 'package:lightmeter/screens/metering/utils/notifier_volume_keys.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
class _MockVolumeEventsService extends Mock implements VolumeEventsService {}
class _ValueChanged {
void onChanged(VolumeKey value) {}
}
class _MockValueChanged extends Mock implements _ValueChanged {}
void main() {
late _MockVolumeEventsService mockVolumeEventsService;
setUp(() {
mockVolumeEventsService = _MockVolumeEventsService();
});
test(
'Listen to `volumeButtonsEventStream()`',
() async {
final StreamController<int> volumeButtonsEvents = StreamController<int>();
when(() => mockVolumeEventsService.volumeButtonsEventStream()).thenAnswer((_) => volumeButtonsEvents.stream);
final volumeKeysNotifier = VolumeKeysNotifier(mockVolumeEventsService);
final functions = _MockValueChanged();
volumeKeysNotifier.addListener(() => functions.onChanged(volumeKeysNotifier.value));
expect(volumeKeysNotifier.value, VolumeKey.up);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(25);
volumeButtonsEvents.add(24);
volumeButtonsEvents.add(24);
volumeButtonsEvents.add(25);
await Future.delayed(Duration.zero);
verify(() => functions.onChanged(VolumeKey.up)).called(2);
verify(() => functions.onChanged(VolumeKey.down)).called(4);
volumeKeysNotifier.removeListener(() => functions.onChanged(volumeKeysNotifier.value));
await volumeKeysNotifier.dispose();
await volumeButtonsEvents.close();
},
);
}