summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/FileAPI/BlobURL/test1-manual.html
blob: 8da42cf647ad6832e47071b693f80278bcc1302d (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Blob and File reference URL Test(1)</title>
    <link rel=help href="http://dev.w3.org/2006/webapi/FileAPI/#convenienceAPI">
    <link rel=author title="Breezewish" href="mailto:me@breeswish.org">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <form name="upload">
        <input type="file" id="fileChooser">
    </form>

    <div>
        <p>Test steps:</p>
        <ol>
            <li>Download the <a href="support/file_test1.js">file</a>.</li>
            <li>Select the file in the file inputbox to run the test.</li>
        </ol>
    </div>

    <div id="log"></div>

    <script>

        var fileChooser = document.querySelector('#fileChooser');

        setup({explicit_done: true});
        setup({explicit_timeout: true});

        //Run the test when user selects a file

        on_event(fileChooser, 'change', function() {

            var testCount = 10000;

            test(function() {

                var list = [], file = fileChooser.files[0];

                for (var i = 0; i <= testCount; i++) {
                    list.push(window.URL.createObjectURL(file));
                }

                list.sort();

                for (var i = 0; i < testCount; i++) {
                    assert_not_equals(list[i], list[i+1], 'generated Blob URL should be unique');
                }

            }, 'Check whether generated Blob/File URL is unique (Notice: only generate for ' + testCount + ' times)');


            async_test(function(t) {

                var url = URL.createObjectURL(fileChooser.files[0]);
                var expected_file_content = "var test_result = 'test1_OK';";

                var xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.onreadystatechange = t.step_func(function() {
                    switch (xhr.readyState) {
                    case xhr.DONE:
                        assert_equals(xhr.status, 200, 'status code should be 200');
                        assert_equals(xhr.responseText, expected_file_content);
                        t.done();
                        return;
                    }
                });

                xhr.send();

            }, 'Check whether Blob/File URL could be used in XHR requests and could get expected data');

            async_test(function(t) {

                var url = URL.createObjectURL(fileChooser.files[0]);
                var expected_run_result = "test1_OK";

                //expected file content:
                //   var test_result = 'test1_OK';

                var e = document.createElement('script');
                e.setAttribute('type', 'text/javascript');
                e.setAttribute('src', url);
                e.onload = t.step_func_done(function() {
                    assert_equals(test_result, expected_run_result);
                });

                document.body.appendChild(e);

            }, 'Check whether Blob/File URL could be used in tags src like <script>');

            async_test(function(t) {

                var url = URL.createObjectURL(fileChooser.files[0]);
                URL.revokeObjectURL(url);

                var xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.onreadystatechange = t.step_func(function() {
                    switch (xhr.readyState) {
                    case xhr.DONE:
                        assert_equals(xhr.status, 500, 'status code should be 500 if Blob URI is revoked.');
                        t.done();
                        return;
                    }
                });

                xhr.send();

            }, 'Check whether revokeObjectURL works well');

            done();

        });

    </script>
</body>
</html>