annyang 2.6.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +56 -32
  3. package/dist/annyang.cjs +361 -0
  4. package/dist/annyang.cjs.map +1 -0
  5. package/dist/annyang.d.cts +288 -0
  6. package/dist/annyang.d.ts +288 -0
  7. package/dist/annyang.iife.min.js +1 -0
  8. package/dist/annyang.js +303 -670
  9. package/dist/annyang.js.map +1 -1
  10. package/package.json +55 -31
  11. package/.editorconfig +0 -13
  12. package/.eslintrc.json +0 -34
  13. package/.gitattributes +0 -1
  14. package/.github/ISSUE_TEMPLATE.md +0 -32
  15. package/.github/PULL_REQUEST_TEMPLATE.md +0 -29
  16. package/.jshintrc +0 -31
  17. package/.travis.yml +0 -20
  18. package/CONTRIBUTING.md +0 -75
  19. package/Gruntfile.js +0 -144
  20. package/bower.json +0 -25
  21. package/demo/css/main.css +0 -152
  22. package/demo/css/main.min.css +0 -1
  23. package/demo/images/flickr.png +0 -0
  24. package/demo/images/footer_background.jpg +0 -0
  25. package/demo/images/icon_js.png +0 -0
  26. package/demo/images/icon_speech.png +0 -0
  27. package/demo/images/icon_user.png +0 -0
  28. package/demo/images/mini_icon_say.png +0 -0
  29. package/demo/images/palette.png +0 -0
  30. package/demo/images/tpscover.jpg +0 -0
  31. package/demo/index.html +0 -225
  32. package/demo/vendor/css/default.css +0 -135
  33. package/demo/vendor/css/github.css +0 -127
  34. package/demo/vendor/css/tomorrow.css +0 -49
  35. package/demo/vendor/html/github-btn.html +0 -243
  36. package/demo/vendor/js/highlight.pack.js +0 -1
  37. package/dist/annyang.min.js +0 -12
  38. package/docs/FAQ.md +0 -223
  39. package/docs/README.md +0 -336
  40. package/require.js +0 -36
  41. package/sites/README.md +0 -6
  42. package/sites/facebook.js +0 -123
  43. package/sites/facebook.min.js +0 -17
  44. package/sites/geektime.js +0 -108
  45. package/sites/geektime.min.js +0 -17
  46. package/src/annyang.js +0 -680
  47. package/test/SpecRunner.html +0 -41
  48. package/test/init_corti.js +0 -1
  49. package/test/spec/.jshintrc +0 -40
  50. package/test/spec/BasicSpec.js +0 -1616
  51. package/test/spec/IssuesSpec.js +0 -41
  52. package/test/vendor/corti.js +0 -202
  53. package/yarn.lock +0 -4190
