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.
5 GEN_INCLUDE(['assert_additions.js']);
6 GEN_INCLUDE(['common.js',
7 'callback_helper.js']);
10 * Base test fixture for ChromeVox unit tests.
12 * Note that while conceptually these are unit tests, these tests need
13 * to run in a full web page, so they're actually run as WebUI browser
17 * @extends {testing.Test}
19 function ChromeVoxUnitTestBase() {
21 this.callbackHelper_
= new CallbackHelper(this);
25 ChromeVoxUnitTestBase
.prototype = {
26 __proto__
: testing
.Test
.prototype,
30 'cvox.ChromeVoxTester',
31 'cvox.ChromeVoxUserCommands',
32 'cvox.SpokenListBuilder',
36 browsePreload
: DUMMY_URL
,
40 * It doesn't make sense to run the accessibility audit on these tests,
41 * since many of them are deliberately testing inaccessible html.
43 runAccessibilityChecks
: false,
46 * Loads some inlined html into the body of the current document, replacing
47 * whatever was there previously.
48 * @param {string} html The html to load as a string.
50 loadHtml: function(html
) {
51 while (document
.head
.firstChild
) {
52 document
.head
.removeChild(document
.head
.firstChild
);
54 while (document
.body
.firstChild
) {
55 document
.body
.removeChild(document
.body
.firstChild
);
57 this.appendHtml(html
);
61 * Loads some inlined html into the current document, replacing
62 * whatever was there previously. This version takes the html
63 * encoded as a comment inside a function, so you can use it like this:
65 * this.loadDoc(function() {/*!
66 * <p>Html goes here</p>
69 * @param {Function} commentEncodedHtml The html to load, embedded as a
70 * comment inside an anonymous function - see example, above.
72 loadDoc: function(commentEncodedHtml
) {
74 TestUtils
.extractHtmlFromCommentEncodedString(commentEncodedHtml
);
79 * Appends some inlined html into the current document, at the end of
80 * the body element. Takes the html encoded as a comment inside a function,
81 * so you can use it like this:
83 * this.appendDoc(function() {/*!
84 * <p>Html goes here</p>
87 * @param {Function} commentEncodedHtml The html to load, embedded as a
88 * comment inside an anonymous function - see example, above.
90 appendDoc: function(commentEncodedHtml
) {
92 TestUtils
.extractHtmlFromCommentEncodedString(commentEncodedHtml
);
93 this.appendHtml(html
);
97 * Appends some inlined html into the current document, at the end of
99 * @param {string} html The html to load as a string.
101 appendHtml: function(html
) {
102 var div
= document
.createElement('div');
103 div
.innerHTML
= html
;
104 var fragment
= document
.createDocumentFragment();
105 while (div
.firstChild
) {
106 fragment
.appendChild(div
.firstChild
);
108 document
.body
.appendChild(fragment
);
112 * Waits for the queued events in ChromeVoxEventWatcher to be
113 * handled, then calls a callback function with provided arguments
114 * in the test case scope. Very useful for asserting the results of events.
116 * @param {function()} func A function to call when ChromeVox is ready.
117 * @param {*} var_args Additional arguments to pass through to the function.
118 * @return {ChromeVoxUnitTestBase} this.
120 waitForCalm: function(func
, var_args
) {
121 var calmArguments
= Array
.prototype.slice
.call(arguments
);
122 calmArguments
.shift();
123 cvox
.ChromeVoxEventWatcher
.addReadyCallback(this.newCallback(function() {
124 func
.apply(this, calmArguments
);
126 return this; // for chaining.
130 * Asserts the TTS engine spoke a certain string. Clears the TTS buffer.
131 * @param {string} expectedText The expected text.
132 * @return {ChromeVoxUnitTestBase} this.
134 assertSpoken: function(expectedText
) {
135 assertEquals(expectedText
,
136 cvox
.ChromeVoxTester
.testTts().getUtterancesAsString());
137 cvox
.ChromeVoxTester
.clearUtterances();
138 return this; // for chaining.
142 * Asserts a list of utterances are in the correct queue mode.
143 * @param {cvox.SpokenListBuilder|Array} expectedList A list
144 * of [text, queueMode] tuples OR a SpokenListBuilder with the expected
146 * @return {ChromeVoxUnitTestBase} this.
148 assertSpokenList: function(expectedList
) {
149 if (expectedList
instanceof cvox
.SpokenListBuilder
) {
150 expectedList
= expectedList
.build();
153 var ulist
= cvox
.ChromeVoxTester
.testTts().getUtteranceInfoList();
154 for (var i
= 0; i
< expectedList
.length
; i
++) {
155 var text
= expectedList
[i
][0];
156 var queueMode
= expectedList
[i
][1];
157 this.assertSingleUtterance_(text
, queueMode
,
158 ulist
[i
].text
, ulist
[i
].queueMode
);
160 cvox
.ChromeVoxTester
.clearUtterances();
161 return this; // for chaining.
164 assertSingleUtterance_: function(
165 expectedText
, expectedQueueMode
, text
, queueMode
) {
166 assertEquals(expectedQueueMode
, queueMode
);
167 assertEquals(expectedText
, text
);
171 * Focuses an element.
172 * @param {string} id The id of the element to focus.
173 * @return {ChromeVoxUnitTestBase} this.
175 setFocus: function(id
) {
177 return this; // for chaining.
181 * Executes a ChromeVox user command.
182 * @param {string} command The name of the command to run.
183 * @return {ChromeVoxUnitTestBase} this.
185 userCommand: function(command
) {
186 cvox
.ChromeVoxUserCommands
.commands
[command
]();
187 return this; // for chaining.
191 * @return {cvox.SpokenListBuilder} A new builder.
193 spokenList: function() {
194 return new cvox
.SpokenListBuilder();
198 * @type {CallbackHelper}
201 callbackHelper_
: null,
204 * Creates a callback that optionally calls {@code opt_callback} when
205 * called. If this method is called one or more times, then
206 * {@code testDone()} will be called when all callbacks have been called.
207 * @param {Function=} opt_callback Wrapped callback that will have its this
208 * reference bound to the test fixture.
211 newCallback: function(opt_callback
) {
212 assertNotEquals(null, this.callbackHelper_
);
213 return this.callbackHelper_
.wrap(opt_callback
);