1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * TestFixture for kiosk app settings WebUI testing.
7 * @extends {testing.Test}
10 function AppListStartPageWebUITest() {}
13 * Mock of audioContext.
16 function mockAudioContext() {
17 this.sampleRate = 44100; /* some dummy number */
20 mockAudioContext.prototype = {
21 createMediaStreamSource: function(stream) {
22 return {connect: function(audioProc) {}};
24 createScriptProcessor: function(bufSize, channels, channels) {
25 return {connect: function(destination) {},
26 disconnect: function() {}};
30 AppListStartPageWebUITest.prototype = {
31 __proto__: testing.Test.prototype,
34 * Browser to app launcher start page.
36 browsePreload: 'chrome://app-list/',
39 * Recommend apps data.
46 'iconUrl': 'icon_url_1'
51 'iconUrl': 'icon_url_2'
56 * Placeholder for mock speech recognizer.
58 speechRecognizer: null,
61 * Sends the speech recognition result.
63 * @param {string} result The testing result.
64 * @param {boolean} isFinal Whether the result is final or not.
66 sendSpeechResult: function(result, isFinal) {
67 var speechEvent = new Event('test');
68 // Each result contains a list of alternatives and 'isFinal' flag.
69 var speechResult = [{transcript: result}];
70 speechResult.isFinal = isFinal;
71 speechEvent.results = [speechResult];
72 this.speechRecognizer.onresult(speechEvent);
76 * Registers the webkitSpeechRecognition mock for test.
79 registerMockSpeechRecognition_: function() {
81 function mockSpeechRecognition() {
82 this.inSpeech_ = false;
83 owner.speechRecognizer = this;
86 mockSpeechRecognition.prototype = {
94 this.onerror(new Error());
99 window.webkitSpeechRecognition = mockSpeechRecognition;
103 * Mock of webkitGetUserMedia for start page.
106 * @param {object} constraint The constraint parameter.
107 * @param {Function} success The success callback.
108 * @param {Function} error The error callback.
110 mockGetUserMedia_: function(constraint, success, error) {
111 assertTrue(constraint.audio);
112 assertNotEquals(null, error, 'error callback must not be null');
117 preLoad: function() {
118 this.makeAndRegisterMockHandler(['initialize',
120 'setSpeechRecognitionState',
122 this.mockHandler.stubs().initialize().will(callFunction(function() {
123 appList.startPage.setRecommendedApps(this.recommendedApps_);
125 this.mockHandler.stubs().launchApp(ANYTHING);
126 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
128 this.registerMockSpeechRecognition_();
129 window.webkitAudioContext = mockAudioContext;
130 navigator.webkitGetUserMedia = this.mockGetUserMedia_.bind(this);
134 TEST_F('AppListStartPageWebUITest', 'Basic', function() {
135 assertEquals(this.browsePreload, document.location.href);
137 var recommendedApp = $('start-page').querySelector('.recommended-apps');
138 assertEquals(this.recommendedApps_.length, recommendedApp.childElementCount);
139 for (var i = 0; i < recommendedApp.childElementCount; ++i) {
140 assertEquals(this.recommendedApps_[i].appId,
141 recommendedApp.children[i].appId);
145 TEST_F('AppListStartPageWebUITest', 'ClickToLaunch', function() {
146 var recommendedApp = $('start-page').querySelector('.recommended-apps');
147 for (var i = 0; i < recommendedApp.childElementCount; ++i) {
148 this.mockHandler.expects(once()).launchApp(
149 [this.recommendedApps_[i].appId]);
150 cr.dispatchSimpleEvent(recommendedApp.children[i], 'click');
154 TEST_F('AppListStartPageWebUITest', 'SpeechRecognitionState', function() {
155 appList.startPage.onAppListShown();
156 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
157 appList.startPage.toggleSpeechRecognition();
158 Mock4JS.verifyAllMocks();
159 Mock4JS.clearMocksToVerify();
161 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
162 appList.startPage.toggleSpeechRecognition();
163 Mock4JS.verifyAllMocks();
164 Mock4JS.clearMocksToVerify();
166 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
167 appList.startPage.toggleSpeechRecognition();
168 Mock4JS.verifyAllMocks();
169 Mock4JS.clearMocksToVerify();
171 this.mockHandler.expects(once()).setSpeechRecognitionState('STOPPING');
172 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
173 appList.startPage.onAppListHidden();
176 TEST_F('AppListStartPageWebUITest', 'SpeechRecognition', function() {
177 appList.startPage.onAppListShown();
178 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
179 appList.startPage.toggleSpeechRecognition();
180 Mock4JS.verifyAllMocks();
181 Mock4JS.clearMocksToVerify();
183 this.mockHandler.expects(once()).setSpeechRecognitionState('IN_SPEECH');
184 this.speechRecognizer.onspeechstart();
185 Mock4JS.verifyAllMocks();
186 Mock4JS.clearMocksToVerify();
188 this.mockHandler.expects(once()).speechResult('test,false');
189 this.sendSpeechResult('test', false);
190 Mock4JS.verifyAllMocks();
191 Mock4JS.clearMocksToVerify();
193 this.mockHandler.expects(once()).speechResult('test,true');
194 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
195 this.sendSpeechResult('test', true);