1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
// Copyright 2014 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.
GEN_INCLUDE([
'chrome/browser/resources/chromeos/chromevox/testing/assert_additions.js']);
GEN_INCLUDE([
'chrome/browser/resources/chromeos/chromevox/testing/common.js']);
/**
* Base test fixture for ChromeVox unit tests.
*
* Note that while conceptually these are unit tests, these tests need
* to run in a full web page, so they're actually run as WebUI browser
* tests.
*
* @constructor
* @extends {testing.Test}
*/
function ChromeVoxUnitTestBase() {}
ChromeVoxUnitTestBase.prototype = {
__proto__: testing.Test.prototype,
/** @override */
closureModuleDeps: [
'cvox.ChromeVoxTester',
'cvox.ChromeVoxUserCommands',
'cvox.SpokenListBuilder',
],
/** @override */
browsePreload: DUMMY_URL,
/**
* @override
* It doesn't make sense to run the accessibility audit on these tests,
* since many of them are deliberately testing inaccessible html.
*/
runAccessibilityChecks: false,
/**
* Loads some inlined html into the body of the current document, replacing
* whatever was there previously.
* @param {string} html The html to load as a string.
*/
loadHtml: function(html) {
while (document.head.firstChild) {
document.head.removeChild(document.head.firstChild);
}
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
this.appendHtml(html);
},
/**
* Loads some inlined html into the current document, replacing
* whatever was there previously. This version takes the html
* encoded as a comment inside a function, so you can use it like this:
*
* this.loadDoc(function() {/*!
* <p>Html goes here</p>
* * /});
*
* @param {Function} commentEncodedHtml The html to load, embedded as a
* comment inside an anonymous function - see example, above.
*/
loadDoc: function(commentEncodedHtml) {
var html =
TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
this.loadHtml(html);
},
/**
* Appends some inlined html into the current document, at the end of
* the body element. Takes the html encoded as a comment inside a function,
* so you can use it like this:
*
* this.appendDoc(function() {/*!
* <p>Html goes here</p>
* * /});
*
* @param {Function} commentEncodedHtml The html to load, embedded as a
* comment inside an anonymous function - see example, above.
*/
appendDoc: function(commentEncodedHtml) {
var html =
TestUtils.extractHtmlFromCommentEncodedString(commentEncodedHtml);
this.appendHtml(html);
},
/**
* Appends some inlined html into the current document, at the end of
* the body element.
* @param {string} html The html to load as a string.
*/
appendHtml: function(html) {
var div = document.createElement('div');
div.innerHTML = html;
var fragment = document.createDocumentFragment();
while (div.firstChild) {
fragment.appendChild(div.firstChild);
}
document.body.appendChild(fragment);
},
/**
* Waits for the queued events in ChromeVoxEventWatcher to be
* handled, then calls a callback function with provided arguments
* in the test case scope. Very useful for asserting the results of events.
*
* @param {function()} func A function to call when ChromeVox is ready.
* @param {*} var_args Additional arguments to pass through to the function.
* @return {ChromeVoxUnitTestBase} this.
*/
waitForCalm: function(func, var_args) {
var me = this;
var calmArguments = Array.prototype.slice.call(arguments);
calmArguments.shift();
cvox.ChromeVoxEventWatcher.addReadyCallback(function() {
func.apply(me, calmArguments);
});
return this; // for chaining.
},
/**
* Asserts the TTS engine spoke a certain string. Clears the TTS buffer.
* @param {string} expectedText The expected text.
* @return {ChromeVoxUnitTestBase} this.
*/
assertSpoken: function(expectedText) {
assertEquals(expectedText,
cvox.ChromeVoxTester.testTts().getUtterancesAsString());
cvox.ChromeVoxTester.clearUtterances();
return this; // for chaining.
},
/**
* Asserts a list of utterances are in the correct queue mode.
* @param {cvox.SpokenListBuilder|Array} expectedList A list
* of [text, queueMode] tuples OR a SpokenListBuilder with the expected
* utterances.
* @return {ChromeVoxUnitTestBase} this.
*/
assertSpokenList: function(expectedList) {
if (expectedList instanceof cvox.SpokenListBuilder) {
expectedList = expectedList.build();
}
var ulist = cvox.ChromeVoxTester.testTts().getUtteranceInfoList();
for (var i = 0; i < expectedList.length; i++) {
var text = expectedList[i][0];
var queueMode = expectedList[i][1];
this.assertSingleUtterance_(text, queueMode,
ulist[i].text, ulist[i].queueMode);
}
cvox.ChromeVoxTester.clearUtterances();
return this; // for chaining.
},
assertSingleUtterance_: function(
expectedText, expectedQueueMode, text, queueMode) {
assertEquals(expectedQueueMode, queueMode);
assertEquals(expectedText, text);
},
/**
* Focuses an element.
* @param {string} id The id of the element to focus.
* @return {ChromeVoxUnitTestBase} this.
*/
setFocus: function(id) {
$(id).focus();
return this; // for chaining.
},
/**
* Executes a ChromeVox user command.
* @param {string} command The name of the command to run.
* @return {ChromeVoxUnitTestBase} this.
*/
userCommand: function(command) {
cvox.ChromeVoxUserCommands.commands[command]();
return this; // for chaining.
},
/**
* @return {cvox.SpokenListBuilder} A new builder.
*/
spokenList: function() {
return new cvox.SpokenListBuilder();
}
};
|