-
Notifications
You must be signed in to change notification settings - Fork 285
/
index.html
234 lines (203 loc) · 7.68 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>PubNub PhoneGap 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 PhoneGap JavaScript on Mobile</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>
<div id=pubnub></div>
<script src=pubnub-3.1.js></script>
<script>(function(){
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 p = PUBNUB
, 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] === 'D', '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].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'
);
}
});
}
});
}
});
}
start_test();
})();</script>
</div></body>
</html>