forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTKUnit.ts
More file actions
316 lines (270 loc) · 9.37 KB
/
TKUnit.ts
File metadata and controls
316 lines (270 loc) · 9.37 KB
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
/* tslint:disable */
/* Notes:
1. all test function names should begin with 'test'
2. (if exists) at the beginning of module test setUpModule() module function is called
3. (if exists) at the beginning of each test setUp() module function is called
4. tests should use TKUnit.assert(condition, message) to mark error. If no assert fails test is successful
5. (if exists) at the end of each test tearDown() module function is called
6. (if exists) at the end of module test tearDownModule() module function is called
*/
import Application = require("application");
import timer = require("timer");
import trace = require("trace");
import types = require("utils/types");
import platform = require("platform");
var sdkVersion = parseInt(platform.device.sdkVersion);
trace.enable();
export var write = function write(message: string, type?: number) {
//console.log(message);
trace.write(message, trace.categories.Test, type);
}
var runTest = function (testInfo) {
try {
testInfo.testFunc();
if (testInfo.isTest) {
write("--- [" + testInfo.testName + "] OK", trace.messageType.info);
testInfo.isPassed = true;
}
}
catch (e) {
if (testInfo.isTest) {
write("--- [" + testInfo.testName + "] FAILED: " + e.message, trace.messageType.error);
testInfo.isPassed = false;
testInfo.errorMessage = e.message;
}
}
};
export interface TestFailure {
moduleName: string;
testName: string;
errorMessage: string;
}
export interface TestModuleRunResult {
name: string;
count: number;
succeeded: number;
failed: Array<TestFailure>;
}
var testsQueue;
var defaultTimeout = 5000;
// testInfo : {testFunc: func, testName: string, isTest: boolean, isPassed: boolean, errorMessage: string}
function runAsync(testInfo, recursiveIndex, testTimeout?) {
var error;
var isDone = false;
var handle;
var testStartTime = new Date().getTime();
//write("--- [" + testInfo.testName + "] Started at: " + testStartTime, trace.messageType.info);
var doneCallback = function (e: Error) {
if (e) {
error = e;
}
else {
isDone = true;
}
}
var testTimeout = testInfo.testTimeout;
if (!testTimeout) {
testTimeout = defaultTimeout;
}
var checkFinished = function () {
if (isDone) {
write("--- [" + testInfo.testName + "] OK", trace.messageType.info);
//write("--- [" + testInfo.testName + "] took: " + (new Date().getTime() - testStartTime), trace.messageType.info);
testInfo.isPassed = true;
runTests(testsQueue, recursiveIndex + 1);
}
else if (error) {
write("--- [" + testInfo.testName + "] FAILED: " + error.message, trace.messageType.error);
//write("--- [" + testInfo.testName + "] took: " + (new Date().getTime() - testStartTime), trace.messageType.info);
testInfo.errorMessage = error.message;
runTests(testsQueue, recursiveIndex + 1);
}
else {
var testEndTime = new Date().getTime();
if (testEndTime - testStartTime > testTimeout) {
write("--- [" + testInfo.testName + "] TIMEOUT", trace.messageType.error);
//write("--- [" + testInfo.testName + "] took: " + (testEndTime - testStartTime), trace.messageType.info);
testInfo.errorMessage = "Test timeout.";
runTests(testsQueue, recursiveIndex + 1);
}
else {
setTimeout(checkFinished, 200);
}
}
}
testInfo.testFunc(doneCallback);
setTimeout(checkFinished, 0);
}
// tests : Array<{testFunc: func, testName: string, isTest: boolean, isPassed: boolean, errorMessage: string}>
export var runTests = function (tests, recursiveIndex) {
testsQueue = tests;
var i;
for (i = recursiveIndex; i < testsQueue.length; i++) {
if (testsQueue[i].testFunc.length > 0) {
return runAsync(testsQueue[i], i);
}
else {
runTest(testsQueue[i]);
}
}
}
export function assert(test: any, message?: string) {
if (!test) {
throw new Error(message);
}
};
export function assertNotEqual(actual: any, expected: any, message?: string) {
var equals = false;
if (types.isUndefined(actual) && types.isUndefined(expected)) {
equals = true;
}
else if (!types.isUndefined(actual) && !types.isUndefined(expected)) {
if (types.isFunction(actual.equals)) {
// Use the equals method
if (actual.equals(expected)) {
equals = true;
}
}
else {
equals = actual === expected;
}
}
if (equals) {
throw new Error(message + " Actual: " + actual + " Expected: " + expected);
}
}
export function assertEqual(actual: any, expected: any, message?: string) {
if (!types.isUndefined(actual)
&& !types.isUndefined(expected)
&& types.getClass(actual) === types.getClass(expected)
&& types.isFunction(actual.equals)) {
// Use the equals method
if (!actual.equals(expected)) {
throw new Error(message + " Actual: " + actual + " Expected: " + expected);
}
}
else if (actual !== expected) {
throw new Error(message + " Actual: " + actual + " Expected: " + expected);
}
};
export function assertAreClose(actual: number, expected: number, delta: number, message?: string) {
if (isNaN(actual) || Math.abs(actual - expected) > delta) {
throw new Error(message + " Numbers are not close enough. Actual: " + actual + " Expected: " + expected + " Delta: " + delta);
}
};
export function arrayAssert(actual: Array<any>, expected: Array<any>, message?: string) {
if (actual.length !== expected.length) {
throw new Error(message + " Actual array length: " + actual.length + " Expected array length: " + expected.length);
}
var i;
for (i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) {
throw new Error(message + " Actual element at " + i + " is: " + actual[i] + " Expected element is: " + expected[i]);
}
}
}
export function assertThrows(testFunc: () => void, assertMessage?: string, expectedMessage?: string) {
var actualError: Error;
try {
testFunc();
} catch (e) {
actualError = e;
}
if (!actualError) {
throw new Error("Missing expected exception. " + assertMessage);
}
if (expectedMessage && actualError.message !== expectedMessage) {
throw new Error("Got unwanted exception. Actual error: " + actualError.message + " Expected error: " + expectedMessage);
}
}
export var wait = function (seconds: number) {
waitUntilReady(function () {
return false;
}, seconds);
};
export var waitUntilReady = function (isReady: () => boolean, timeoutSec?: number) {
if (!isReady) {
return;
}
if (!timeoutSec) {
// TODO: How much should be the default timeout in seconds?
timeoutSec = 20;
}
if (Application.ios) {
var waitTime = 20 / 1000;
var totalWaitTime = 0;
while (true) {
NSRunLoop.currentRunLoop().runUntilDate(NSDate.dateWithTimeIntervalSinceNow(waitTime));
if (isReady()) {
break;
}
totalWaitTime += waitTime;
if (timeoutSec && totalWaitTime >= timeoutSec) {
break;
}
}
} else if (Application.android) {
doModalAndroid(isReady, timeoutSec);
}
};
// Setup for the Android modal loop implementation
// TODO: If these platform-specific implementations continue to grow, think of per-platform separation (TKUnit.android)
var nextMethod;
var targetField;
var prepared;
var prepareModal = function () {
if (prepared) {
return;
}
var clsMsgQueue = java.lang.Class.forName("android.os.MessageQueue");
var clsMsg = java.lang.Class.forName("android.os.Message");
nextMethod;
var methods = clsMsgQueue.getDeclaredMethods();
var i;
for (i = 0; i < methods.length; i++) {
if (methods[i].getName() === "next") {
nextMethod = methods[i];
nextMethod.setAccessible(true);
break;
}
}
targetField;
var fields = clsMsg.getDeclaredFields();
for (i = 0; i < fields.length; i++) {
if (fields[i].getName() === "target") {
targetField = fields[i];
targetField.setAccessible(true);
break;
}
}
prepared = true;
}
var doModalAndroid = function (quitLoop: () => boolean, timeoutSec: number) {
if (!quitLoop) {
return;
}
prepareModal();
var queue = android.os.Looper.myQueue();
var quit = false;
timer.setTimeout(function () {
quit = true;
}, timeoutSec * 1000);
var msg;
while (!quit) {
msg = nextMethod.invoke(queue, null);
if (msg) {
var target = targetField.get(msg);
if (!target) {
quit = true;
} else {
target.dispatchMessage(msg);
}
if (sdkVersion < 21) {//https://code.google.com/p/android-test-kit/issues/detail?id=84
msg.recycle();
}
}
if (!quit && quitLoop()) {
quit = true;
}
}
};