@@ -1,1616 +0,0 @@
1
- (function (root, factory) {
2
- // jshint strict: false
3
- if (typeof module === 'object' && module.exports) { // CommonJS
4
- factory(require('../../annyang'));
5
- } else if (typeof define === 'function' && define.amd) { // AMD
6
- define(['annyang'], factory);
7
- } else { // Browser globals
8
- factory(root.annyang);
9
- }
10
- }(typeof window !== 'undefined' ? window : this, function factory(annyang) {
11
- "use strict";
12
-
13
- describe('annyang', function() {
14
-
15
- it('should exist in global namespace', function() {
16
- expect(annyang).toEqual(jasmine.any(Object));
17
- });
18
-
19
- it('should expose init method', function() {
20
- expect(annyang.init).toEqual(jasmine.any(Function));
21
- });
22
-
23
- it('should expose start method', function() {
24
- expect(annyang.start).toEqual(jasmine.any(Function));
25
- });
26
-
27
- it('should expose abort method', function() {
28
- expect(annyang.abort).toEqual(jasmine.any(Function));
29
- });
30
-
31
- it('should expose pause method', function() {
32
- expect(annyang.pause).toEqual(jasmine.any(Function));
33
- });
34
-
35
- it('should expose resume method', function() {
36
- expect(annyang.resume).toEqual(jasmine.any(Function));
37
- });
38
-
39
- it('should expose debug method', function() {
40
- expect(annyang.debug).toEqual(jasmine.any(Function));
41
- });
42
-
43
- it('should expose debug method', function() {
44
- expect(annyang.debug).toEqual(jasmine.any(Function));
45
- });
46
-
47
- it('should expose setLanguage method', function() {
48
- expect(annyang.setLanguage).toEqual(jasmine.any(Function));
49
- });
50
-
51
- it('should expose addCommands method', function() {
52
- expect(annyang.addCommands).toEqual(jasmine.any(Function));
53
- });
54
-
55
- it('should expose removeCommands method', function() {
56
- expect(annyang.removeCommands).toEqual(jasmine.any(Function));
57
- });
58
-
59
- it('should expose addCallback method', function() {
60
- expect(annyang.addCallback).toEqual(jasmine.any(Function));
61
- });
62
-
63
- it('should expose removeCallback method', function() {
64
- expect(annyang.removeCallback).toEqual(jasmine.any(Function));
65
- });
66
-
67
- it('should expose isListening method', function() {
68
- expect(annyang.isListening).toEqual(jasmine.any(Function));
69
- });
70
-
71
- it('should expose getSpeechRecognizer method', function() {
72
- expect(annyang.getSpeechRecognizer).toEqual(jasmine.any(Function));
73
- });
74
-
75
- it('should expose trigger method', function() {
76
- expect(annyang.trigger).toEqual(jasmine.any(Function));
77
- });
78
-
79
- });
80
-
81
- describe('annyang.abort', function() {
82
-
83
- it('should not throw an error when called before annyang starts', function() {
84
- expect(annyang.abort()).toBe(undefined);
85
- });
86
-
87
- });
88
-
89
- describe('annyang.isListening', function() {
90
-
91
- beforeEach(function() {
92
- annyang.abort();
93
- });
94
-
95
- it('should return false when called before annyang starts', function() {
96
- expect(annyang.isListening()).toBe(false);
97
- });
98
-
99
- it('should return true when called after annyang starts', function() {
100
- annyang.start();
101
- expect(annyang.isListening()).toBe(true);
102
- });
103
-
104
- it('should return true when called after annyang aborts', function() {
105
- annyang.start();
106
- annyang.abort();
107
- expect(annyang.isListening()).toBe(false);
108
- });
109
-
110
- it('should return false when called when annyang is paused', function() {
111
- annyang.start();
112
- annyang.pause();
113
- expect(annyang.isListening()).toBe(false);
114
- });
115
-
116
- it('should return true when called after annyang is resumed', function() {
117
- annyang.start();
118
- annyang.pause();
119
- annyang.resume();
120
- expect(annyang.isListening()).toBe(true);
121
- });
122
-
123
- });
124
-
125
- describe('annyang.abort', function() {
126
-
127
- var spyOnEnd;
128
-
129
- beforeEach(function() {
130
- annyang.abort();
131
- spyOnEnd = jasmine.createSpy();
132
- annyang.addCallback('end', spyOnEnd);
133
- });
134
-
135
- it('should stop annyang and Speech Recognition if it is started', function() {
136
- annyang.start();
137
- expect(annyang.isListening()).toBe(true);
138
- expect(spyOnEnd).not.toHaveBeenCalled();
139
- annyang.abort();
140
- expect(annyang.isListening()).toBe(false);
141
- expect(spyOnEnd).toHaveBeenCalledTimes(1);
142
- });
143
-
144
- it('should stop annyang if it is paused', function() {
145
- annyang.start();
146
- expect(annyang.isListening()).toBe(true);
147
- expect(spyOnEnd).not.toHaveBeenCalled();
148
- annyang.abort();
149
- expect(annyang.isListening()).toBe(false);
150
- expect(spyOnEnd).toHaveBeenCalledTimes(1);
151
- });
152
-
153
- it('should do nothing when annyang is stopped', function() {
154
- expect(annyang.isListening()).toBe(false);
155
- expect(spyOnEnd).not.toHaveBeenCalled();
156
- annyang.abort();
157
- expect(annyang.isListening()).toBe(false);
158
- expect(spyOnEnd).not.toHaveBeenCalled();
159
- });
160
-
161
- });
162
-
163
- describe('annyang.start', function() {
164
-
165
- var spyOnStart;
166
- var recognition;
167
-
168
- beforeEach(function() {
169
- jasmine.clock().install();
170
- annyang.abort();
171
- spyOnStart = jasmine.createSpy();
172
- annyang.addCallback('start', spyOnStart);
173
- recognition = annyang.getSpeechRecognizer();
174
- });
175
-
176
- afterEach(function() {
177
- jasmine.clock().tick(2000);
178
- jasmine.clock().uninstall();
179
- });
180
-
181
- it('should start annyang and Speech Recognition if it was aborted', function() {
182
- annyang.start();
183
- expect(annyang.isListening()).toBe(true);
184
- expect(spyOnStart).toHaveBeenCalledTimes(1);
185
- });
186
-
187
- it('should resume annyang if it was paused', function() {
188
- annyang.start();
189
- expect(annyang.isListening()).toBe(true);
190
- expect(spyOnStart).toHaveBeenCalledTimes(1);
191
- annyang.pause();
192
- expect(annyang.isListening()).toBe(false);
193
- annyang.start();
194
- expect(annyang.isListening()).toBe(true);
195
- expect(spyOnStart).toHaveBeenCalledTimes(1);
196
- });
197
-
198
- it('should do nothing when annyang is already started', function() {
199
- annyang.start();
200
- expect(annyang.isListening()).toBe(true);
201
- expect(spyOnStart).toHaveBeenCalledTimes(1);
202
- expect(function() {
203
- annyang.start();
204
- }).not.toThrowError();
205
- expect(annyang.isListening()).toBe(true);
206
- expect(spyOnStart).toHaveBeenCalledTimes(1);
207
- });
208
-
209
- it('should write a message to log when annyang is already started and debug is on', function() {
210
- spyOn(console, 'log');
211
- annyang.debug(true);
212
- annyang.start();
213
- annyang.start();
214
- annyang.debug(false);
215
- expect(console.log).toHaveBeenCalledTimes(1);
216
- expect(console.log).toHaveBeenCalledWith('Failed to execute \'start\' on \'SpeechRecognition\': recognition has already started.');
217
- });
218
-
219
- it('should not write a message to log when annyang is already started but debug is off', function() {
220
- spyOn(console, 'log');
221
- annyang.debug(false);
222
- annyang.start();
223
- annyang.start();
224
- expect(console.log).not.toHaveBeenCalled();
225
- });
226
-
227
- it('should accept an options object as its first argument', function() {
228
- expect(function() {annyang.start({});}).not.toThrowError();
229
- });
230
-
231
- it('should accept an autoRestart property in options object which defaults to true', function() {
232
- annyang.start();
233
- recognition.abort();
234
- expect(annyang.isListening()).toBe(false);
235
- jasmine.clock().tick(1000);
236
- expect(annyang.isListening()).toBe(true);
237
- });
238
-
239
- describe('annyang.start({ autoRestart: true })', function() {
240
-
241
- it('should cause annyang to restart after 1 second when Speech Recognition engine was aborted', function() {
242
- annyang.start({ autoRestart: true });
243
- expect(annyang.isListening()).toBe(true);
244
- recognition.abort();
245
- expect(annyang.isListening()).toBe(false);
246
- jasmine.clock().tick(900);
247
- expect(annyang.isListening()).toBe(false);
248
- jasmine.clock().tick(100);
249
- expect(annyang.isListening()).toBe(true);
250
- });
251
-
252
- });
253
-
254
- describe('annyang.start({ autoRestart: false })', function() {
255
-
256
- it('should cause annyang to not restart when Speech Recognition engine was aborted', function() {
257
- annyang.start({ autoRestart: false });
258
- expect(annyang.isListening()).toBe(true);
259
- recognition.abort();
260
- expect(annyang.isListening()).toBe(false);
261
- jasmine.clock().tick(1000);
262
- expect(annyang.isListening()).toBe(false);
263
- });
264
-
265
- });
266
-
267
- it('should accept a pause property in options object which defaults to false', function() {
268
- annyang.start();
269
- expect(annyang.isListening()).toBe(true);
270
- });
271
-
272
- describe('annyang.start({ paused: true })', function() {
273
-
274
- it('should start annyang in paused mode', function() {
275
- annyang.start({ paused: true });
276
- expect(annyang.isListening()).toBe(false);
277
- });
278
-
279
- });
280
-
281
- });
282
-
283
- describe('annyang.debug', function() {
284
-
285
- beforeEach(function(){
286
- spyOn(console, 'log');
287
- });
288
-
289
- it('should be off by default', function() {
290
- annyang.addCommands({'test command': function() {}});
291
- expect(console.log).not.toHaveBeenCalled();
292
- });
293
-
294
- it('should turn on debug messages when called without a parameter', function() {
295
- annyang.debug();
296
- annyang.addCommands({'test command': function() {}});
297
- expect(console.log).toHaveBeenCalled();
298
- });
299
-
300
- it('should turn off debug messages when called with a parameter that is false', function() {
301
- annyang.debug(true);
302
- annyang.debug(false);
303
- annyang.addCommands({'test command': function() {}});
304
- expect(console.log).not.toHaveBeenCalled();
305
- annyang.debug(0);
306
- annyang.addCommands({'test command': function() {}});
307
- expect(console.log).not.toHaveBeenCalled();
308
- });
309
-
310
- it('should turn on debug messages when called with a parameter that is true', function() {
311
- annyang.debug(false);
312
- annyang.debug(1);
313
- annyang.addCommands({'test command': function() {}});
314
- expect(console.log).toHaveBeenCalledTimes(1);
315
- annyang.debug(true);
316
- annyang.addCommands({'test command': function() {}});
317
- expect(console.log).toHaveBeenCalledTimes(2);
318
- });
319
-
320
- });
321
-
322
- describe('annyang.addCallback', function() {
323
-
324
- beforeEach(function() {
325
- annyang.abort();
326
- });
327
-
328
- it('should always return undefined', function() {
329
- expect(annyang.addCallback()).toEqual(undefined);
330
- expect(annyang.addCallback('blergh')).toEqual(undefined);
331
- expect(annyang.addCallback('start')).toEqual(undefined);
332
- expect(annyang.addCallback('start', function() {})).toEqual(undefined);
333
- expect(annyang.addCallback('start', function() {}, this)).toEqual(undefined);
334
- });
335
-
336
- it('should be able to register multiple callbacks to one event', function() {
337
- var spyOnStart = jasmine.createSpy();
338
- var spyOnStart2 = jasmine.createSpy();
339
- annyang.addCallback('start', spyOnStart);
340
- annyang.addCallback('start', spyOnStart2);
341
- expect(spyOnStart).not.toHaveBeenCalled();
342
- expect(spyOnStart2).not.toHaveBeenCalled();
343
- annyang.start();
344
- expect(spyOnStart).toHaveBeenCalledTimes(1);
345
- expect(spyOnStart2).toHaveBeenCalledTimes(1);
346
- });
347
-
348
- it('should run callback in the context of annyang by default', function() {
349
- annyang.variableChanged = false;
350
- var changeVariable = function() {
351
- annyang.variableChanged = true;
352
- };
353
- annyang.addCallback('start', changeVariable);
354
- annyang.start();
355
- expect(annyang.variableChanged).toEqual(true);
356
- annyang.abort();
357
- });
358
-
359
- it('should run callbacks in the context given as the third parameter', function() {
360
- // First set it in this context
361
- this.variableChanged = false;
362
- var changeVariable = function() {
363
- this.variableChanged = true;
364
- };
365
- annyang.addCallback('start', changeVariable, this);
366
- annyang.start();
367
- expect(this.variableChanged).toEqual(true);
368
- });
369
-
370
- });
371
-
372
- describe("annyang.addCallback('start')", function() {
373
-
374
- beforeEach(function() {
375
- annyang.abort();
376
- });
377
-
378
- it('should add a callback which will be called when annyang starts', function() {
379
- var spyOnStart = jasmine.createSpy();
380
- annyang.addCallback('start', spyOnStart);
381
- expect(spyOnStart).not.toHaveBeenCalled();
382
- annyang.start();
383
- expect(spyOnStart).toHaveBeenCalledTimes(1);
384
- });
385
-
386
- it('should not fire callback when annyang resumes from a paused state', function() {
387
- // Turn off debugging during this test, as it logs a message when resuming from a paused state which we are not testing for here
388
- annyang.debug(false);
389
- var spyOnStart = jasmine.createSpy();
390
- annyang.start();
391
- annyang.pause();
392
- annyang.addCallback('start', spyOnStart);
393
- expect(spyOnStart).not.toHaveBeenCalled();
394
- annyang.resume();
395
- expect(spyOnStart).not.toHaveBeenCalled();
396
- annyang.debug(true);
397
- });
398
-
399
- it('should fire callback when annyang resumes from an aborted (stopped) state', function() {
400
- var spyOnStart = jasmine.createSpy();
401
- annyang.start();
402
- annyang.abort();
403
- annyang.addCallback('start', spyOnStart);
404
- expect(spyOnStart).not.toHaveBeenCalled();
405
- annyang.resume();
406
- expect(spyOnStart).toHaveBeenCalledTimes(1);
407
- });
408
-
409
- });
410
-
411
- describe("annyang.addCallback('end')", function() {
412
-
413
- beforeEach(function() {
414
- annyang.debug(false);
415
- annyang.start();
416
- annyang.debug(true);
417
- });
418
-
419
- it('should add a callback which will be called when annyang aborts', function() {
420
- var spyOnAbort = jasmine.createSpy();
421
- annyang.addCallback('end', spyOnAbort);
422
- expect(spyOnAbort).not.toHaveBeenCalled();
423
- annyang.abort();
424
- expect(spyOnAbort).toHaveBeenCalledTimes(1);
425
- });
426
-
427
- it('should not fire callback when annyang enters paused state', function() {
428
- var spyOnPause = jasmine.createSpy();
429
- annyang.addCallback('end', spyOnPause);
430
- expect(spyOnPause).not.toHaveBeenCalled();
431
- annyang.pause();
432
- expect(spyOnPause).not.toHaveBeenCalled();
433
- });
434
-
435
- });
436
-
437
- describe("annyang.addCallback('soundstart')", function() {
438
-
439
- var recognition;
440
-
441
- beforeEach(function() {
442
- recognition = annyang.getSpeechRecognizer();
443
- annyang.debug(false);
444
- jasmine.clock().install();
445
- });
446
-
447
- afterEach(function() {
448
- annyang.abort();
449
- annyang.removeCallback();
450
- jasmine.clock().tick(2000);
451
- jasmine.clock().uninstall();
452
- });
453
-
454
- it('should add a callback which will be called when annyang detects sound', function() {
455
- annyang.start();
456
- var spyOnSoundStart = jasmine.createSpy();
457
- annyang.addCallback('soundstart', spyOnSoundStart);
458
- expect(spyOnSoundStart).not.toHaveBeenCalled();
459
- recognition.say('Time for some thrilling heroics');
460
- expect(spyOnSoundStart).toHaveBeenCalledTimes(1);
461
- });
462
-
463
- it('should fire callback once in continuous mode', function() {
464
- annyang.start({ continuous: true });
465
- var spyOnSoundStart = jasmine.createSpy();
466
- annyang.addCallback('soundstart', spyOnSoundStart);
467
- expect(spyOnSoundStart).not.toHaveBeenCalled();
468
- recognition.say('Time for some thrilling heroics');
469
- expect(spyOnSoundStart).toHaveBeenCalledTimes(1);
470
- recognition.say('That sounds like something out of science fiction');
471
- expect(spyOnSoundStart).toHaveBeenCalledTimes(1);
472
- });
473
-
474
- it('should fire callback multiple times in non-continuous mode with autorestart', function() {
475
- annyang.start({ continuous: false, autoRestart: true });
476
- var spyOnSoundStart = jasmine.createSpy();
477
- annyang.addCallback('soundstart', spyOnSoundStart);
478
- expect(spyOnSoundStart).not.toHaveBeenCalled();
479
- recognition.say('Time for some thrilling heroics');
480
- expect(spyOnSoundStart).toHaveBeenCalledTimes(1);
481
- jasmine.clock().tick(1000);
482
- recognition.say('That sounds like something out of science fiction');
483
- expect(spyOnSoundStart).toHaveBeenCalledTimes(2);
484
- });
485
-
486
- });
487
-
488
- describe("annyang.addCallback('resultMatch')", function() {
489
-
490
- var recognition;
491
- var spyOnResultMatch;
492
- var args;
493
- var saveArguments = function() {
494
- args = arguments;
495
- };
496
-
497
- beforeEach(function() {
498
- args = undefined;
499
- annyang.debug(false);
500
- recognition = annyang.getSpeechRecognizer();
501
- spyOnResultMatch = jasmine.createSpy();
502
- annyang.abort();
503
- annyang.start();
504
- annyang.removeCommands();
505
- annyang.addCommands({
506
- 'Time for some thrilling :action': function() {}
507
- });
508
- });
509
-
510
- it('should add a callback which will be called when result returned from Speech Recognition and a command was matched', function() {
511
- annyang.addCallback('resultMatch', spyOnResultMatch);
512
- expect(spyOnResultMatch).not.toHaveBeenCalled();
513
- recognition.say('Time for some thrilling heroics');
514
- expect(spyOnResultMatch).toHaveBeenCalledTimes(1);
515
- });
516
-
517
- it('should add a callback which will not be called when result returned from Speech Recognition does not match a command', function() {
518
- annyang.addCallback('resultMatch', spyOnResultMatch);
519
- expect(spyOnResultMatch).not.toHaveBeenCalled();
520
- recognition.say('What was my line again?');
521
- expect(spyOnResultMatch).not.toHaveBeenCalled();
522
- });
523
-
524
- it('should call the callback with the first argument containing the phrase the user said that matched a command', function() {
525
- annyang.addCallback('resultMatch', saveArguments);
526
- recognition.say('Time for some thrilling heroics');
527
- expect(args[0]).toEqual('Time for some thrilling heroics');
528
- });
529
-
530
- it('should call the callback with the first argument containing the phrase the user said that matched a command', function() {
531
- annyang.addCallback('resultMatch', saveArguments);
532
- recognition.say('Time for some thrilling heroics');
533
- expect(args[0]).toEqual('Time for some thrilling heroics');
534
- });
535
-
536
- it('should call the callback with the second argument containing the matched command', function() {
537
- annyang.addCallback('resultMatch', saveArguments);
538
- recognition.say('Time for some thrilling heroics');
539
- expect(args[1]).toEqual('Time for some thrilling :action');
540
- });
541
-
542
- it('should call the callback with the third argument containing an array of all possible Speech Recognition Alternatives the user may have said', function() {
543
- annyang.addCallback('resultMatch', saveArguments);
544
- recognition.say('Time for some thrilling heroics');
545
- expect(typeof args[2]).toEqual('object');
546
- expect(args[2][0]).toEqual('Time for some thrilling heroics');
547
- expect(args[2][1]).toEqual('Time for some thrilling heroics and so on');
548
- expect(args[2][2]).toEqual('Time for some thrilling heroics and so on and so on');
549
- });
550
-
551
- });
552
-
553
- describe("annyang.addCallback('resultNoMatch')", function() {
554
-
555
- var recognition;
556
- var spyOnResultNoMatch;
557
- var args;
558
- var saveArguments = function() {
559
- args = arguments;
560
- };
561
-
562
- beforeEach(function() {
563
- args = undefined;
564
- annyang.debug(false);
565
- recognition = annyang.getSpeechRecognizer();
566
- spyOnResultNoMatch = jasmine.createSpy();
567
- annyang.abort();
568
- annyang.start();
569
- annyang.removeCommands();
570
- annyang.addCommands({
571
- 'Time for some thrilling heroics': function() {}
572
- });
573
- });
574
-
575
- it('should add a callback which will be called when result returned from Speech Recognition but no commands were matched', function() {
576
- annyang.addCallback('resultNoMatch', spyOnResultNoMatch);
577
- expect(spyOnResultNoMatch).not.toHaveBeenCalled();
578
- recognition.say('That sounds like something out of science fiction');
579
- expect(spyOnResultNoMatch).toHaveBeenCalledTimes(1);
580
- });
581
-
582
- it('should add a callback which will not be called when result returned from Speech Recognition matches a command', function() {
583
- annyang.addCallback('resultNoMatch', spyOnResultNoMatch);
584
- expect(spyOnResultNoMatch).not.toHaveBeenCalled();
585
- recognition.say('Time for some thrilling heroics');
586
- expect(spyOnResultNoMatch).not.toHaveBeenCalled();
587
- });
588
-
589
- it('should call the callback with the first argument containing an array of all possible Speech Recognition Alternatives the user may have said', function() {
590
- annyang.addCallback('resultNoMatch', saveArguments);
591
- recognition.say('That sounds like something out of science fiction');
592
- expect(typeof args[0]).toEqual('object');
593
- expect(args[0][0]).toEqual('That sounds like something out of science fiction');
594
- expect(args[0][1]).toEqual('That sounds like something out of science fiction and so on');
595
- expect(args[0][2]).toEqual('That sounds like something out of science fiction and so on and so on');
596
- });
597
-
598
- });
599
-
600
- describe("annyang.addCallback('result')", function() {
601
-
602
- var recognition;
603
- var spyOnResult;
604
- var args;
605
- var saveArguments = function() {
606
- args = arguments;
607
- };
608
-
609
- beforeEach(function() {
610
- annyang.debug(false);
611
- recognition = annyang.getSpeechRecognizer();
612
- spyOnResult = jasmine.createSpy();
613
- annyang.abort();
614
- annyang.start();
615
- annyang.removeCommands();
616
- annyang.addCommands({
617
- 'Time for some thrilling heroics': function() {}
618
- });
619
- });
620
-
621
- it('should add a callback which will be called when result returned from Speech Recognition and a command was matched', function() {
622
- annyang.addCallback('result', spyOnResult);
623
- expect(spyOnResult).not.toHaveBeenCalled();
624
- recognition.say('Time for some thrilling heroics');
625
- expect(spyOnResult).toHaveBeenCalledTimes(1);
626
- });
627
-
628
- it('should add a callback which will be called when result returned from Speech Recognition and a command was not matched', function() {
629
- annyang.addCallback('result', spyOnResult);
630
- expect(spyOnResult).not.toHaveBeenCalled();
631
- recognition.say('That sounds like something out of science fiction');
632
- expect(spyOnResult).toHaveBeenCalledTimes(1);
633
- });
634
-
635
- it('should call the callback with the first argument containing an array of all possible Speech Recognition Alternatives the user may have said', function() {
636
- annyang.addCallback('result', saveArguments);
637
- recognition.say('That sounds like something out of science fiction');
638
- expect(typeof args[0]).toEqual('object');
639
- expect(args[0][0]).toEqual('That sounds like something out of science fiction');
640
- expect(args[0][1]).toEqual('That sounds like something out of science fiction and so on');
641
- expect(args[0][2]).toEqual('That sounds like something out of science fiction and so on and so on');
642
- });
643
-
644
- });
645
-
646
- describe('annyang.removeCallback', function() {
647
-
648
- var spy1;
649
- var spy2;
650
- var spy3;
651
- var spy4;
652
-
653
- beforeEach(function() {
654
- annyang.abort();
655
- spy1 = jasmine.createSpy();
656
- spy2 = jasmine.createSpy();
657
- spy3 = jasmine.createSpy();
658
- spy4 = jasmine.createSpy();
659
- });
660
-
661
- it('should always return undefined', function() {
662
- expect(annyang.removeCallback()).toEqual(undefined);
663
- expect(annyang.removeCallback('blergh')).toEqual(undefined);
664
- expect(annyang.removeCallback('start', function() {})).toEqual(undefined);
665
- });
666
-
667
- it('should delete all callbacks on all event types if passed undefined as the first parameter', function() {
668
- annyang.addCallback('start', spy1);
669
- annyang.addCallback('start', spy2);
670
- annyang.addCallback('end', spy3);
671
- annyang.addCallback('end', spy4);
672
- annyang.removeCallback();
673
- annyang.start();
674
- annyang.abort();
675
- expect(spy1).not.toHaveBeenCalled();
676
- expect(spy2).not.toHaveBeenCalled();
677
- expect(spy3).not.toHaveBeenCalled();
678
- expect(spy4).not.toHaveBeenCalled();
679
- });
680
-
681
- it('should delete all callbacks on an event type if passed the event name as the first parameter and undefined as the second parameter', function() {
682
- annyang.addCallback('start', spy1);
683
- annyang.addCallback('start', spy2);
684
- annyang.addCallback('end', spy3);
685
- annyang.addCallback('end', spy4);
686
- annyang.removeCallback('start');
687
- annyang.start();
688
- annyang.abort();
689
- expect(spy1).not.toHaveBeenCalled();
690
- expect(spy2).not.toHaveBeenCalled();
691
- expect(spy3).toHaveBeenCalledTimes(1);
692
- expect(spy4).toHaveBeenCalledTimes(1);
693
- });
694
-
695
- it('should delete all callbacks on an event type passed as first parameter, and matching a function passed as the second parameter', function() {
696
- annyang.addCallback('start', spy1);
697
- annyang.addCallback('start', spy2);
698
- annyang.addCallback('end', spy3);
699
- annyang.addCallback('end', spy4);
700
- annyang.removeCallback('start', spy2);
701
- annyang.start();
702
- annyang.abort();
703
- expect(spy1).toHaveBeenCalledTimes(1);
704
- expect(spy2).not.toHaveBeenCalled();
705
- expect(spy3).toHaveBeenCalledTimes(1);
706
- expect(spy4).toHaveBeenCalledTimes(1);
707
- });
708
-
709
- it('should delete all callbacks matching a function passed as the second parameter on all event types if first parameter is undefined', function() {
710
- annyang.addCallback('start', spy1);
711
- annyang.addCallback('start', spy2);
712
- annyang.addCallback('end', spy1);
713
- annyang.addCallback('end', spy2);
714
- annyang.removeCallback(undefined, spy1);
715
- annyang.start();
716
- annyang.abort();
717
- expect(spy1).not.toHaveBeenCalled();
718
- expect(spy2).toHaveBeenCalledTimes(2);
719
- });
720
-
721
- });
722
-
723
- describe("annyang.getSpeechRecognizer", function() {
724
-
725
- beforeEach(function() {
726
- annyang.abort();
727
- });
728
-
729
- it('should return the instance of SpeechRecognition used by annyang', function() {
730
- var spyOnStart = jasmine.createSpy();
731
- var recognition = annyang.getSpeechRecognizer();
732
- recognition.addEventListener('start', spyOnStart);
733
- expect(spyOnStart).not.toHaveBeenCalled();
734
- annyang.start();
735
- expect(spyOnStart).toHaveBeenCalledTimes(1);
736
- });
737
-
738
- });
739
-
740
- describe("annyang.addCommands", function() {
741
-
742
- var recognition;
743
-
744
- beforeEach(function() {
745
- annyang.debug(false);
746
- annyang.abort();
747
- annyang.removeCommands();
748
- recognition = annyang.getSpeechRecognizer();
749
- spyOn(console, 'log');
750
- annyang.start();
751
- });
752
-
753
- it('should accept an object consisting of key (sentence) and value (callback function)', function() {
754
- expect(function() {
755
- annyang.addCommands(
756
- {'Time for some thrilling heroics': function() {}}
757
- );
758
- }).not.toThrowError();
759
- });
760
-
761
- it('should match commands when a sentence is recognized and call the callback', function() {
762
- var spyOnMatch = jasmine.createSpy();
763
- annyang.addCommands(
764
- {'Time for some thrilling heroics': spyOnMatch}
765
- );
766
- expect(spyOnMatch).not.toHaveBeenCalled();
767
- recognition.say('Time for some thrilling heroics');
768
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
769
- });
770
-
771
- it('should match commands even if a recognition is not the first SpeechRecognitionAlternative', function() {
772
- var spyOnMatch = jasmine.createSpy();
773
- // For this test, the command text is what we will be saying plus 'and so on'.
774
- // This is the structure of alternative text recognitions in Corti.
775
- annyang.addCommands(
776
- {'Time for some thrilling heroics and so on': spyOnMatch}
777
- );
778
- expect(spyOnMatch).not.toHaveBeenCalled();
779
- recognition.say('Time for some thrilling heroics');
780
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
781
- });
782
-
783
- it('should write to console when a command matches if debug is on', function() {
784
- annyang.addCommands(
785
- {
786
- 'Time for some thrilling heroics': function() {}
787
- }
788
- );
789
- annyang.start();
790
- annyang.debug(true);
791
- expect(console.log).not.toHaveBeenCalled();
792
- recognition.say('Time for some thrilling heroics');
793
- expect(console.log).toHaveBeenCalledTimes(2); // 1 console log for speech recognized + 1 for the command matching
794
- expect(console.log).toHaveBeenCalledWith('command matched: %cTime for some thrilling heroics', 'font-weight: bold; color: #00f;');
795
- });
796
-
797
- it('should not write to console when a command matches if debug is off', function() {
798
- annyang.addCommands(
799
- {
800
- 'Time for some thrilling heroics': function() {}
801
- }
802
- );
803
- annyang.start();
804
- annyang.debug(false);
805
- recognition.say('Time for some thrilling heroics');
806
- expect(console.log).not.toHaveBeenCalled();
807
- });
808
-
809
- it('should write to console with argument matched when command with an argument matches if debug is on', function() {
810
- annyang.addCommands(
811
- {
812
- 'Time for some thrilling :action': function() {}
813
- }
814
- );
815
- annyang.start();
816
- annyang.debug(true);
817
- expect(console.log).not.toHaveBeenCalled();
818
- recognition.say('Time for some thrilling heroics');
819
- expect(console.log).toHaveBeenCalledTimes(3); // 1 console log for speech recognized + 1 for the command matching + 1 for the parameter
820
- expect(console.log).toHaveBeenCalledWith('command matched: %cTime for some thrilling :action', 'font-weight: bold; color: #00f;');
821
- expect(console.log).toHaveBeenCalledWith('with parameters', ['heroics']);
822
- });
823
-
824
- it('should not write to console the argument matched when command with an argument matches if debug is off', function() {
825
- annyang.addCommands(
826
- {
827
- 'Time for some thrilling :action': function() {}
828
- }
829
- );
830
- annyang.start();
831
- annyang.debug(false);
832
- recognition.say('Time for some thrilling heroics');
833
- expect(console.log).not.toHaveBeenCalled();
834
- });
835
-
836
- it('should ignore commands in subsequent addCommands calls with existing command texts', function() {
837
- var spyOnMatch1 = jasmine.createSpy();
838
- var spyOnMatch2 = jasmine.createSpy();
839
- annyang.addCommands(
840
- {
841
- 'Time for some thrilling heroics': spyOnMatch1
842
- }
843
- );
844
- annyang.addCommands(
845
- {
846
- 'Time for some thrilling heroics': spyOnMatch2
847
- }
848
- );
849
- expect(spyOnMatch1).not.toHaveBeenCalled();
850
- expect(spyOnMatch2).not.toHaveBeenCalled();
851
- recognition.say('Time for some thrilling heroics');
852
- expect(spyOnMatch1).toHaveBeenCalledTimes(1);
853
- expect(spyOnMatch2).not.toHaveBeenCalled();
854
- });
855
-
856
- it("should accept callbacks in commands object by reference. e.g. {'hello': helloFunc}", function() {
857
- var spyOnMatch = jasmine.createSpy();
858
- annyang.addCommands(
859
- {
860
- 'Time for some thrilling heroics': spyOnMatch
861
- }
862
- );
863
- expect(spyOnMatch).not.toHaveBeenCalled();
864
- recognition.say('Time for some thrilling heroics');
865
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
866
- });
867
-
868
- it("should accept callbacks in commands object by name. e.g. {'hello': 'helloFunc'}", function() {
869
- window.globalSpy = jasmine.createSpy();
870
- annyang.addCommands(
871
- {
872
- 'Time for some thrilling heroics': 'globalSpy'
873
- }
874
- );
875
- expect(window.globalSpy).not.toHaveBeenCalled();
876
- recognition.say('Time for some thrilling heroics');
877
- expect(window.globalSpy).toHaveBeenCalledTimes(1);
878
- });
879
-
880
- it('should write to console each command that was successfully added when debug is on', function() {
881
- annyang.debug(true);
882
- expect(console.log).not.toHaveBeenCalled();
883
- annyang.addCommands(
884
- {
885
- 'Time for some thrilling heroics': function() {}
886
- }
887
- );
888
- expect(console.log).toHaveBeenCalledTimes(1);
889
- expect(console.log).toHaveBeenCalledWith('Command successfully loaded: %cTime for some thrilling heroics', 'font-weight: bold; color: #00f;');
890
- annyang.addCommands(
891
- {
892
- 'That sounds like something out of science fiction': function() {},
893
- 'We should start dealing in those black-market beagles': function() {}
894
- }
895
- );
896
- expect(console.log).toHaveBeenCalledTimes(3);
897
- });
898
-
899
- it('should not write to console each command that is successfully added when debug is off', function() {
900
- annyang.debug(false);
901
- annyang.addCommands(
902
- {
903
- 'Time for some thrilling heroics': function() {}
904
- }
905
- );
906
- annyang.addCommands(
907
- {
908
- 'That sounds like something out of science fiction': function() {},
909
- 'We should start dealing in those black-market beagles': function() {}
910
- }
911
- );
912
- expect(console.log).not.toHaveBeenCalled();
913
- });
914
-
915
- it('should write to console when commands could not be added and debug is on', function() {
916
- annyang.debug(true);
917
- expect(console.log).not.toHaveBeenCalled();
918
- annyang.addCommands(
919
- {
920
- 'Time for some thrilling heroics': 'not_a_function'
921
- }
922
- );
923
- expect(console.log).toHaveBeenCalledTimes(1);
924
- expect(console.log).toHaveBeenCalledWith('Can not register command: %cTime for some thrilling heroics', 'font-weight: bold; color: #00f;');
925
- });
926
-
927
- it('should not write to console when commands could not be added but debug is off', function() {
928
- annyang.debug(false);
929
- expect(console.log).not.toHaveBeenCalled();
930
- annyang.addCommands(
931
- {
932
- 'Time for some thrilling heroics': 'not_a_function'
933
- }
934
- );
935
- expect(console.log).not.toHaveBeenCalled();
936
- });
937
-
938
- it('should accept commands with an object as the value which consists of a regexp and callback', function() {
939
- expect(function() {
940
- annyang.addCommands(
941
- {
942
- 'It is time': {
943
- regexp: /\w* for some thrilling.*/,
944
- callback: function() {}
945
- }
946
- });
947
- }).not.toThrowError();
948
- });
949
-
950
- it('should match commands passed as an object as the value which consists of a regexp and callback', function() {
951
- var spyOnMatch = jasmine.createSpy();
952
- annyang.addCommands(
953
- {
954
- 'It is time': {
955
- regexp: /\w* for some thrilling.*/,
956
- callback: spyOnMatch
957
- }
958
- }
959
- );
960
- expect(spyOnMatch).not.toHaveBeenCalled();
961
- recognition.say('Time for some thrilling heroics');
962
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
963
- });
964
-
965
- it('should pass variables from regexp capturing groups to the callback function', function() {
966
- var capture1 = '';
967
- var capture2 = '';
968
- var getVariablesCaptured = function(s1, s2) {
969
- capture1 = s1;
970
- capture2 = s2;
971
- };
972
-
973
- annyang.addCommands(
974
- {
975
- 'It is time': {
976
- regexp: /Time for some (\w*) (\w*)/,
977
- callback: getVariablesCaptured
978
- }
979
- }
980
- );
981
- recognition.say('Time for some thrilling heroics');
982
- expect(capture1).toEqual('thrilling');
983
- expect(capture2).toEqual('heroics');
984
- });
985
-
986
- it('should match commands with a named variable as the last word in the sentence', function() {
987
- var spyOnMatch = jasmine.createSpy();
988
- annyang.addCommands(
989
- {
990
- 'Time for some thrilling :stuff': spyOnMatch
991
- }
992
- );
993
- recognition.say('Time for some thrilling heroics');
994
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
995
- });
996
-
997
- it('should match match commands with a named variable in the middle of the sentence', function() {
998
- var spyOnMatch = jasmine.createSpy();
999
- annyang.addCommands(
1000
- {
1001
- 'Time for some :description heroics': spyOnMatch
1002
- }
1003
- );
1004
- recognition.say('Time for some thrilling heroics');
1005
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1006
- });
1007
-
1008
- it('should not match commands with more than one word in the position of a named variable', function() {
1009
- var spyOnMatch = jasmine.createSpy();
1010
- annyang.addCommands(
1011
- {
1012
- 'Time for some :description heroics': spyOnMatch
1013
- }
1014
- );
1015
- recognition.say('Time for some thrilling and fun heroics');
1016
- expect(spyOnMatch).not.toHaveBeenCalled();
1017
- });
1018
-
1019
- it('should not match commands with nothing in the position of a named variable', function() {
1020
- var spyOnMatch = jasmine.createSpy();
1021
- annyang.addCommands(
1022
- {
1023
- 'Time for some :description heroics': spyOnMatch
1024
- }
1025
- );
1026
- recognition.say('Time for some heroics');
1027
- expect(spyOnMatch).not.toHaveBeenCalled();
1028
- });
1029
-
1030
- it('should pass named variables to the callback function', function() {
1031
- var capture = '';
1032
- var getVariablesCaptured = function(s) {
1033
- capture = s;
1034
- };
1035
- annyang.addCommands(
1036
- {
1037
- 'Time for some thrilling :stuff': getVariablesCaptured
1038
- }
1039
- );
1040
- recognition.say('Time for some thrilling heroics');
1041
- expect(capture).toEqual('heroics');
1042
- });
1043
-
1044
- it('should match commands with splats', function() {
1045
- var spyOnMatch = jasmine.createSpy();
1046
- annyang.addCommands(
1047
- {
1048
- 'Time for some *stuff': spyOnMatch
1049
- }
1050
- );
1051
- recognition.say('Time for some thrilling heroics');
1052
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1053
- });
1054
-
1055
- it('should pass splats to the callback function', function() {
1056
- var capture = '';
1057
- var getVariablesCaptured = function(s) {
1058
- capture = s;
1059
- };
1060
- annyang.addCommands(
1061
- {
1062
- 'Time for some *stuff': getVariablesCaptured
1063
- }
1064
- );
1065
- recognition.say('Time for some thrilling heroics');
1066
- expect(capture).toEqual('thrilling heroics');
1067
- });
1068
-
1069
- it('should match commands with optional words when the word is in the sentence', function() {
1070
- var spyOnMatch = jasmine.createSpy();
1071
- annyang.addCommands(
1072
- {
1073
- 'Time for some (thrilling) heroics': spyOnMatch
1074
- }
1075
- );
1076
- recognition.say('Time for some thrilling heroics');
1077
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1078
- });
1079
-
1080
- it('should match commands with optional words when the word is not in the sentence', function() {
1081
- var spyOnMatch = jasmine.createSpy();
1082
- annyang.addCommands(
1083
- {
1084
- 'Time for some (thrilling) heroics': spyOnMatch
1085
- }
1086
- );
1087
- recognition.say('Time for some heroics');
1088
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1089
- });
1090
-
1091
- it('should not match commands with optional words when a different word is in the sentence', function() {
1092
- var spyOnMatch = jasmine.createSpy();
1093
- annyang.addCommands(
1094
- {
1095
- 'Time for some (thrilling) heroics': spyOnMatch
1096
- }
1097
- );
1098
- recognition.say('Time for some gorram heroics');
1099
- expect(spyOnMatch).not.toHaveBeenCalled();
1100
- });
1101
-
1102
- it('should not break when a command is removed by another command being called', function() {
1103
- var commands = {
1104
- 'Malcolm': function() { annyang.removeCommands(); },
1105
- 'Wash': function() { annyang.removeCommands('Malcolm'); }
1106
- };
1107
-
1108
- annyang.addCommands(commands);
1109
- expect(function() {
1110
- recognition.say('Malcolm');
1111
- }).not.toThrowError();
1112
-
1113
- annyang.removeCommands();
1114
- annyang.addCommands(commands);
1115
- expect(function() {
1116
- recognition.say('Wash');
1117
- }).not.toThrowError();
1118
- });
1119
-
1120
- it('should not break when a command is added by another command being called', function() {
1121
- var commands = {
1122
- 'Malcolm': function() { annyang.addCommands({'Zoe': function() {}}); }
1123
- };
1124
- annyang.addCommands(commands);
1125
- expect(function() {
1126
- recognition.say('Malcolm');
1127
- }).not.toThrowError();
1128
- });
1129
-
1130
- });
1131
-
1132
- describe("annyang.removeCommands", function() {
1133
-
1134
- var recognition;
1135
- var spyOnMatch1;
1136
- var spyOnMatch2;
1137
- var spyOnMatch3;
1138
- var spyOnMatch4;
1139
- var spyOnMatch5;
1140
-
1141
- beforeEach(function() {
1142
- spyOnMatch1 = jasmine.createSpy();
1143
- spyOnMatch2 = jasmine.createSpy();
1144
- spyOnMatch3 = jasmine.createSpy();
1145
- spyOnMatch4 = jasmine.createSpy();
1146
- spyOnMatch5 = jasmine.createSpy();
1147
- annyang.debug(false);
1148
- annyang.abort();
1149
- annyang.removeCommands();
1150
- annyang.addCommands({
1151
- 'Time for some (thrilling) heroics': spyOnMatch1,
1152
- 'We should start dealing in those *merchandise': spyOnMatch2,
1153
- 'That sounds like something out of science fiction': spyOnMatch3,
1154
- 'too pretty': {'regexp': /We are just too pretty for God to let us die/, callback: spyOnMatch4},
1155
- 'You can\'t take the :thing from me': spyOnMatch5
1156
- });
1157
- recognition = annyang.getSpeechRecognizer();
1158
- annyang.start({ continuous: true });
1159
- annyang.start();
1160
- });
1161
-
1162
- it('should remove a single command when its name is passed as a string in the first parameter', function() {
1163
- annyang.removeCommands('Time for some (thrilling) heroics');
1164
- recognition.say('Time for some heroics');
1165
- recognition.say('We should start dealing in those black-market beagles');
1166
- recognition.say('That sounds like something out of science fiction');
1167
- recognition.say('We are just too pretty for God to let us die');
1168
- recognition.say('You can\'t take the sky from me');
1169
- expect(spyOnMatch1).not.toHaveBeenCalled();
1170
- expect(spyOnMatch2).toHaveBeenCalledTimes(1);
1171
- expect(spyOnMatch3).toHaveBeenCalledTimes(1);
1172
- expect(spyOnMatch4).toHaveBeenCalledTimes(1);
1173
- expect(spyOnMatch5).toHaveBeenCalledTimes(1);
1174
- });
1175
-
1176
- it('should remove multiple commands when their names are passed as an array in the first parameter', function() {
1177
- annyang.removeCommands(['Time for some (thrilling) heroics', 'That sounds like something out of science fiction']);
1178
- recognition.say('Time for some heroics');
1179
- recognition.say('We should start dealing in those black-market beagles');
1180
- recognition.say('That sounds like something out of science fiction');
1181
- recognition.say('We are just too pretty for God to let us die');
1182
- recognition.say('You can\'t take the sky from me');
1183
- expect(spyOnMatch1).not.toHaveBeenCalled();
1184
- expect(spyOnMatch2).toHaveBeenCalledTimes(1);
1185
- expect(spyOnMatch3).not.toHaveBeenCalled();
1186
- expect(spyOnMatch4).toHaveBeenCalledTimes(1);
1187
- expect(spyOnMatch5).toHaveBeenCalledTimes(1);
1188
- });
1189
-
1190
- it('should remove all commands when called with no parameters', function() {
1191
- annyang.removeCommands();
1192
- recognition.say('Time for some heroics');
1193
- recognition.say('We should start dealing in those black-market beagles');
1194
- recognition.say('That sounds like something out of science fiction');
1195
- recognition.say('We are just too pretty for God to let us die');
1196
- recognition.say('You can\'t take the sky from me');
1197
- expect(spyOnMatch1).not.toHaveBeenCalled();
1198
- expect(spyOnMatch2).not.toHaveBeenCalled();
1199
- expect(spyOnMatch3).not.toHaveBeenCalled();
1200
- expect(spyOnMatch4).not.toHaveBeenCalled();
1201
- expect(spyOnMatch5).not.toHaveBeenCalled();
1202
- });
1203
-
1204
- it('should remove a single command with an optional word when its name is passed as a string in the first parameter', function() {
1205
- annyang.removeCommands('Time for some (thrilling) heroics');
1206
- recognition.say('Time for some heroics');
1207
- recognition.say('We should start dealing in those black-market beagles');
1208
- recognition.say('That sounds like something out of science fiction');
1209
- recognition.say('We are just too pretty for God to let us die');
1210
- recognition.say('You can\'t take the sky from me');
1211
- expect(spyOnMatch1).not.toHaveBeenCalled();
1212
- expect(spyOnMatch2).toHaveBeenCalledTimes(1);
1213
- expect(spyOnMatch3).toHaveBeenCalledTimes(1);
1214
- expect(spyOnMatch4).toHaveBeenCalledTimes(1);
1215
- expect(spyOnMatch5).toHaveBeenCalledTimes(1);
1216
- });
1217
-
1218
- it('should remove a single command with a named variable when its name is passed as a string in the first parameter', function() {
1219
- annyang.removeCommands('You can\'t take the :thing from me');
1220
- recognition.say('Time for some heroics');
1221
- recognition.say('We should start dealing in those black-market beagles');
1222
- recognition.say('That sounds like something out of science fiction');
1223
- recognition.say('We are just too pretty for God to let us die');
1224
- recognition.say('You can\'t take the sky from me');
1225
- expect(spyOnMatch1).toHaveBeenCalledTimes(1);
1226
- expect(spyOnMatch2).toHaveBeenCalledTimes(1);
1227
- expect(spyOnMatch3).toHaveBeenCalledTimes(1);
1228
- expect(spyOnMatch4).toHaveBeenCalledTimes(1);
1229
- expect(spyOnMatch5).not.toHaveBeenCalled();
1230
- });
1231
-
1232
- it('should remove a single command with a splat when its name is passed as a parameter', function() {
1233
- annyang.removeCommands('We should start dealing in those *merchandise');
1234
- recognition.say('Time for some heroics');
1235
- recognition.say('We should start dealing in those black-market beagles');
1236
- recognition.say('That sounds like something out of science fiction');
1237
- recognition.say('We are just too pretty for God to let us die');
1238
- recognition.say('You can\'t take the sky from me');
1239
- expect(spyOnMatch1).toHaveBeenCalledTimes(1);
1240
- expect(spyOnMatch2).not.toHaveBeenCalled();
1241
- expect(spyOnMatch3).toHaveBeenCalledTimes(1);
1242
- expect(spyOnMatch4).toHaveBeenCalledTimes(1);
1243
- expect(spyOnMatch5).toHaveBeenCalledTimes(1);
1244
- });
1245
-
1246
- it('should remove a single regexp command when its name is passed as a parameter', function() {
1247
- annyang.removeCommands('too pretty');
1248
- recognition.say('Time for some heroics');
1249
- recognition.say('We should start dealing in those black-market beagles');
1250
- recognition.say('That sounds like something out of science fiction');
1251
- recognition.say('We are just too pretty for God to let us die');
1252
- recognition.say('You can\'t take the sky from me');
1253
- expect(spyOnMatch1).toHaveBeenCalledTimes(1);
1254
- expect(spyOnMatch2).toHaveBeenCalledTimes(1);
1255
- expect(spyOnMatch3).toHaveBeenCalledTimes(1);
1256
- expect(spyOnMatch4).not.toHaveBeenCalled();
1257
- expect(spyOnMatch5).toHaveBeenCalledTimes(1);
1258
- });
1259
-
1260
- });
1261
-
1262
- describe('annyang.pause', function() {
1263
-
1264
- var recognition;
1265
- var spyOnMatch;
1266
-
1267
- beforeEach(function() {
1268
- annyang.debug(false);
1269
- spyOnMatch = jasmine.createSpy();
1270
- annyang.abort();
1271
- annyang.removeCommands();
1272
- annyang.addCommands({
1273
- 'Time for some thrilling heroics': spyOnMatch
1274
- });
1275
- annyang.start();
1276
- recognition = annyang.getSpeechRecognizer();
1277
- spyOn(console, 'log');
1278
- });
1279
-
1280
- it('should return undefined when called', function() {
1281
- expect(annyang.pause()).toEqual(undefined);
1282
- });
1283
-
1284
- it('should cause annyang.isListening() to return false', function() {
1285
- expect(annyang.isListening()).toBe(true);
1286
- annyang.pause();
1287
- expect(annyang.isListening()).toBe(false);
1288
- });
1289
-
1290
- it('should cause commands not to fire even if user says the right thing', function() {
1291
- expect(spyOnMatch).not.toHaveBeenCalled();
1292
- recognition.say('Time for some thrilling heroics');
1293
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1294
- annyang.pause();
1295
- recognition.say('Time for some thrilling heroics');
1296
- expect(spyOnMatch).toHaveBeenCalledTimes(1);
1297
- });
1298
-
1299
- it('should cause a debug message if speech detected while paused and debug is on', function() {
1300
- annyang.debug(true);
1301
- expect(console.log).not.toHaveBeenCalled();
1302
- annyang.pause();
1303
- recognition.say('Time for some thrilling heroics');
1304
- expect(console.log).toHaveBeenCalledTimes(1);
1305
- expect(console.log).toHaveBeenCalledWith('Speech heard, but annyang is paused');
1306
- });
1307
-
1308
- it('should not cause a debug message if speech detected while paused but debug is off', function() {
1309
- annyang.debug(false);
1310
- expect(console.log).not.toHaveBeenCalled();
1311
- annyang.pause();
1312
- recognition.say('Time for some thrilling heroics');
1313
- expect(console.log).not.toHaveBeenCalled();
1314
- });
1315
-
1316
- it('should not stop the browser\'s Speech Recognition engine', function() {
1317
- expect(recognition.isStarted()).toBe(true);
1318
- annyang.pause();
1319
- expect(recognition.isStarted()).toBe(true);
1320
- });
1321
-
1322
- it('should leave annyang paused if called after annyang.abort()', function() {
1323
- expect(annyang.isListening()).toBe(true);
1324
- annyang.abort();
1325
- expect(annyang.isListening()).toBe(false);
1326
- annyang.pause();
1327
- expect(annyang.isListening()).toBe(false);
1328
- });
1329
-
1330
- it("should leave the browser's Speech Recognition off, if called after annyang.abort()", function() {
1331
- expect(recognition.isStarted()).toBe(true);
1332
- annyang.abort();
1333
- expect(recognition.isStarted()).toBe(false);
1334
- annyang.pause();
1335
- expect(recognition.isStarted()).toBe(false);
1336
- });
1337
-
1338
- });
1339
-
1340
- describe('annyang.resume', function() {
1341
-
1342
- var recognition;
1343
- var spyOnMatch;
1344
-
1345
- beforeEach(function() {
1346
- annyang.debug(false);
1347
- spyOnMatch = jasmine.createSpy();
1348
- annyang.abort();
1349
- annyang.removeCommands();
1350
- annyang.addCommands({
1351
- 'Time for some thrilling heroics': spyOnMatch
1352
- });
1353
- recognition = annyang.getSpeechRecognizer();
1354
- spyOn(console, 'log');
1355
- });
1356
-
1357
- it('should return undefined when called', function() {
1358
- expect(annyang.resume()).toEqual(undefined);
1359
- });
1360
-
1361
- it('should leave speech recognition on and turn annyang on, if called when annyang is paused', function() {
1362
- annyang.start();
1363
- annyang.pause();
1364
- expect(annyang.isListening()).toBe(false);
1365
- expect(recognition.isStarted()).toBe(true);
1366
- annyang.resume();
1367
- expect(annyang.isListening()).toBe(true);
1368
- expect(recognition.isStarted()).toBe(true);
1369
- });
1370
-
1371
- it('should turn speech recognition and annyang on, if called when annyang is stopped', function() {
1372
- expect(annyang.isListening()).toBe(false);
1373
- expect(recognition.isStarted()).toBe(false);
1374
- annyang.resume();
1375
- expect(annyang.isListening()).toBe(true);
1376
- expect(recognition.isStarted()).toBe(true);
1377
- });
1378
-
1379
- it('should leave speech recognition and annyang on, if called when annyang is listening', function() {
1380
- annyang.start();
1381
- expect(annyang.isListening()).toBe(true);
1382
- expect(recognition.isStarted()).toBe(true);
1383
- annyang.resume();
1384
- expect(annyang.isListening()).toBe(true);
1385
- expect(recognition.isStarted()).toBe(true);
1386
- });
1387
-
1388
- it('should log a message if debug is on, and resume was called when annyang is listening', function() {
1389
- annyang.debug(true);
1390
- annyang.start();
1391
- annyang.resume();
1392
- expect(console.log).toHaveBeenCalledTimes(1);
1393
- expect(console.log).toHaveBeenCalledWith('Failed to execute \'start\' on \'SpeechRecognition\': recognition has already started.');
1394
- });
1395
-
1396
- it('should not log a message if debug is off, and resume was called when annyang is listening', function() {
1397
- annyang.debug(false);
1398
- annyang.start();
1399
- annyang.resume();
1400
- expect(console.log).not.toHaveBeenCalled();
1401
- });
1402
-
1403
- });
1404
-
1405
- describe('annyang.setLanguage', function() {
1406
-
1407
- var recognition;
1408
-
1409
- beforeEach(function() {
1410
- recognition = annyang.getSpeechRecognizer();
1411
- });
1412
-
1413
- it('should return undefined when called', function() {
1414
- expect(annyang.setLanguage()).toEqual(undefined);
1415
- });
1416
-
1417
- it('should set the Speech Recognition engine to the value passed', function() {
1418
- annyang.setLanguage('he');
1419
- expect(recognition.lang).toEqual('he');
1420
- });
1421
-
1422
- });
1423
-
1424
- describe('annyang.trigger', function() {
1425
-
1426
- var spyOnCommand;
1427
- var spyOnResult;
1428
- var spyOnResultMatch;
1429
- var spyOnResultNoMatch;
1430
- var sentence1 = 'Time for some thrilling heroics';
1431
- var sentence2 = 'That sounds like something out of science fiction';
1432
-
1433
- beforeEach(function() {
1434
- annyang.debug(false);
1435
- spyOnCommand = jasmine.createSpy();
1436
- spyOnResult = jasmine.createSpy();
1437
- spyOnResultMatch = jasmine.createSpy();
1438
- spyOnResultNoMatch = jasmine.createSpy();
1439
- annyang.abort();
1440
- annyang.start();
1441
- annyang.removeCommands();
1442
- annyang.addCommands({
1443
- 'Time for some :type heroics': spyOnCommand
1444
- });
1445
- spyOn(console, 'log');
1446
- });
1447
-
1448
- it('should accept a string with a word or sentence as the first argument', function() {
1449
- expect(annyang.trigger(sentence1)).toEqual(undefined);
1450
- });
1451
-
1452
- it('should accept an array of strings, each with a word or sentence as the first argument', function() {
1453
- expect(annyang.trigger([sentence1, sentence1+' and so on'])).toEqual(undefined);
1454
- });
1455
-
1456
- it('should match a sentence passed as a string to a command and execute it as if it was passed from Speech Recognition', function() {
1457
- expect(spyOnCommand).not.toHaveBeenCalled();
1458
- annyang.trigger(sentence1);
1459
- expect(spyOnCommand).toHaveBeenCalledTimes(1);
1460
- });
1461
-
1462
- it('should match a sentence passed in an array to a command and execute it as if it was passed from Speech Recognition', function() {
1463
- expect(spyOnCommand).not.toHaveBeenCalled();
1464
- annyang.trigger([sentence1+' and so on', sentence1]);
1465
- expect(spyOnCommand).toHaveBeenCalledTimes(1);
1466
- });
1467
-
1468
- it('should trigger a result event', function() {
1469
- annyang.addCallback('result', spyOnResult);
1470
- expect(spyOnResult).not.toHaveBeenCalled();
1471
- annyang.trigger(sentence1);
1472
- expect(spyOnResult).toHaveBeenCalledTimes(1);
1473
- });
1474
-
1475
- it('should trigger a resultMatch event if sentence matches a command', function() {
1476
- annyang.addCallback('resultMatch', spyOnResultMatch);
1477
- expect(spyOnResultMatch).not.toHaveBeenCalled();
1478
- annyang.trigger(sentence1);
1479
- expect(spyOnResultMatch).toHaveBeenCalledTimes(1);
1480
- });
1481
-
1482
- it('should trigger a resultNoMatch event if sentence does not match a command', function() {
1483
- annyang.addCallback('resultNoMatch', spyOnResultNoMatch);
1484
- expect(spyOnResultNoMatch).not.toHaveBeenCalled();
1485
- annyang.trigger(sentence2);
1486
- expect(spyOnResultNoMatch).toHaveBeenCalledTimes(1);
1487
- });
1488
-
1489
- it('should not trigger a matching command if annyang is aborted or not started', function() {
1490
- expect(spyOnCommand).not.toHaveBeenCalled();
1491
- annyang.abort();
1492
- annyang.trigger(sentence1);
1493
- expect(spyOnCommand).not.toHaveBeenCalled();
1494
- });
1495
-
1496
- it('should not trigger a matching command if annyang is paused', function() {
1497
- expect(spyOnCommand).not.toHaveBeenCalled();
1498
- annyang.pause();
1499
- annyang.trigger(sentence1);
1500
- expect(spyOnCommand).not.toHaveBeenCalled();
1501
- });
1502
-
1503
- it('should log to console if attemting to trigger a command while annyang is aborted or not started', function() {
1504
- annyang.debug(true);
1505
- annyang.abort();
1506
- expect(console.log).not.toHaveBeenCalled();
1507
- annyang.trigger(sentence1);
1508
- expect(console.log).toHaveBeenCalledTimes(1);
1509
- expect(console.log).toHaveBeenCalledWith('Cannot trigger while annyang is aborted');
1510
- });
1511
-
1512
- it('should not log to console if attemting to trigger a command while annyang is aborted or not started and debug is off', function() {
1513
- annyang.debug(false);
1514
- annyang.abort();
1515
- annyang.trigger(sentence1);
1516
- expect(console.log).not.toHaveBeenCalled();
1517
- });
1518
-
1519
- });
1520
-
1521
- describe('annyang', function() {
1522
-
1523
- var recognition;
1524
-
1525
- beforeEach(function() {
1526
- jasmine.clock().install();
1527
- annyang.debug(false);
1528
- annyang.abort();
1529
- annyang.removeCommands();
1530
- recognition = annyang.getSpeechRecognizer();
1531
- spyOn(console, 'log');
1532
- });
1533
-
1534
- afterEach(function() {
1535
- jasmine.clock().tick(2000);
1536
- jasmine.clock().uninstall();
1537
- });
1538
-
1539
- it('should write to console each speech recognition alternative that is recognized when no command matches', function() {
1540
- annyang.start();
1541
- annyang.debug(true);
1542
- expect(console.log).not.toHaveBeenCalled();
1543
- recognition.say('Time for some thrilling heroics');
1544
- expect(console.log).toHaveBeenCalledTimes(5);
1545
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics', 'font-weight: bold; color: #00f;');
1546
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics and so on', 'font-weight: bold; color: #00f;');
1547
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics and so on and so on', 'font-weight: bold; color: #00f;');
1548
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics and so on and so on and so on', 'font-weight: bold; color: #00f;');
1549
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics and so on and so on and so on and so on', 'font-weight: bold; color: #00f;');
1550
- });
1551
-
1552
- it('should not write to console the speech recognition alternatives when no command matches and debug is off', function() {
1553
- annyang.start();
1554
- annyang.debug(false);
1555
- recognition.say('Time for some thrilling heroics');
1556
- expect(console.log).not.toHaveBeenCalled();
1557
- });
1558
-
1559
- it('should write to console each speech recognition alternative that is recognized, until a command is matched', function() {
1560
- annyang.addCommands(
1561
- {
1562
- 'Time for some thrilling heroics and so on': function() {}
1563
- }
1564
- );
1565
- annyang.start();
1566
- annyang.debug(true);
1567
- expect(console.log).not.toHaveBeenCalled();
1568
- recognition.say('Time for some thrilling heroics');
1569
- expect(console.log).toHaveBeenCalledTimes(3); // 2 console logs for speech recognized + 1 for the command matching
1570
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics', 'font-weight: bold; color: #00f;');
1571
- expect(console.log).toHaveBeenCalledWith('Speech recognized: %cTime for some thrilling heroics and so on', 'font-weight: bold; color: #00f;');
1572
- });
1573
-
1574
- it('should not write to console speech recognition alternatives when debug is off', function() {
1575
- annyang.addCommands(
1576
- {
1577
- 'Time for some thrilling heroics and so on': function() {}
1578
- }
1579
- );
1580
- annyang.start();
1581
- annyang.debug(false);
1582
- recognition.say('Time for some thrilling heroics');
1583
- expect(console.log).not.toHaveBeenCalled();
1584
- });
1585
-
1586
- it('should recognize when Speech Recognition engine was aborted', function() {
1587
- annyang.start();
1588
- expect(annyang.isListening()).toBe(true);
1589
- recognition.abort();
1590
- expect(annyang.isListening()).toBe(false);
1591
- });
1592
-
1593
- it('should recognize when Speech Recognition engine is repeatedly aborted as soon as it is started and console.log about it once every 10 seconds', function() {
1594
- recognition.addEventListener('start', function() {
1595
- setTimeout(function() {recognition.abort();}, 1);
1596
- });
1597
- annyang.start();
1598
- annyang.debug(true);
1599
- jasmine.clock().tick(20000);
1600
- expect(console.log).toHaveBeenCalledTimes(2);
1601
- expect(console.log).toHaveBeenCalledWith('Speech Recognition is repeatedly stopping and starting. See http://is.gd/annyang_restarts for tips.');
1602
- });
1603
-
1604
- it('should not log to console when Speech Recognition engine is repeatedly aborted if debug is off', function() {
1605
- recognition.addEventListener('start', function() {
1606
- setTimeout(function() {recognition.abort();}, 1);
1607
- });
1608
- annyang.start();
1609
- annyang.debug(false);
1610
- jasmine.clock().tick(20000);
1611
- expect(console.log).not.toHaveBeenCalled();
1612
- });
1613
-
1614
- });
1615
-
1616
- }));