-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
295 lines (256 loc) · 12 KB
/
test.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
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
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="bytheway.js"></script>
<script>
var PREFIX, PAGENUMBER, PAGES = {}, PINGPONGIDS = [];
location.search.replace(/^\?([12]),(TEST-\d+-)$/, function (whole, page, pref) {
PREFIX = pref;
PAGENUMBER = parseInt(page);
});
if (!PREFIX) {
PREFIX = "TEST-" + (Math.random() * 1e9 | 0) + "-";
PAGENUMBER = 0;
}
var PASSED = 0; FAILED = 0;
var TESTS = [];
function TEST(description, func) {
TESTS.push({d: description, f: func});
};
function nextTest() {
var t = TESTS.shift();
if (t) {
var p = $("<p/>").html(t.d).appendTo("#content");
var w = waiting().appendTo(p);
var def = $.Deferred().done(function () {
PASSED++;
p.append("<span style='color:#008000'> ✔</span>");
w.remove();
}).fail(function () {
FAILED++;
p.append("<span style='color:#a00000'> ✗</span>");
w.remove();
}).always(nextTest);
t.f(def, p);
}
}
var PINGPONG = ByTheWay(PREFIX + "pingpong");
var PINGPONGWITHSELF = ByTheWay(PREFIX + "pingpong", true);
var PAGECALLED = {};
PINGPONGWITHSELF.onReceive(function (msg, env, resp) {
if (msg === "ping")
resp("pong");
else if (msg === "page number please") {
PINGPONGWITHSELF.unicast(PAGENUMBER, env.sender);
} else if (typeof msg === "number") {
if (PAGECALLED)
PAGECALLED(msg);
}
});
var forwarder = function(message) {
ByTheWay(message.channel).unicast(message.message, message.tab);
}
var FORWARDCHANNEL = PREFIX + "forwarder";
var FORWARDMSGR = ByTheWay(FORWARDCHANNEL, true);
FORWARDMSGR.onReceive(forwarder);
function startTest() {
$("#content").empty();
// the pages reported themselves as having opened, so this works fine
TEST("basic communication", function (def) { def.resolve(); });
TEST("ping pong, excluding self", function (def) {
PINGPONG.request("ping", function (msgs, envs) {
for (var i = 0; i< envs.length; i++) {
PINGPONGIDS.push(envs[i].sender);
}
if (msgs.length === 2)
def.resolve();
else
def.reject();
});
});
TEST("ping pong, including self", function (def) {
PINGPONGWITHSELF.request("ping", function (msgs) {
if (msgs.length === 3)
def.resolve();
else
def.reject();
});
});
TEST("ping pong, only with one page", function (def) {
PINGPONGWITHSELF.request("ping", function (msgs) {
if (msgs.length === 1)
def.resolve();
else
def.reject();
}, PINGPONGIDS[0]);
});
TEST("broadcast v. unicast part 1", function (def) {
var calls = [0, 0, 0]
PAGECALLED = function (p) { calls[p]++; }
PINGPONGWITHSELF.unicast("page number please", PINGPONGIDS[0]);
setTimeout(function () {
if (calls[0] === 0 && calls[1] + calls[2] === 1)
def.resolve();
else
def.reject();
}, 200);
});
TEST("broadcast v. unicast part 2", function (def) {
var calls = [0, 0, 0]
PAGECALLED = function (p) { calls[p]++; }
PINGPONGWITHSELF.broadcast("page number please");
setTimeout(function () {
if (calls[0] === 1 && calls[1] === 1 && calls[2] === 1)
def.resolve();
else
def.reject();
}, 200);
});
TEST("broadcast v. unicast part 3", function (def) {
var calls = [0, 0, 0]
PAGECALLED = function (p) { calls[p]++; }
PINGPONGWITHSELF.unicast("page number please", PINGPONGWITHSELF.id);
setTimeout(function () {
if (calls[0] === 1 && calls[1] === 0 && calls[2] === 0)
def.resolve();
else
def.reject();
}, 200);
});
TEST("all messengers in the window have the same id", function (def) {
var m1 = ByTheWay(PREFIX + "m1");
var m2 = ByTheWay(PREFIX + "m2");
var m3 = ByTheWay(PREFIX + "m1", true);
if (m1.id === m2.id && m2.id == m3.id)
def.resolve();
else
def.reject();
});
TEST("same-window message only arrives on the correct channel", function (def) {
var m1 = ByTheWay(PREFIX + "samewindow!", true);
var m2 = ByTheWay(PREFIX + "same.indow", true);
var m3 = ByTheWay(PREFIX + "samewindow", true);
var c1 = 0, c2 = 0, c3 = 0;
m1.onReceive(function () { c1++; });
m2.onReceive(function () { c2++; });
m3.onReceive(function () { c3++; });
m1.broadcast("hello");
setTimeout(function() {
if (c1 === 1 && c2 === 0 && c3 === 0)
def.resolve();
else
def.reject();
}, 200)
});
TEST("forwarding via different channels", function (def) {
var k = PREFIX + "forwardingreceiver";
var peer1 = PINGPONGIDS[0];
var peer2 = PINGPONGIDS[1];
var ourself = FORWARDMSGR.id;
var m3 = "here I am"; // this message should come back to us from peer1
var m2 = { channel: k, message: m3, tab: ourself }; // peer2 should send this to peer1
var m1 = { channel: FORWARDCHANNEL, message: m2, tab: peer1 }; // we send this to peer2
var n = 0;
var good = true;
ByTheWay(k).onReceive(function (msg, env) {
n++;
good = good && msg === m3 && env.sender === peer1;
});
FORWARDMSGR.unicast(m1, peer2);
setTimeout(function () {
if (n === 1 && good)
def.resolve();
else
def.reject();
}, 200)
});
TEST("closing the subpages", function (def) {
var closed = 0;
$("#message").text("Please close the two test pages now (you have 10 seconds)");
var t = setTimeout(function () {
def.reject();
$("#message").empty();
}, 10000);
ByTheWay(PREFIX + "pageclosing").onReceive(function (msg) {
if (msg === "I'm out") {
closed++;
if (closed === 2) {
def.resolve();
clearTimeout(t);
$("#message").empty();
}
}
});
});
TEST("<b>all tests done</b>", function (def, p) {
p.append("<span>, " + PASSED + " passed, " + FAILED + " failed" + "</span>");
if (FAILED === 0)
def.resolve();
else
def.reject();
});
nextTest();
}
function prepareSecondaryPage() {
var closingMsgr = ByTheWay(PREFIX + "pageclosing");
closingMsgr.onReceive(function (msg) {
if (msg === "that's all") {
window.close();
}
});
$(window).on("unload", function () {
closingMsgr.broadcast("I'm out");
});
}
PAGES[0] = function() {
var links = {};
ByTheWay(PREFIX + "pageready").onReceive(function(msg, env) {
var link = links[msg];
if (link) {
link.next().remove();
link.after("<span style='color:#008000'> ✔</span>");
delete links[msg];
if (!links[1] && !links[2]) {
startTest();
}
} else {
$("#content").text("Please open each test page only once. Reload this page to start over.");
}
});
$("<p/>").text("Please open these two pages in new tabs:").appendTo("#content");
for (var i = 1; i < 3; i++) {
links[i] = $("<a target='_blank' rel='noreferrer' href='?" + i + "," + PREFIX + "'>test page " + i + "</a>");
$("#content").append(links[i]).append(waiting()).append("<br/>");
}
setInterval(function () {
$(".waiting").each(function () {
var j = $(this);
var t = $(this).text();
j.text(" " + t.charAt(5) + t.substr(1, 4));
});
}, 300);
};
PAGES[1] = function() {
prepareSecondaryPage();
ByTheWay(PREFIX + "pageready").broadcast(1);
};
PAGES[2] = function() {
prepareSecondaryPage();
ByTheWay(PREFIX + "pageready").broadcast(2);
};
$(function () {
$("h1").text("Test page number " + PAGENUMBER);
PAGES[PAGENUMBER]();
});
function waiting() {
return $("<span class='waiting' style='white-space: pre;'> ... </span>");
}
</script>
</head>
<body>
<h1></h1>
<div id="content"></div>
<div id="message" style="color: #800000; font-size: 24px; font-weight: bold; margin-left: 30px;"></div>
</body>
</html>