1 var kSerializedScriptValueVersion = 9;
3 function forVersion(version, values) {
5 var versionPrefix = [ (version << 8) | versionTag ];
7 return versionPrefix.concat(values)
10 function expectBufferValue(bytesPerElement, expectedValues, buffer) {
11 expectedBufferValues = expectedValues;
13 if (bytesPerElement == 1)
14 arrayClass = Uint8Array;
16 arrayClass = Uint16Array;
17 bufferView = new arrayClass(buffer);
18 shouldBe("bufferView.length", "expectedBufferValues.length");
19 var success = (bufferView.length == expectedBufferValues.length);
21 for (var i = 0; i < expectedValues.length; i++) {
22 if (expectedValues[i] != bufferView[i]) {
23 testFailed("ArrayBufferViews differ at index " + i + ". Should be " + expectedValues[i] + ". Was " + bufferView[i]);
31 // output the full buffer for adding back into the test
33 for (i = 0; i < bufferView.length; i++) {
34 var hexVal = bufferView[i].toString(16);
35 if (hexVal.length < bytesPerElement * 2) {
36 hexVal = "0000".slice(hexVal.length, bytesPerElement * 2) + hexVal;
38 output.push("0x" + hexVal);
40 debug("Actual buffer: [" + output.join(", ") + "]");
44 function makeBuffer(bytesPerElement, serializedValues) {
46 if (bytesPerElement == 1)
47 arrayClass = Uint8Array;
49 arrayClass = Uint16Array;
51 var bufferView = new arrayClass(new ArrayBuffer(serializedValues.length * bytesPerElement));
52 for (var i = 0; i < serializedValues.length; i++) {
53 bufferView[i] = serializedValues[i];
55 return bufferView.buffer;
58 function areValuesIdentical(a, b) {
59 function sortObject(object) {
60 if (typeof object != "object" || object === null)
62 return Object.keys(object).sort().map(function(key) {
63 return { key: key, value: sortObject(object[key]) };
66 var jsonA = JSON.stringify(sortObject(a));
67 var jsonB = JSON.stringify(sortObject(b));
69 debug("areValuesIdentical will fail");
70 debug("object A: " + jsonA);
71 debug("object B: " + jsonB);
73 return JSON.stringify(sortObject(a)) === JSON.stringify(sortObject(b));
76 function _testSerialization(bytesPerElement, obj, values, oldFormat, serializeExceptionValue) {
79 values = forVersion(kSerializedScriptValueVersion, values);
81 if (!serializeExceptionValue) {
83 debug("Deserialize to " + JSON.stringify(obj) + ":");
84 self.newObj = internals.deserializeBuffer(makeBuffer(bytesPerElement, values));
85 shouldBe("JSON.stringify(newObj)", "JSON.stringify(obj)");
86 shouldBeTrue("areValuesIdentical(newObj, obj)");
89 self.newObj = internals.deserializeBuffer(makeBuffer(bytesPerElement, oldFormat));
90 shouldBe("JSON.stringify(newObj)", "JSON.stringify(obj)");
91 shouldBeTrue("areValuesIdentical(newObj, obj)");
95 debug("Serialize " + JSON.stringify(obj) + ":");
97 var serialized = internals.serializeObject(obj);
98 if (serializeExceptionValue) {
99 testFailed("Should have thrown an exception of type ", serializeExceptionValue);
102 if (!serializeExceptionValue) {
103 testFailed("Threw exception " + e);
106 self.thrownException = e;
107 self.expectedException = serializeExceptionValue;
108 shouldBe("thrownException.code", "expectedException");
112 expectBufferValue(bytesPerElement, values, serialized);
115 function testBlobSerialization() {
117 self.blobObj1 = new Blob(['Hi'], {type: 'text/plain'});
118 self.blobObj2 = internals.deserializeBuffer(internals.serializeObject(blobObj1));
119 shouldBeTrue("areValuesIdentical(blobObj1, blobObj2)");
120 self.dictionaryWithBlob1 = {blob: blobObj1, string: 'stringValue'};
121 self.dictionaryWithBlob2 = internals.deserializeBuffer(internals.serializeObject(dictionaryWithBlob1));
122 shouldBeTrue("areValuesIdentical(dictionaryWithBlob1, dictionaryWithBlob2)");
124 // Compare contents too.
125 var xhr1 = new XMLHttpRequest();
126 xhr1.open("GET", URL.createObjectURL(self.blobObj1), false);
128 self.blobContent1 = xhr1.response;
129 var xhr2 = new XMLHttpRequest();
130 xhr2.open("GET", URL.createObjectURL(self.blobObj2), false);
132 self.blobContent2 = xhr2.response;
133 shouldBe("self.blobContent1", "self.blobContent2");
136 function _testFileSerialization(sourceFile, done) {
137 function checkIfSameContent(file1, file2, continuation) {
139 var fileReader = new FileReader();
140 fileReader.onload = function () {
141 self.fileContents1 = fileReader.result;
144 fileReader.onerror = function () {
145 testFailed("could not read file1. " + fileReader.error.message);
148 fileReader.readAsText(file1);
151 var fileReader = new FileReader();
152 fileReader.onload = function () {
153 self.fileContents2 = fileReader.result;
156 fileReader.onerror = function () {
157 testFailed("could not read file2. " + fileReader.error.message);
160 fileReader.readAsText(file2);
163 shouldBe("self.fileContents1", "self.fileContents2");
169 self.fileObj1 = sourceFile;
170 self.fileObj2 = internals.deserializeBuffer(internals.serializeObject(self.fileObj1));
171 shouldBeTrue("areValuesIdentical(fileObj1, fileObj2)");
172 self.dictionaryWithFile1 = {file: fileObj1, string: 'stringValue'};
173 self.dictionaryWithFile2 = internals.deserializeBuffer(internals.serializeObject(dictionaryWithFile1));
174 shouldBeTrue("areValuesIdentical(dictionaryWithFile1, dictionaryWithFile2)");
176 // Read and compare actual contents.
177 checkIfSameContent(self.fileObj1, self.fileObj2, done);
180 function testConstructedFileSerialization(done) {
182 debug("Files created using the File constructor");
184 self.sourceFile = new File(['The', ' contents'], 'testfile.txt', {type: 'text/plain', lastModified: 0});
185 _testFileSerialization(sourceFile, done);
188 function testUserSelectedFileSerialization(done) {
190 debug("Files selected by the user in an <input type='file'>");
192 var fileInput = document.getElementById("fileInput");
193 fileInput.addEventListener("change", function () {
194 self.sourceFile = fileInput.files.item(0);
195 _testFileSerialization(sourceFile, done);
198 eventSender.beginDragWithFiles(["resources/file.txt"]);
199 var centerX = fileInput.offsetLeft + fileInput.offsetWidth / 2;
200 var centerY = fileInput.offsetTop + fileInput.offsetHeight / 2;
201 eventSender.mouseMoveTo(centerX, centerY);
202 eventSender.mouseUp();
205 function testFileSerialization() {
206 window.jsTestIsAsync = true;
207 testConstructedFileSerialization(function () {
208 testUserSelectedFileSerialization(function () {