|
1 | 1 | // @flow |
2 | 2 |
|
3 | | -import { decodeCoordinate, decodeDirection, encodeCoordinate, encodeDirection, extend, isLoopBlock, generateEncodedProgramURL, getThemeFromString, getWorldFromString, getStartingPositionFromString, focusByQuerySelector, focusFirstInNodeList, focusLastInNodeList, generateLoopLabel, parseLoopLabel } from './Utils.js'; |
| 3 | +import { decodeCoordinate, decodeDirection, encodeCoordinate, encodeDirection, extend, isLoopBlock, generateEncodedProgramURL, getThemeFromString, getWorldFromString, getStartingPositionFromString, focusByQuerySelector, focusFirstInNodeList, focusLastInNodeList, generateLoopLabel, parseLoopLabel, selectSpeechSynthesisVoice } from './Utils.js'; |
4 | 4 | import React from 'react'; |
5 | 5 | import Adapter from 'enzyme-adapter-react-16'; |
6 | 6 | import { mount, configure } from 'enzyme'; |
@@ -224,3 +224,216 @@ test('Test isLoopBlock', () => { |
224 | 224 | expect(isLoopBlock('endLoop')).toEqual(true); |
225 | 225 | expect(isLoopBlock('forward1')).toEqual(false); |
226 | 226 | }); |
| 227 | + |
| 228 | +describe('selectSpeechSynthesisVoice', () => { |
| 229 | + describe('Check parameters', () => { |
| 230 | + const voices = (([ |
| 231 | + { |
| 232 | + default: true, |
| 233 | + lang: 'en-US', |
| 234 | + localService: true, |
| 235 | + name: 'Voice', |
| 236 | + voiceURI: 'Voice' |
| 237 | + } |
| 238 | + ]: any): Array<SpeechSynthesisVoice>); |
| 239 | + |
| 240 | + test('When utteranceLangTag or userLangTag is bad, return null', () => { |
| 241 | + expect(selectSpeechSynthesisVoice(null, 'en', voices)).toBeNull(); |
| 242 | + expect(selectSpeechSynthesisVoice('', 'en', voices)).toBeNull(); |
| 243 | + expect(selectSpeechSynthesisVoice('a', 'en', voices)).toBeNull(); |
| 244 | + expect(selectSpeechSynthesisVoice('en', null, voices)).toBeNull(); |
| 245 | + expect(selectSpeechSynthesisVoice('en', '', voices)).toBeNull(); |
| 246 | + expect(selectSpeechSynthesisVoice('en', 'a', voices)).toBeNull(); |
| 247 | + }); |
| 248 | + |
| 249 | + test('When there are no voices, return null', () => { |
| 250 | + expect(selectSpeechSynthesisVoice('en', 'en', [])).toBeNull(); |
| 251 | + expect(selectSpeechSynthesisVoice('en', 'en', null)).toBeNull(); |
| 252 | + }); |
| 253 | + }); |
| 254 | + |
| 255 | + describe('Selection by language', () => { |
| 256 | + const voices = (([ |
| 257 | + { |
| 258 | + default: true, |
| 259 | + lang: 'en-CA', |
| 260 | + localService: true, |
| 261 | + name: 'Voice', |
| 262 | + voiceURI: 'Voice' |
| 263 | + }, |
| 264 | + { |
| 265 | + default: true, |
| 266 | + lang: 'en-US', |
| 267 | + localService: true, |
| 268 | + name: 'Voice', |
| 269 | + voiceURI: 'Voice' |
| 270 | + }, |
| 271 | + { |
| 272 | + default: true, |
| 273 | + lang: 'fr-CA', |
| 274 | + localService: true, |
| 275 | + name: 'Voice', |
| 276 | + voiceURI: 'Voice' |
| 277 | + }, |
| 278 | + { |
| 279 | + default: true, |
| 280 | + lang: 'fr-FR', |
| 281 | + localService: true, |
| 282 | + name: 'Voice', |
| 283 | + voiceURI: 'Voice' |
| 284 | + } |
| 285 | + ]: any): Array<SpeechSynthesisVoice>); |
| 286 | + |
| 287 | + test('When available, match the user language', () => { |
| 288 | + expect(selectSpeechSynthesisVoice('en', 'en-CA', voices)).toBe(voices[0]); |
| 289 | + expect(selectSpeechSynthesisVoice('en', 'en-US', voices)).toBe(voices[1]); |
| 290 | + expect(selectSpeechSynthesisVoice('fr', 'fr-CA', voices)).toBe(voices[2]); |
| 291 | + expect(selectSpeechSynthesisVoice('fr', 'fr-FR', voices)).toBe(voices[3]); |
| 292 | + }); |
| 293 | + |
| 294 | + test('When utterance is English and user language is English, but we have no matching voice, find en-US', () => { |
| 295 | + expect(selectSpeechSynthesisVoice('en', 'en-GB', voices)).toBe(voices[1]); |
| 296 | + }); |
| 297 | + |
| 298 | + test('When utterance is English and user language is not, find en-US', () => { |
| 299 | + expect(selectSpeechSynthesisVoice('en', 'fr-FR', voices)).toBe(voices[1]); |
| 300 | + }); |
| 301 | + |
| 302 | + test('When utterance is English, user language is not, and there is no en-US, find the first English', () => { |
| 303 | + const noEnUSvoices = (([ |
| 304 | + { |
| 305 | + default: true, |
| 306 | + lang: 'fr-CA', |
| 307 | + localService: true, |
| 308 | + name: 'Voice', |
| 309 | + voiceURI: 'Voice' |
| 310 | + }, |
| 311 | + { |
| 312 | + default: true, |
| 313 | + lang: 'fr-FR', |
| 314 | + localService: true, |
| 315 | + name: 'Voice', |
| 316 | + voiceURI: 'Voice' |
| 317 | + }, |
| 318 | + { |
| 319 | + default: true, |
| 320 | + lang: 'en-CA', |
| 321 | + localService: true, |
| 322 | + name: 'Voice', |
| 323 | + voiceURI: 'Voice' |
| 324 | + }, |
| 325 | + { |
| 326 | + default: true, |
| 327 | + lang: 'en-GB', |
| 328 | + localService: true, |
| 329 | + name: 'Voice', |
| 330 | + voiceURI: 'Voice' |
| 331 | + } |
| 332 | + ]: any): Array<SpeechSynthesisVoice>); |
| 333 | + |
| 334 | + expect(selectSpeechSynthesisVoice('en', 'fr-FR', noEnUSvoices)).toBe(noEnUSvoices[2]); |
| 335 | + }); |
| 336 | + |
| 337 | + test('When utterance is not English, and user language is the same, but we have no matching voice, find the first voice for the utterance language', () => { |
| 338 | + expect(selectSpeechSynthesisVoice('fr', 'fr-CH', voices)).toBe(voices[2]); |
| 339 | + }); |
| 340 | + |
| 341 | + test('When utterance is not English, and user language is not the same, find the first voice for the utterance language', () => { |
| 342 | + expect(selectSpeechSynthesisVoice('fr', 'en-US', voices)).toBe(voices[2]); |
| 343 | + }); |
| 344 | + |
| 345 | + test('When there is no match, return null', () => { |
| 346 | + const noEnVoices = (([ |
| 347 | + { |
| 348 | + default: true, |
| 349 | + lang: 'fr-CA', |
| 350 | + localService: true, |
| 351 | + name: 'Voice', |
| 352 | + voiceURI: 'Voice' |
| 353 | + }, |
| 354 | + { |
| 355 | + default: true, |
| 356 | + lang: 'fr-FR', |
| 357 | + localService: true, |
| 358 | + name: 'Voice', |
| 359 | + voiceURI: 'Voice' |
| 360 | + } |
| 361 | + ]: any): Array<SpeechSynthesisVoice>); |
| 362 | + |
| 363 | + expect(selectSpeechSynthesisVoice('en', 'fr-FR', noEnVoices)).toBeNull(); |
| 364 | + }); |
| 365 | + }); |
| 366 | + |
| 367 | + test('Prefer voices with default: true, then localService: true', () => { |
| 368 | + const voices = (([ |
| 369 | + { |
| 370 | + default: false, |
| 371 | + lang: 'fr-CA', |
| 372 | + localService: false, |
| 373 | + name: 'Voice', |
| 374 | + voiceURI: 'Voice' |
| 375 | + }, |
| 376 | + { |
| 377 | + default: false, |
| 378 | + lang: 'fr-FR', |
| 379 | + localService: true, |
| 380 | + name: 'Voice', |
| 381 | + voiceURI: 'Voice' |
| 382 | + }, |
| 383 | + { |
| 384 | + default: false, |
| 385 | + lang: 'en-CA', |
| 386 | + localService: true, |
| 387 | + name: 'Voice', |
| 388 | + voiceURI: 'Voice' |
| 389 | + }, |
| 390 | + { |
| 391 | + default: true, |
| 392 | + lang: 'en-CA', |
| 393 | + localService: false, |
| 394 | + name: 'Voice', |
| 395 | + voiceURI: 'Voice' |
| 396 | + }, |
| 397 | + { |
| 398 | + default: true, |
| 399 | + lang: 'en-US', |
| 400 | + localService: false, |
| 401 | + name: 'Voice', |
| 402 | + voiceURI: 'Voice' |
| 403 | + }, |
| 404 | + { |
| 405 | + default: true, |
| 406 | + lang: 'en-US', |
| 407 | + localService: true, |
| 408 | + name: 'Voice', |
| 409 | + voiceURI: 'Voice' |
| 410 | + } |
| 411 | + ]: any): Array<SpeechSynthesisVoice>); |
| 412 | + |
| 413 | + expect(selectSpeechSynthesisVoice('fr', 'fr-CA', voices)).toBe(voices[0]); |
| 414 | + expect(selectSpeechSynthesisVoice('fr', 'en-US', voices)).toBe(voices[1]); |
| 415 | + expect(selectSpeechSynthesisVoice('en', 'en-CA', voices)).toBe(voices[3]); |
| 416 | + expect(selectSpeechSynthesisVoice('en', 'en-US', voices)).toBe(voices[5]); |
| 417 | + }); |
| 418 | + |
| 419 | + test('When there are multiple matches, pick the first', () => { |
| 420 | + const voices = (([ |
| 421 | + { |
| 422 | + default: true, |
| 423 | + lang: 'en-US', |
| 424 | + localService: true, |
| 425 | + name: 'Voice', |
| 426 | + voiceURI: 'Voice' |
| 427 | + }, |
| 428 | + { |
| 429 | + default: true, |
| 430 | + lang: 'en-US', |
| 431 | + localService: true, |
| 432 | + name: 'Voice', |
| 433 | + voiceURI: 'Voice' |
| 434 | + } |
| 435 | + ]: any): Array<SpeechSynthesisVoice>); |
| 436 | + |
| 437 | + expect(selectSpeechSynthesisVoice('en', 'en-US', voices)).toBe(voices[0]); |
| 438 | + }); |
| 439 | +}); |
0 commit comments