summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/files/blob-close-read.html
blob: bd17f0c584b3294efa359276bcce09f1e46146e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<script src="../../resources/js-test.js"></script>
<script src="resources/read-common.js"></script>
<script>
description("Test the Blob.close() method, reading.");

window.jsTestIsAsync = true;

var blobContents = ['0123456789abcdef'];

var blob;
var sliced;
var reader;
var reader2;
var result;

function testReadAfterClose()
{
    debug("Testing that the reading of closed Blobs fail.");
    blob = new Blob(blobContents);
    shouldBe("blob.close(); blob.size", "0");
    reader = new FileReader();
    shouldThrow("reader.readAsArrayBuffer(blob)");
    shouldThrow("reader.readAsBinaryString(blob)");
    shouldThrow("reader.readAsText(blob)");
    shouldThrow("reader.readAsDataURL(blob)");
    runNextTest();
}

function testSlicedReadAfterClose()
{
    debug("Testing that sliced reads aren't affected by close() on 'parent' Blob.");
    blob = new Blob(blobContents);
    sliced = blob.slice(2);
    shouldBe("sliced.size", "14");
    blob.close();
    var reader = new FileReader();
    reader.onload = function(event) {
        result = event.target.result;
        shouldBeEqualToString("result", blobContents[0].slice(2));
    }
    reader.onloadend = function() {
        testPassed("readAsText() completed");
        shouldBe("sliced.size", "14");
        runNextTest();
    }
    reader.onerror = function(event) {
        testFailed("Received error event: " + event.target.error.code);
    };
    reader.readAsText(sliced);
}

function testContinuedReadAfterClose()
{
    debug("Testing that ongoing async reads aren't interrupted by close()");
    blob = new Blob(blobContents);
    var reader = new FileReader();
    reader.onloadstart = function(event) {
        // Close the Blob being read.
        blob.close();
        reader2 = new FileReader();
        shouldThrow("reader2.readAsArrayBuffer(blob)");
        shouldBe("blob.size", "0");
    }
    reader.onload = function(event) {
        testPassed("FileReader loaded: " + event.target.result);
    }
    reader.onloadend = function() {
        testPassed("readAsText() completed");
        shouldBe("blob.size", "0");
        runNextTest();
    }
    reader.onerror = function(event) {
        testFailed("Received error event: " + event.target.error.code);
        runNextTest();
    };
    reader.readAsText(blob);
}

var tests = [
    testReadAfterClose,
    testContinuedReadAfterClose,
    testSlicedReadAfterClose ];

function runNextTest()
{
    if (!tests.length) {
        finishJSTest();
        return;
    }
    tests.shift()();
}

runNextTest();
</script>