| OLD | NEW |
| (Empty) |
| 1 var kSerializedScriptValueVersion = 9; | |
| 2 | |
| 3 function forVersion(version, values) { | |
| 4 var versionTag = 0xff; | |
| 5 var versionPrefix = [ (version << 8) | versionTag ]; | |
| 6 | |
| 7 return versionPrefix.concat(values) | |
| 8 } | |
| 9 | |
| 10 function expectBufferValue(bytesPerElement, expectedValues, buffer) { | |
| 11 expectedBufferValues = expectedValues; | |
| 12 var arrayClass; | |
| 13 if (bytesPerElement == 1) | |
| 14 arrayClass = Uint8Array; | |
| 15 else | |
| 16 arrayClass = Uint16Array; | |
| 17 bufferView = new arrayClass(buffer); | |
| 18 shouldBe("bufferView.length", "expectedBufferValues.length"); | |
| 19 var success = (bufferView.length == expectedBufferValues.length); | |
| 20 if (success) { | |
| 21 for (var i = 0; i < expectedValues.length; i++) { | |
| 22 if (expectedValues[i] != bufferView[i]) { | |
| 23 testFailed("ArrayBufferViews differ at index " + i + ". Should b
e " + expectedValues[i] + ". Was " + bufferView[i]); | |
| 24 success = false; | |
| 25 break; | |
| 26 } | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 if (!success) { | |
| 31 // output the full buffer for adding back into the test | |
| 32 var output = []; | |
| 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) + hexV
al; | |
| 37 } | |
| 38 output.push("0x" + hexVal); | |
| 39 } | |
| 40 debug("Actual buffer: [" + output.join(", ") + "]"); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 function makeBuffer(bytesPerElement, serializedValues) { | |
| 45 var arrayClass; | |
| 46 if (bytesPerElement == 1) | |
| 47 arrayClass = Uint8Array; | |
| 48 else | |
| 49 arrayClass = Uint16Array; | |
| 50 | |
| 51 var bufferView = new arrayClass(new ArrayBuffer(serializedValues.length * by
tesPerElement)); | |
| 52 for (var i = 0; i < serializedValues.length; i++) { | |
| 53 bufferView[i] = serializedValues[i]; | |
| 54 } | |
| 55 return bufferView.buffer; | |
| 56 } | |
| 57 | |
| 58 function areValuesIdentical(a, b) { | |
| 59 function sortObject(object) { | |
| 60 if (typeof object != "object" || object === null) | |
| 61 return object; | |
| 62 return Object.keys(object).sort().map(function(key) { | |
| 63 return { key: key, value: sortObject(object[key]) }; | |
| 64 }); | |
| 65 } | |
| 66 var jsonA = JSON.stringify(sortObject(a)); | |
| 67 var jsonB = JSON.stringify(sortObject(b)); | |
| 68 if (jsonA != jsonB) { | |
| 69 debug("areValuesIdentical will fail"); | |
| 70 debug("object A: " + jsonA); | |
| 71 debug("object B: " + jsonB); | |
| 72 } | |
| 73 return JSON.stringify(sortObject(a)) === JSON.stringify(sortObject(b)); | |
| 74 } | |
| 75 | |
| 76 function _testSerialization(bytesPerElement, obj, values, oldFormat, serializeEx
ceptionValue) { | |
| 77 debug(""); | |
| 78 | |
| 79 values = forVersion(kSerializedScriptValueVersion, values); | |
| 80 | |
| 81 if (!serializeExceptionValue) { | |
| 82 self.obj = obj; | |
| 83 debug("Deserialize to " + JSON.stringify(obj) + ":"); | |
| 84 self.newObj = internals.deserializeBuffer(makeBuffer(bytesPerElement, va
lues)); | |
| 85 shouldBe("JSON.stringify(newObj)", "JSON.stringify(obj)"); | |
| 86 shouldBeTrue("areValuesIdentical(newObj, obj)"); | |
| 87 | |
| 88 if (oldFormat) { | |
| 89 self.newObj = internals.deserializeBuffer(makeBuffer(bytesPerElement
, oldFormat)); | |
| 90 shouldBe("JSON.stringify(newObj)", "JSON.stringify(obj)"); | |
| 91 shouldBeTrue("areValuesIdentical(newObj, obj)"); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 debug("Serialize " + JSON.stringify(obj) + ":"); | |
| 96 try { | |
| 97 var serialized = internals.serializeObject(obj); | |
| 98 if (serializeExceptionValue) { | |
| 99 testFailed("Should have thrown an exception of type ", serializeExce
ptionValue); | |
| 100 } | |
| 101 } catch(e) { | |
| 102 if (!serializeExceptionValue) { | |
| 103 testFailed("Threw exception " + e); | |
| 104 return; | |
| 105 } else { | |
| 106 self.thrownException = e; | |
| 107 self.expectedException = serializeExceptionValue; | |
| 108 shouldBe("thrownException.code", "expectedException"); | |
| 109 return; | |
| 110 } | |
| 111 } | |
| 112 expectBufferValue(bytesPerElement, values, serialized); | |
| 113 } | |
| 114 | |
| 115 function testBlobSerialization() { | |
| 116 debug(""); | |
| 117 self.blobObj1 = new Blob(['Hi'], {type: 'text/plain'}); | |
| 118 self.blobObj2 = internals.deserializeBuffer(internals.serializeObject(blobOb
j1)); | |
| 119 shouldBeTrue("areValuesIdentical(blobObj1, blobObj2)"); | |
| 120 self.dictionaryWithBlob1 = {blob: blobObj1, string: 'stringValue'}; | |
| 121 self.dictionaryWithBlob2 = internals.deserializeBuffer(internals.serializeOb
ject(dictionaryWithBlob1)); | |
| 122 shouldBeTrue("areValuesIdentical(dictionaryWithBlob1, dictionaryWithBlob2)")
; | |
| 123 | |
| 124 // Compare contents too. | |
| 125 var xhr1 = new XMLHttpRequest(); | |
| 126 xhr1.open("GET", URL.createObjectURL(self.blobObj1), false); | |
| 127 xhr1.send(); | |
| 128 self.blobContent1 = xhr1.response; | |
| 129 var xhr2 = new XMLHttpRequest(); | |
| 130 xhr2.open("GET", URL.createObjectURL(self.blobObj2), false); | |
| 131 xhr2.send(); | |
| 132 self.blobContent2 = xhr2.response; | |
| 133 shouldBe("self.blobContent1", "self.blobContent2"); | |
| 134 } | |
| 135 | |
| 136 function _testFileSerialization(sourceFile, done) { | |
| 137 function checkIfSameContent(file1, file2, continuation) { | |
| 138 function step1() { | |
| 139 var fileReader = new FileReader(); | |
| 140 fileReader.onload = function () { | |
| 141 self.fileContents1 = fileReader.result; | |
| 142 step2(); | |
| 143 }; | |
| 144 fileReader.onerror = function () { | |
| 145 testFailed("could not read file1. " + fileReader.error.message); | |
| 146 done(); | |
| 147 } | |
| 148 fileReader.readAsText(file1); | |
| 149 } | |
| 150 function step2() { | |
| 151 var fileReader = new FileReader(); | |
| 152 fileReader.onload = function () { | |
| 153 self.fileContents2 = fileReader.result; | |
| 154 finish(); | |
| 155 }; | |
| 156 fileReader.onerror = function () { | |
| 157 testFailed("could not read file2. " + fileReader.error.message); | |
| 158 done(); | |
| 159 } | |
| 160 fileReader.readAsText(file2); | |
| 161 } | |
| 162 function finish() { | |
| 163 shouldBe("self.fileContents1", "self.fileContents2"); | |
| 164 continuation(); | |
| 165 } | |
| 166 step1(); | |
| 167 } | |
| 168 | |
| 169 self.fileObj1 = sourceFile; | |
| 170 self.fileObj2 = internals.deserializeBuffer(internals.serializeObject(self.f
ileObj1)); | |
| 171 shouldBeTrue("areValuesIdentical(fileObj1, fileObj2)"); | |
| 172 self.dictionaryWithFile1 = {file: fileObj1, string: 'stringValue'}; | |
| 173 self.dictionaryWithFile2 = internals.deserializeBuffer(internals.serializeOb
ject(dictionaryWithFile1)); | |
| 174 shouldBeTrue("areValuesIdentical(dictionaryWithFile1, dictionaryWithFile2)")
; | |
| 175 | |
| 176 // Read and compare actual contents. | |
| 177 checkIfSameContent(self.fileObj1, self.fileObj2, done); | |
| 178 } | |
| 179 | |
| 180 function testConstructedFileSerialization(done) { | |
| 181 debug(""); | |
| 182 debug("Files created using the File constructor"); | |
| 183 | |
| 184 self.sourceFile = new File(['The', ' contents'], 'testfile.txt', {type: 'tex
t/plain', lastModified: 0}); | |
| 185 _testFileSerialization(sourceFile, done); | |
| 186 } | |
| 187 | |
| 188 function testUserSelectedFileSerialization(done) { | |
| 189 debug(""); | |
| 190 debug("Files selected by the user in an <input type='file'>"); | |
| 191 | |
| 192 var fileInput = document.getElementById("fileInput"); | |
| 193 fileInput.addEventListener("change", function () { | |
| 194 self.sourceFile = fileInput.files.item(0); | |
| 195 _testFileSerialization(sourceFile, done); | |
| 196 }); | |
| 197 | |
| 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(); | |
| 203 } | |
| 204 | |
| 205 function testFileSerialization() { | |
| 206 window.jsTestIsAsync = true; | |
| 207 testConstructedFileSerialization(function () { | |
| 208 testUserSelectedFileSerialization(function () { | |
| 209 finishJSTest(); | |
| 210 }); | |
| 211 }); | |
| 212 } | |
| OLD | NEW |