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
3 // found in the LICENSE file.
5 // Include test fixture.
6 GEN_INCLUDE(['chromevox_unittest_base.js',
9 function speak(text, opt_properties) {
10 cvox.ChromeVox.tts.speak(text, 0, opt_properties);
13 function braille(text) {
14 var navBraille = cvox.NavBraille.fromText(text);
15 cvox.ChromeVox.braille.write(navBraille);
22 * @extends {ChromeVoxUnitTestBase}
24 function MockFeedbackUnitTest() {
25 ChromeVoxUnitTestBase.call(this);
26 this.expectedCalls = [];
29 MockFeedbackUnitTest.prototype = {
30 __proto__: ChromeVoxUnitTestBase.prototype,
33 cvox.ChromeVox = cvox.ChromeVox || {};
37 'cvox.BrailleInterface',
43 TEST_F('MockFeedbackUnitTest', 'speechAndCallbacks', function() {
44 var afterThirdStringCalled = false;
45 var spruiousStringEndCallbackCalled = false;
46 var finishCalled = false;
47 var mock = new MockFeedback(function() {
48 assertFalse(finishCalled);
51 assertTrue(afterThirdStringCalled);
52 assertTrue(spruiousStringEndCallbackCalled);
55 speak('First string');
56 speak('Second string');
57 mock.expectSpeech('First string', 'Second string')
58 .expectSpeech('Third string')
60 assertFalse(afterThirdStringCalled);
61 afterThirdStringCalled = true;
62 speak('Spurious string', {endCallback: function() {
63 assertFalse(spruiousStringEndCallbackCalled);
64 spruiousStringEndCallbackCalled = true;
66 speak('Fourth string');
68 .expectSpeech('Fourth string')
70 assertFalse(finishCalled);
71 speak('Third string');
72 assertTrue(finishCalled);
75 TEST_F('MockFeedbackUnitTest', 'startAndEndCallbacks', function() {
76 var onlyStartCallbackCalled = false;
77 var onlyEndCallbackCalled = false;
78 var bothCallbacksStartCalled = false;
79 var bothCallbacksEndCalled = false;
80 var mock = new MockFeedback();
82 speak('No callbacks', {});
83 speak('Only start callback', {startCallback: function() {
84 assertFalse(onlyStartCallbackCalled);
85 onlyStartCallbackCalled = true;
86 assertFalse(onlyEndCallbackCalled);
88 speak('Only end callback', {endCallback: function() {
89 assertTrue(onlyStartCallbackCalled);
90 assertFalse(onlyEndCallbackCalled);
91 onlyEndCallbackCalled = true;
92 assertFalse(bothCallbacksStartCalled);
94 speak('Both callbacks',
95 {startCallback: function() {
96 assertTrue(onlyEndCallbackCalled);
97 assertFalse(bothCallbacksStartCalled);
98 bothCallbacksStartCalled = true;
99 assertFalse(bothCallbacksEndCalled);
101 endCallback: function() {
102 assertTrue(bothCallbacksStartCalled);
103 assertFalse(bothCallbacksEndCalled);
104 bothCallbacksEndCalled = true;
106 mock.expectSpeech('Both callbacks');
108 assertTrue(bothCallbacksEndCalled);
111 TEST_F('MockFeedbackUnitTest', 'SpeechAndBraille', function() {
112 var secondCallbackCalled = false;
113 var finishCalled = false;
114 var mock = new MockFeedback(function() { finishCalled = true; });
115 var firstExpectedNavBraille;
117 braille('Some braille');
118 speak('Some speech');
119 mock.call(function() {
120 assertEquals(null, mock.lastMatchedBraille);
121 firstExpectedNavBraille = braille('First expected braille');
122 speak('First expected speech');
123 braille('Some other braille');
125 .expectSpeech('First expected speech')
126 .expectBraille('First expected braille')
128 secondCallbackCalled = true;
129 assertEquals(firstExpectedNavBraille, mock.lastMatchedBraille);
132 assertTrue(secondCallbackCalled);
133 assertTrue(finishCalled);
136 TEST_F('MockFeedbackUnitTest', 'expectWithRegex', function() {
138 var mock = new MockFeedback();
140 mock.call(function() { braille('Item 1 of 14'); })
141 .expectBraille(/Item \d+ of \d+/)
142 .call(function() { done = true;})
147 TEST_F('MockFeedbackUnitTest', 'expectAfterReplayThrows', function() {
148 var mock = new MockFeedback();
150 assertException('', function() {
151 mock.expectSpeech('hello');
155 TEST_F('MockFeedbackUnitTest', 'NoMatchDoesNotFinish', function() {
156 var firstCallbackCalled = false;
157 var mock = new MockFeedback(function() {
158 throw Error('Should not be called');
161 braille('Some string');
162 mock.call(function() {
163 braille('Some other string');
164 firstCallbackCalled = true;
166 .expectBraille('Unmatched string')
168 throw Error('Should not be called');
171 assertTrue(firstCallbackCalled);