This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from joewalker/gcli
/
test.js
125 lines (116 loc) · 3.44 KB
/
test.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
/*
* Copyright 2012, Mozilla Foundation and contributors
*
* 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 fs = require('fs');
var jsdom = require('jsdom').jsdom;
var main = require('../../../gcli');
var gcli = main.require('gcli/index');
var Display = main.require('gcli/ui/display').Display;
main.require('gclitest/suite');
var examiner = main.require('test/examiner');
var stati = main.require('test/status').stati;
/**
* Registration and de-registration.
*/
exports.startup = function() {
gcli.addCommand(testCmdSpec);
};
exports.shutdown = function() {
gcli.removeCommand(testCmdSpec);
};
/**
* 'test' command.
*/
var testCmdSpec = {
name: 'test',
description: 'Run GCLI unit tests',
returnType: 'terminal',
exec: function(args, context) {
var deferred = context.defer();
jsdom.env({
html: fs.readFileSync(main.gcliHome + '/index.html').toString(),
src: [ ],
features: {
QuerySelector: true
},
done: function(errors, window) {
patchDom(window);
window.display = new Display({
document: window.document
});
var options = {
window: window,
display: window.display,
isNode: true,
isJsdom: true,
isUnamdized: main.useUnamd
};
examiner.run(options).then(function() {
var reply = examiner.detailedResultLog();
if (examiner.toRemote().summary.status === stati.pass) {
deferred.resolve(reply);
}
else {
deferred.reject(reply);
}
});
}
});
return deferred.promise;
}
};
function patchDom(window) {
if ("classList" in window.document.documentElement) {
console.log('test.js may not need patchDom any more');
}
else {
Object.defineProperty(window.HTMLElement.prototype, 'classList', {
get: function() {
var self = this;
function update(fn) {
return function(value) {
var classes = self.className.split(/\s+/);
var index = classes.indexOf(value);
fn(classes, index, value);
self.className = classes.join(" ");
};
}
var ret = {
add: update(function(classes, index, value) {
~index || classes.push(value);
}),
remove: update(function(classes, index) {
~index && classes.splice(index, 1);
}),
toggle: update(function(classes, index, value) {
~index ? classes.splice(index, 1) : classes.push(value);
}),
contains: function(value) {
return !!~self.className.split(/\s+/).indexOf(value);
},
item: function(i) {
return self.className.split(/\s+/)[i] || null;
}
};
Object.defineProperty(ret, 'length', {
get: function() {
return self.className.split(/\s+/).length;
}
});
return ret;
}
});
}
}