tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / fast / xmlhttprequest / xmlhttprequest-responsetype-arraybuffer.html
1 <html>
2 <head>
3 <script src="../js/resources/js-test-pre.js"></script>
4 </head>
5 <body>
6 <div id="description"></div>
7 <div id="console"></div>
8
9 <script>
10 description("Tests XMLHttpRequest 'arraybuffer' loading with the .responseType and .response attributes.");
11
12 var xhr = 0;
13 var lastState = 0;
14
15 function stateChange() {
16     // Protect against race-condition where .onreadystatechange sometimes will be called multiple times for the same state.
17     if (xhr.readyState == lastState)
18         return;
19     lastState = xhr.readyState;
20     
21     if (xhr.readyState == XMLHttpRequest.DONE) {
22         // Check that .response exists when .readyState is DONE
23         if (xhr.response)
24             testPassed("'arraybuffer' .response exists when .readyState is " + xhr.readyState + ".");
25         else
26             testFailed("'arraybuffer' .response should exist when .readyState is " + xhr.readyState + ".");
27     } else {
28         // Otherwise, for 'arraybuffer' the .response should not yet exist.
29         if (!xhr.response)
30             testPassed("'arraybuffer' .response does not exist when .readyState is " + xhr.readyState + ".");
31         else
32             testFailed("'arraybuffer' .response should not exist when .readyState is " + xhr.readyState + ".");
33     }
34 }
35
36 function logBytesAtOffset(buffer8, offset) {
37     var s = "bytes at offset " + offset + " : ";
38     for (var i = 0; i < 8; ++i)
39         s += buffer8[i + offset].toString(16) + ' ';
40     
41     debug(s);
42 }
43
44 function load() {
45     testPassed('DONE LOADING');
46     testPassed('received response object : ' + xhr.response + ".");
47
48     // Make sure exception is thrown if responseType is set too late in the loading process.
49     try {
50         xhr.responseType = "text";
51     } catch(e) {
52         testPassed("exception correctly thrown when xhr.responseType is set to valid value too late in the loading process : " + e + ".");
53     }
54
55     var buffer = xhr.response;
56
57     // Interpret the ArrayBuffer as Uint8Array.
58     var buffer8 = new Uint8Array(buffer);
59     buffer8.set(buffer);
60
61     var totalLength = buffer8.length;
62     debug('response length : ' + totalLength + ".");
63     
64     // Log the bytes at the start, in the middle, and near the end:
65     logBytesAtOffset(buffer8, 0);
66     logBytesAtOffset(buffer8, 0x5720A);
67     logBytesAtOffset(buffer8, 0xA39D6);
68     
69     // Calculate checksum.
70     var sum = 0;
71     for (var i = 0; i < totalLength; ++i) {
72         sum += buffer8[i];
73     }
74     
75     debug('checksum : ' + sum);
76
77     // Check that xhr.responseText throws an exception:
78     try {
79         var x = xhr.responseText;
80     } catch(e) {
81         testPassed("exception correctly thrown when xhr.responseText is accessed but responseType is 'arraybuffer' : " + e + ".");
82     }
83
84     // Check that xhr.responseXML throws an exception:
85     try {
86         var x = xhr.responseXML;
87     } catch(e) {
88         testPassed("exception correctly thrown when xhr.responseXML is accessed but responseType is 'arraybuffer' : " + e + ".");
89     }
90     
91     // Test .response garbage collection.
92     xhr.response.foo = "bar";
93     gc();
94     shouldBe("xhr.response.foo", "'bar'");    
95
96     xhr = null;
97     finishJSTest();
98 }
99
100 function runTest() {
101     if (window.layoutTestController) {
102         layoutTestController.dumpAsText();
103         layoutTestController.waitUntilDone();
104     }
105
106     xhr = new XMLHttpRequest();
107     xhr.onreadystatechange = stateChange;
108     xhr.onload = load;
109     xhr.open("GET", "../../http/tests/resources/balls-of-the-orient.aif", true);
110         
111     try {
112         if ("responseType" in xhr)
113             testPassed("responseType property exists.");
114
115         if ("response" in xhr)
116             testPassed("response property exists.");
117             
118         if (xhr.responseType == "")
119             testPassed("xhr.responseType is initially set to default value of empty string.");
120
121         // Make sure we can set responseType to valid values before send() is called.
122         try {
123             xhr.responseType = "";
124             if (xhr.responseType == "")
125                 testPassed("xhr.responseType has been correctly set to ''.");
126
127             xhr.responseType = "text";
128             if (xhr.responseType == "text")
129                 testPassed("xhr.responseType has been correctly set to 'text'.");
130
131             xhr.responseType = "document";
132             if (xhr.responseType == "document")
133                 testPassed("xhr.responseType has been correctly set to 'document'.");
134
135             xhr.responseType = "arraybuffer";
136             if (xhr.responseType == "arraybuffer")
137                 testPassed("xhr.responseType has been correctly set to 'arraybuffer'.");
138         } catch(e) {
139             testFailed("unable to set xhr.responseType to a valid value " + e + ".");
140         }
141
142         // Make sure exception is thrown if responseType is set to invalid value.
143         try {
144             xhr.responseType = "dkjdfkjdfkj";
145         } catch(e) {
146             testPassed("exception correctly thrown when xhr.responseType is set to invalid value : " + e + ".");
147         }
148     } catch(e) {
149         testFailed("Caught exception " + e + ".");
150     }
151
152     xhr.send(null);
153     window.jsTestIsAsync = true;
154 }
155
156 runTest();
157
158 </script>
159
160 <script src="../js/resources/js-test-post.js"></script>
161 </body>
162 </html>