forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-test.js
More file actions
221 lines (183 loc) · 7.1 KB
/
Copy pathunit-test.js
File metadata and controls
221 lines (183 loc) · 7.1 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
(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();
if (test.fail) return p.css(
p.$('finished-fail'),
{ display : 'inline-block' }
);
p.css( p.$('finished-success'), { display : 'inline-block' } );
}
}
var p = PUBNUB
, 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 = 16; // # of tests
test.pass = 0; // 0 passes so far
test.fail = 0; // 0 failes so far
test.done = 0; // 0 tests done 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' in window, 'PubNub Lib Exists' );
test( 'io' in window, 'Socket.IO Lib Exists' );
test( 'secure' in PUBNUB, 'Crypto Lib Exists' );
var pubnub_setup = {
channel : 'my_mobile_app',
publish_key : 'demo',
subscribe_key : 'demo',
password : '', // Encrypt with Password
ssl : false, // Use SSL
geo : false // Include Geo Data (Lat/Lng)
};
var pubnub_setup_2 = {
channel : 'my_mobile_app2',
publish_key : 'demo',
subscribe_key : 'demo',
password : '', // Encrypt with Password
ssl : false, // Use SSL
geo : false, // Include Geo Data (Lat/Lng)
presence : false
};
// Multi-plexing Single Connection
var feed = io.connect(
'http://pubsub.pubnub.com/feed',
pubnub_setup
);
var chat = io.connect(
'http://pubsub.pubnub.com/chat',
pubnub_setup_2
);
// Ordinary Socket
var socket = io.connect( 'http://pubsub.pubnub.com', pubnub_setup );
socket.on( 'connect', function() {
test( feed, 'Socket Connection Estabilshed' );
socket.send('sock');
// Connected Users
test( socket.get_user_count(), 'Counts of Active User' );
// Acknowledgement Receipt Confirmation
socket.emit(
'important-message',
{ data : true },
function (receipt) {
test( receipt, 'Acknowledgement Receipt Confirmation' );
}
);
} );
socket.on( 'message', function(message) {
test( message === 'sock', 'Received Socket Message' );
} );
// CONNECTION ESTABLISHED
feed.on( 'connect', function() {
test( feed, 'Feed Connection Estabilshed' );
feed.send({ title : 'Update', body : 'Text Body' });
feed.emit( 'my-custom-event', 123456 );
} );
// MESSAGE SEND/RECEIVE
feed.on( 'message', function(message) {
test( message.title === "Update", 'Feed Message Received' );
} );
feed.on( 'my-custom-event', function(message) {
test( message === 123456, 'Custom Feed Event Received' );
} );
chat.on( 'connect', function() {
test( chat, 'Chat Connection Estabilshed' );
chat.send("Hi");
} );
// MESSAGE SEND/RECEIVE
chat.on( 'message', function(message) {
test( message === "Hi", 'Chat Message Received' );
test( message, 'Connection Send/Receive Successfully Completed' );
} );
// CONNECTION LOST/RESTORED
feed.on( 'disconnect', function() {
console.log('Error, Lost Connection');
} );
feed.on( 'reconnect', function() {
console.log('Connection Restored');
} );
// USER JOINS/PARTS CHAT
chat.on( 'join', function(user) {
console.log(user);
} );
chat.on( 'leave', function(user) {
console.log(user);
} );
// STANFORD CRYPTO LIBRARY WITH AES ENCRYPTION
var pubnub_setup_secret = {
channel : 'my_mobile_app_secret',
publish_key : 'demo',
subscribe_key : 'demo',
password : '12345678', // Encrypt with Password
presence : false, // Disable Presence
ssl : false, // Use SSL
geo : false // Include Geo Data (Lat/Lng)
};
var encrypted = io.connect(
'http://pubsub.pubnub.com/secret',
pubnub_setup_secret
);
encrypted.on( 'connect', function() {
encrypted.send({ my_encrypted_data : 'Super Secret!' });
test( 1, 'Connected to Encrypted Channel' );
} );
encrypted.on( 'message', function(message) {
test(
message.my_encrypted_data === 'Super Secret!',
'Stanford Crypto AES'
);
} );
// CUSTOM USER DATA ON PRESENCE EVENTS
var pubnub_setup_custom = {
channel : 'my_mobile_app',
presence : false, // Disable Presence
publish_key : 'demo',
subscribe_key : 'demo',
user : { name : "John" }
};
// Multi-plexing Single Connection
var cpresence = io.connect(
'http://pubsub.pubnub.com/custom-presence',
pubnub_setup_custom
);
}
start_test();
})();