Skip to content

Commit

Permalink
audio tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Syutkin committed Jan 20, 2025
1 parent 9836574 commit e44abe6
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 16 deletions.
5 changes: 4 additions & 1 deletion integration_test/main_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart' as bloc_concurrency;
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';
Expand Down Expand Up @@ -43,8 +44,10 @@ void main() {
);

final flutterTts = FlutterTts();
final pool =
await AudioPool.createFromAsset(path: 'beeps.mp3', maxPlayers: 2);
final ttsProvider = TtsProvider(flutterTts);
final beepProvider = AudioplayersProvider();
final beepProvider = AudioPoolProvider(pool);

final audioProvider =
AudioProvider(ttsProvider: ttsProvider, beepProvider: beepProvider);
Expand Down
9 changes: 6 additions & 3 deletions integration_test/smoke_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart' as bloc_concurrency;
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:device_info_plus/device_info_plus.dart';
Expand Down Expand Up @@ -45,9 +46,11 @@ void main() async {
bluetoothBackgroundConnection: bluetoothBackgroundConnection,
);

final flutterTts = FlutterTts();
final ttsProvider = TtsProvider(flutterTts);
final beepProvider = AudioplayersProvider();
final flutterTts = FlutterTts();
final pool =
await AudioPool.createFromAsset(path: 'beeps.mp3', maxPlayers: 2);
final ttsProvider = TtsProvider(flutterTts);
final beepProvider = AudioPoolProvider(pool);

final audioProvider =
AudioProvider(ttsProvider: ttsProvider, beepProvider: beepProvider);
Expand Down
5 changes: 4 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:audioplayers/audioplayers.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart' as bloc_concurrency;
import 'package:bot_toast/bot_toast.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
Expand Down Expand Up @@ -60,8 +61,10 @@ Future<void> main() async {
);

final flutterTts = FlutterTts();
final pool =
await AudioPool.createFromAsset(path: 'beeps.mp3', maxPlayers: 2);
final ttsProvider = TtsProvider(flutterTts);
final beepProvider = AudioplayersProvider();
final beepProvider = AudioPoolProvider(pool);

final audioProvider =
AudioProvider(ttsProvider: ttsProvider, beepProvider: beepProvider);
Expand Down
9 changes: 5 additions & 4 deletions lib/src/feature/audio/logic/beep_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ abstract interface class IBeepProvider {
Future<void> beep();
}

class AudioplayersProvider implements IBeepProvider {
AudioPool? _pool;
class AudioPoolProvider implements IBeepProvider {
AudioPoolProvider(this.audioPool);

AudioPool audioPool;

@override
Future<void> beep() async {
_pool ??= await AudioPool.createFromAsset(path: 'beeps.mp3', maxPlayers: 2);
await _pool?.start();
await audioPool.start();
}
}
4 changes: 2 additions & 2 deletions lib/src/feature/audio/logic/tts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class TtsProvider {
final FlutterTts flutterTts;
TtsState ttsState = TtsState.stopped;

Future<String?> get getDefaultEngine async {
return flutterTts.getDefaultEngine as String?;
Future<String> get getDefaultEngine async {
return flutterTts.getDefaultEngine.toString();
}

Future<dynamic> setLanguage(String language) =>
Expand Down
5 changes: 0 additions & 5 deletions test/src/feature/audio/logic/audio_provider_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:entime/src/feature/audio/audio.dart';
import 'package:entime/src/feature/database/database.dart';
import 'package:entime/src/feature/settings/settings.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:entime/src/feature/audio/logic/audio_provider.dart';

class MockTtsProvider extends Mock implements TtsProvider {}

Expand Down
36 changes: 36 additions & 0 deletions test/src/feature/audio/logic/beep_provider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:audioplayers/audioplayers.dart';
import 'package:entime/src/feature/audio/logic/beep_provider.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockAudioPool extends Mock implements AudioPool {}

void main() {
late AudioPool pool;
late IBeepProvider beepProvider;

setUp(() {
pool = MockAudioPool();
beepProvider = AudioPoolProvider(pool);

Future<void> mockStop() {
return Future.value();
}

when(() => pool.start())
.thenAnswer((_) => Future.value(Future.value(mockStop)));
});

group(
'BeepProvider tests',
() {
test(
'BeepProvider beep',
() async {
await beepProvider.beep();
verify(() => pool.start()).called(1);
},
);
},
);
}
92 changes: 92 additions & 0 deletions test/src/feature/audio/logic/tts_provider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:entime/src/feature/audio/logic/tts_provider.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:mocktail/mocktail.dart';

class MockFlutterTts extends Mock implements FlutterTts {}

void main() {
late FlutterTts flutterTts;
late TtsProvider ttsProvider;
const text = 'text';

setUp(() {
flutterTts = MockFlutterTts();
ttsProvider = TtsProvider(flutterTts);

when(() => flutterTts.getDefaultEngine).thenAnswer((_) => Future.value(''));
when(() => flutterTts.setLanguage(any())).thenAnswer((_) => Future.value());
when(() => flutterTts.setVolume(any())).thenAnswer((_) => Future.value());
when(() => flutterTts.setSpeechRate(any()))
.thenAnswer((_) => Future.value());
when(() => flutterTts.setPitch(any())).thenAnswer((_) => Future.value());
when(() => flutterTts.speak(text)).thenAnswer((_) => Future.value(1));
});
group(
'TtsProvider tests',
() {
test(
'TtsProvider initial state',
() async {
expect(ttsProvider.ttsState, TtsState.stopped);
},
);
test(
'TtsProvider getDefaultEngine',
() async {
await ttsProvider.getDefaultEngine;
verify(() => flutterTts.getDefaultEngine).called(1);
},
);
test(
'TtsProvider setLanguage',
() async {
await ttsProvider.setLanguage('language');
verify(() => flutterTts.setLanguage(any())).called(1);
},
);
test(
'TtsProvider setVolume',
() async {
await ttsProvider.setVolume(1);
verify(() => flutterTts.setVolume(any())).called(1);
},
);
test(
'TtsProvider setSpeechRate',
() async {
await ttsProvider.setSpeechRate(1);
verify(() => flutterTts.setSpeechRate(any())).called(1);
},
);
test(
'TtsProvider setPitch',
() async {
await ttsProvider.setPitch(1);
verify(() => flutterTts.setPitch(any())).called(1);
},
);
test(
'TtsProvider change state when call speak',
() async {
await ttsProvider.speak(text);
expect(ttsProvider.ttsState, TtsState.playing);
},
);
test(
'TtsProvider call speak',
() async {
await ttsProvider.speak(text);
verify(() => flutterTts.speak(text)).called(1);
},
);
test(
'TtsProvider call speak but text is empty',
() async {
await ttsProvider.speak('');
verifyNever(() => flutterTts.speak(text));
},
);
},
);
}

0 comments on commit e44abe6

Please sign in to comment.