| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview This file contains the |MockFeedback| class which is | 6 * @fileoverview This file contains the |MockFeedback| class which is |
| 7 * a combined mock class for speech, braille, and earcon feedback. A | 7 * a combined mock class for speech, braille, and earcon feedback. A |
| 8 * test that uses this class may add expectations for speech | 8 * test that uses this class may add expectations for speech |
| 9 * utterances, braille display content to be output, and earcons | 9 * utterances, braille display content to be output, and earcons |
| 10 * played (by name). The |install| method sets appropriate mock | 10 * played (by name). The |install| method sets appropriate mock |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 * @return {MockFeedback} |this| for chaining | 146 * @return {MockFeedback} |this| for chaining |
| 147 */ | 147 */ |
| 148 expectSpeech: function() { | 148 expectSpeech: function() { |
| 149 assertFalse(this.replaying_); | 149 assertFalse(this.replaying_); |
| 150 Array.prototype.forEach.call(arguments, function(text) { | 150 Array.prototype.forEach.call(arguments, function(text) { |
| 151 this.pendingActions_.push({ | 151 this.pendingActions_.push({ |
| 152 perform: function() { | 152 perform: function() { |
| 153 return !!MockFeedback.matchAndConsume_( | 153 return !!MockFeedback.matchAndConsume_( |
| 154 text, {}, this.pendingUtterances_); | 154 text, {}, this.pendingUtterances_); |
| 155 }.bind(this), | 155 }.bind(this), |
| 156 toString: function() { return 'Speak \'' + text + '\''; } | 156 toString: function() { |
| 157 return 'Speak \'' + text + '\''; |
| 158 } |
| 157 }); | 159 }); |
| 158 }.bind(this)); | 160 }.bind(this)); |
| 159 return this; | 161 return this; |
| 160 }, | 162 }, |
| 161 | 163 |
| 162 /** | 164 /** |
| 163 * Adds an expectation for one spoken utterance that will be enqueued | 165 * Adds an expectation for one spoken utterance that will be enqueued |
| 164 * with a given queue mode. | 166 * with a given queue mode. |
| 165 * @param {string|RegExp} text One utterance expectation. | 167 * @param {string|RegExp} text One utterance expectation. |
| 166 * @param {cvox.QueueMode} queueMode The expected queue mode. | 168 * @param {cvox.QueueMode} queueMode The expected queue mode. |
| 167 * @return {MockFeedback} |this| for chaining | 169 * @return {MockFeedback} |this| for chaining |
| 168 */ | 170 */ |
| 169 expectSpeechWithQueueMode: function(text, queueMode) { | 171 expectSpeechWithQueueMode: function(text, queueMode) { |
| 170 assertFalse(this.replaying_); | 172 assertFalse(this.replaying_); |
| 171 this.pendingActions_.push({ | 173 this.pendingActions_.push({ |
| 172 perform: function() { | 174 perform: function() { |
| 173 return !!MockFeedback.matchAndConsume_( | 175 return !!MockFeedback.matchAndConsume_( |
| 174 text, {queueMode: queueMode}, this.pendingUtterances_); | 176 text, {queueMode: queueMode}, this.pendingUtterances_); |
| 175 }.bind(this), | 177 }.bind(this), |
| 176 toString: function() { | 178 toString: function() { |
| 177 return 'Speak \'' + text + '\' with mode ' + queueMode; | 179 return 'Speak \'' + text + '\' with mode ' + queueMode; |
| 178 } | 180 } |
| 179 }); | 181 }); |
| 180 return this; | 182 return this; |
| 181 }, | 183 }, |
| 182 | 184 |
| 183 /** | 185 /** |
| 184 * Adds an expectation for one spoken utterance that will be queued. | 186 * Adds an expectation for one spoken utterance that will be queued. |
| 185 * @param {string|RegExp} text One utterance expectation. | 187 * @param {string|RegExp} text One utterance expectation. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 this.pendingActions_.push({ | 228 this.pendingActions_.push({ |
| 227 perform: function() { | 229 perform: function() { |
| 228 if (this.pendingUtterances_.length == 0) | 230 if (this.pendingUtterances_.length == 0) |
| 229 return false; | 231 return false; |
| 230 if (MockFeedback.matchAndConsume_( | 232 if (MockFeedback.matchAndConsume_( |
| 231 text, {}, this.pendingUtterances_)) { | 233 text, {}, this.pendingUtterances_)) { |
| 232 throw new Error('Got disallowed utterance "' + text + '".'); | 234 throw new Error('Got disallowed utterance "' + text + '".'); |
| 233 } | 235 } |
| 234 return true; | 236 return true; |
| 235 }.bind(this), | 237 }.bind(this), |
| 236 toString: function() { return 'Do not speak \'' + text + '\''; } | 238 toString: function() { |
| 239 return 'Do not speak \'' + text + '\''; |
| 240 } |
| 237 }); | 241 }); |
| 238 }.bind(this)); | 242 }.bind(this)); |
| 239 return this; | 243 return this; |
| 240 }, | 244 }, |
| 241 | 245 |
| 242 /** | 246 /** |
| 243 * Adds an expectation for braille output. | 247 * Adds an expectation for braille output. |
| 244 * @param {string|RegExp} text | 248 * @param {string|RegExp} text |
| 245 * @param {Object=} opt_props Additional properties to match in the | 249 * @param {Object=} opt_props Additional properties to match in the |
| 246 * |NavBraille| | 250 * |NavBraille| |
| 247 * @return {MockFeedback} |this| for chaining | 251 * @return {MockFeedback} |this| for chaining |
| 248 */ | 252 */ |
| 249 expectBraille: function(text, opt_props) { | 253 expectBraille: function(text, opt_props) { |
| 250 assertFalse(this.replaying_); | 254 assertFalse(this.replaying_); |
| 251 var props = opt_props || {}; | 255 var props = opt_props || {}; |
| 252 this.pendingActions_.push({ | 256 this.pendingActions_.push({ |
| 253 perform: function() { | 257 perform: function() { |
| 254 var match = MockFeedback.matchAndConsume_( | 258 var match = |
| 255 text, props, this.pendingBraille_); | 259 MockFeedback.matchAndConsume_(text, props, this.pendingBraille_); |
| 256 if (match) | 260 if (match) |
| 257 this.lastMatchedBraille_ = match; | 261 this.lastMatchedBraille_ = match; |
| 258 return !!match; | 262 return !!match; |
| 259 }.bind(this), | 263 }.bind(this), |
| 260 toString: function() { | 264 toString: function() { |
| 261 return 'Braille \'' + text + '\' ' + JSON.stringify(props); | 265 return 'Braille \'' + text + '\' ' + JSON.stringify(props); |
| 262 } | 266 } |
| 263 }); | 267 }); |
| 264 return this; | 268 return this; |
| 265 }, | 269 }, |
| 266 | 270 |
| 267 /** | 271 /** |
| 268 * Adds an expectation for a played earcon. | 272 * Adds an expectation for a played earcon. |
| 269 * @param {string} earconName The name of the earcon. | 273 * @param {string} earconName The name of the earcon. |
| 270 * @return {MockFeedback} |this| for chaining | 274 * @return {MockFeedback} |this| for chaining |
| 271 */ | 275 */ |
| 272 expectEarcon: function(earconName, opt_props) { | 276 expectEarcon: function(earconName, opt_props) { |
| 273 assertFalse(this.replaying_); | 277 assertFalse(this.replaying_); |
| 274 this.pendingActions_.push({ | 278 this.pendingActions_.push({ |
| 275 perform: function() { | 279 perform: function() { |
| 276 var match = MockFeedback.matchAndConsume_( | 280 var match = |
| 277 earconName, {}, this.pendingEarcons_); | 281 MockFeedback.matchAndConsume_(earconName, {}, this.pendingEarcons_); |
| 278 return !!match; | 282 return !!match; |
| 279 }.bind(this), | 283 }.bind(this), |
| 280 toString: function() { | 284 toString: function() { |
| 281 return 'Earcon \'' + earconName + '\''; | 285 return 'Earcon \'' + earconName + '\''; |
| 282 } | 286 } |
| 283 }); | 287 }); |
| 284 return this; | 288 return this; |
| 285 }, | 289 }, |
| 286 | 290 |
| 287 /** | 291 /** |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 var callback; | 344 var callback; |
| 341 if (properties && (properties.startCallback || properties.endCallback)) { | 345 if (properties && (properties.startCallback || properties.endCallback)) { |
| 342 var startCallback = properties.startCallback; | 346 var startCallback = properties.startCallback; |
| 343 var endCallback = properties.endCallback; | 347 var endCallback = properties.endCallback; |
| 344 callback = function() { | 348 callback = function() { |
| 345 startCallback && startCallback(); | 349 startCallback && startCallback(); |
| 346 endCallback && endCallback(); | 350 endCallback && endCallback(); |
| 347 }; | 351 }; |
| 348 } | 352 } |
| 349 this.pendingUtterances_.push( | 353 this.pendingUtterances_.push( |
| 350 {text: textString, | 354 {text: textString, queueMode: queueMode, callback: callback}); |
| 351 queueMode: queueMode, | |
| 352 callback: callback}); | |
| 353 this.process_(); | 355 this.process_(); |
| 354 }, | 356 }, |
| 355 | 357 |
| 356 /** @private */ | 358 /** @private */ |
| 357 addBraille_: function(navBraille) { | 359 addBraille_: function(navBraille) { |
| 358 this.pendingBraille_.push(navBraille); | 360 this.pendingBraille_.push(navBraille); |
| 359 this.process_(); | 361 this.process_(); |
| 360 }, | 362 }, |
| 361 | 363 |
| 362 /** @private */ | 364 /** @private */ |
| (...skipping 22 matching lines...) Expand all Loading... |
| 385 } | 387 } |
| 386 if (this.pendingActions_.length == 0) { | 388 if (this.pendingActions_.length == 0) { |
| 387 if (this.finishedCallback_) { | 389 if (this.finishedCallback_) { |
| 388 this.finishedCallback_(); | 390 this.finishedCallback_(); |
| 389 this.finishedCallback_ = null; | 391 this.finishedCallback_ = null; |
| 390 } | 392 } |
| 391 } else { | 393 } else { |
| 392 // If there are pending actions and no matching feedback for a few | 394 // If there are pending actions and no matching feedback for a few |
| 393 // seconds, log the pending state to ease debugging. | 395 // seconds, log the pending state to ease debugging. |
| 394 if (!this.logTimeoutId_) { | 396 if (!this.logTimeoutId_) { |
| 395 this.logTimeoutId_ = window.setTimeout( | 397 this.logTimeoutId_ = |
| 396 this.logPendingState_.bind(this), 2000); | 398 window.setTimeout(this.logPendingState_.bind(this), 2000); |
| 397 } | 399 } |
| 398 } | 400 } |
| 399 } finally { | 401 } finally { |
| 400 this.inProcess_ = false; | 402 this.inProcess_ = false; |
| 401 } | 403 } |
| 402 }, | 404 }, |
| 403 | 405 |
| 404 /** @private */ | 406 /** @private */ |
| 405 logPendingState_: function() { | 407 logPendingState_: function() { |
| 406 if (this.pendingActions_.length > 0) | 408 if (this.pendingActions_.length > 0) |
| 407 console.log('Still waiting for ' + this.pendingActions_[0].toString()); | 409 console.log('Still waiting for ' + this.pendingActions_[0].toString()); |
| 408 function logPending(desc, list) { | 410 function logPending(desc, list) { |
| 409 if (list.length > 0) | 411 if (list.length > 0) |
| 410 console.log('Pending ' + desc + ':\n ' + | 412 console.log( |
| 413 'Pending ' + desc + ':\n ' + |
| 411 list.map(function(i) { | 414 list.map(function(i) { |
| 412 var ret = '\'' + i.text + '\''; | 415 var ret = '\'' + i.text + '\''; |
| 413 if ('startIndex' in i) | 416 if ('startIndex' in i) |
| 414 ret += ' startIndex=' + i.startIndex; | 417 ret += ' startIndex=' + i.startIndex; |
| 415 if ('endIndex' in i) | 418 if ('endIndex' in i) |
| 416 ret += ' endIndex=' + i.endIndex; | 419 ret += ' endIndex=' + i.endIndex; |
| 417 return ret; | 420 return ret; |
| 418 }).join('\n ') + '\n '); | 421 }) |
| 422 .join('\n ') + |
| 423 '\n '); |
| 419 } | 424 } |
| 420 logPending('speech utterances', this.pendingUtterances_); | 425 logPending('speech utterances', this.pendingUtterances_); |
| 421 logPending('braille', this.pendingBraille_); | 426 logPending('braille', this.pendingBraille_); |
| 422 logPending('earcons', this.pendingEarcons_); | 427 logPending('earcons', this.pendingEarcons_); |
| 423 this.logTimeoutId_ = 0; | 428 this.logTimeoutId_ = 0; |
| 424 }, | 429 }, |
| 425 }; | 430 }; |
| 426 | 431 |
| 427 /** | 432 /** |
| 428 * @param {string} text | 433 * @param {string} text |
| (...skipping 24 matching lines...) Expand all Loading... |
| 453 } | 458 } |
| 454 if (candidate) { | 459 if (candidate) { |
| 455 var consumed = pending.splice(0, i + 1); | 460 var consumed = pending.splice(0, i + 1); |
| 456 consumed.forEach(function(item) { | 461 consumed.forEach(function(item) { |
| 457 if (item.callback) | 462 if (item.callback) |
| 458 item.callback(); | 463 item.callback(); |
| 459 }); | 464 }); |
| 460 } | 465 } |
| 461 return candidate; | 466 return candidate; |
| 462 }; | 467 }; |
| OLD | NEW |