Commit 2b5b968f authored by Ondrej Skopek's avatar Ondrej Skopek Committed by Commit Bot

[Local NTP Voice] Small refactoring.

Removed toggleStartStop and made start/stop public.
Refactored placement of constants in files.
Added unknown error string.
Changed maxAlternative to 1.

Bug: 583291
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I203636264c495dd6b450d80b60c5796f41bb73c1
Reviewed-on: https://chromium-review.googlesource.com/660281
Commit-Queue: Ondrej Škopek <oskopek@google.com>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501230}
parent 251a3311
...@@ -7971,6 +7971,10 @@ I don't think this site should be blocked! ...@@ -7971,6 +7971,10 @@ I don't think this site should be blocked!
desc="Used to indicate that the speech recognition service didn't receive any voice input."> desc="Used to indicate that the speech recognition service didn't receive any voice input.">
Please check your microphone and audio levels. Please check your microphone and audio levels.
</message> </message>
<message name="IDS_NEW_TAB_VOICE_OTHER_ERROR"
desc="Used to indicate that the speech recognition service failed in an unknown way.">
Unknown error.
</message>
<message name="IDS_NEW_TAB_VOICE_PERMISSION_ERROR" <message name="IDS_NEW_TAB_VOICE_PERMISSION_ERROR"
desc="Used to indicate that the speech recognition service did not get permission to use the microphone."> desc="Used to indicate that the speech recognition service did not get permission to use the microphone.">
Voice search has been turned off. Voice search has been turned off.
......
...@@ -86,6 +86,7 @@ let speech = {}; ...@@ -86,6 +86,7 @@ let speech = {};
* networkError: string, * networkError: string,
* noTranslation: string, * noTranslation: string,
* noVoice: string, * noVoice: string,
* otherError: string,
* permissionError: string, * permissionError: string,
* ready: string, * ready: string,
* tryAgain: string, * tryAgain: string,
...@@ -101,6 +102,7 @@ speech.messages = { ...@@ -101,6 +102,7 @@ speech.messages = {
networkError: '', networkError: '',
noTranslation: '', noTranslation: '',
noVoice: '', noVoice: '',
otherError: '',
permissionError: '', permissionError: '',
ready: '', ready: '',
tryAgain: '', tryAgain: '',
...@@ -279,12 +281,12 @@ speech.init = function( ...@@ -279,12 +281,12 @@ speech.init = function(
fakeboxMicrophoneElem.onmouseup = function(event) { fakeboxMicrophoneElem.onmouseup = function(event) {
// If propagated, closes the overlay (click on the background). // If propagated, closes the overlay (click on the background).
event.stopPropagation(); event.stopPropagation();
speech.toggleStartStop(); speech.start();
}; };
fakeboxMicrophoneElem.onkeydown = function(event) { fakeboxMicrophoneElem.onkeydown = function(event) {
if (!event.repeat && if (!event.repeat &&
(event.code == KEYCODE.ENTER || event.code == KEYCODE.NUMPAD_ENTER)) { (event.code == KEYCODE.ENTER || event.code == KEYCODE.NUMPAD_ENTER)) {
speech.toggleStartStop(); speech.start();
} }
}; };
window.addEventListener('keydown', speech.onKeyDown); window.addEventListener('keydown', speech.onKeyDown);
...@@ -304,6 +306,7 @@ speech.init = function( ...@@ -304,6 +306,7 @@ speech.init = function(
networkError: translatedStrings.networkError, networkError: translatedStrings.networkError,
noTranslation: translatedStrings.noTranslation, noTranslation: translatedStrings.noTranslation,
noVoice: translatedStrings.noVoice, noVoice: translatedStrings.noVoice,
otherError: translatedStrings.otherError,
permissionError: translatedStrings.permissionError, permissionError: translatedStrings.permissionError,
ready: translatedStrings.ready, ready: translatedStrings.ready,
tryAgain: translatedStrings.tryAgain, tryAgain: translatedStrings.tryAgain,
...@@ -324,7 +327,6 @@ speech.initWebkitSpeech_ = function() { ...@@ -324,7 +327,6 @@ speech.initWebkitSpeech_ = function() {
speech.recognition_.continuous = false; speech.recognition_.continuous = false;
speech.recognition_.interimResults = true; speech.recognition_.interimResults = true;
speech.recognition_.lang = getChromeUILanguage(); speech.recognition_.lang = getChromeUILanguage();
speech.recognition_.maxAlternatives = 4;
speech.recognition_.onaudiostart = speech.handleRecognitionAudioStart_; speech.recognition_.onaudiostart = speech.handleRecognitionAudioStart_;
speech.recognition_.onend = speech.handleRecognitionEnd_; speech.recognition_.onend = speech.handleRecognitionEnd_;
speech.recognition_.onerror = speech.handleRecognitionError_; speech.recognition_.onerror = speech.handleRecognitionError_;
...@@ -337,9 +339,8 @@ speech.initWebkitSpeech_ = function() { ...@@ -337,9 +339,8 @@ speech.initWebkitSpeech_ = function() {
/** /**
* Sets up the necessary states for voice search and then starts the * Sets up the necessary states for voice search and then starts the
* speech recognition interface. * speech recognition interface.
* @private
*/ */
speech.start_ = function() { speech.start = function() {
view.show(); view.show();
speech.resetIdleTimer_(speech.IDLE_TIMEOUT_MS_); speech.resetIdleTimer_(speech.IDLE_TIMEOUT_MS_);
...@@ -352,7 +353,7 @@ speech.start_ = function() { ...@@ -352,7 +353,7 @@ speech.start_ = function() {
speech.initWebkitSpeech_(); speech.initWebkitSpeech_();
} }
// If |speech.start_()| is called too soon after |speech.stop_()| then the // If |speech.start()| is called too soon after |speech.stop()| then the
// recognition interface hasn't yet reset and an error occurs. In this case // recognition interface hasn't yet reset and an error occurs. In this case
// we need to hard-reset it and reissue the |recognition_.start()| command. // we need to hard-reset it and reissue the |recognition_.start()| command.
try { try {
...@@ -364,7 +365,7 @@ speech.start_ = function() { ...@@ -364,7 +365,7 @@ speech.start_ = function() {
speech.recognition_.start(); speech.recognition_.start();
speech.currentState_ = speech.State_.STARTED; speech.currentState_ = speech.State_.STARTED;
} catch (error2) { } catch (error2) {
speech.stop_(); speech.stop();
} }
} }
}; };
...@@ -372,9 +373,8 @@ speech.start_ = function() { ...@@ -372,9 +373,8 @@ speech.start_ = function() {
/** /**
* Hides the overlay and resets the speech state. * Hides the overlay and resets the speech state.
* @private
*/ */
speech.stop_ = function() { speech.stop = function() {
speech.recognition_.abort(); speech.recognition_.abort();
speech.currentState_ = speech.State_.STOPPED; speech.currentState_ = speech.State_.STOPPED;
view.hide(); view.hide();
...@@ -586,13 +586,13 @@ speech.onKeyDown = function(event) { ...@@ -586,13 +586,13 @@ speech.onKeyDown = function(event) {
event.ctrlKey || (speech.isUserAgentMac_() && event.metaKey); event.ctrlKey || (speech.isUserAgentMac_() && event.metaKey);
if (speech.currentState_ == speech.State_.READY && if (speech.currentState_ == speech.State_.READY &&
event.code == KEYCODE.PERIOD && event.shiftKey && ctrlKeyPressed) { event.code == KEYCODE.PERIOD && event.shiftKey && ctrlKeyPressed) {
speech.toggleStartStop(); speech.start();
} }
} else { } else {
// Ensures that keyboard events are not propagated during voice input. // Ensures that keyboard events are not propagated during voice input.
event.stopPropagation(); event.stopPropagation();
if (event.code == KEYCODE.ESC) { if (event.code == KEYCODE.ESC) {
speech.stop_(); speech.stop();
} else if ( } else if (
(event.code == KEYCODE.ENTER || event.code == KEYCODE.NUMPAD_ENTER) && (event.code == KEYCODE.ENTER || event.code == KEYCODE.NUMPAD_ENTER) &&
speech.finalResult_) { speech.finalResult_) {
...@@ -621,7 +621,7 @@ speech.onIdleTimeout_ = function() { ...@@ -621,7 +621,7 @@ speech.onIdleTimeout_ = function() {
case speech.State_.SPEECH_RECEIVED: case speech.State_.SPEECH_RECEIVED:
case speech.State_.RESULT_RECEIVED: case speech.State_.RESULT_RECEIVED:
case speech.State_.ERROR_RECEIVED: case speech.State_.ERROR_RECEIVED:
speech.stop_(); speech.stop();
break; break;
} }
}; };
...@@ -638,7 +638,7 @@ speech.onVisibilityChange_ = function() { ...@@ -638,7 +638,7 @@ speech.onVisibilityChange_ = function() {
} }
if (document.webkitHidden) { if (document.webkitHidden) {
speech.stop_(); speech.stop();
} }
}; };
...@@ -648,7 +648,7 @@ speech.onVisibilityChange_ = function() { ...@@ -648,7 +648,7 @@ speech.onVisibilityChange_ = function() {
*/ */
speech.onOmniboxFocused = function() { speech.onOmniboxFocused = function() {
if (!speech.isUiDefinitelyHidden_()) { if (!speech.isUiDefinitelyHidden_()) {
speech.stop_(); speech.stop();
} }
}; };
...@@ -684,7 +684,7 @@ speech.submitFinalResult_ = function() { ...@@ -684,7 +684,7 @@ speech.submitFinalResult_ = function() {
const queryUrl = new URL('/search', speech.googleBaseUrl_); const queryUrl = new URL('/search', speech.googleBaseUrl_);
queryUrl.search = searchParams; queryUrl.search = searchParams;
speech.stop_(); speech.stop();
speech.navigateToUrl_(queryUrl); speech.navigateToUrl_(queryUrl);
}; };
...@@ -761,7 +761,7 @@ speech.resetIdleTimer_ = function(duration) { ...@@ -761,7 +761,7 @@ speech.resetIdleTimer_ = function(duration) {
*/ */
speech.resetErrorTimer_ = function(duration) { speech.resetErrorTimer_ = function(duration) {
window.clearTimeout(speech.errorTimer_); window.clearTimeout(speech.errorTimer_);
speech.errorTimer_ = window.setTimeout(speech.stop_, duration); speech.errorTimer_ = window.setTimeout(speech.stop, duration);
}; };
...@@ -801,18 +801,6 @@ speech.isUiDefinitelyHidden_ = function() { ...@@ -801,18 +801,6 @@ speech.isUiDefinitelyHidden_ = function() {
}; };
/**
* Toggles starting and stopping of speech recognition by the speech tool.
*/
speech.toggleStartStop = function() {
if (speech.currentState_ == speech.State_.READY) {
speech.start_();
} else {
speech.stop_();
}
};
/** /**
* Handles click events during speech recognition. * Handles click events during speech recognition.
* @param {boolean} shouldSubmit True if a query should be submitted. * @param {boolean} shouldSubmit True if a query should be submitted.
...@@ -830,17 +818,16 @@ speech.onClick_ = function(shouldSubmit, shouldRetry, navigatingAway) { ...@@ -830,17 +818,16 @@ speech.onClick_ = function(shouldSubmit, shouldRetry, navigatingAway) {
// If the user clicks on a "Learn more" or "Details" support page link // If the user clicks on a "Learn more" or "Details" support page link
// from an error message, do nothing, and let Chrome navigate to that page. // from an error message, do nothing, and let Chrome navigate to that page.
} else { } else {
speech.stop_(); speech.stop();
} }
}; };
/** /**
* Restarts voice recognition. Used for the 'Try again' error link. * Restarts voice recognition. Used for the 'Try again' error link.
* @private
*/ */
speech.restart = function() { speech.restart = function() {
speech.reset_(); speech.reset_();
speech.toggleStartStop(); speech.start();
}; };
...@@ -928,6 +915,15 @@ text.INITIALIZING_TIMEOUT_MS_ = 300; ...@@ -928,6 +915,15 @@ text.INITIALIZING_TIMEOUT_MS_ = 300;
text.LISTENING_TIMEOUT_MS_ = 2000; text.LISTENING_TIMEOUT_MS_ = 2000;
/**
* Base link target for help regarding voice search. To be appended
* with a locale string for proper target site localization.
* @const @private
*/
text.SUPPORT_LINK_BASE_ =
'https://support.google.com/chrome/?p=ui_voice_search&hl=';
/** /**
* The final / high confidence speech recognition result element. * The final / high confidence speech recognition result element.
* @private {Element} * @private {Element}
...@@ -956,15 +952,6 @@ text.initializingTimer_; ...@@ -956,15 +952,6 @@ text.initializingTimer_;
text.listeningTimer_; text.listeningTimer_;
/**
* Base link target for help regarding voice search. To be appended
* with a locale string for proper target site localization.
* @const @private
*/
text.SUPPORT_LINK_BASE_ =
'https://support.google.com/chrome/?p=ui_voice_search&hl=';
/** /**
* Finds the text view elements. * Finds the text view elements.
*/ */
...@@ -1061,7 +1048,7 @@ text.getErrorMessage_ = function(error) { ...@@ -1061,7 +1048,7 @@ text.getErrorMessage_ = function(error) {
case RecognitionError.LANGUAGE_NOT_SUPPORTED: case RecognitionError.LANGUAGE_NOT_SUPPORTED:
return speech.messages.languageError; return speech.messages.languageError;
default: default:
return ''; return speech.messages.otherError;
} }
}; };
......
...@@ -141,6 +141,8 @@ std::unique_ptr<base::DictionaryValue> GetTranslatedStrings(bool is_google) { ...@@ -141,6 +141,8 @@ std::unique_ptr<base::DictionaryValue> GetTranslatedStrings(bool is_google) {
AddString(translated_strings.get(), "tryAgain", AddString(translated_strings.get(), "tryAgain",
IDS_NEW_TAB_VOICE_TRY_AGAIN); IDS_NEW_TAB_VOICE_TRY_AGAIN);
AddString(translated_strings.get(), "waiting", IDS_NEW_TAB_VOICE_WAITING); AddString(translated_strings.get(), "waiting", IDS_NEW_TAB_VOICE_WAITING);
AddString(translated_strings.get(), "otherError",
IDS_NEW_TAB_VOICE_OTHER_ERROR);
} }
return translated_strings; return translated_strings;
......
...@@ -42,6 +42,7 @@ test.speech.TEST_STRINGS = { ...@@ -42,6 +42,7 @@ test.speech.TEST_STRINGS = {
networkError: 'Network error', networkError: 'Network error',
noTranslation: 'No translation', noTranslation: 'No translation',
noVoice: 'No voice', noVoice: 'No voice',
otherError: 'Unknown error',
permissionError: 'Permission error', permissionError: 'Permission error',
ready: 'Ready', ready: 'Ready',
tryAgain: 'Try again', tryAgain: 'Try again',
...@@ -158,7 +159,7 @@ test.speech.setUp = function() { ...@@ -158,7 +159,7 @@ test.speech.setUp = function() {
}; };
this.continuous = false; this.continuous = false;
this.interimResults = false; this.interimResults = false;
this.maxAlternatives = 0; this.maxAlternatives = 1;
this.onerror = null; this.onerror = null;
this.onnomatch = null; this.onnomatch = null;
this.onend = null; this.onend = null;
...@@ -178,10 +179,12 @@ test.speech.setUp = function() { ...@@ -178,10 +179,12 @@ test.speech.setUp = function() {
* Tests if the controller has the correct speech recognition settings. * Tests if the controller has the correct speech recognition settings.
*/ */
test.speech.testSpeechRecognitionInitSettings = function() { test.speech.testSpeechRecognitionInitSettings = function() {
test.speech.recognitionStubs.reset();
test.speech.initSpeech(); test.speech.initSpeech();
assertFalse(speech.recognition_.continuous); assertFalse(speech.recognition_.continuous);
assertEquals('en-ZA', speech.recognition_.lang); assertEquals('en-ZA', speech.recognition_.lang);
assertEquals(4, speech.recognition_.maxAlternatives); assertEquals(1, speech.recognition_.maxAlternatives);
assert(!!speech.recognition_); assert(!!speech.recognition_);
test.speech.validateInactive(); test.speech.validateInactive();
}; };
...@@ -230,7 +233,7 @@ test.speech.testFakeboxClickStartsSpeechWithWorkingView = function() { ...@@ -230,7 +233,7 @@ test.speech.testFakeboxClickStartsSpeechWithWorkingView = function() {
*/ */
test.speech.testOmniboxFocusWithWorkingView = function() { test.speech.testOmniboxFocusWithWorkingView = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.start_(); speech.start();
assertEquals(speech.State_.STARTED, speech.currentState_); assertEquals(speech.State_.STARTED, speech.currentState_);
assertEquals(1, test.speech.recognitionActiveCount); assertEquals(1, test.speech.recognitionActiveCount);
...@@ -254,7 +257,7 @@ test.speech.testClickHandlingWithUnitializedSpeechRecognition = function() { ...@@ -254,7 +257,7 @@ test.speech.testClickHandlingWithUnitializedSpeechRecognition = function() {
speech.recognition_ = undefined; speech.recognition_ = undefined;
assertEquals(speech.State_.READY, speech.currentState_); assertEquals(speech.State_.READY, speech.currentState_);
assert(!speech.recognition_); assert(!speech.recognition_);
speech.toggleStartStop(); speech.start();
assertEquals(1, test.speech.recognitionActiveCount); assertEquals(1, test.speech.recognitionActiveCount);
assertEquals(1, test.speech.viewActiveCount); assertEquals(1, test.speech.viewActiveCount);
...@@ -268,7 +271,7 @@ test.speech.testClickHandlingWithUnitializedSpeechRecognition = function() { ...@@ -268,7 +271,7 @@ test.speech.testClickHandlingWithUnitializedSpeechRecognition = function() {
*/ */
test.speech.testHandleAudioStart = function() { test.speech.testHandleAudioStart = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
assertTrue('ready' in test.speech.viewState); assertTrue('ready' in test.speech.viewState);
...@@ -284,7 +287,7 @@ test.speech.testHandleAudioStart = function() { ...@@ -284,7 +287,7 @@ test.speech.testHandleAudioStart = function() {
*/ */
test.speech.testHandleSpeechStart = function() { test.speech.testHandleSpeechStart = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
...@@ -307,7 +310,7 @@ test.speech.testHandleInterimSpeechResponse = function() { ...@@ -307,7 +310,7 @@ test.speech.testHandleInterimSpeechResponse = function() {
const responseEvent = const responseEvent =
test.speech.createInterimResponse(lowConfidenceText, highConfidenceText); test.speech.createInterimResponse(lowConfidenceText, highConfidenceText);
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.recognition_.onresult(responseEvent); speech.recognition_.onresult(responseEvent);
...@@ -331,7 +334,7 @@ test.speech.testHandleFinalSpeechResponse = function() { ...@@ -331,7 +334,7 @@ test.speech.testHandleFinalSpeechResponse = function() {
const responseEvent = const responseEvent =
test.speech.createFinalResponse(lowConfidenceText, highConfidenceText); test.speech.createFinalResponse(lowConfidenceText, highConfidenceText);
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
// Handle a final transcript from the recognition API. // Handle a final transcript from the recognition API.
...@@ -363,13 +366,13 @@ test.speech.testInterruptSpeechInputAfterInterimResult = function() { ...@@ -363,13 +366,13 @@ test.speech.testInterruptSpeechInputAfterInterimResult = function() {
const responseEvent = const responseEvent =
test.speech.createInterimResponse(lowConfidenceText, highConfidenceText); test.speech.createInterimResponse(lowConfidenceText, highConfidenceText);
speech.start_(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
// Handle an interim trancript from the recognition API. // Handle an interim trancript from the recognition API.
speech.recognition_.onresult(responseEvent); speech.recognition_.onresult(responseEvent);
// The user interrupts speech. // The user interrupts speech.
speech.stop_(); speech.stop();
assertFalse(speech.isRecognizing_()); assertFalse(speech.isRecognizing_());
assertEquals('', speech.interimResult_); assertEquals('', speech.interimResult_);
...@@ -393,10 +396,10 @@ test.speech.testInterruptSpeechInputAfterInterimResult = function() { ...@@ -393,10 +396,10 @@ test.speech.testInterruptSpeechInputAfterInterimResult = function() {
*/ */
test.speech.testInterruptSpeechInputBeforeResult = function() { test.speech.testInterruptSpeechInputBeforeResult = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.start_(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.stop_(); speech.stop();
test.speech.validateInactive(); test.speech.validateInactive();
}; };
...@@ -408,7 +411,7 @@ test.speech.testInterruptSpeechInputBeforeResult = function() { ...@@ -408,7 +411,7 @@ test.speech.testInterruptSpeechInputBeforeResult = function() {
*/ */
test.speech.testSpeechRecognitionErrorTimeout = function() { test.speech.testSpeechRecognitionErrorTimeout = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onerror({error: 'some-error'}); speech.recognition_.onerror({error: 'some-error'});
assertFalse(speech.isRecognizing_()); assertFalse(speech.isRecognizing_());
...@@ -431,7 +434,7 @@ test.speech.testSpeechRecognitionErrorTimeout = function() { ...@@ -431,7 +434,7 @@ test.speech.testSpeechRecognitionErrorTimeout = function() {
*/ */
test.speech.testNoSpeechInput = function() { test.speech.testNoSpeechInput = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onend(null); speech.recognition_.onend(null);
...@@ -480,7 +483,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() { ...@@ -480,7 +483,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() {
test.speech.initSpeech(); test.speech.initSpeech();
assertRecognitionHandlers(true); assertRecognitionHandlers(true);
speech.start_(); speech.start();
assertRecognitionHandlers(true); assertRecognitionHandlers(true);
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
...@@ -497,7 +500,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() { ...@@ -497,7 +500,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() {
test.speech.validateInactive(); test.speech.validateInactive();
assertRecognitionHandlers(true); assertRecognitionHandlers(true);
speech.start_(); speech.start();
assertRecognitionHandlers(true); assertRecognitionHandlers(true);
}; };
...@@ -505,7 +508,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() { ...@@ -505,7 +508,7 @@ test.speech.testRecognitionHandlersStayInitialized = function() {
/** /**
* Tests starting and stopping the Speech Recognition API quickly * Tests starting and stopping the Speech Recognition API quickly
* in succession. * in succession.
* Motivation: If |speech.start_()| is called too soon after |speech.stop_()|, * Motivation: If |speech.start()| is called too soon after |speech.stop()|,
* then the recognition interface hasn't yet reset and an error occurs. * then the recognition interface hasn't yet reset and an error occurs.
* In this case we need to hard-reset it and reissue the |recognition_.start()| * In this case we need to hard-reset it and reissue the |recognition_.start()|
* command. * command.
...@@ -514,7 +517,7 @@ test.speech.testStopStartErrorHandling = function() { ...@@ -514,7 +517,7 @@ test.speech.testStopStartErrorHandling = function() {
test.speech.recognitionStubs.reset(); test.speech.recognitionStubs.reset();
test.speech.initSpeech(); test.speech.initSpeech();
speech.start_(); speech.start();
assertEquals(speech.State_.STARTED, speech.currentState_); assertEquals(speech.State_.STARTED, speech.currentState_);
assertTrue(speech.isRecognizing_()); assertTrue(speech.isRecognizing_());
...@@ -522,15 +525,15 @@ test.speech.testStopStartErrorHandling = function() { ...@@ -522,15 +525,15 @@ test.speech.testStopStartErrorHandling = function() {
assertEquals(speech.State_.AUDIO_RECEIVED, speech.currentState_); assertEquals(speech.State_.AUDIO_RECEIVED, speech.currentState_);
assertTrue(speech.isRecognizing_()); assertTrue(speech.isRecognizing_());
speech.stop_(); speech.stop();
assertEquals(speech.State_.READY, speech.currentState_); assertEquals(speech.State_.READY, speech.currentState_);
test.speech.validateInactive(); test.speech.validateInactive();
speech.start_(); speech.start();
assertEquals(speech.State_.STARTED, speech.currentState_); assertEquals(speech.State_.STARTED, speech.currentState_);
assertTrue(speech.isRecognizing_()); assertTrue(speech.isRecognizing_());
speech.stop_(); speech.stop();
assertEquals(speech.State_.READY, speech.currentState_); assertEquals(speech.State_.READY, speech.currentState_);
test.speech.validateInactive(); test.speech.validateInactive();
}; };
...@@ -572,7 +575,7 @@ test.speech.testStopStartKeyboardShortcutErrorHandling = function() { ...@@ -572,7 +575,7 @@ test.speech.testStopStartKeyboardShortcutErrorHandling = function() {
*/ */
test.speech.testEnterToSubmit = function() { test.speech.testEnterToSubmit = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.finalResult_ = 'test query'; speech.finalResult_ = 'test query';
...@@ -596,7 +599,7 @@ test.speech.testEnterToSubmit = function() { ...@@ -596,7 +599,7 @@ test.speech.testEnterToSubmit = function() {
*/ */
test.speech.testClickToSubmit = function() { test.speech.testClickToSubmit = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.finalResult_ = 'test query'; speech.finalResult_ = 'test query';
...@@ -680,7 +683,7 @@ test.speech.testKeyboardStartWithCmd = function() { ...@@ -680,7 +683,7 @@ test.speech.testKeyboardStartWithCmd = function() {
*/ */
test.speech.testClickToAbort = function() { test.speech.testClickToAbort = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.onClick_( speech.onClick_(
...@@ -695,7 +698,7 @@ test.speech.testClickToAbort = function() { ...@@ -695,7 +698,7 @@ test.speech.testClickToAbort = function() {
*/ */
test.speech.testClickToRetryWhenStopped = function() { test.speech.testClickToRetryWhenStopped = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
// An onend event after onpeechstart forces an error and stops recognition. // An onend event after onpeechstart forces an error and stops recognition.
...@@ -715,7 +718,7 @@ test.speech.testClickToRetryWhenStopped = function() { ...@@ -715,7 +718,7 @@ test.speech.testClickToRetryWhenStopped = function() {
*/ */
test.speech.testClickToRetryWhenNotStopped = function() { test.speech.testClickToRetryWhenNotStopped = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.onClick_( speech.onClick_(
...@@ -731,7 +734,7 @@ test.speech.testClickToRetryWhenNotStopped = function() { ...@@ -731,7 +734,7 @@ test.speech.testClickToRetryWhenNotStopped = function() {
*/ */
test.speech.testNoSpeechInputMatched = function() { test.speech.testNoSpeechInputMatched = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.recognition_.onnomatch(null); speech.recognition_.onnomatch(null);
...@@ -762,7 +765,7 @@ test.speech.testIdleTimeoutWithConfidentSpeechResults = function() { ...@@ -762,7 +765,7 @@ test.speech.testIdleTimeoutWithConfidentSpeechResults = function() {
test.speech.createInterimResponse(lowConfidenceText, highConfidenceText); test.speech.createInterimResponse(lowConfidenceText, highConfidenceText);
speech.reset_(); speech.reset_();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.recognition_.onresult(responseEvent); speech.recognition_.onresult(responseEvent);
...@@ -800,7 +803,7 @@ test.speech.testIdleTimeoutWithNonConfidentSpeechResults = function() { ...@@ -800,7 +803,7 @@ test.speech.testIdleTimeoutWithNonConfidentSpeechResults = function() {
test.speech.createInterimResponse(lowConfidenceText, highConfidenceText); test.speech.createInterimResponse(lowConfidenceText, highConfidenceText);
speech.reset_(); speech.reset_();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.recognition_.onresult(responseEvent); speech.recognition_.onresult(responseEvent);
...@@ -823,7 +826,7 @@ test.speech.testIdleTimeoutWithNonConfidentSpeechResults = function() { ...@@ -823,7 +826,7 @@ test.speech.testIdleTimeoutWithNonConfidentSpeechResults = function() {
*/ */
test.speech.testQueryEncoding = function() { test.speech.testQueryEncoding = function() {
test.speech.initSpeech(); test.speech.initSpeech();
speech.toggleStartStop(); speech.start();
speech.recognition_.onaudiostart(null); speech.recognition_.onaudiostart(null);
speech.recognition_.onspeechstart(null); speech.recognition_.onspeechstart(null);
speech.finalResult_ = '🔍t&qôr 文字+weird*chär%?s?'; speech.finalResult_ = '🔍t&qôr 文字+weird*chär%?s?';
......
...@@ -48,6 +48,7 @@ test.text.setUp = function() { ...@@ -48,6 +48,7 @@ test.text.setUp = function() {
networkError: 'Network error', networkError: 'Network error',
noTranslation: 'No translation', noTranslation: 'No translation',
noVoice: 'No voice', noVoice: 'No voice',
otherError: 'Unknown error',
permissionError: 'Permission error', permissionError: 'Permission error',
ready: 'Ready', ready: 'Ready',
tryAgain: 'Try again', tryAgain: 'Try again',
......
...@@ -16,12 +16,13 @@ test.view = {}; ...@@ -16,12 +16,13 @@ test.view = {};
/** /**
* The set of textual strings for different states. * The set of textual strings for different states.
* @enum {string} * @const
*/ */
test.view.Text = { test.view.TEXT = {
WAITING: 'Waiting...', BLANK: '',
ERROR: 'Error',
SPEAK_NOW: 'Speak now', SPEAK_NOW: 'Speak now',
BLANK: '' WAITING: 'Waiting...'
}; };
...@@ -80,20 +81,24 @@ test.view.setUp = function() { ...@@ -80,20 +81,24 @@ test.view.setUp = function() {
// Mock text area manipulating functions. // Mock text area manipulating functions.
test.view.stubs.replace(text, 'showInitializingMessage', function() { test.view.stubs.replace(text, 'showInitializingMessage', function() {
// Ignore the short timeout before showing "Waiting...". // Ignore the short timeout before showing "Waiting...".
test.view.interimText = test.view.Text.WAITING; test.view.interimText = test.view.TEXT.WAITING;
test.view.finalText = test.view.Text.BLANK; test.view.finalText = test.view.TEXT.BLANK;
}); });
test.view.stubs.replace(text, 'showReadyMessage', function() { test.view.stubs.replace(text, 'showReadyMessage', function() {
test.view.interimText = test.view.Text.SPEAK_NOW; test.view.interimText = test.view.TEXT.SPEAK_NOW;
test.view.finalText = test.view.Text.BLANK; test.view.finalText = test.view.TEXT.BLANK;
});
test.view.stubs.replace(text, 'showErrorMessage', function() {
test.view.interimText = test.view.TEXT.ERROR;
test.view.finalText = test.view.TEXT.BLANK;
}); });
test.view.stubs.replace(text, 'updateTextArea', function(texti, textf = '') { test.view.stubs.replace(text, 'updateTextArea', function(texti, textf = '') {
test.view.interimText = texti; test.view.interimText = texti;
test.view.finalText = textf; test.view.finalText = textf;
}); });
test.view.stubs.replace(text, 'clear', function() { test.view.stubs.replace(text, 'clear', function() {
test.view.interimText = test.view.Text.BLANK; test.view.interimText = test.view.TEXT.BLANK;
test.view.finalText = test.view.Text.BLANK; test.view.finalText = test.view.TEXT.BLANK;
}); });
// Mock level animation state. // Mock level animation state.
...@@ -127,8 +132,8 @@ test.view.testShowWithReadyElements = function() { ...@@ -127,8 +132,8 @@ test.view.testShowWithReadyElements = function() {
view.show(); view.show();
test.view.assertViewActive( test.view.assertViewActive(
/*interim=*/test.view.Text.WAITING, /*interim=*/test.view.TEXT.WAITING,
/*final=*/test.view.Text.BLANK, /*final=*/test.view.TEXT.BLANK,
/*containerClass=*/view.INACTIVE_CLASS_, /*containerClass=*/view.INACTIVE_CLASS_,
/*levelAnimationActive=*/false); /*levelAnimationActive=*/false);
}; };
...@@ -143,8 +148,8 @@ test.view.testShowCalledTwice = function() { ...@@ -143,8 +148,8 @@ test.view.testShowCalledTwice = function() {
view.show(); view.show();
test.view.assertViewActive( test.view.assertViewActive(
/*interim=*/test.view.Text.WAITING, /*interim=*/test.view.TEXT.WAITING,
/*final=*/test.view.Text.BLANK, /*final=*/test.view.TEXT.BLANK,
/*containerClass=*/view.INACTIVE_CLASS_, /*containerClass=*/view.INACTIVE_CLASS_,
/*levelAnimationActive=*/false); /*levelAnimationActive=*/false);
}; };
...@@ -173,8 +178,8 @@ test.view.testAudioDeviceReady = function() { ...@@ -173,8 +178,8 @@ test.view.testAudioDeviceReady = function() {
view.setReadyForSpeech(); view.setReadyForSpeech();
test.view.assertViewActive( test.view.assertViewActive(
/*interim=*/test.view.Text.SPEAK_NOW, /*interim=*/test.view.TEXT.SPEAK_NOW,
/*final=*/test.view.Text.BLANK, /*final=*/test.view.TEXT.BLANK,
/*containerClass=*/view.MICROPHONE_LISTENING_CLASS_, /*containerClass=*/view.MICROPHONE_LISTENING_CLASS_,
/*levelAnimationActive=*/false); /*levelAnimationActive=*/false);
}; };
...@@ -202,8 +207,8 @@ test.view.testSpeechStartWithWorkingViews = function() { ...@@ -202,8 +207,8 @@ test.view.testSpeechStartWithWorkingViews = function() {
view.setReceivingSpeech(); view.setReceivingSpeech();
test.view.assertViewActive( test.view.assertViewActive(
/*interim=*/test.view.Text.SPEAK_NOW, /*interim=*/test.view.TEXT.SPEAK_NOW,
/*final=*/test.view.Text.BLANK, /*final=*/test.view.TEXT.BLANK,
/*containerClass=*/view.RECEIVING_SPEECH_CLASS_, /*containerClass=*/view.RECEIVING_SPEECH_CLASS_,
/*levelAnimationActive=*/true); /*levelAnimationActive=*/true);
}; };
...@@ -458,8 +463,8 @@ test.view.testShowingUnknownErrorDoesNotProduceAnError = function() { ...@@ -458,8 +463,8 @@ test.view.testShowingUnknownErrorDoesNotProduceAnError = function() {
view.showError(RecognitionError.OTHER); view.showError(RecognitionError.OTHER);
test.view.assertViewActive( test.view.assertViewActive(
/*interim=*/test.view.Text.BLANK, /*interim=*/test.view.TEXT.ERROR,
/*final=*/test.view.Text.BLANK, /*final=*/test.view.TEXT.BLANK,
/*containerClass=*/view.ERROR_RECEIVED_CLASS_, /*containerClass=*/view.ERROR_RECEIVED_CLASS_,
/*levelAnimationActive=*/false); /*levelAnimationActive=*/false);
}; };
...@@ -477,8 +482,8 @@ test.view.assertViewInactive = function() { ...@@ -477,8 +482,8 @@ test.view.assertViewInactive = function() {
assertFalse(view.isNoMatchShown_); assertFalse(view.isNoMatchShown_);
assertEquals(view.OVERLAY_HIDDEN_CLASS_, view.background_.className); assertEquals(view.OVERLAY_HIDDEN_CLASS_, view.background_.className);
assertEquals(test.view.Text.BLANK, test.view.interimText); assertEquals(test.view.TEXT.BLANK, test.view.interimText);
assertEquals(test.view.Text.BLANK, test.view.finalText); assertEquals(test.view.TEXT.BLANK, test.view.finalText);
assertEquals(view.INACTIVE_CLASS_, view.container_.className); assertEquals(view.INACTIVE_CLASS_, view.container_.className);
assertFalse(test.view.levelAnimationActive); assertFalse(test.view.levelAnimationActive);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment