Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / websocket / resources / close-code-and-reason-common.js
1 // Common code for both the plain and Worker variants of close-code-and-reason.
2
3 var test;
4 var closeEvent;
5 var codeNormalClosure = 1000;
6 var codeNoStatusRcvd = 1005;
7 var codeAbnormalClosure = 1006;
8 var emptyString = "";
9
10 function closeDuringOpen()
11 {
12   var ws = new WebSocket("ws://127.0.0.1:8880/echo");
13
14   ws.onopen = function(event)
15   {
16     testFailed("ws.onopen() was called. (message = \"" + event.data + "\")");
17   };
18
19   ws.onclose = function(event)
20   {
21     debug("ws.onclose() was called.");
22     closeEvent = event;
23     shouldBeFalse("closeEvent.wasClean");
24     shouldBe("closeEvent.code", "codeAbnormalClosure");
25     shouldBe("closeEvent.reason", "emptyString");
26   };
27
28   ws.close();
29 }
30
31 var testId = 0;
32 var testNum = 9;
33 var sendData = [
34     "-", // request close frame without code and reason
35     "--", // request close frame with invalid body which size is 1
36     "1000 ok",  // request close frame with code 1000 and reason
37     "1005 foo",  // request close frame with forbidden code 1005 and reason
38     "1006 bar",  // request close frame with forbidden code 1006 and reason
39     "1015 baz",  // request close frame with forbidden code 1015 and reason
40     "0 good bye", // request close frame with specified code and reason
41     "10 good bye", // request close frame with specified code and reason
42     "65535 good bye", // request close frame with specified code and reason
43 ];
44 var expectedCode = [
45     codeNoStatusRcvd,
46     codeAbnormalClosure,
47     codeNormalClosure,
48     codeAbnormalClosure,
49     codeAbnormalClosure,
50     codeAbnormalClosure,
51     0,
52     10,
53     65535,
54 ];
55 var expectedReason = [
56     "''",
57     "''",
58     "'ok'",
59     "''",
60     "''",
61     "''",
62     "'good bye'",
63     "'good bye'",
64     "'good bye'",
65 ];
66 var expectedWasClean = [
67     true,
68     false,
69     true,
70     false,
71     false,
72     false,
73     true,
74     true,
75     true,
76 ];
77
78 WebSocketTest = function()
79 {
80     this.ws = new WebSocket("ws://127.0.0.1:8880/close-code-and-reason");
81     this.ws.onopen = this.onopen;
82     this.ws.onmessage = this.onmessage;
83     this.ws.onclose = this.onclose.bind(this);
84     this.timeoutID = setTimeout(this.ontimeout.bind(this), 400);
85 };
86
87 WebSocketTest.prototype.onopen = function()
88 {
89     debug("WebSocketTest.onopen() was called with testId = " + testId + ".");
90     this.send(sendData[testId]);
91 };
92
93 WebSocketTest.prototype.onmessage = function(event)
94 {
95     testFailed("WebSocketTest.onmessage() was called. (message = \"" + event.data + "\")");
96 };
97
98 WebSocketTest.prototype.onclose = function(event)
99 {
100     closeEvent = event;
101     debug("WebSocketTest.onclose() was called with testId = " + testId + ".");
102
103     shouldEvaluateTo("closeEvent.wasClean", expectedWasClean[testId]);
104     shouldEvaluateTo("closeEvent.code", expectedCode[testId]);
105     shouldEvaluateTo("closeEvent.reason", expectedReason[testId]);
106
107     // Test that the attributes of the CloseEvent are readonly.
108     closeEvent.code = 0;
109     closeEvent.reason = "readonly";
110     closeEvent.wasClean = !closeEvent.wasClean;
111     shouldEvaluateTo("closeEvent.wasClean", expectedWasClean[testId]);
112     shouldEvaluateTo("closeEvent.code", expectedCode[testId]);
113     shouldEvaluateTo("closeEvent.reason", expectedReason[testId]);
114
115     clearTimeout(this.timeoutID);
116     this.ws = null;
117     testId++;
118     if (testId < testNum)
119         test = new WebSocketTest();
120     else
121         finishJSTest();
122 };
123
124 WebSocketTest.prototype.ontimeout = function()
125 {
126     testFailed("WebSocketTest.ontimeout() was called. (testId = " + testId + ")");
127     // Ensure that none of the WebSocket handlers run after finishJSTest().
128     var ignoreEvent = function(event) {};
129     this.ws.onopen = ignoreEvent;
130     this.ws.onmessage = ignoreEvent;
131     this.ws.onclose = ignoreEvent;
132     this.ws = null;
133
134     finishJSTest();
135 };
136
137 function testCloseCodeAndReason()
138 {
139     closeDuringOpen();
140     test = new WebSocketTest();
141 }