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']);
11 * Base test fixture for ChromeVox unit tests.
13 * Note that while conceptually these are unit tests, these tests need
14 * to run in a full web page, so they're actually run as WebUI browser
18 * @extends {testing.Test}
20 function ChromeVoxUnitTestBase() {}
22 ChromeVoxUnitTestBase.prototype = {
23 __proto__: testing.Test.prototype,
27 'cvox.ChromeVoxTester',
28 'cvox.ChromeVoxUserCommands',
29 'cvox.SpokenListBuilder',
33 browsePreload: DUMMY_URL,
37 * It doesn't make sense to run the accessibility audit on these tests,
38 * since many of them are deliberately testing inaccessible html.
40 runAccessibilityChecks: false,
43 * Loads some inlined html into the body of the current document, replacing
44 * whatever was there previously.
45 * @param {string} html The html to load as a string.
47 loadHtml: function(html) {
48 while (document.head.firstChild) {
49 document.head.removeChild(document.head.firstChild);
51 while (document.body.firstChild) {
52 document.body.removeChild(document.body.firstChild);
54 this.appendHtml(html);
58 * Loads some inlined html into the current document, replacing
59 * whatever was there previously. This version takes the html
60 * encoded as a comment inside a function, so you can use it like this:
62 * this.loadDoc(function() {/*!
63 * <p>Html goes here</p>
66 * @param {Function} commentEncodedHtml The html to load, embedded as a
67 * comment inside an anonymous function - see example, above.
69 loadDoc: function(commentEncodedHtml) {
71 TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
76 * Appends some inlined html into the current document, at the end of
77 * the body element. Takes the html encoded as a comment inside a function,
78 * so you can use it like this:
80 * this.appendDoc(function() {/*!
81 * <p>Html goes here</p>
84 * @param {Function} commentEncodedHtml The html to load, embedded as a
85 * comment inside an anonymous function - see example, above.
87 appendDoc: function(commentEncodedHtml) {
89 TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
90 this.appendHtml(html);
94 * Appends some inlined html into the current document, at the end of
96 * @param {string} html The html to load as a string.
98 appendHtml: function(html) {
99 var div = document.createElement('div');
100 div.innerHTML = html;
101 var fragment = document.createDocumentFragment();
102 while (div.firstChild) {
103 fragment.appendChild(div.firstChild);
105 document.body.appendChild(fragment);
109 * Waits for the queued events in ChromeVoxEventWatcher to be
110 * handled, then calls a callback function with provided arguments
111 * in the test case scope. Very useful for asserting the results of events.
113 * @param {function()} func A function to call when ChromeVox is ready.
114 * @param {*} var_args Additional arguments to pass through to the function.
115 * @return {ChromeVoxUnitTestBase} this.
117 waitForCalm: function(func, var_args) {
119 var calmArguments = Array.prototype.slice.call(arguments);
120 calmArguments.shift();
121 cvox.ChromeVoxEventWatcher.addReadyCallback(function() {
122 func.apply(me, calmArguments);
124 return this; // for chaining.
128 * Asserts the TTS engine spoke a certain string. Clears the TTS buffer.
129 * @param {string} expectedText The expected text.
130 * @return {ChromeVoxUnitTestBase} this.
132 assertSpoken: function(expectedText) {
133 assertEquals(expectedText,
134 cvox.ChromeVoxTester.testTts().getUtterancesAsString());
135 cvox.ChromeVoxTester.clearUtterances();
136 return this; // for chaining.
140 * Asserts a list of utterances are in the correct queue mode.
141 * @param {cvox.SpokenListBuilder|Array} expectedList A list
142 * of [text, queueMode] tuples OR a SpokenListBuilder with the expected
144 * @return {ChromeVoxUnitTestBase} this.
146 assertSpokenList: function(expectedList) {
147 if (expectedList instanceof cvox.SpokenListBuilder) {
148 expectedList = expectedList.build();
151 var ulist = cvox.ChromeVoxTester.testTts().getUtteranceInfoList();
152 for (var i = 0; i < expectedList.length; i++) {
153 var text = expectedList[i][0];
154 var queueMode = expectedList[i][1];
155 this.assertSingleUtterance_(text, queueMode,
156 ulist[i].text, ulist[i].queueMode);
158 cvox.ChromeVoxTester.clearUtterances();
159 return this; // for chaining.
162 assertSingleUtterance_: function(
163 expectedText, expectedQueueMode, text, queueMode) {
164 assertEquals(expectedQueueMode, queueMode);
165 assertEquals(expectedText, text);
169 * Focuses an element.
170 * @param {string} id The id of the element to focus.
171 * @return {ChromeVoxUnitTestBase} this.
173 setFocus: function(id) {
175 return this; // for chaining.
179 * Executes a ChromeVox user command.
180 * @param {string} command The name of the command to run.
181 * @return {ChromeVoxUnitTestBase} this.
183 userCommand: function(command) {
184 cvox.ChromeVoxUserCommands.commands[command]();
185 return this; // for chaining.
189 * @return {cvox.SpokenListBuilder} A new builder.
191 spokenList: function() {
192 return new cvox.SpokenListBuilder();