forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-test.html
More file actions
333 lines (290 loc) · 10.5 KB
/
Copy pathunit-test.html
File metadata and controls
333 lines (290 loc) · 10.5 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!doctype html>
<html>
<head>
<title>PubNub JavaScript Unit Test</title>
<link
rel=stylesheet
href=http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css
>
<style type=text/css>
#stop-test,
#finished-fail,
#finished-success,
.tpl {display:none}
</style>
</head>
<body><div class=container>
<!-- NOTICE / TITLE -->
<div class="alert-message warning">
<a class="close" href="#">×</a>
<p><strong>PubNub Unit Tests</strong> for JavaScript on Mobile and Desktop Web Browser</p>
</div>
<!-- BUTTON PANNEL -->
<div class=well>
<button id=start-test class='btn info'>Start Test</button>
<button id=stop-test class='btn danger'>STOP</button>
<span id=finished-success class='label success'>100% Successful</span>
<span id=finished-fail class='label important'>Finished With Errors</span>
<span
id=test-status
class=label
template='Result: {pass}/{fail} (pass/fail) - Total: {total}'
>...</span>
</div>
<!-- OUTPUT -->
<table id=unit-test-out class="zebra-striped">
<tr><th><span class=label>Pass/Fail</span><th>Test Ready
</table>
<!-- TEMPLATES -->
<script type=text/template id=test_template class=tpl>
<td><span class="label {result}">{display}</span></td>
<td>{message}</td>
</script>
<script src=../pubnub.min.js></script>
<script>(function(){
function each( o, f ) {
if ( !o || !f ) return;
if ( typeof o[0] != 'undefined' )
for ( var i = 0, l = o.length; i < l; )
f.call( o[i], o[i], i++ );
else
for ( var i in o )
o.hasOwnProperty &&
o.hasOwnProperty(i) &&
f.call( o[i], i, o[i] );
}
/**
* MAP
* ===
* var list = map( [1,2,3], function(item) { return item + 1 } )
*/
function map( list, fun ) {
var fin = [];
each( list || [], function( k, v ) { fin.push(fun( k, v )) } );
return fin;
}
/**
* GREP
* ====
* var list = grep( [1,2,3], function(item) { return item % 2 } )
*/
function grep( list, fun ) {
var fin = [];
each( list || [], function(l) { fun(l) && fin.push(l) } );
return fin
}
/**
* SUPPLANT
* ========
* var text = supplant( 'Hello {name}!', { name : 'John' } )
*/
function supplant( str, values ) {
return str.replace( REPL, function( _, match ) {
return values[match] || _
} );
}
function test( t, msg ) {
if (!test.run) return;
var entry = p.create('tr');
entry.innerHTML = p.supplant( test_tpl, {
result : t ? 'success' : 'important',
display : t ? 'pass' : 'fail',
message : msg
} );
t ? test.pass++ : test.fail++;
test.done++;
out.insertBefore( entry, out.firstChild );
console.log( t, msg );
status_area.innerHTML = p.supplant( status_tpl, {
pass : test.pass+'',
fail : test.fail+'',
total : test.done+''
} );
if (test.done === test.plan) {
stop_test();
test.fail ||
p.css( p.$('finished-success'), { display : 'inline-block' } ) &&
p.css( p.$('finished-fail'), { display : 'inline-block' } );
}
}
var REPL = /{([\w\-]+)}/g
, p = PUBNUB({ publish_key : 'demo', subscribe_key : 'demo' });
p.supplant = function supplant( str, values ) {
return str.replace( REPL, function( _, match ) {
return values[match] || _
} );
}
p.$ = function $(id) { return document.getElementById(id) };
p.attr = function attr( node, attribute, value ) {
if (value) node.setAttribute( attribute, value );
else return node && node.getAttribute && node.getAttribute(attribute);
};
p.create = function create(element) {
return document.createElement(element)
};
p.css = function css( element, styles ) {
for (var style in styles) if (styles.hasOwnProperty(style))
try {element.style[style] = styles[style] + (
'|width|height|top|left|'.indexOf(style) > 0 &&
typeof styles[style] == 'number'
? 'px' : ''
)}catch(e){}
};
p.bind = function bind( type, el, fun ) {
each( type.split(','), function(etype) {
var rapfun = function(e) {
if (!e) e = window.event;
if (!fun(e)) {
e.cancelBubble = true;
e.returnValue = false;
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
}
};
if ( el.addEventListener ) el.addEventListener( etype, rapfun, false );
else if ( el.attachEvent ) el.attachEvent( 'on' + etype, rapfun );
else el[ 'on' + etype ] = rapfun;
} );
};
var many_con_tst = 'opera' in window ? -1 : 10
, channel = 'pn-javascript-unit-test'
, out = p.$('unit-test-out')
, test_tpl = p.$('test_template').innerHTML
, start_button = p.$('start-test')
, stop_button = p.$('stop-test')
, status_area = p.$('test-status')
, status_tpl = p.attr( status_area, 'template' );
/* ======================================================================
Stop Test
====================================================================== */
p.bind( 'mousedown,touchstart', stop_button, stop_test );
function stop_test() {
p.css( start_button, { display : 'inline-block' } );
p.css( stop_button, { display : 'none' } );
test.run = 0;
}
/* ======================================================================
Start Test
====================================================================== */
p.bind( 'mousedown,touchstart', start_button, start_test );
function start_test() {
test.plan = 19 + many_con_tst*3; // # of tests
test.done = 0; // 0 tests done so far
test.pass = 0; // 0 passes so far
test.fail = 0; // 0 failes so far
test.run = 1; // continue running?
p.css( stop_button, { display : 'inline-block' } );
p.css( start_button, { display : 'none' } );
p.css( p.$('finished-fail'), { display : 'none' } );
p.css( p.$('finished-success'), { display : 'none' } );
test( 1, 'Ready to Test' );
test( PUBNUB, 'PubNub Lib Exists' );
p.time(function(time){
test( time, 'TimeToken' );
test( time > 0, 'Time Value' );
});
p.uuid(function(uuid){
test( uuid, 'UUID Response');
test( uuid.length > 10, 'UUID Long Enough');
});
function publish_test(con) {
con || test( 1, 'Connection Established' );
p.publish({
channel : channel,
message : { test : "test" },
callback : function(response) {
test( response[0], 'Successful Publish' );
test( response[1] === 'Sent', 'Success With Demo' );
history_test();
}
});
}
publish_test(1);
function history_test() {
p.history({
limit : 1,
channel : channel,
callback : function(messages) {
test( messages, 'History Response' );
test( messages[0][0].test === "test", 'History Content' );
}
});
}
p.subscribe({
channel : channel,
connect : publish_test,
callback : function( message, stack ) {
p.unsubscribe({ channel : channel });
test( message, 'Subscribe Message' );
test( message.test === "test", 'Subscribe Message Data' );
test( stack[1], 'TimeToken Returned: ' + stack[1] );
}
});
/* ------------------------------------------------------------------
--- MANY CONNECTIONS TESTS
------------------------------------------------------------------ */
p.map( Array(many_con_tst).join('-').split('-'), function( _, conn ) {
var chan = 'many-conn-test-' + conn;
test( chan, 'Many Connections: ' + conn + ' Connecting...' );
p.subscribe({
channel : chan,
connect : function() {
test( chan, 'Many Connections: ' + conn + ' Connected!' );
p.publish({ channel : chan, message : chan });
},
callback : function(message) {
test(
chan === message,
'Many Connections: ' + conn + ' Received'
);
setTimeout(
function() { p.unsubscribe({ channel : chan }) },
5000
);
}
});
} );
/* ------------------------------------------------------------------
--- TESTING NEW CONNECTION RESTORE FEATURE
------------------------------------------------------------------ */
var restore_channel = 'restore-channel'+Math.random();
p.subscribe({
restore : true,
channel : restore_channel,
callback : function(){},
connect : function() {
p.unsubscribe({ channel : restore_channel });
// Send Message While Not Connected
p.publish({
channel : restore_channel,
message : 'test',
callback : function() {
p.subscribe({
restore : true,
channel : restore_channel,
callback : function( message, stack ) {
p.unsubscribe({ channel : restore_channel });
test(
message === "test",
'Subscribe Restore'
);
}
});
}
});
}
});
/*
restore : true,
disconnect : function() {
},
reconnect : function() {
},
connect : function() {
},
*/
}
start_test();
})();</script>
</div></body>
</html>