Skip to content

Commit 9cc41f9

Browse files
committed
TestCase: implement QtTest.TestCase (WIP)
1 parent a573dbe commit 9cc41f9

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

src/modules/QtTest/TestCase.js

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
QmlWeb.registerQmlType({
2+
module: "QtTest",
3+
name: "TestCase",
4+
versions: /^1\./,
5+
baseClass: "QtQuick.Item",
6+
properties: {
7+
completed: "bool",
8+
name: "string",
9+
optional: "bool",
10+
running: "bool",
11+
when: "bool",
12+
windowShown: "bool"
13+
}
14+
}, class {
15+
constructor(meta) {
16+
QmlWeb.callSuper(this, meta);
17+
this.Component.completed.connect(this, this.Component$onCompleted);
18+
19+
const engine = QmlWeb.engine;
20+
if (!engine.tests) {
21+
QmlWeb.engine.tests = {
22+
name: engine.name || `Run_${Math.random().toString(36).slice(2, 10)}`,
23+
started: false,
24+
finished: false,
25+
duration: 0,
26+
total: 0,
27+
completed: 0,
28+
stats: {
29+
pass: 0,
30+
fail: 0,
31+
skip: 0
32+
}
33+
};
34+
}
35+
QmlWeb.engine.tests.total++;
36+
37+
this.console = {
38+
assert: (...a) => console.assert(...a),
39+
error: (...a) => console.error(`QSYSTEM: ${this.$testId} qml:`, ...a),
40+
info: (...a) => console.info(`QINFO : ${this.$testId} qml:`, ...a),
41+
log: (...a) => console.log(`QDEBUG : ${this.$testId} qml:`, ...a),
42+
time: (...a) => console.time(...a),
43+
timeEnd: (...a) => console.timeEnd(...a),
44+
trace: (...a) => console.trace(...a),
45+
warn: (...a) => console.warn(`QWARN : ${this.$testId} qml:`, ...a)
46+
};
47+
}
48+
49+
Component$onCompleted() {
50+
const info = QmlWeb.engine.tests;
51+
if (!info.started) {
52+
console.log(`********* Start testing of ${info.name} *********`);
53+
console.log(`Config: Using QmlWeb, ${window.navigator.userAgent}`);
54+
info.started = true;
55+
}
56+
57+
const keys = Object.keys(this);
58+
const tests = keys
59+
.filter(key => key.lastIndexOf("test_", 0) === 0)
60+
.filter(key => key.indexOf("_data", key.length - 5) === -1)
61+
.sort();
62+
63+
tests.unshift("initTestCase");
64+
tests.push("cleanupTestCase");
65+
tests.forEach(test => {
66+
this.$testId = `${info.name}::${this.name}::${test}()`;
67+
const start = performance.now();
68+
let error;
69+
try {
70+
if (test !== "initTestCase" && test !== "cleanupTestCase") {
71+
this.init();
72+
}
73+
this[test]();
74+
} catch (e) {
75+
error = e;
76+
} finally {
77+
if (test !== "initTestCase" && test !== "cleanupTestCase") {
78+
this.cleanup();
79+
}
80+
}
81+
const end = performance.now();
82+
info.duration += end - start;
83+
if (error && error.skip) {
84+
info.stats.skip++;
85+
console.log(`SKIP : ${this.$testId} ${error.message}`);
86+
} else if (error) {
87+
info.stats.fail++;
88+
console.log(`FAIL! : ${this.$testId} ${error.message}`);
89+
if ("actual" in error) {
90+
console.log(` Actual (): ${error.actual}`);
91+
}
92+
if ("expected" in error) {
93+
console.log(` Expected (): ${error.expected}`);
94+
}
95+
} else {
96+
info.stats.pass++;
97+
console.log(`PASS : ${this.$testId}`);
98+
}
99+
this.$testId = `${info.name}::UnknownTestFunc()`;
100+
});
101+
102+
// TODO: benchmarks
103+
104+
info.completed++;
105+
if (info.completed === info.total) {
106+
info.finished = true;
107+
const { pass, fail, skip } = info.stats;
108+
const duration = Math.round(info.duration * 100) / 100;
109+
console.log(
110+
`Totals: ${pass} passed, ${fail} failed, ${skip} skipped, ${duration}ms`
111+
);
112+
console.log(`********* Finished testing of ${info.name} *********`);
113+
}
114+
}
115+
116+
// No-ops
117+
init() {}
118+
initTestCase() {}
119+
cleanup() {}
120+
cleanupTestCase() {}
121+
122+
// API
123+
compare(actual, expected, message = "") {
124+
if (actual !== expected) {
125+
const err = new Error(message);
126+
err.actual = actual;
127+
err.expected = expected;
128+
throw err;
129+
}
130+
}
131+
expectFail(tag, message) {
132+
// TODO
133+
}
134+
expectFailContinue(tag, message) {
135+
// TODO
136+
}
137+
fail(message = "") {
138+
throw new Error(message);
139+
}
140+
findChild(parent, objectName) {
141+
// TODO
142+
// return QtObject
143+
}
144+
fuzzyCompare(actual, expected, delta, message) {
145+
// TODO
146+
}
147+
grabImage(item) {
148+
if (!window.top || !window.top.callPhantom) {
149+
this.skip("Can't use TestCase::grabImage() without PhantomJS.");
150+
}
151+
// TODO
152+
return {
153+
red: (x, y) => {},
154+
green: (x, y) => {},
155+
blue: (x, y) => {},
156+
alpha: (x, y) => {},
157+
pixel: (x, y) => {},
158+
equals: image => false
159+
};
160+
}
161+
ignoreWarning(message) {
162+
// TODO
163+
}
164+
skip(message = "") {
165+
const err = new Error(message);
166+
err.skip = true;
167+
throw err;
168+
}
169+
sleep(ms) {
170+
// TODO
171+
}
172+
tryCompare(obj, property, expected, timeout, message) {
173+
// TODO
174+
}
175+
verify(condition, message) {
176+
if (!condition) {
177+
throw new Error(`'${message}' returned FALSE. ()`);
178+
}
179+
}
180+
wait(ms) {
181+
// TODO
182+
}
183+
waitForRendering(item, timeout = 5000) {
184+
// TODO
185+
}
186+
warn(message) {
187+
console.warn(`WARNING: ${this.$testId} ${message}`);
188+
}
189+
190+
// Events
191+
keyClick(key, modifiers, delay = -1) {
192+
// TODO
193+
}
194+
keyPress(key, modifiers, delay = -1) {
195+
// TODO
196+
}
197+
keyRelease(key, modifiers, delay = -1) {
198+
// TODO
199+
}
200+
mouseClick(item, x, y, button, modifiers, delay = -1) {
201+
// TODO
202+
}
203+
mouseDoubleClick(item, x, y, button, modifiers, delay = -1) {
204+
// TODO
205+
}
206+
mouseDoubleClickSequence(item, x, y, button, modifiers, delay = -1) {
207+
// TODO
208+
}
209+
mouseDrag(item, x, y, dx, dy, button, modifiers, delay = -1) {
210+
// TODO
211+
}
212+
mouseMove(item, x, y, delay = -1) {
213+
// TODO
214+
}
215+
mousePress(item, x, y, button, modifiers, delay = -1) {
216+
// TODO
217+
}
218+
mouseRelease(item, x, y, button, modifiers, delay = -1) {
219+
// TODO
220+
}
221+
mouseWheel(item, x, y, xDelta, yDelta, button, modifiers, delay = -1) {
222+
// button = Qt.LeftButton, modifiers = Qt.NoModifier
223+
// TODO
224+
}
225+
});

0 commit comments

Comments
 (0)