2d1a8717b05dacb36cb7fd71e2a7534701e78531
[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         pingPong,
14         serverPortBusy,
15         endTest
16       ];
17
18       function runNextTest() {
19         test_list[current_test++]();
20       };
21
22       function reportFail(message) {
23         console.log(message);
24         document.title = "Fail";
25       };
26
27       function endTest() {
28         document.title = "Pass";
29       };
30
31       function memoryManagement() {
32         var eventCount = 0;
33         var garbageCollectionCount = 0;
34
35         // We cannot run the GC inside a callback, because the callee of the
36         // callback holds a reference (that is why runGCandCheck is called from
37         // a setTimeout. Another important thing to notice is nullifying the
38         // server or client object right away won't work because they use
39         // delayed initialization, which is fine, because as soon as the
40         // initialization is completed, the object will be collected if no
41         // variable is keeping a reference to it.
42         function runGCandCheck() {
43           gc();
44           if (garbageCollectionCount != 2)
45             reportFail("TCPSocket or TCPServerSocket is leaking.");
46           else
47             runNextTest();
48         };
49
50         var server = new api.TCPServerSocket(
51             {"localAddress": "127.0.0.1", "localPort": 54321});
52         server.onerror = server.onopen = nullifyServer;
53         server.tracker = v8tools.lifecycleTracker();
54         server.tracker.destructor = function() {
55           garbageCollectionCount++;
56         };
57
58         function nullifyServer() {
59           server.onerror = null;
60           server.onopen = null;
61           server = null;
62
63           if (++eventCount == 2)
64             setTimeout(runGCandCheck, 0);
65         };
66
67         var client = new api.TCPSocket("127.0.0.1", 54321);
68         client.onerror = client.onopen = nullifyClient;
69         client.tracker = v8tools.lifecycleTracker();
70         client.tracker.destructor = function() {
71           garbageCollectionCount++;
72         };
73
74         function nullifyClient() {
75           client.onerror = null;
76           client.onopen = null;
77           client = null;
78
79           if (++eventCount == 2)
80             setTimeout(runGCandCheck, 0);
81         };
82       };
83
84       // This test is designed like a data ping pong. First we open a server
85       // listening for incoming connections. Secondly we connect a client socket
86       // into this server, that immediately sends a greetings message that is
87       // readily verified. The client then sends back some data to the server,
88       // which also gets checked.
89       //
90       // This is not an API conformance test, it just verifies if the basic
91       // functionality works. W3C should provide the former.
92       function pingPong(serverPort) {
93         serverPort = serverPort || 5000;
94         var serverPortMax = 5020;
95         var testData = "Hello World!";
96
97         var server = new api.TCPServerSocket(
98             {"localAddress": "127.0.0.1", "localPort": serverPort});
99
100         server.onerror = function() {
101           // The default port might be busy, so we try different ports
102           // before reporting failure.
103           if (serverPort < serverPortMax)
104             pingPong(++serverPort);
105           else
106             reportFail("Not able to listen at port " + serverPort + ".");
107         };
108
109         server.onopen = function() {
110           var client = new api.TCPSocket("127.0.0.1", serverPort);
111
112           client.onerror = function() {
113             reportFail("Not able to connect to port " + serverPort + ".");
114           };
115
116           client.ondata = function(event) {
117             var view = new Uint8Array(event.data);
118             var data = String.fromCharCode.apply(null, view);
119
120             if (data != testData)
121               reportFail("Invalid data received by the client socket.");
122             else
123               client.send(testData);
124           };
125         };
126
127         server.onconnect = function(event) {
128           event.connectedSocket.send(testData);
129           event.connectedSocket.ondata = function (event) {
130             var view = new Uint8Array(event.data);
131             var data = String.fromCharCode.apply(null, view);
132
133             if (data != testData)
134               reportFail("Invalid data received by server socket.");
135             else
136               runNextTest();
137           };
138         };
139       };
140
141       function serverPortBusy(serverPort) {
142         serverPort = serverPort || 7000;
143         var serverPortMax = 7020;
144
145         var server = new api.TCPServerSocket(
146             {"localAddress": "127.0.0.1", "localPort": serverPort});
147
148         server.onerror = function() {
149           if (serverPort < serverPortMax)
150             serverPortBusy(++serverPort);
151           else
152             reportFail("Not able to listen at port " + serverPort + ".");
153         };
154
155         server.onopen = function() {
156           var serverShouldFail = new api.TCPServerSocket(
157               {"localAddress": "127.0.0.1", "localPort": serverPort});
158
159           // Should fail, port already in use by another server.
160           serverShouldFail.onerror = runNextTest();
161           serverShouldFail.onopen = function() {
162             reportFail("Port " + serverPort + " should be busy.");
163           };
164         };
165       };
166
167       runNextTest();
168     </script>
169   </body>
170 </html>