Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_list / start_page_browsertest.js
blobb68f5906832669105c1f11de4a2f207342439259
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.
5 /**
6 * TestFixture for kiosk app settings WebUI testing.
7 * @extends {testing.Test}
8 * @constructor
9 */
10 function AppListStartPageWebUITest() {}
12 /**
13 * Mock of audioContext.
14 * @constructor
16 function mockAudioContext() {
17 this.sampleRate = 44100; /* some dummy number */
20 mockAudioContext.prototype = {
21 createMediaStreamSource: function(stream) {
22 return {connect: function(audioProc) {},
23 disconnect: function() {}};
25 createScriptProcessor: function(bufSize, in_channels, out_channels) {
26 return {connect: function(destination) {},
27 disconnect: function() {}};
31 AppListStartPageWebUITest.prototype = {
32 __proto__: testing.Test.prototype,
34 /**
35 * Browser to app launcher start page.
37 browsePreload: 'chrome://app-list/',
39 /**
40 * Recommend apps data.
41 * @private
43 recommendedApps_: [
45 'appId': 'app_id_1',
46 'textTitle': 'app 1',
47 'iconUrl': 'icon_url_1'
50 'appId': 'app_id_2',
51 'textTitle': 'app 2',
52 'iconUrl': 'icon_url_2'
56 /**
57 * Placeholder for mock speech recognizer.
59 speechRecognizer: null,
61 /**
62 * Sends the speech recognition result.
64 * @param {string} result The testing result.
65 * @param {boolean} isFinal Whether the result is final or not.
67 sendSpeechResult: function(result, isFinal) {
68 var speechEvent = new Event('test');
69 // Each result contains a list of alternatives and 'isFinal' flag.
70 var speechResult = [{transcript: result}];
71 speechResult.isFinal = isFinal;
72 speechEvent.results = [speechResult];
73 this.speechRecognizer.onresult(speechEvent);
76 /**
77 * Registers the webkitSpeechRecognition mock for test.
78 * @private
80 registerMockSpeechRecognition_: function() {
81 var owner = this;
82 function mockSpeechRecognition() {
83 this.inSpeech_ = false;
84 owner.speechRecognizer = this;
87 mockSpeechRecognition.prototype = {
88 start: function() {
89 this.onstart();
92 abort: function() {
93 if (this.inSpeech_)
94 this.onspeechend();
95 this.onerror(new Error());
96 this.onend();
100 window.webkitSpeechRecognition = mockSpeechRecognition;
104 * Mock of webkitGetUserMedia for start page.
106 * @private
107 * @param {object} constraint The constraint parameter.
108 * @param {Function} success The success callback.
109 * @param {Function} error The error callback.
111 mockGetUserMedia_: function(constraint, success, error) {
112 function getAudioTracks() {
114 assertTrue(constraint.audio);
115 assertNotEquals(null, error, 'error callback must not be null');
116 var audioTracks = [];
117 for (var i = 0; i < 2; ++i) {
118 audioTracks.push(this.audioTrackMocks[i].proxy());
120 success({getAudioTracks: function() { return audioTracks; }});
123 /** @override */
124 preLoad: function() {
125 this.makeAndRegisterMockHandler(['initialize',
126 'launchApp',
127 'setSpeechRecognitionState',
128 'speechResult']);
129 this.mockHandler.stubs().initialize().will(callFunction(function() {
130 appList.startPage.setRecommendedApps(this.recommendedApps_);
131 }.bind(this)));
132 this.mockHandler.stubs().launchApp(ANYTHING);
134 this.registerMockSpeechRecognition_();
135 window.AudioContext = mockAudioContext;
136 navigator.webkitGetUserMedia = this.mockGetUserMedia_.bind(this);
137 this.audioTrackMocks = [mock(MediaStreamTrack), mock(MediaStreamTrack)];
141 TEST_F('AppListStartPageWebUITest', 'Basic', function() {
142 assertEquals(this.browsePreload, document.location.href);
144 var recommendedApp = $('start-page').querySelector('.recommended-apps');
145 assertEquals(this.recommendedApps_.length, recommendedApp.childElementCount);
146 for (var i = 0; i < recommendedApp.childElementCount; ++i) {
147 assertEquals(this.recommendedApps_[i].appId,
148 recommendedApp.children[i].appId);
152 TEST_F('AppListStartPageWebUITest', 'ClickToLaunch', function() {
153 var recommendedApp = $('start-page').querySelector('.recommended-apps');
154 for (var i = 0; i < recommendedApp.childElementCount; ++i) {
155 this.mockHandler.expects(once()).launchApp(
156 [this.recommendedApps_[i].appId]);
157 cr.dispatchSimpleEvent(recommendedApp.children[i], 'click');
161 TEST_F('AppListStartPageWebUITest', 'SpeechRecognitionState', function() {
162 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
163 appList.startPage.onAppListShown();
164 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
165 appList.startPage.toggleSpeechRecognition();
166 Mock4JS.verifyAllMocks();
167 Mock4JS.clearMocksToVerify();
169 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
170 for (var i = 0; i < this.audioTrackMocks.length; ++i) {
171 this.audioTrackMocks[i].expects(once()).stop();
173 appList.startPage.toggleSpeechRecognition();
174 Mock4JS.verifyAllMocks();
175 Mock4JS.clearMocksToVerify();
177 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
178 appList.startPage.toggleSpeechRecognition();
179 Mock4JS.verifyAllMocks();
180 Mock4JS.clearMocksToVerify();
182 this.mockHandler.expects(once()).setSpeechRecognitionState('STOPPING');
183 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
184 for (var i = 0; i < this.audioTrackMocks.length; ++i) {
185 this.audioTrackMocks[i].expects(once()).stop();
187 appList.startPage.onAppListHidden();
190 TEST_F('AppListStartPageWebUITest', 'SpeechRecognition', function() {
191 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
192 appList.startPage.onAppListShown();
193 this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
194 appList.startPage.toggleSpeechRecognition();
195 Mock4JS.verifyAllMocks();
196 Mock4JS.clearMocksToVerify();
198 this.mockHandler.expects(once()).setSpeechRecognitionState('IN_SPEECH');
199 this.speechRecognizer.onspeechstart();
200 Mock4JS.verifyAllMocks();
201 Mock4JS.clearMocksToVerify();
203 this.mockHandler.expects(once()).speechResult('test,false');
204 this.sendSpeechResult('test', false);
205 Mock4JS.verifyAllMocks();
206 Mock4JS.clearMocksToVerify();
208 this.mockHandler.expects(once()).speechResult('test,true');
209 this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
210 for (var i = 0; i < this.audioTrackMocks.length; ++i) {
211 this.audioTrackMocks[i].expects(once()).stop();
213 this.sendSpeechResult('test', true);