-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
350 lines (283 loc) · 11.4 KB
/
test.js
File metadata and controls
350 lines (283 loc) · 11.4 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"use strict;"
/// TESTS
jQuery(document).ready(function ()
{
var pr1 = new eventNode();
var pr2 = new eventNode();
var pr3 = new eventNode();
var pr4 = new eventNode();
function getLogger(message)
{
var msg = message;
return function()
{
console.log(msg);
};
};
pr1.addEvent("test1");
pr1.addEvent("test2");
pr1.addEvent("test3");
pr1.addEvent("test4");
pr1.addEventListener("test1", getLogger("pr1 test1"));
pr1.addEventListener("test2", getLogger("pr1 test2"));
pr1.addEventListener("test3", getLogger("pr1 test3"));
pr1.addEventListener("test4", getLogger("pr1 test4"));
pr2.addEvent("test2");
pr2.addEvent("test3");
pr2.addEvent("test4");
pr2.addEventListener("test2", getLogger("pr2 test2"));
pr2.addEventListener("test3", getLogger("pr2 test3"));
pr2.addEventListener("test4", getLogger("pr2 test4"));
pr3.addEvent("test3");
pr3.addEvent("test4");
pr3.addEventListener("test3", getLogger("pr3 test3"));
pr3.addEventListener("test4", getLogger("pr3 test4"));
pr4.addEvent("test1");
pr4.addEvent("test4");
pr4.addEvent("test8");
pr4.addEventListener("test1", getLogger("pr4 test1"));
pr4.addEventListener("test4", getLogger("pr4 test4"));
pr4.addEventListener("test8", getLogger("pr4 test8"));
console.log("Non-connected node tests:");
pr1.triggerEvent("test1");
pr2.triggerEvent("test2");
pr3.triggerEvent("test3");
pr4.triggerEvent("test4");
pr1.connectNode(pr2);
pr2.connectNode(pr3);
pr3.connectNode(pr4);
// After this point, all nodes are effectively connected and the events below should
// propagate between the nodes.
console.log("Connnected node tests:");
pr1.triggerEvent("test1");
pr2.triggerEvent("test2");
pr3.triggerEvent("test3");
pr4.triggerEvent("test4");
// But just to be sure that we don't have any strange behavior, let's run the tests with all nodes connected to eachother.
pr1.connectNode(pr2);
pr1.connectNode(pr3);
pr1.connectNode(pr4);
pr2.connectNode(pr1);
pr2.connectNode(pr3);
pr2.connectNode(pr4);
pr3.connectNode(pr1);
pr3.connectNode(pr2);
pr3.connectNode(pr4);
pr4.connectNode(pr1);
pr4.connectNode(pr2);
pr4.connectNode(pr3);
console.log("Completely connnected node tests:");
pr1.triggerEvent("test1");
pr2.triggerEvent("test2");
pr3.triggerEvent("test3");
pr4.triggerEvent("test4");
console.log("Network should now contain four nodes.");
console.log("Nodes in network from pr1:"+pr1.getNetworkNodes().length)
console.log("Nodes in network from pr2:"+pr2.getNetworkNodes().length)
console.log("Creating a more complex network and connecting it...");
var a1 = new eventNode();
var a2 = new eventNode();
var a3 = new eventNode();
var a4 = new eventNode();
var a5 = new eventNode();
var a6 = new eventNode();
var a7 = new eventNode();
var a8 = new eventNode();
var a9 = new eventNode();
console.log("Network should now contain nine nodes.");
a1.connectNode(a2);
a2.connectNode(a3);
a3.connectNode(a9);
a9.connectNode(a8);
a8.connectNode(a7);
a7.connectNode(a6);
a6.connectNode(a4);
a4.connectNode(a5);
console.log("Nodes in network from a1: "+ a1.getNetworkNodes().length);
console.log("Nodes in network from a5: "+ a5.getNetworkNodes().length);
console.log("Nodes in network from a9: "+ a9.getNetworkNodes().length);
console.log("Network node identifiers:\n"+ a9.getNetworkIdentifiers().join(",\n"));
console.log("Adding events FringeTester to nodes a1 and a4...");
console.log("Adding events ThreeTester to nodes a1, a6 and a9...");
a1.addEvent("FringeTester");
a4.addEvent("FringeTester");
a6.addEvent("ThreeTester");
a1.addEvent("ThreeTester");
a9.addEvent("ThreeTester");
console.log("Does a1 have the event FringeTester? " + a1.hasEvent("FringeTester"));
console.log("Does a2 have the event FringeTester? " + a2.hasEvent("FringeTester"));
console.log("Does a3 have the event FringeTester? " + a3.hasEvent("FringeTester"));
console.log("Does a4 have the event FringeTester? " + a4.hasEvent("FringeTester"));
console.log("Does a5 have the event FringeTester? " + a5.hasEvent("FringeTester"));
console.log("Does the network connected to a2 have the event FringeTester? " +
a2.networkHasEvent("FringeTester"));
console.log("Does the network connected to a3 have the event FringeTester? " +
a3.networkHasEvent("FringeTester"));
console.log("Does the network connected to a4 have the event FringeTester? " +
a4.networkHasEvent("FringeTester"));
console.log("Does a1 have the event ThreeTester? " + a1.hasEvent("ThreeTester"));
console.log("Does a2 have the event ThreeTester? " + a2.hasEvent("ThreeTester"));
console.log("Does a5 have the event ThreeTester? " + a5.hasEvent("ThreeTester"));
console.log("Does a6 have the event ThreeTester? " + a6.hasEvent("ThreeTester"));
console.log("Does a8 have the event ThreeTester? " + a8.hasEvent("ThreeTester"));
console.log("Does a9 have the event ThreeTester? " + a9.hasEvent("ThreeTester"));
console.log("Does the network connected to a2 have the event ThreeTester? " +
a2.networkHasEvent("ThreeTester"));
console.log("Does the network connected to a3 have the event ThreeTester? " +
a3.networkHasEvent("ThreeTester"));
console.log("Does the network connected to a8 have the event ThreeTester? " +
a8.networkHasEvent("ThreeTester"));
console.log("Does the network connected to a8 have the event DoesNotExist? " +
a8.networkHasEvent("DoesNotExist"));
console.log("Does the network connected to a2 have the event DoesNotExist? " +
a2.networkHasEvent("DoesNotExist"));
console.log("And now, let's connect the aX network to the prX network!");
a1.connectNode(pr1);
console.log("Does the network connected to a1 have the event test1? " +
a1.networkHasEvent("test1"));
console.log("Does the network connected to a9 have the event test1? " +
a9.networkHasEvent("test1"));
console.log("Does the network connected to pr3 have the event ThreeTester? " +
pr3.networkHasEvent("ThreeTester"));
console.log("And let's disconnect the two networks, and rerun.");
pr1.disconnectNode(a1);
console.log("Does the network connected to a1 have the event test1? " +
a1.networkHasEvent("test1"));
console.log("Does the network connected to a9 have the event test1? " +
a9.networkHasEvent("test1"));
console.log("Does the network connected to pr3 have the event ThreeTester? " +
pr3.networkHasEvent("ThreeTester"));
console.log("Now let's separate the aX network into two networks by splitting at a9, and adding an event to either.")
a9.disconnectNode(a8);
a9.addEvent("FirstNetwork");
a8.addEvent("SecondNetwork");
console.log("Does the network connected to a9 have the event FirstNetwork? " +
a9.networkHasEvent("FirstNetwork"));
console.log("Does the network connected to a9 have the event SecondNetwork? " +
a9.networkHasEvent("SecondNetwork"));
console.log("Does the network connected to a8 have the event FirstNetwork? " +
a8.networkHasEvent("FirstNetwork"));
console.log("Does the network connected to a8 have the event SecondNetwork? " +
a8.networkHasEvent("SecondNetwork"));
console.log("Reconnecting networks via nodes a4 and a3 and rerunning tests.")
a4.connectNode(a3);
console.log("Does the network connected to a9 have the event FirstNetwork? " +
a9.networkHasEvent("FirstNetwork"));
console.log("Does the network connected to a9 have the event SecondNetwork? " +
a9.networkHasEvent("SecondNetwork"));
console.log("Does the network connected to a8 have the event FirstNetwork? " +
a8.networkHasEvent("FirstNetwork"));
console.log("Does the network connected to a8 have the event SecondNetwork? " +
a8.networkHasEvent("SecondNetwork"));
setUpDemonstration();
});
function setUpDemonstration()
{
var A1 = jQuery("#demonodeA1");
var B1 = jQuery("#demonodeB1");
var C1 = jQuery("#demonodeC1");
var D1 = jQuery("#demonodeD1");
var A2 = jQuery("#demonodeA2");
var B2 = jQuery("#demonodeB2");
var C2 = jQuery("#demonodeC2");
var D2 = jQuery("#demonodeD2");
var colorChanger = function(element, color)
{
var e = element, c=color, originalColor = jQuery(e).css("background-color"), t;
return function()
{
if(t)
clearTimeout(t);
jQuery(e).css("background-color", c);
t = setTimeout(function()
{
if(jQuery(e).css("background-color") === c)
jQuery(e).css("background-color", originalColor);
// if it's not, someone else changes it and we'll let them change it back.
},2500)
}
};
var nodeA1 = new eventNode();
var nodeB1 = new eventNode();
var nodeC1 = new eventNode();
var nodeD1 = new eventNode();
var nodeA2 = new eventNode();
var nodeB2 = new eventNode();
var nodeC2 = new eventNode();
var nodeD2 = new eventNode();
nodeA1.connectNode(nodeB1);
nodeB1.connectNode(nodeC1);
nodeC1.connectNode(nodeD1);
nodeA2.connectNode(nodeB2);
nodeB2.connectNode(nodeC2);
nodeC2.connectNode(nodeD2);
nodeA1.addEvent("A");
nodeA1.addEvent("B");
nodeA1.addEvent("C");
nodeA1.addEvent("D");
nodeA2.addEvent("A");
nodeA2.addEvent("B");
nodeA2.addEvent("C");
nodeA2.addEvent("D");
var red = "rgb(166, 45, 45)";
var green = "rgb(45, 166, 45)";
var blue = "rgb(45, 45, 166)";
var gray = "rgb(144, 144, 144)";
var purple = "rgb(144, 66, 144)";
nodeA1.addEventListener("A", colorChanger(A1, red));
nodeA1.addEventListener("B", colorChanger(A1, green));
nodeB1.addEventListener("B", colorChanger(B1, green));
nodeB1.addEventListener("C", colorChanger(B1, purple));
nodeC1.addEventListener("A", colorChanger(C1, red));
nodeC1.addEventListener("C", colorChanger(C1, purple));
nodeC1.addEventListener("D", colorChanger(C1, gray));
nodeD1.addEventListener("A", colorChanger(D1, red));
nodeD1.addEventListener("B", colorChanger(D1, green));
nodeD1.addEventListener("D", colorChanger(D1, gray));
nodeA2.addEventListener("A", colorChanger(A2, red));
nodeA2.addEventListener("B", colorChanger(A2, green));
nodeA2.addEventListener("C", colorChanger(A2, purple));
nodeB2.addEventListener("A", colorChanger(B2, red));
nodeB2.addEventListener("C", colorChanger(B2, purple));
nodeB2.addEventListener("D", colorChanger(B2, gray));
nodeC2.addEventListener("A", colorChanger(C2, red));
nodeC2.addEventListener("C", colorChanger(C2, purple));
nodeD2.addEventListener("B", colorChanger(D2, green));
nodeD2.addEventListener("D", colorChanger(D2, gray));
jQuery("#trigger-A1").click(function(){
nodeA1.triggerEvent("A");
});
jQuery("#trigger-B1").click(function(){
nodeB1.triggerEvent("B");
});
jQuery("#trigger-C1").click(function(){
nodeC1.triggerEvent("C");
});
jQuery("#trigger-D1").click(function(){
nodeD1.triggerEvent("D");
});
jQuery("#trigger-A2").click(function(){
nodeA2.triggerEvent("A");
});
jQuery("#trigger-B2").click(function(){
nodeB2.triggerEvent("B");
});
jQuery("#trigger-C2").click(function(){
nodeC2.triggerEvent("C");
});
jQuery("#trigger-D2").click(function(){
nodeD2.triggerEvent("D");
});
jQuery("#toggleNetworkConnection").click(function()
{
if(nodeA1.connectNode(nodeA2))
{
jQuery(this).text("Disconnect the networks");
}
else
{
nodeA1.disconnectNode(nodeA2);
jQuery(this).text("Connect the networks");
}
});
}