Commit 1eb9cf3a authored by mukai@chromium.org's avatar mukai@chromium.org

Adds speech recognition test case to AppListStartPageWebUITest.

BUG=341926
R=xiyuan@chromium.org
TEST=browser_tests

Review URL: https://codereview.chromium.org/159973004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251360 0039d316-1c4b-4281-b951-d872f2087c98
parent fa61f300
...@@ -44,6 +44,9 @@ cr.define('speech', function() { ...@@ -44,6 +44,9 @@ cr.define('speech', function() {
* @private * @private
*/ */
SpeechManager.prototype.setState_ = function(newState) { SpeechManager.prototype.setState_ = function(newState) {
if (this.state == newState)
return;
this.state = newState; this.state = newState;
chrome.send('setSpeechRecognitionState', [this.state]); chrome.send('setSpeechRecognitionState', [this.state]);
}; };
......
...@@ -9,6 +9,24 @@ ...@@ -9,6 +9,24 @@
**/ **/
function AppListStartPageWebUITest() {} function AppListStartPageWebUITest() {}
/**
* Mock of audioContext.
* @constructor
*/
function mockAudioContext() {
this.sampleRate = 44100; /* some dummy number */
}
mockAudioContext.prototype = {
createMediaStreamSource: function(stream) {
return {connect: function(audioProc) {}};
},
createScriptProcessor: function(bufSize, channels, channels) {
return {connect: function(destination) {},
disconnect: function() {}};
}
};
AppListStartPageWebUITest.prototype = { AppListStartPageWebUITest.prototype = {
__proto__: testing.Test.prototype, __proto__: testing.Test.prototype,
...@@ -31,18 +49,85 @@ AppListStartPageWebUITest.prototype = { ...@@ -31,18 +49,85 @@ AppListStartPageWebUITest.prototype = {
'appId': 'app_id_2', 'appId': 'app_id_2',
'textTitle': 'app 2', 'textTitle': 'app 2',
'iconUrl': 'icon_url_2' 'iconUrl': 'icon_url_2'
}, }
], ],
/**
* Placeholder for mock speech recognizer.
*/
speechRecognizer: null,
/**
* Sends the speech recognition result.
*
* @param {string} result The testing result.
* @param {boolean} isFinal Whether the result is final or not.
*/
sendSpeechResult: function(result, isFinal) {
var speechEvent = new Event('test');
// Each result contains a list of alternatives and 'isFinal' flag.
var speechResult = [{transcript: result}];
speechResult.isFinal = isFinal;
speechEvent.results = [speechResult];
this.speechRecognizer.onresult(speechEvent);
},
/**
* Registers the webkitSpeechRecognition mock for test.
* @private
*/
registerMockSpeechRecognition_: function() {
var owner = this;
function mockSpeechRecognition() {
this.inSpeech_ = false;
owner.speechRecognizer = this;
}
mockSpeechRecognition.prototype = {
start: function() {
this.onstart();
},
abort: function() {
if (this.inSpeech_)
this.onspeechend();
this.onerror(new Error());
this.onend();
}
},
window.webkitSpeechRecognition = mockSpeechRecognition;
},
/**
* Mock of webkitGetUserMedia for start page.
*
* @private
* @param {object} constraint The constraint parameter.
* @param {Function} success The success callback.
* @param {Function} error The error callback.
*/
mockGetUserMedia_: function(constraint, success, error) {
assertTrue(constraint.audio);
assertNotEquals(null, error, 'error callback must not be null');
success();
},
/** @override */ /** @override */
preLoad: function() { preLoad: function() {
this.makeAndRegisterMockHandler( this.makeAndRegisterMockHandler(['initialize',
['initialize', 'launchApp', 'setSpeechRecognitionState']); 'launchApp',
'setSpeechRecognitionState',
'speechResult']);
this.mockHandler.stubs().initialize().will(callFunction(function() { this.mockHandler.stubs().initialize().will(callFunction(function() {
appList.startPage.setRecommendedApps(this.recommendedApps_); appList.startPage.setRecommendedApps(this.recommendedApps_);
}.bind(this))); }.bind(this)));
this.mockHandler.stubs().launchApp(ANYTHING); this.mockHandler.stubs().launchApp(ANYTHING);
this.mockHandler.expects(once()).setSpeechRecognitionState('READY'); this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
this.registerMockSpeechRecognition_();
window.webkitAudioContext = mockAudioContext;
navigator.webkitGetUserMedia = this.mockGetUserMedia_.bind(this);
} }
}; };
...@@ -65,3 +150,47 @@ TEST_F('AppListStartPageWebUITest', 'ClickToLaunch', function() { ...@@ -65,3 +150,47 @@ TEST_F('AppListStartPageWebUITest', 'ClickToLaunch', function() {
cr.dispatchSimpleEvent(recommendedApp.children[i], 'click'); cr.dispatchSimpleEvent(recommendedApp.children[i], 'click');
} }
}); });
TEST_F('AppListStartPageWebUITest', 'SpeechRecognitionState', function() {
appList.startPage.onAppListShown();
this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
appList.startPage.toggleSpeechRecognition();
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
appList.startPage.toggleSpeechRecognition();
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
appList.startPage.toggleSpeechRecognition();
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).setSpeechRecognitionState('STOPPING');
this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
appList.startPage.onAppListHidden();
});
TEST_F('AppListStartPageWebUITest', 'SpeechRecognition', function() {
appList.startPage.onAppListShown();
this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
appList.startPage.toggleSpeechRecognition();
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).setSpeechRecognitionState('IN_SPEECH');
this.speechRecognizer.onspeechstart();
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).speechResult('test,false');
this.sendSpeechResult('test', false);
Mock4JS.verifyAllMocks();
Mock4JS.clearMocksToVerify();
this.mockHandler.expects(once()).speechResult('test,true');
this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
this.sendSpeechResult('test', true);
});
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