Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setPauseFor method for late setup #403

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions speech_to_text/lib/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ class SpeechToText {
throw ListenFailedException(e.message, e.details, e.stacktrace);
}
}
void setPauseFor(Duration pauseFor) {
//Setup new pauseFor only if listen is active and pauseFor is different
if ((_listenTimer?.isActive ?? false) && (_pauseFor == null || _pauseFor!.compareTo(pauseFor) != 0)) {
_listenTimer?.cancel();
_listenTimer = null;
_setupListenAndPause(pauseFor, _listenFor);
} else {
throw ListenNotStartedException();
}
}

void _setupListenAndPause(
Duration? initialPauseFor, Duration? initialListenFor) {
Expand Down Expand Up @@ -658,3 +668,5 @@ class ListenFailedException implements Exception {
final String? stackTrace;
ListenFailedException(this.message, [this.details, this.stackTrace]);
}

class ListenNotStartedException implements Exception {}
54 changes: 54 additions & 0 deletions speech_to_text/test/speech_to_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,60 @@ void main() {
expect(speech.isListening, isTrue);
});
});
test('trows on setPauseFor when not listening', () async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
testPlatform.onStatus!(SpeechToText.notListeningStatus);
fa.flushMicrotasks();
expect(speech.isListening, isFalse);
try {
speech.setPauseFor(Duration(seconds: 5));
fail('Should have thrown');
} on ListenNotStartedException {
// This is a good result
} catch (wrongE) {
fail('Should have been ListenNotStartedException');
}
});
});
test('stops listen after late setPauseFor with no speech', () async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
speech.listen(pauseFor: Duration(seconds: 2));
testPlatform.onStatus!(SpeechToText.listeningStatus);
fa.flushMicrotasks();
expect(speech.isListening, isTrue);
fa.elapse(Duration(seconds: 1));
speech.setPauseFor(Duration(seconds: 5));
fa.flushMicrotasks();
fa.elapse(Duration(seconds: 3));
expect(speech.isListening, isTrue);
fa.elapse(Duration(seconds: 2));
expect(speech.isListening, isFalse);
});
});
test('keeps listening after late setPauseFor with speech event', () async {
fakeAsync((fa) {
speech.initialize();
fa.flushMicrotasks();
speech.listen(pauseFor: Duration(seconds: 2));
testPlatform.onStatus!(SpeechToText.listeningStatus);
fa.flushMicrotasks();
fa.elapse(Duration(seconds: 1));
expect(speech.isListening, isTrue);
speech.setPauseFor(Duration(seconds: 5));
fa.flushMicrotasks();
fa.elapse(Duration(seconds: 3));
expect(speech.isListening, isTrue);
testPlatform
.onTextRecognition!(TestSpeechChannelHandler.firstRecognizedJson);
fa.flushMicrotasks();
fa.elapse(Duration(seconds: 3));
expect(speech.isListening, isTrue);
});
});
test('creates finalResult true if none provided', () async {
fakeAsync((fa) {
speech.initialize(finalTimeout: Duration(milliseconds: 100));
Expand Down