This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mockti.js
314 lines (259 loc) · 8.76 KB
/
mockti.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
function mockti (apiPath) {
if (!apiPath) apiPath = __dirname + '/api.jsca';
var fs = require('fs');
var data = JSON.parse(fs.readFileSync(__dirname + '/api.jsca'));
var Emitter = require('eventemitter2').EventEmitter2;
var util = require('util');
var _ = require('underscore');
var md5 = require('MD5');
var request = require('request');
var querystring = require('querystring');
var assert = require('assert');
// Modify Emitter's prototype to conform with what Ti does
Emitter.prototype.addEventListener = Emitter.prototype.on;
Emitter.prototype.removeEventListener = Emitter.prototype.removeListener;
Emitter.prototype.fireEvent = Emitter.prototype.emit;
var Ti;
Ti = {};
function retrieve (root, list) {
var name = list.shift();
if (!root[name]) {
if (root['_' + name]) {
root[name] = root['_' + name];
} else {
root[name] = {};
_.extend(root[name], Emitter.prototype);
}
}
var current = root[name];
if (list.length > 0) return retrieve(current, list);
else return current;
}
var assertTypeMsg = '<%=type%>#<%=name%> expected argument <%=index%> to be of' +
' type <%=expected%>, got type <%=found%>';
function assertType (type, expect, objType, index, name) {
assert(type === expect, _.template(assertTypeMsg, {
type: objType, index: index, expected: expect, found: type, name: name
}));
}
function assertTypeOr (type, expect, expectOr, objType, index, name) {
assert(type === expect || type === expectOr, _.template(assertTypeMsg, {
type: objType, index: index, expected: expect + " or " + expectOr,
found: type, name: name
}));
}
function assertNotUndefined (arg, objType, index, name) {
assert(arg !== 'undefined', objType + "#" + name + " requires argument " +
index + " to be specified");
}
function assertTypes (args, expectations, obj, name) {
var builtins = ["object", "function", "number", "string"];
var possibleObject = ["Titanium.UI.Animation"];
// If the argument is required and defined, we check.
// If the argument is not required and defined, we check.
// If the argument is not required and not defined, we don't check.
// If the argument is required and not defined, we check.
expectations.forEach(function (item, index) {
if (item.type && (args[index] !== undefined || item.usage == 'required')) {
if (builtins.indexOf(item.type.toLowerCase()) != -1)
assertType(typeof args[index], item.type.toLowerCase(), obj._type, index, name);
else if (possibleObject.indexOf(item.type) != -1)
assertTypeOr(
(args[index] && args[index]._type) || typeof args[index],
'object', item.type, obj._type, index, name
);
// otherwise just make sure it's defined
else assertNotUndefined(args[index], obj._type, index, name);
}
});
}
function method (namespace, obj, fn) {
// dont want to overwrite these
if (['addEventListener', 'removeEventListener', 'fireEvent'].indexOf(fn.name) != -1) return;
// factory
if (fn.name.indexOf('create') != -1) {
obj[fn.name] = function (props) {
var base = retrieve(Ti, namespace.concat([fn.name.slice(6)]));
var ret = {};
_.extend(ret, base, props);
return ret;
};
return;
}
if (fn.name == 'add') {
obj.add = function (view) {
this.children = this.children || [];
this.children.push(view);
if (this.fireEvent) this.fireEvent('function::add', arguments);
if (fn.parameters && fn.parameters.length > 0 && Ti.assertTypes)
assertTypes(arguments, fn.parameters, obj, fn.name);
};
return;
}
if (fn.name == 'remove') {
obj.remove = function (view) {
this.children = this.children || [];
this.children = this.children.filter(function (item) {
return item !== view;
});
if (this.fireEvent) this.fireEvent('function::remove', arguments);
if (fn.parameters && fn.parameters.length > 0 && Ti.assertTypes)
assertTypes(arguments, fn.parameters, obj, fn.name);
};
return;
}
// otherwise it's some method so just make it emit that this method was
// called
obj[fn.name] = function () {
if (this.fireEvent) this.fireEvent('function::' + fn.name, arguments);
if (fn.parameters && fn.parameters.length > 0 && Ti.assertTypes)
assertTypes(arguments, fn.parameters, obj, fn.name);
};
}
data.types.forEach(function (item) {
// Don't worry about stuff that isn't actually in the Titanium namespace for
// now
if (item.name.indexOf('Titanium.') == -1) return;
var name = item.name.slice(9);
var namespace = name.split('.').slice(-1);
var obj = retrieve(Ti, name.split('.'));
obj._type = item.name;
item.functions.forEach(function (fn) {
method(namespace, obj, fn);
});
// TODO: do something with this data
item.properties.forEach(function (prop) {
});
});
// Some manual stuff
Ti.Platform = {
osname: 'android',
version: '6',
model: 'mock',
id: 'mock-id',
is24HourTimeFormat: function () { return false; },
Android: {API_LEVEL: 18},
displayCaps: {
platformWidth: 480,
platformHeight: 800,
dpi: 160
}
};
// Assert types by default.
Ti.assertTypes = true;
// This is my sortof silly request mocking system. It just adds requests
// into these objects, they're like Titanium XHR requests, they can just be
// pulled out and modified, or you can call their `.onerror()` or `.onload()`
// method.
// Requests by URL
Ti.Network._requestURLs = {};
// Requests in an array
Ti.Network._requests = [];
// Whether or not to use the silly fake system
Ti.Network.fakeRequests = false;
var old = Ti.Network.createHTTPClient;
Ti.Network.createHTTPClient = function (spec) {
var xhr = old(spec);
xhr.headers = {};
xhr.open = function (method, url) {
xhr.method = method;
xhr.url = url;
Ti.Network._requestURLs[url] = xhr;
Ti.Network._requests.push(xhr);
xhr.fireEvent('function::open', arguments);
};
xhr.send = function (data) {
if (!data) data = {};
xhr.data = data;
xhr.fireEvent('function::send', arguments);
if (!Ti.Network.fakeRequests) {
request({
headers: xhr.headers,
url: xhr.url,
method: xhr.method,
body: querystring.stringify(xhr.data)
}, function(error, response, body){
var event = {source: xhr};
xhr.responseText = body;
xhr.status = response.statusCode;
xhr.readyState = 4;
if (error || xhr.status != 200) {
xhr.onerror.call(xhr, event);
}
else {
if (typeof xhr.onreadystatechange == "function")
xhr.onreadystatechange.call(xhr, event);
xhr.onload.call(xhr, event);
}
});
}
};
xhr.setRequestHeader = function(key, value){
xhr.headers[key] = value;
};
return xhr;
};
Ti.Network._clear = function () {
Ti.Network._requestURLs = {};
Ti.Network._requests = [];
};
Ti.Network.online = true;
// Mock some file stuff
Ti.Filesystem._files = {};
// Non-standard, for convenience
Ti.Filesystem.createFile = function (spec) {
spec = spec || {};
var file = {};
_.extend(file, Ti.Filesystem.File);
file._exists = spec.exists;
file.exists = function () {
return file._exists;
};
file.read = function () {
return {text: spec.text};
};
return file;
};
Ti.Filesystem.getFile = function (name) {
return Ti.Filesystem._files[name] || Ti.Filesystem.createFile();
};
Ti.Filesystem.getApplicationDataDirectory = function () {
return '';
};
Ti.Utils.md5HexDigest = function (input) {
return md5(input);
};
(function () {
var o = Ti.UI.createWindow;
Ti.UI.createWindow = function () {
var win = o.apply(null, arguments);
if (Ti.Platform.osname == "android") {
win.activity = {};
_.extend(win.activity, Ti.Android.Activity);
}
return win;
};
}());
Ti.include = function () {};
Ti.App.Properties.getString = function (name, def) { return def; };
Ti.App.Properties.getBool = function (name, def) { return def; };
Ti.App.Properties.getList = function (name, def) { return def; };
//Ti.API
Ti.API.info = function(s){
console.log(s);
};
Ti.API.debug = function(d){
console.log(d);
};
Ti.API.error = function(e){
console.error(e);
};
Ti.UI.Window.open = function () {
Ti.UI.fireEvent("window::open", this);
};
alert = function () {
Ti.fireEvent('alert', arguments);
};
return Ti;
}
module.exports = mockti;