1 // Copyright 2014 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.
6 'chrome/browser/resources/chromeos/chromevox/testing/assert_additions.js']);
8 'chrome/browser/resources/chromeos/chromevox/testing/common.js',
9 'chrome/browser/resources/chromeos/chromevox/testing/callback_helper.js']);
12 * Base test fixture for ChromeVox unit tests.
14 * Note that while conceptually these are unit tests, these tests need
15 * to run in a full web page, so they're actually run as WebUI browser
19 * @extends {testing.Test}
21 function ChromeVoxUnitTestBase() {
23 this.callbackHelper_ = new CallbackHelper(this);
27 ChromeVoxUnitTestBase.prototype = {
28 __proto__: testing.Test.prototype,
32 'cvox.ChromeVoxTester',
33 'cvox.ChromeVoxUserCommands',
34 'cvox.SpokenListBuilder',
38 browsePreload: DUMMY_URL,
42 * It doesn't make sense to run the accessibility audit on these tests,
43 * since many of them are deliberately testing inaccessible html.
45 runAccessibilityChecks: false,
48 * Loads some inlined html into the body of the current document, replacing
49 * whatever was there previously.
50 * @param {string} html The html to load as a string.
52 loadHtml: function(html) {
53 while (document.head.firstChild) {
54 document.head.removeChild(document.head.firstChild);
56 while (document.body.firstChild) {
57 document.body.removeChild(document.body.firstChild);
59 this.appendHtml(html);
63 * Loads some inlined html into the current document, replacing
64 * whatever was there previously. This version takes the html
65 * encoded as a comment inside a function, so you can use it like this:
67 * this.loadDoc(function() {/*!
68 * <p>Html goes here</p>
71 * @param {Function} commentEncodedHtml The html to load, embedded as a
72 * comment inside an anonymous function - see example, above.
74 loadDoc: function(commentEncodedHtml) {
76 TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
81 * Appends some inlined html into the current document, at the end of
82 * the body element. Takes the html encoded as a comment inside a function,
83 * so you can use it like this:
85 * this.appendDoc(function() {/*!
86 * <p>Html goes here</p>
89 * @param {Function} commentEncodedHtml The html to load, embedded as a
90 * comment inside an anonymous function - see example, above.
92 appendDoc: function(commentEncodedHtml) {
94 TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
95 this.appendHtml(html);
99 * Appends some inlined html into the current document, at the end of
101 * @param {string} html The html to load as a string.
103 appendHtml: function(html) {
104 var div = document.createElement('div');
105 div.innerHTML = html;
106 var fragment = document.createDocumentFragment();
107 while (div.firstChild) {
108 fragment.appendChild(div.firstChild);
110 document.body.appendChild(fragment);
114 * Waits for the queued events in ChromeVoxEventWatcher to be
115 * handled, then calls a callback function with provided arguments
116 * in the test case scope. Very useful for asserting the results of events.
118 * @param {function()} func A function to call when ChromeVox is ready.
119 * @param {*} var_args Additional arguments to pass through to the function.
120 * @return {ChromeVoxUnitTestBase} this.
122 waitForCalm: function(func, var_args) {
123 var calmArguments = Array.prototype.slice.call(arguments);
124 calmArguments.shift();
125 cvox.ChromeVoxEventWatcher.addReadyCallback(this.newCallback(function() {
126 func.apply(this, calmArguments);
128 return this; // for chaining.
132 * Asserts the TTS engine spoke a certain string. Clears the TTS buffer.
133 * @param {string} expectedText The expected text.
134 * @return {ChromeVoxUnitTestBase} this.
136 assertSpoken: function(expectedText) {
137 assertEquals(expectedText,
138 cvox.ChromeVoxTester.testTts().getUtterancesAsString());
139 cvox.ChromeVoxTester.clearUtterances();
140 return this; // for chaining.
144 * Asserts a list of utterances are in the correct queue mode.
145 * @param {cvox.SpokenListBuilder|Array} expectedList A list
146 * of [text, queueMode] tuples OR a SpokenListBuilder with the expected
148 * @return {ChromeVoxUnitTestBase} this.
150 assertSpokenList: function(expectedList) {
151 if (expectedList instanceof cvox.SpokenListBuilder) {
152 expectedList = expectedList.build();
155 var ulist = cvox.ChromeVoxTester.testTts().getUtteranceInfoList();
156 for (var i = 0; i < expectedList.length; i++) {
157 var text = expectedList[i][0];
158 var queueMode = expectedList[i][1];
159 this.assertSingleUtterance_(text, queueMode,
160 ulist[i].text, ulist[i].queueMode);
162 cvox.ChromeVoxTester.clearUtterances();
163 return this; // for chaining.
166 assertSingleUtterance_: function(
167 expectedText, expectedQueueMode, text, queueMode) {
168 assertEquals(expectedQueueMode, queueMode);
169 assertEquals(expectedText, text);
173 * Focuses an element.
174 * @param {string} id The id of the element to focus.
175 * @return {ChromeVoxUnitTestBase} this.
177 setFocus: function(id) {
179 return this; // for chaining.
183 * Executes a ChromeVox user command.
184 * @param {string} command The name of the command to run.
185 * @return {ChromeVoxUnitTestBase} this.
187 userCommand: function(command) {
188 cvox.ChromeVoxUserCommands.commands[command]();
189 return this; // for chaining.
193 * @return {cvox.SpokenListBuilder} A new builder.
195 spokenList: function() {
196 return new cvox.SpokenListBuilder();
200 * @type {CallbackHelper}
203 callbackHelper_: null,
206 * Creates a callback that optionally calls {@code opt_callback} when
207 * called. If this method is called one or more times, then
208 * {@code testDone()} will be called when all callbacks have been called.
209 * @param {Function=} opt_callback Wrapped callback that will have its this
210 * reference bound to the test fixture.
213 newCallback: function(opt_callback) {
214 assertNotEquals(null, this.callbackHelper_);
215 return this.callbackHelper_.wrap(opt_callback);