Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / app_list / start_page_browsertest.js
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.
4
5 /**
6  * TestFixture for kiosk app settings WebUI testing.
7  * @extends {testing.Test}
8  * @constructor
9  **/
10 function AppListStartPageWebUITest() {}
11
12 /**
13  * Mock of audioContext.
14  * @constructor
15  */
16 function mockAudioContext() {
17   this.sampleRate = 44100; /* some dummy number */
18 }
19
20 mockAudioContext.prototype = {
21   createMediaStreamSource: function(stream) {
22     return {connect: function(audioProc) {}};
23   },
24   createScriptProcessor: function(bufSize, channels, channels) {
25     return {connect: function(destination) {},
26             disconnect: function() {}};
27   }
28 };
29
30 AppListStartPageWebUITest.prototype = {
31   __proto__: testing.Test.prototype,
32
33   /**
34    * Browser to app launcher start page.
35    */
36   browsePreload: 'chrome://app-list/',
37
38   /**
39    * Recommend apps data.
40    * @private
41    */
42   recommendedApps_: [
43     {
44       'appId': 'app_id_1',
45       'textTitle': 'app 1',
46       'iconUrl': 'icon_url_1'
47     },
48     {
49       'appId': 'app_id_2',
50       'textTitle': 'app 2',
51       'iconUrl': 'icon_url_2'
52     }
53   ],
54
55   /**
56    * Placeholder for mock speech recognizer.
57    */
58   speechRecognizer: null,
59
60   /**
61    * Sends the speech recognition result.
62    *
63    * @param {string} result The testing result.
64    * @param {boolean} isFinal Whether the result is final or not.
65    */
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);
73   },
74
75   /**
76    * Registers the webkitSpeechRecognition mock for test.
77    * @private
78    */
79   registerMockSpeechRecognition_: function() {
80     var owner = this;
81     function mockSpeechRecognition() {
82       this.inSpeech_ = false;
83       owner.speechRecognizer = this;
84     }
85
86     mockSpeechRecognition.prototype = {
87       start: function() {
88         this.onstart();
89       },
90
91       abort: function() {
92         if (this.inSpeech_)
93           this.onspeechend();
94         this.onerror(new Error());
95         this.onend();
96       }
97     },
98
99     window.webkitSpeechRecognition = mockSpeechRecognition;
100   },
101
102   /**
103    * Mock of webkitGetUserMedia for start page.
104    *
105    * @private
106    * @param {object} constraint The constraint parameter.
107    * @param {Function} success The success callback.
108    * @param {Function} error The error callback.
109    */
110   mockGetUserMedia_: function(constraint, success, error) {
111     assertTrue(constraint.audio);
112     assertNotEquals(null, error, 'error callback must not be null');
113     success();
114   },
115
116   /** @override */
117   preLoad: function() {
118     this.makeAndRegisterMockHandler(['initialize',
119                                      'launchApp',
120                                      'setSpeechRecognitionState',
121                                      'speechResult']);
122     this.mockHandler.stubs().initialize().will(callFunction(function() {
123       appList.startPage.setRecommendedApps(this.recommendedApps_);
124     }.bind(this)));
125     this.mockHandler.stubs().launchApp(ANYTHING);
126     this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
127
128     this.registerMockSpeechRecognition_();
129     window.webkitAudioContext = mockAudioContext;
130     navigator.webkitGetUserMedia = this.mockGetUserMedia_.bind(this);
131   }
132 };
133
134 TEST_F('AppListStartPageWebUITest', 'Basic', function() {
135   assertEquals(this.browsePreload, document.location.href);
136
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);
142   }
143 });
144
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');
151   }
152 });
153
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();
160
161   this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
162   appList.startPage.toggleSpeechRecognition();
163   Mock4JS.verifyAllMocks();
164   Mock4JS.clearMocksToVerify();
165
166   this.mockHandler.expects(once()).setSpeechRecognitionState('RECOGNIZING');
167   appList.startPage.toggleSpeechRecognition();
168   Mock4JS.verifyAllMocks();
169   Mock4JS.clearMocksToVerify();
170
171   this.mockHandler.expects(once()).setSpeechRecognitionState('STOPPING');
172   this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
173   appList.startPage.onAppListHidden();
174 });
175
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();
182
183   this.mockHandler.expects(once()).setSpeechRecognitionState('IN_SPEECH');
184   this.speechRecognizer.onspeechstart();
185   Mock4JS.verifyAllMocks();
186   Mock4JS.clearMocksToVerify();
187
188   this.mockHandler.expects(once()).speechResult('test,false');
189   this.sendSpeechResult('test', false);
190   Mock4JS.verifyAllMocks();
191   Mock4JS.clearMocksToVerify();
192
193   this.mockHandler.expects(once()).speechResult('test,true');
194   this.mockHandler.expects(once()).setSpeechRecognitionState('READY');
195   this.sendSpeechResult('test', true);
196 });