/
JawsTestCase.js
178 lines (166 loc) · 7.23 KB
/
JawsTestCase.js
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
/*
* Copyright 2015 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Aria = require("../Aria");
var ariaUtilsString = require("../utils/String");
/**
* Class to be extended to create a template test case which checks the behavior with
* the Jaws screen reader. It contains method to help checking what Jaws said.
*/
module.exports = Aria.classDefinition({
$classpath : "aria.jsunit.JawsTestCase",
$extends : require("./RobotTestCase"),
$prototype : {
/**
* Private helper function, starts the tests once the template has been successfully loaded and rendered
*/
_startTests : function () {
// automatically clear Jaws history before starting the test
this.clearJawsHistory(this.$RobotTestCase._startTests);
},
/**
* Creates an empty text area that completely fills the test document, with a high zIndex,
* and returns its reference.
*/
_createFullScreenTextArea : function () {
var textArea = this.testDocument.createElement("textarea");
var textAreaStyle = textArea.style;
textAreaStyle.position = "absolute";
textAreaStyle.display = "block";
textAreaStyle.top = "0px";
textAreaStyle.left = "0px";
textAreaStyle.width = "100%";
textAreaStyle.height = "100%";
textAreaStyle.zIndex = "999999";
this.testDocument.body.appendChild(textArea);
return textArea;
},
/**
* Sends the keyboard events necessary to clear Jaws history.
* In case Jaws is not running, the test is ended.
* @param {aria.core.CfgBeans:Callback} cb
*/
clearJawsHistory : function (cb) {
var textArea = this._createFullScreenTextArea();
this.synEvent.execute([
["click", textArea],
["pause", 1000],
["type", null, "[<insert>][space][>insert<][<shift>]h[>shift<]"],
["pause", 2000]
],{
fn: function () {
var textAreaContent = textArea.value;
textArea.parentNode.removeChild(textArea);
// if there is something in the text area, there is something
// wrong in the setup (e.g.: JAWS not enabled or wrong robot
// implementation)
if (textAreaContent === "") {
this.$callback(cb);
} else {
// ends the test
this.raiseFailure("JAWS is not running or wrong robot implementation");
this.end();
}
},
scope: this
});
},
/**
* Sends the keyboard/mouse events necessary to retrieve Jaws history.
* The raw history is processed to make it easier to compare between
* several executions.
* @param {aria.core.CfgBeans:Callback} cb
*/
retrieveJawsHistory : function (cb) {
var textArea = this._createFullScreenTextArea();
this.synEvent.execute([
["type", null, "[<insert>][space][>insert<]h"], // displays history window
["pause", 2000],
["type", null, "[<ctrl>]a[>ctrl<][<ctrl>]c[>ctrl<]"], // copies the whole history in the clipboard
["pause", 1000],
["type", null, "[<alt>][F4][>alt<]"], // closes history window
["click", textArea],
["type", null, "[<ctrl>]v[>ctrl<]"], // pastes history in our text area
["pause", 2000]
], {
fn: function () {
textArea.parentNode.removeChild(textArea);
var textAreaContent = textArea.value;
var globalFilterRegExp = /^(Aria Templates tests frame|Aria Templates tests document|New tab page)$/gi;
var lines = ariaUtilsString.trim(textAreaContent).split("\n");
for (var i = lines.length - 1; i >= 0; i--) {
var curLine = lines[i];
curLine = curLine.replace(/\s+/g, " ");
curLine = ariaUtilsString.trim(curLine);
if (curLine && !globalFilterRegExp.test(curLine)) {
lines[i] = curLine;
} else {
lines.splice(i, 1);
}
}
textAreaContent = lines.join("\n");
this.$callback(cb, textAreaContent);
},
scope: this
});
},
/**
* Retrieves Jaws history (with this.retrieveJawsHistory) and
* asserts that it is equal to the expected string.
* @param {aria.core.CfgBeans:Callback} cb
* @param {function} filterFn optionnal. The filter function to apply to the response.
*/
assertJawsHistoryEquals : function (expectedOutput, callback, filterFn) {
this.retrieveJawsHistory({
fn: function (response) {
var originalResponse = response;
if (filterFn) {
response = filterFn.call(this, response);
}
var changed = response !== originalResponse;
var message = [];
if (changed) {
message.push('History was filtered');
} else {
message.push('History was not filtered');
}
message.push("JAWS history" + (changed ? ' (filtered)' : '') + ":", "", response, "");
message.push("Expected history:", "", expectedOutput, "");
if (changed) {
message.push("JAWS history (original): ", "", originalResponse, "");
}
message = message.join('\n');
this.assertEquals(response, expectedOutput, message);
this.$callback(callback);
},
scope: this
});
},
/**
* Remove duplicate sentences
* @param {String} response JAWS response to be processed
*/
removeDuplicates : function (response) {
var lines = response.split("\n");
var newLines = [lines[0]];
for(var i = 1, ii = lines.length; i < ii; i++) {
var line = lines[i];
if (line != lines[i - 1]) {
newLines.push(line);
}
}
return newLines.join("\n");
}
}
});