3 <script src="../js/resources/js-test-pre.js"></script>
6 <div id="description"></div>
7 <div id="console"></div>
10 description("Tests XMLHttpRequest 'arraybuffer' loading with the .responseType and .response attributes.");
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)
19 lastState = xhr.readyState;
21 if (xhr.readyState == XMLHttpRequest.DONE) {
22 // Check that .response exists when .readyState is DONE
24 testPassed("'arraybuffer' .response exists when .readyState is " + xhr.readyState + ".");
26 testFailed("'arraybuffer' .response should exist when .readyState is " + xhr.readyState + ".");
28 // Otherwise, for 'arraybuffer' the .response should not yet exist.
30 testPassed("'arraybuffer' .response does not exist when .readyState is " + xhr.readyState + ".");
32 testFailed("'arraybuffer' .response should not exist when .readyState is " + xhr.readyState + ".");
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) + ' ';
45 testPassed('DONE LOADING');
46 testPassed('received response object : ' + xhr.response + ".");
48 // Make sure exception is thrown if responseType is set too late in the loading process.
50 xhr.responseType = "text";
52 testPassed("exception correctly thrown when xhr.responseType is set to valid value too late in the loading process : " + e + ".");
55 var buffer = xhr.response;
57 // Interpret the ArrayBuffer as Uint8Array.
58 var buffer8 = new Uint8Array(buffer);
61 var totalLength = buffer8.length;
62 debug('response length : ' + totalLength + ".");
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);
69 // Calculate checksum.
71 for (var i = 0; i < totalLength; ++i) {
75 debug('checksum : ' + sum);
77 // Check that xhr.responseText throws an exception:
79 var x = xhr.responseText;
81 testPassed("exception correctly thrown when xhr.responseText is accessed but responseType is 'arraybuffer' : " + e + ".");
84 // Check that xhr.responseXML throws an exception:
86 var x = xhr.responseXML;
88 testPassed("exception correctly thrown when xhr.responseXML is accessed but responseType is 'arraybuffer' : " + e + ".");
91 // Test .response garbage collection.
92 xhr.response.foo = "bar";
94 shouldBe("xhr.response.foo", "'bar'");
101 if (window.layoutTestController) {
102 layoutTestController.dumpAsText();
103 layoutTestController.waitUntilDone();
106 xhr = new XMLHttpRequest();
107 xhr.onreadystatechange = stateChange;
109 xhr.open("GET", "../../http/tests/resources/balls-of-the-orient.aif", true);
112 if ("responseType" in xhr)
113 testPassed("responseType property exists.");
115 if ("response" in xhr)
116 testPassed("response property exists.");
118 if (xhr.responseType == "")
119 testPassed("xhr.responseType is initially set to default value of empty string.");
121 // Make sure we can set responseType to valid values before send() is called.
123 xhr.responseType = "";
124 if (xhr.responseType == "")
125 testPassed("xhr.responseType has been correctly set to ''.");
127 xhr.responseType = "text";
128 if (xhr.responseType == "text")
129 testPassed("xhr.responseType has been correctly set to 'text'.");
131 xhr.responseType = "document";
132 if (xhr.responseType == "document")
133 testPassed("xhr.responseType has been correctly set to 'document'.");
135 xhr.responseType = "arraybuffer";
136 if (xhr.responseType == "arraybuffer")
137 testPassed("xhr.responseType has been correctly set to 'arraybuffer'.");
139 testFailed("unable to set xhr.responseType to a valid value " + e + ".");
142 // Make sure exception is thrown if responseType is set to invalid value.
144 xhr.responseType = "dkjdfkjdfkj";
146 testPassed("exception correctly thrown when xhr.responseType is set to invalid value : " + e + ".");
149 testFailed("Caught exception " + e + ".");
153 window.jsTestIsAsync = true;
160 <script src="../js/resources/js-test-post.js"></script>