-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathhelper.js
319 lines (287 loc) · 9.42 KB
/
helper.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// Simple Helper Functions For Testing CSS
//
(function(root) {
'use strict';
// serialize styles object and dump to dom
// appends <style id="dynamic-style"> to <head>
// setStyle("#some-selector", {"some-style" : "value"})
// setStyle({"#some-selector": {"some-style" : "value"}})
root.setStyle = function(selector, styles) {
var target = document.getElementById('dynamic-style');
if (!target) {
target = document.createElement('style');
target.id = 'dynamic-style';
target.type = "text/css";
document.getElementsByTagName('head')[0].appendChild(target);
}
var data = [];
// single selector/styles
if (typeof selector === 'string' && styles !== undefined) {
data = [selector, '{', serializeStyles(styles), '}'];
target.textContent = data.join("\n");
return;
}
// map of selector/styles
for (var key in selector) {
if (Object.prototype.hasOwnProperty.call(selector, key)) {
var _data = [key, '{', serializeStyles(selector[key]), '}'];
data.push(_data.join('\n'));
}
}
target.textContent = data.join("\n");
};
function serializeStyles(styles) {
var data = [];
for (var property in styles) {
if (Object.prototype.hasOwnProperty.call(styles, property)) {
var prefixedProperty = addVendorPrefix(property);
data.push(prefixedProperty + ":" + styles[property] + ";");
}
}
return data.join('\n');
}
// shorthand for computed style
root.computedStyle = function(element, property, pseudo) {
var prefixedProperty = addVendorPrefix(property);
return window
.getComputedStyle(element, pseudo || null)
.getPropertyValue(prefixedProperty);
};
// flush rendering buffer
root.reflow = function() {
document.body.offsetWidth;
};
// merge objects
root.extend = function(target /*, ..rest */) {
Array.prototype.slice.call(arguments, 1).forEach(function(obj) {
Object.keys(obj).forEach(function(key) {
target[key] = obj[key];
});
});
return target;
};
// dom fixture helper ("resetting dom test elements")
var _domFixture;
var _domFixtureSelector;
root.domFixture = function(selector) {
var fixture = document.querySelector(selector || _domFixtureSelector);
if (!fixture) {
throw new Error('fixture ' + (selector || _domFixtureSelector) + ' not found!');
}
if (!_domFixture && selector) {
// save a copy
_domFixture = fixture.cloneNode(true);
_domFixtureSelector = selector;
} else if (_domFixture) {
// restore the copy
var tmp = _domFixture.cloneNode(true);
fixture.parentNode.replaceChild(tmp, fixture);
} else {
throw new Error('domFixture must be initialized first!');
}
};
root.MS_PER_SEC = 1000;
/*
* The recommended minimum precision to use for time values.
*
* Based on Web Animations:
* https://w3c.github.io/web-animations/#precision-of-time-values
*/
const TIME_PRECISION = 0.0005; // ms
/*
* Allow implementations to substitute an alternative method for comparing
* times based on their precision requirements.
*/
root.assert_times_equal = function(actual, expected, description) {
assert_approx_equals(actual, expected, TIME_PRECISION, description);
};
/*
* Compare a time value based on its precision requirements with a fixed value.
*/
root.assert_time_equals_literal = (actual, expected, description) => {
assert_approx_equals(actual, expected, TIME_PRECISION, description);
};
/**
* Assert that CSSTransition event, |evt|, has the expected property values
* defined by |propertyName|, |elapsedTime|, and |pseudoElement|.
*/
root.assert_end_events_equal = function(evt, propertyName, elapsedTime,
pseudoElement = '') {
assert_equals(evt.propertyName, propertyName);
assert_times_equal(evt.elapsedTime, elapsedTime);
assert_equals(evt.pseudoElement, pseudoElement);
};
/**
* Assert that array of simultaneous CSSTransition events, |evts|, have the
* corresponding property names listed in |propertyNames|, and the expected
* |elapsedTimes| and |pseudoElement| members.
*
* |elapsedTimes| may be a single value if all events are expected to have the
* same elapsedTime, or an array parallel to |propertyNames|.
*/
root.assert_end_event_batch_equal = function(evts, propertyNames, elapsedTimes,
pseudoElement = '') {
assert_equals(
evts.length,
propertyNames.length,
'Test harness error: should have waited for the correct number of events'
);
assert_true(
typeof elapsedTimes === 'number' ||
(Array.isArray(elapsedTimes) &&
elapsedTimes.length === propertyNames.length),
'Test harness error: elapsedTimes must either be a number or an array of' +
' numbers with the same length as propertyNames'
);
if (typeof elapsedTimes === 'number') {
elapsedTimes = Array(propertyNames.length).fill(elapsedTimes);
}
const testPairs = propertyNames.map((propertyName, index) => ({
propertyName,
elapsedTime: elapsedTimes[index]
}));
const sortByPropertyName = (a, b) =>
a.propertyName.localeCompare(b.propertyName);
evts.sort(sortByPropertyName);
testPairs.sort(sortByPropertyName);
for (let evt of evts) {
const expected = testPairs.shift();
assert_end_events_equal(
evt,
expected.propertyName,
expected.elapsedTime,
pseudoElement
);
}
}
/**
* Appends a div to the document body.
*
* @param t The testharness.js Test object. If provided, this will be used
* to register a cleanup callback to remove the div when the test
* finishes.
*
* @param attrs A dictionary object with attribute names and values to set on
* the div.
*/
root.addDiv = function(t, attrs) {
var div = document.createElement('div');
if (attrs) {
for (var attrName in attrs) {
div.setAttribute(attrName, attrs[attrName]);
}
}
document.body.appendChild(div);
if (t && typeof t.add_cleanup === 'function') {
t.add_cleanup(function() {
if (div.parentNode) {
div.remove();
}
});
}
return div;
};
/**
* Appends a style div to the document head.
*
* @param t The testharness.js Test object. If provided, this will be used
* to register a cleanup callback to remove the style element
* when the test finishes.
*
* @param rules A dictionary object with selector names and rules to set on
* the style sheet.
*/
root.addStyle = (t, rules) => {
const extraStyle = document.createElement('style');
document.head.appendChild(extraStyle);
if (rules) {
const sheet = extraStyle.sheet;
for (const selector in rules) {
sheet.insertRule(selector + '{' + rules[selector] + '}',
sheet.cssRules.length);
}
}
if (t && typeof t.add_cleanup === 'function') {
t.add_cleanup(() => {
extraStyle.remove();
});
}
};
/**
* Promise wrapper for requestAnimationFrame.
*/
root.waitForFrame = () => {
return new Promise(resolve => {
window.requestAnimationFrame(resolve);
});
};
/**
* Returns a Promise that is resolved after the given number of consecutive
* animation frames have occured (using requestAnimationFrame callbacks).
*
* @param frameCount The number of animation frames.
* @param onFrame An optional function to be processed in each animation frame.
*/
root.waitForAnimationFrames = (frameCount, onFrame) => {
const timeAtStart = document.timeline.currentTime;
return new Promise(resolve => {
function handleFrame() {
if (onFrame && typeof onFrame === 'function') {
onFrame();
}
if (timeAtStart != document.timeline.currentTime &&
--frameCount <= 0) {
resolve();
} else {
window.requestAnimationFrame(handleFrame); // wait another frame
}
}
window.requestAnimationFrame(handleFrame);
});
};
/**
* Wrapper that takes a sequence of N animations and returns:
*
* Promise.all([animations[0].ready, animations[1].ready, ... animations[N-1].ready]);
*/
root.waitForAllAnimations = animations =>
Promise.all(animations.map(animation => animation.ready));
/**
* Utility that takes a Promise and a maximum number of frames to wait and
* returns a new Promise that behaves as follows:
*
* - If the provided Promise resolves _before_ the specified number of frames
* have passed, resolves with the result of the provided Promise.
* - If the provided Promise rejects _before_ the specified number of frames
* have passed, rejects with the error result of the provided Promise.
* - Otherwise, rejects with a 'Timed out' error message. If |message| is
* provided, it will be appended to the error message.
*/
root.frameTimeout = (promiseToWaitOn, framesToWait, message) => {
let framesRemaining = framesToWait;
let aborted = false;
const timeoutPromise = new Promise(function waitAFrame(resolve, reject) {
if (aborted) {
resolve();
return;
}
if (framesRemaining-- > 0) {
requestAnimationFrame(() => {
waitAFrame(resolve, reject);
});
return;
}
let errorMessage = 'Timed out waiting for Promise to resolve';
if (message) {
errorMessage += `: ${message}`;
}
reject(new Error(errorMessage));
});
const wrappedPromiseToWaitOn = promiseToWaitOn.then(result => {
aborted = true;
return result;
});
return Promise.race([timeoutPromise, wrappedPromiseToWaitOn]);
};
})(window);