filereader_result.any.js (3156B)
1 // META: title=FileAPI Test: filereader_result 2 3 var blob, blob2; 4 setup(function() { 5 blob = new Blob(["This test the result attribute"]); 6 blob2 = new Blob(["This is a second blob"]); 7 }); 8 9 async_test(function() { 10 var readText = new FileReader(); 11 assert_equals(readText.result, null); 12 13 readText.onloadend = this.step_func(function(evt) { 14 assert_equals(typeof readText.result, "string", "The result type is string"); 15 assert_equals(readText.result, "This test the result attribute", "The result is correct"); 16 this.done(); 17 }); 18 19 readText.readAsText(blob); 20 }, "readAsText"); 21 22 async_test(function() { 23 var readDataURL = new FileReader(); 24 assert_equals(readDataURL.result, null); 25 26 readDataURL.onloadend = this.step_func(function(evt) { 27 assert_equals(typeof readDataURL.result, "string", "The result type is string"); 28 assert_true(readDataURL.result.indexOf("VGhpcyB0ZXN0IHRoZSByZXN1bHQgYXR0cmlidXRl") != -1, "return the right base64 string"); 29 this.done(); 30 }); 31 32 readDataURL.readAsDataURL(blob); 33 }, "readAsDataURL"); 34 35 async_test(function() { 36 var readArrayBuffer = new FileReader(); 37 assert_equals(readArrayBuffer.result, null); 38 39 readArrayBuffer.onloadend = this.step_func(function(evt) { 40 assert_true(readArrayBuffer.result instanceof ArrayBuffer, "The result is instanceof ArrayBuffer"); 41 this.done(); 42 }); 43 44 readArrayBuffer.readAsArrayBuffer(blob); 45 }, "readAsArrayBuffer"); 46 47 async_test(function() { 48 var readBinaryString = new FileReader(); 49 assert_equals(readBinaryString.result, null); 50 51 readBinaryString.onloadend = this.step_func(function(evt) { 52 assert_equals(typeof readBinaryString.result, "string", "The result type is string"); 53 assert_equals(readBinaryString.result, "This test the result attribute", "The result is correct"); 54 this.done(); 55 }); 56 57 readBinaryString.readAsBinaryString(blob); 58 }, "readAsBinaryString"); 59 60 61 for (let event of ['loadstart', 'progress']) { 62 for (let method of ['readAsText', 'readAsDataURL', 'readAsArrayBuffer', 'readAsBinaryString']) { 63 promise_test(async function(t) { 64 var reader = new FileReader(); 65 assert_equals(reader.result, null, 'result is null before read'); 66 67 var eventWatcher = new EventWatcher(t, reader, 68 [event, 'loadend']); 69 70 reader[method](blob); 71 assert_equals(reader.result, null, 'result is null after first read call'); 72 await eventWatcher.wait_for(event); 73 assert_equals(reader.result, null, 'result is null during event'); 74 await eventWatcher.wait_for('loadend'); 75 assert_not_equals(reader.result, null); 76 reader[method](blob); 77 assert_equals(reader.result, null, 'result is null after second read call'); 78 await eventWatcher.wait_for(event); 79 assert_equals(reader.result, null, 'result is null during second read event'); 80 }, 'result is null during "' + event + '" event for ' + method); 81 } 82 }