FileSystemSyncAccessHandle-read-write.https.worker.js (11509B)
1 importScripts("/resources/testharness.js"); 2 importScripts('resources/sync-access-handle-test.js'); 3 4 'use strict'; 5 6 sync_access_handle_test((t, handle) => { 7 const readBuffer = new Uint8Array(24); 8 const readBytes = handle.read(readBuffer, {at: 0}); 9 assert_equals(0, readBytes, 'Check that no bytes were read'); 10 }, 'Test reading an empty file through a sync access handle.'); 11 12 sync_access_handle_test((t, handle) => { 13 const readBuffer = new ArrayBuffer(0); 14 const readBytes = handle.read(readBuffer, {at: 0}); 15 assert_equals(0, readBytes, 'Check that no bytes were read'); 16 }, 'Test using an empty ArrayBuffer.'); 17 18 sync_access_handle_test((t, handle) => { 19 const readBuffer = new ArrayBuffer(24); 20 const readBytes = handle.read(readBuffer, {at: 0}); 21 assert_equals(0, readBytes, 'Check that no bytes were read'); 22 }, 'Test using an ArrayBuffer.'); 23 24 sync_access_handle_test((t, handle) => { 25 if (!('TextEncoder' in self)) { 26 return; 27 } 28 29 const decoder = new TextDecoder(); 30 31 const text = 'Hello Storage Foundation'; 32 const writeBuffer = new TextEncoder().encode(text); 33 const writtenBytes = handle.write(writeBuffer, {at: 0}); 34 assert_equals( 35 writeBuffer.byteLength, writtenBytes, 36 'Check that all bytes were written.'); 37 let readBuffer = new Uint8Array(writtenBytes); 38 let readBytes = handle.read(readBuffer, {at: 0}); 39 assert_equals(writtenBytes, readBytes, 'Check that all bytes were read'); 40 assert_equals( 41 text, decoder.decode(readBuffer), 42 'Check that the written bytes and the read bytes match'); 43 44 // Test a read of less bytes than available. 45 const expected = 'Storage'; 46 readBuffer = new Uint8Array(expected.length); 47 readBytes = handle.read(readBuffer, {at: text.indexOf(expected)}); 48 assert_equals(readBuffer.length, readBytes, 'Check that all bytes were read'); 49 const actual = decoder.decode(readBuffer); 50 assert_equals( 51 expected, actual, 52 'Partial read returned unexpected contents'); 53 }, 'Test writing and reading through a sync access handle.'); 54 55 sync_access_handle_test((t, handle) => { 56 if (!('TextEncoder' in self)) { 57 return; 58 } 59 60 const encoder = new TextEncoder(); 61 const decoder = new TextDecoder(); 62 63 for (text of ['Hello', 'Longer Text']) { 64 const writeBuffer = encoder.encode(text); 65 const writtenBytes = handle.write(writeBuffer, {at: 0}); 66 assert_equals( 67 writeBuffer.byteLength, writtenBytes, 68 'Check that all bytes were written.'); 69 const readBuffer = new Uint8Array(writtenBytes); 70 const readBytes = handle.read(readBuffer, {at: 0}); 71 assert_equals(writtenBytes, readBytes, 'Check that all bytes were read'); 72 assert_equals( 73 text, decoder.decode(readBuffer), 74 'Check that the written bytes and the read bytes match'); 75 } 76 }, 'Test second write that is bigger than the first write'); 77 78 sync_access_handle_test((t, handle) => { 79 if (!('TextEncoder' in self)) { 80 return; 81 } 82 83 const encoder = new TextEncoder(); 84 const decoder = new TextDecoder(); 85 86 for (tuple 87 of [{input: 'Hello World', expected: 'Hello World'}, 88 {input: 'foobar', expected: 'foobarWorld'}]) { 89 const text = tuple.input; 90 const expected = tuple.expected; 91 const writeBuffer = encoder.encode(text); 92 const writtenBytes = handle.write(writeBuffer, {at: 0}); 93 assert_equals( 94 writeBuffer.byteLength, writtenBytes, 95 'Check that all bytes were written.'); 96 const readBuffer = new Uint8Array(expected.length); 97 const readBytes = handle.read(readBuffer, {at: 0}); 98 assert_equals(expected.length, readBytes, 'Check that all bytes were read'); 99 assert_equals( 100 expected, decoder.decode(readBuffer), 101 'Check that the written bytes and the read bytes match'); 102 } 103 }, 'Test second write that is smaller than the first write'); 104 105 sync_access_handle_test((t, handle) => { 106 const expected = 17; 107 const writeBuffer = new Uint8Array(1); 108 writeBuffer[0] = expected; 109 const offset = 5; 110 const writtenBytes = handle.write(writeBuffer, {at: offset}); 111 assert_equals( 112 writeBuffer.byteLength, writtenBytes, 113 'Check that all bytes were written.'); 114 const fileLength = writeBuffer.byteLength + offset; 115 const readBuffer = new Uint8Array(fileLength); 116 const readBytes = handle.read(readBuffer, {at: 0}); 117 assert_equals(fileLength, readBytes, 'Check that all bytes were read'); 118 for (let i = 0; i < offset; ++i) { 119 assert_equals( 120 readBuffer[i], 0, 121 `Gaps in the file should be filled with 0, but got ${readBuffer[i]}.`); 122 } 123 124 assert_equals( 125 readBuffer[offset], expected, 126 'Gaps in the file should be filled with 0.'); 127 }, 'Test initial write with an offset'); 128 129 sync_access_handle_test((t, handle) => { 130 if (!('TextEncoder' in self)) { 131 return; 132 } 133 134 const encoder = new TextEncoder(); 135 const decoder = new TextDecoder(); 136 137 for (tuple 138 of [{input: 'Hello World', expected: 'Hello World', offset: 0}, 139 {input: 'foobar', expected: 'Hello foobar', offset: 6}]) { 140 const text = tuple.input; 141 const expected = tuple.expected; 142 const offset = tuple.offset; 143 const writeBuffer = encoder.encode(text); 144 const writtenBytes = handle.write(writeBuffer, {at: offset}); 145 assert_equals( 146 writeBuffer.byteLength, writtenBytes, 147 'Check that all bytes were written.'); 148 const readBuffer = new Uint8Array(expected.length); 149 const readBytes = handle.read(readBuffer, {at: 0}); 150 assert_equals(expected.length, readBytes, 'Check that all bytes were read'); 151 const actual = decoder.decode(readBuffer); 152 assert_equals( 153 expected, actual, 154 'Check content read from the handle'); 155 } 156 }, 'Test overwriting the file at an offset'); 157 158 sync_access_handle_test((t, handle) => { 159 if (!('TextEncoder' in self)) { 160 return; 161 } 162 163 const decoder = new TextDecoder(); 164 165 const text = 'Hello Storage Foundation'; 166 const writeBuffer = new TextEncoder().encode(text); 167 const writtenBytes = handle.write(writeBuffer, {at: 0}); 168 assert_equals( 169 writeBuffer.byteLength, writtenBytes, 170 'Check that all bytes were written.'); 171 const bufferLength = text.length; 172 for (tuple 173 of [{offset: 0, expected: text}, 174 {offset: 6, expected: text.substring(6)}]) { 175 const offset = tuple.offset; 176 const expected = tuple.expected; 177 178 const readBuffer = new Uint8Array(bufferLength); 179 const readBytes = handle.read(readBuffer, {at: offset}); 180 assert_equals(expected.length, readBytes, 'Check that all bytes were read'); 181 const actual = decoder.decode(readBuffer); 182 assert_true( 183 actual.startsWith(expected), 184 `Expected to read ${expected} but the actual value was ${actual}.`); 185 } 186 187 const readBuffer = new Uint8Array(bufferLength); 188 // Offset is greater than the file length. 189 const readBytes = handle.read(readBuffer, {at: bufferLength + 1}); 190 assert_equals(0, readBytes, 'Check that no bytes were read'); 191 for (let i = 0; i < readBuffer.byteLength; ++i) { 192 assert_equals(0, readBuffer[i], 'Check that the read buffer is unchanged.'); 193 } 194 }, 'Test read at an offset'); 195 196 sync_access_handle_test((t, handle) => { 197 if (!('TextEncoder' in self)) { 198 return; 199 } 200 201 const expected = 'Hello Storage Foundation'; 202 const writeBuffer = new TextEncoder().encode(expected); 203 const writtenBytes = handle.write(writeBuffer, {at: 0}); 204 assert_equals( 205 writeBuffer.byteLength, writtenBytes, 206 'Check that all bytes were written.'); 207 208 const bufferLength = expected.length; 209 const readBuffer = new Uint8Array(expected.length); 210 // No options parameter provided, should read at offset 0. 211 const readBytes = handle.read(readBuffer, {at: 0}); 212 assert_equals(expected.length, readBytes, 'Check that all bytes were read'); 213 const actual = new TextDecoder().decode(readBuffer); 214 assert_equals( 215 expected, actual, 216 `Expected to read ${expected} but the actual value was ${actual}.`); 217 }, 'Test read with default options'); 218 219 sync_access_handle_test((t, handle) => { 220 if (!('TextEncoder' in self)) { 221 return; 222 } 223 224 const expected = 'Hello Storage Foundation'; 225 const writeBuffer = new TextEncoder().encode(expected); 226 // No options parameter provided, should write at offset 0. 227 const writtenBytes = handle.write(writeBuffer); 228 assert_equals( 229 writeBuffer.byteLength, writtenBytes, 230 'Check that all bytes were written.'); 231 232 const bufferLength = expected.length; 233 const readBuffer = new Uint8Array(expected.length); 234 const readBytes = handle.read(readBuffer, {at: 0}); 235 assert_equals(expected.length, readBytes, 'Check that all bytes were read'); 236 const actual = new TextDecoder().decode(readBuffer); 237 assert_equals( 238 expected, actual, 239 `Expected to read ${expected} but the actual value was ${actual}.`); 240 }, 'Test write with default options'); 241 242 sync_access_handle_test((t, handle) => { 243 const readBuffer = new Uint8Array(24); 244 assert_throws_js(TypeError, () => handle.read(readBuffer, {at: -1})); 245 }, 'Test reading at a negative offset fails.'); 246 247 sync_access_handle_test((t, handle) => { 248 const text = 'foobar'; 249 const writeBuffer = new TextEncoder().encode(text); 250 assert_throws_js(TypeError, () => handle.write(writeBuffer, {at: -1})); 251 252 const readBuffer = new Uint8Array(24); 253 const readBytes = handle.read(readBuffer, {at: 0}); 254 255 assert_equals(0, readBytes, 'Check that no bytes were written'); 256 }, 'Test writing at a negative offset fails.'); 257 258 sync_access_handle_test((t, handle) => { 259 if (!('TextEncoder' in self)) { 260 return; 261 } 262 263 const encoder = new TextEncoder(); 264 const decoder = new TextDecoder(); 265 266 let writeBuffer = encoder.encode("Hello "); 267 let writtenBytes = handle.write(writeBuffer); 268 writeBuffer = encoder.encode("World"); 269 writtenBytes += handle.write(writeBuffer); 270 let readBuffer = new Uint8Array(256); 271 let readBytes = handle.read(readBuffer, {at: 0}); 272 assert_equals(readBytes, "Hello World".length, 'Check that all bytes were read'); 273 let actual = decoder.decode(readBuffer).substring(0, readBytes); 274 assert_equals( 275 actual, "Hello World", 276 'Check content read from the handle'); 277 278 readBuffer = new Uint8Array(5); 279 readBytes = handle.read(readBuffer, {at: 0}); 280 assert_equals(readBytes, 5, 'Check that all bytes were read'); 281 actual = decoder.decode(readBuffer).substring(0, readBytes); 282 assert_equals( 283 actual, "Hello", 284 'Check content read from the handle'); 285 286 readBuffer = new Uint8Array(256); 287 readBytes = handle.read(readBuffer); 288 assert_equals(readBytes, "Hello World".length - 5, 'Check that all bytes were read'); 289 actual = decoder.decode(readBuffer).substring(0, readBytes); 290 assert_equals( 291 actual, " World", 292 'Check content read from the handle'); 293 294 readBuffer = new Uint8Array(5); 295 readBytes = handle.read(readBuffer, {at: 0}); 296 assert_equals(readBytes, 5, 'Check that all bytes were read'); 297 actual = decoder.decode(readBuffer); 298 assert_equals( 299 actual, "Hello", 300 'Check content read from the handle'); 301 writeBuffer = encoder.encode(" X"); 302 writtenBytes = handle.write(writeBuffer); 303 assert_equals(writtenBytes, 2, 'Check overwrite length'); 304 305 readBuffer = new Uint8Array(256); 306 readBytes = handle.read(readBuffer, {at: 0}); 307 assert_equals(readBytes, "Hello Xorld".length, 'Check that all bytes were read'); 308 actual = decoder.decode(readBuffer).substring(0, readBytes); 309 assert_equals( 310 actual, "Hello Xorld", 311 'Check content read from the handle'); 312 }, 'Test reading and writing a file using the cursor'); 313 314 done();