// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Include test fixture. GEN_INCLUDE(['chromevox_unittest_base.js', 'mock_feedback.js']); function speak(text, opt_properties) { cvox.ChromeVox.tts.speak(text, 0, opt_properties); } function braille(text) { var navBraille = cvox.NavBraille.fromText(text); cvox.ChromeVox.braille.write(navBraille); return navBraille; } function earcon(earconName) { cvox.ChromeVox.earcons.playEarcon(cvox.Earcon[earconName]); } /** * Test fixture. * @constructor * @extends {ChromeVoxUnitTestBase} */ function MockFeedbackUnitTest() { ChromeVoxUnitTestBase.call(this); this.expectedCalls = []; } MockFeedbackUnitTest.prototype = { __proto__: ChromeVoxUnitTestBase.prototype, setUp: function() { cvox.ChromeVox = cvox.ChromeVox || {}; }, closureModuleDeps: [ 'cvox.BrailleInterface', 'cvox.NavBraille', 'cvox.TtsInterface', 'cvox.AbstractEarcons' ] }; TEST_F('MockFeedbackUnitTest', 'speechAndCallbacks', function() { var afterThirdStringCalled = false; var spruiousStringEndCallbackCalled = false; var finishCalled = false; var mock = new MockFeedback(function() { assertFalse(finishCalled); finishCalled = true; assertTrue(afterThirdStringCalled); assertTrue(spruiousStringEndCallbackCalled); }); mock.install(); speak('First string'); speak('Second string'); mock.expectSpeech('First string', 'Second string') .expectSpeech('Third string') .call(function() { assertFalse(afterThirdStringCalled); afterThirdStringCalled = true; speak('Spurious string', {endCallback: function() { assertFalse(spruiousStringEndCallbackCalled); spruiousStringEndCallbackCalled = true; }}); speak('Fourth string'); }) .expectSpeech('Fourth string') .replay(); assertFalse(finishCalled); speak('Third string'); assertTrue(finishCalled); }); TEST_F('MockFeedbackUnitTest', 'startAndEndCallbacks', function() { var onlyStartCallbackCalled = false; var onlyEndCallbackCalled = false; var bothCallbacksStartCalled = false; var bothCallbacksEndCalled = false; var mock = new MockFeedback(); mock.install(); speak('No callbacks', {}); speak('Only start callback', {startCallback: function() { assertFalse(onlyStartCallbackCalled); onlyStartCallbackCalled = true; assertFalse(onlyEndCallbackCalled); }}); speak('Only end callback', {endCallback: function() { assertTrue(onlyStartCallbackCalled); assertFalse(onlyEndCallbackCalled); onlyEndCallbackCalled = true; assertFalse(bothCallbacksStartCalled); }}); speak('Both callbacks', {startCallback: function() { assertTrue(onlyEndCallbackCalled); assertFalse(bothCallbacksStartCalled); bothCallbacksStartCalled = true; assertFalse(bothCallbacksEndCalled); }, endCallback: function() { assertTrue(bothCallbacksStartCalled); assertFalse(bothCallbacksEndCalled); bothCallbacksEndCalled = true; }}); mock.expectSpeech('Both callbacks'); mock.replay(); assertTrue(bothCallbacksEndCalled); }); TEST_F('MockFeedbackUnitTest', 'SpeechAndBraille', function() { var secondCallbackCalled = false; var finishCalled = false; var mock = new MockFeedback(function() { finishCalled = true; }); var firstExpectedNavBraille; mock.install(); braille('Some braille'); speak('Some speech'); mock.call(function() { assertEquals(null, mock.lastMatchedBraille); firstExpectedNavBraille = braille('First expected braille'); speak('First expected speech'); braille('Some other braille'); }) .expectSpeech('First expected speech') .expectBraille('First expected braille') .call(function() { secondCallbackCalled = true; assertEquals(firstExpectedNavBraille, mock.lastMatchedBraille); }) .replay(); assertTrue(secondCallbackCalled); assertTrue(finishCalled); }); TEST_F('MockFeedbackUnitTest', 'expectWithRegex', function() { var done = false; var mock = new MockFeedback(); mock.install(); mock.call(function() { braille('Item 1 of 14'); }) .expectBraille(/Item \d+ of \d+/) .call(function() { done = true;}) .replay(); assertTrue(done); }); TEST_F('MockFeedbackUnitTest', 'expectAfterReplayThrows', function() { var mock = new MockFeedback(); mock.replay(); assertException('', function() { mock.expectSpeech('hello'); }, 'AssertionError'); }); TEST_F('MockFeedbackUnitTest', 'NoMatchDoesNotFinish', function() { var firstCallbackCalled = false; var mock = new MockFeedback(function() { throw Error('Should not be called'); }); mock.install(); braille('Some string'); mock.call(function() { braille('Some other string'); firstCallbackCalled = true; }) .expectBraille('Unmatched string') .call(function() { throw Error('Should not be called'); }) .replay(); assertTrue(firstCallbackCalled); }); TEST_F('MockFeedbackUnitTest', 'SpeechAndEarcons', function() { var finishCalled = false; var mock = new MockFeedback(function() { finishCalled = true; }); mock.install(); mock.call(function() { speak('MyButton', {startCallback: function() { earcon('BUTTON'); }}); }) .expectSpeech('MyButton') .expectEarcon(cvox.Earcon.BUTTON) .call(function() { earcon('ALERT_MODAL'); speak('MyTextField', {startCallback: function() { earcon('EDITABLE_TEXT'); }}); }) .expectEarcon(cvox.Earcon.ALERT_MODAL) .expectSpeech('MyTextField') .expectEarcon(cvox.Earcon.EDITABLE_TEXT) .replay(); assertTrue(finishCalled); });