forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_fileapi_slice.html
137 lines (119 loc) · 4.57 KB
/
test_fileapi_slice.html
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE HTML>
<html>
<head>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=575946
-->
<title>Test for Bug 575946</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="fileutils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=575946">Mozilla Bug 575946</a>
<p id="display">
<canvas id=canvas width=1100 height=1100 hidden moz-opaque></canvas>
<canvas id=testcanvas hidden moz-opaque></canvas>
<input id="fileList" type="file"></input>
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var fileNum = 1;
SimpleTest.waitForExplicitFinish();
// Create files containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
// Create a decent-sized image
cx = $("canvas").getContext('2d');
var s = cx.canvas.width;
var grad = cx.createLinearGradient(0, 0, s-1, s-1);
for (i = 0; i < 0.95; i += .1) {
grad.addColorStop(i, "white");
grad.addColorStop(i + .05, "black");
}
grad.addColorStop(1, "white");
cx.fillStyle = grad;
cx.fillRect(0, 0, s-1, s-1);
cx.fillStyle = "rgba(200, 0, 0, 0.9)";
cx.fillRect(.1 * s, .1 * s, .7 * s, .7 * s);
cx.strokeStyle = "rgba(0, 0, 130, 0.5)";
cx.lineWidth = .14 * s;
cx.beginPath();
cx.arc(.6 * s, .6 * s, .3 * s, 0, Math.PI*2, true);
cx.stroke();
cx.closePath();
cx.fillStyle = "rgb(0, 255, 0)";
cx.beginPath();
cx.arc(.1 * s, .8 * s, .1 * s, 0, Math.PI*2, true);
cx.fill();
cx.closePath();
var fileData =
atob(cx.canvas.toDataURL("image/png").substring("data:text/png;base64,".length + 1));
var memFile = cx.canvas.mozGetAsFile("image/png");
var fileFile = createFileWithData(fileData);
var size = fileData.length;
// This might fail if we dramatically improve the png encoder. If that happens
// please increase the complexity or size of the image generated above to ensure
// that we're testing with files that are large enough.
ok(size > 65536, "test data sufficiently large");
// Test that basic properties work
testSlice(memFile, size, "image/png", fileData, "memFile");
testSlice(fileFile, size, "", fileData, "fileFile");
// Try loading directly from slice into an image
var testBinaryData = "";
for (var i = 0; i < 256; i++) {
testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < 20000) {
testBinaryData += testBinaryData;
}
function imageLoadHandler(event) {
var origcanvas = $("canvas");
var testcanvas = $("testcanvas");
var image = event.target;
is(image.naturalWidth, origcanvas.width, "width correct");
is(image.naturalHeight, origcanvas.height, "height correct");
testcanvas.width = origcanvas.width;
testcanvas.height = origcanvas.height;
testcanvas.getContext("2d").drawImage(image, 0, 0);
// Do not use |is(testcanvas.toDataURL("image/png"), origcanvas.toDataURL("image/png"), "...");| that results in a _very_ long line.
var origDataURL = origcanvas.toDataURL("image/png");
var testDataURL = testcanvas.toDataURL("image/png");
is(testDataURL.length, origDataURL.length,
"Length of correct image data");
ok(testDataURL == origDataURL,
"Content of correct image data");
testHasRun();
}
// image in the middle
var imgfile = createFileWithData(testBinaryData + fileData + testBinaryData);
is(imgfile.size, size + testBinaryData.length * 2, "correct file size (middle)");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image at start
var imgfile = createFileWithData(fileData + testBinaryData);
is(imgfile.size, size + testBinaryData.length, "correct file size (start)");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(0, size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image at end
var imgfile = createFileWithData(testBinaryData + fileData);
is(imgfile.size, size + testBinaryData.length, "correct file size (end)");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image past end
var imgfile = createFileWithData(testBinaryData + fileData);
is(imgfile.size, size + testBinaryData.length, "correct file size (past end)");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, testBinaryData.length + size + 1000));
img.onload = imageLoadHandler;
expectedTestCount++;
</script>
</pre>
</body> </html>