Upstream version 6.34.113.0
[platform/framework/web/crosswalk.git] / src / xwalk / sysapps / raw_socket / raw_socket_api_browsertest.html
1 <html>
2   <head>
3     <title></title>
4   </head>
5   <body>
6     <script>
7       var v8tools = sysapps_raw_socket_test.v8tools;
8       var api = xwalk.experimental.raw_socket;
9
10       var current_test = 0;
11       var test_list = [
12         memoryManagement,
13         pingPongTCP,
14         pingPongUDP,
15         serverPortBusyTCP,
16         serverPortBusyUDP,
17         endTest
18       ];
19
20       function runNextTest() {
21         test_list[current_test++]();
22       };
23
24       function reportFail(message) {
25         console.log(message);
26         document.title = "Fail";
27       };
28
29       function endTest() {
30         document.title = "Pass";
31       };
32
33       function memoryManagement() {
34         var eventCount = 0;
35         var garbageCollectionCount = 0;
36
37         // We cannot run the GC inside a callback, because the callee of the
38         // callback holds a reference (that is why runGCandCheck is called from
39         // a setTimeout. Another important thing to notice is nullifying the
40         // server or client object right away won't work because they use
41         // delayed initialization, which is fine, because as soon as the
42         // initialization is completed, the object will be collected if no
43         // variable is keeping a reference to it.
44         function runGCandCheck() {
45           // The GC is called here more than once to make sure
46           // all the dangling objects get collected. Calling the GC
47           // one time does not give a guarantee that everything that
48           // can be collected will be collected.
49           gc();
50           gc();
51           gc();
52
53           if (garbageCollectionCount != 3)
54             reportFail("TCPSocket, TCPServerSocket or UDPSocket is leaking.");
55           else
56             runNextTest();
57         };
58
59         var server = new api.TCPServerSocket(
60             {"localAddress": "127.0.0.1", "localPort": 54321});
61         server.onerror = server.onopen = nullifyServer;
62         server.tracker = v8tools.lifecycleTracker();
63         server.tracker.destructor = function() {
64           garbageCollectionCount++;
65         };
66
67         function nullifyServer() {
68           server.onerror = null;
69           server.onopen = null;
70           server = null;
71
72           if (++eventCount == 3)
73             setTimeout(runGCandCheck, 0);
74         };
75
76         var client = new api.TCPSocket("127.0.0.1", 54321);
77         client.onerror = client.onopen = nullifyClient;
78         client.tracker = v8tools.lifecycleTracker();
79         client.tracker.destructor = function() {
80           garbageCollectionCount++;
81         };
82
83         function nullifyClient() {
84           client.onerror = null;
85           client.onopen = null;
86           client = null;
87
88           if (++eventCount == 3)
89             setTimeout(runGCandCheck, 0);
90         };
91
92         var udp = new api.UDPSocket({localAddress: "127.0.0.1", localPort: 7000});
93         udp.onerror = udp.onopen = nullifyUDP;
94         udp.tracker = v8tools.lifecycleTracker();
95         udp.tracker.destructor = function() {
96           garbageCollectionCount++;
97         };
98
99         function nullifyUDP() {
100           udp.onerror = null;
101           udp.onopen = null;
102           udp = null;
103
104           if (++eventCount == 3)
105             setTimeout(runGCandCheck, 0);
106         };
107       };
108
109       // This test is designed like a data ping pong. First we open a server
110       // listening for incoming connections. Secondly we connect a client socket
111       // into this server, that immediately sends a greetings message that is
112       // readily verified. The client then sends back some data to the server,
113       // which also gets checked.
114       //
115       // This is not an API conformance test, it just verifies if the basic
116       // functionality works. W3C should provide the former.
117       function pingPongTCP(serverPort) {
118         serverPort = serverPort || 5000;
119         var serverPortMax = 5020;
120         var testData = "Hello World!";
121
122         var server = new api.TCPServerSocket(
123             {"localAddress": "127.0.0.1", "localPort": serverPort});
124
125         server.onerror = function() {
126           // The default port might be busy, so we try different ports
127           // before reporting failure.
128           if (serverPort < serverPortMax)
129             pingPongTCP(++serverPort);
130           else
131             reportFail("Not able to listen at port " + serverPort + ".");
132         };
133
134         server.onopen = function() {
135           var client = new api.TCPSocket("127.0.0.1", serverPort);
136
137           client.onerror = function() {
138             reportFail("Not able to connect to port " + serverPort + ".");
139           };
140
141           client.ondata = function(event) {
142             var view = new Uint8Array(event.data);
143             var data = String.fromCharCode.apply(null, view);
144
145             if (data != testData)
146               reportFail("Invalid data received by the client socket.");
147             else
148               client.send(testData);
149           };
150         };
151
152         server.onconnect = function(event) {
153           event.connectedSocket.send(testData);
154           event.connectedSocket.ondata = function (event) {
155             var view = new Uint8Array(event.data);
156             var data = String.fromCharCode.apply(null, view);
157
158             if (data != testData)
159               reportFail("Invalid data received by server socket.");
160             else
161               runNextTest();
162           };
163         };
164       };
165
166       function pingPongUDP(serverPort) {
167         serverPort = serverPort || 6000;
168         var serverPortMax = 6020;
169         var testData = "Hello World!";
170
171         var server = new api.UDPSocket(
172             {"localAddress": "127.0.0.1", "localPort": serverPort});
173
174         server.onerror = function() {
175           // The default port might be busy, so we try different ports
176           // before reporting failure.
177           if (serverPort < serverPortMax)
178             pingPongUDP(++serverPort);
179           else
180             reportFail("Not able to listen at port " + serverPort + ".");
181         };
182
183         server.onopen = function() {
184           var client = new api.UDPSocket(
185               {remoteAddress: "127.0.0.1", remotePort: serverPort});
186           client.onopen = function() {
187             client.send(testData);
188           };
189
190           client.onerror = function() {
191             reportFail("Not able to connect to port " + serverPort + ".");
192           };
193
194           client.onmessage = function(event) {
195             var view = new Uint8Array(event.data);
196             var data = String.fromCharCode.apply(null, view);
197
198             if (data != testData)
199               reportFail("Invalid data received by the client socket.");
200             else
201               runNextTest();
202           };
203         };
204
205         server.onmessage = function(event) {
206           var view = new Uint8Array(event.data);
207           var data = String.fromCharCode.apply(null, view);
208
209           if (data != testData)
210             reportFail("Invalid data received by server socket.");
211           else
212             server.send(data, event.remoteAddress, event.remotePort);
213         };
214       };
215
216       function serverPortBusy(Socket, serverPort) {
217         serverPort = serverPort || 7000;
218         var serverPortMax = 7020;
219
220         var server = new Socket(
221             {"localAddress": "127.0.0.1", "localPort": serverPort});
222
223         server.onerror = function() {
224           if (serverPort < serverPortMax)
225             serverPortBusy(Socket, ++serverPort);
226           else
227             reportFail("Not able to listen at port " + serverPort + ".");
228         };
229
230         server.onopen = function() {
231           var serverShouldFail = new Socket(
232               {"localAddress": "127.0.0.1", "localPort": serverPort});
233
234           // Should fail, port already in use by another server.
235           serverShouldFail.onerror = runNextTest();
236           serverShouldFail.onopen = function() {
237             reportFail("Port " + serverPort + " should be busy.");
238           };
239         };
240       };
241
242       function serverPortBusyTCP() {
243         serverPortBusy(api.TCPServerSocket);
244       };
245
246       function serverPortBusyUDP() {
247         serverPortBusy(api.UDPSocket);
248       };
249
250       runNextTest();
251     </script>
252   </body>
253 </html>