1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/image/test/unit/test_imgtools.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,735 @@ 1.4 +/* 1.5 + * Tests for imgITools 1.6 + */ 1.7 + 1.8 +const Ci = Components.interfaces; 1.9 +const Cc = Components.classes; 1.10 + 1.11 + 1.12 +/* 1.13 + * dumpToFile() 1.14 + * 1.15 + * For test development, dumps the specified array to a file. 1.16 + * Call |dumpToFile(outData);| in a test to file to a file. 1.17 + */ 1.18 +function dumpToFile(aData) { 1.19 + var outputFile = do_get_tempdir(); 1.20 + outputFile.append("testdump.png"); 1.21 + 1.22 + var outputStream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.23 + createInstance(Ci.nsIFileOutputStream); 1.24 + // WR_ONLY|CREAT|TRUNC 1.25 + outputStream.init(outputFile, 0x02 | 0x08 | 0x20, 0644, null); 1.26 + 1.27 + var bos = Cc["@mozilla.org/binaryoutputstream;1"]. 1.28 + createInstance(Ci.nsIBinaryOutputStream); 1.29 + bos.setOutputStream(outputStream); 1.30 + 1.31 + bos.writeByteArray(aData, aData.length); 1.32 + 1.33 + outputStream.close(); 1.34 +} 1.35 + 1.36 + 1.37 +/* 1.38 + * getFileInputStream() 1.39 + * 1.40 + * Returns an input stream for the specified file. 1.41 + */ 1.42 +function getFileInputStream(aFile) { 1.43 + var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]. 1.44 + createInstance(Ci.nsIFileInputStream); 1.45 + // init the stream as RD_ONLY, -1 == default permissions. 1.46 + inputStream.init(aFile, 0x01, -1, null); 1.47 + 1.48 + // Blah. The image decoders use ReadSegments, which isn't implemented on 1.49 + // file input streams. Use a buffered stream to make it work. 1.50 + var bis = Cc["@mozilla.org/network/buffered-input-stream;1"]. 1.51 + createInstance(Ci.nsIBufferedInputStream); 1.52 + bis.init(inputStream, 1024); 1.53 + 1.54 + return bis; 1.55 +} 1.56 + 1.57 + 1.58 +/* 1.59 + * streamToArray() 1.60 + * 1.61 + * Consumes an input stream, and returns its bytes as an array. 1.62 + */ 1.63 +function streamToArray(aStream) { 1.64 + var size = aStream.available(); 1.65 + 1.66 + // use a binary input stream to grab the bytes. 1.67 + var bis = Cc["@mozilla.org/binaryinputstream;1"]. 1.68 + createInstance(Ci.nsIBinaryInputStream); 1.69 + bis.setInputStream(aStream); 1.70 + 1.71 + var bytes = bis.readByteArray(size); 1.72 + if (size != bytes.length) 1.73 + throw "Didn't read expected number of bytes"; 1.74 + 1.75 + return bytes; 1.76 +} 1.77 + 1.78 + 1.79 +/* 1.80 + * compareArrays 1.81 + * 1.82 + * Compares two arrays, and throws if there's a difference. 1.83 + */ 1.84 +function compareArrays(aArray1, aArray2) { 1.85 + do_check_eq(aArray1.length, aArray2.length); 1.86 + 1.87 + for (var i = 0; i < aArray1.length; i++) 1.88 + if (aArray1[i] != aArray2[i]) 1.89 + throw "arrays differ at index " + i; 1.90 +} 1.91 + 1.92 + 1.93 +/* 1.94 + * checkExpectedError 1.95 + * 1.96 + * Checks to see if a thrown error was expected or not, and if it 1.97 + * matches the expected value. 1.98 + */ 1.99 +function checkExpectedError (aExpectedError, aActualError) { 1.100 + if (aExpectedError) { 1.101 + if (!aActualError) 1.102 + throw "Didn't throw as expected (" + aExpectedError + ")"; 1.103 + 1.104 + if (!aExpectedError.test(aActualError)) 1.105 + throw "Threw (" + aActualError + "), not (" + aExpectedError; 1.106 + 1.107 + // We got the expected error, so make a note in the test log. 1.108 + dump("...that error was expected.\n\n"); 1.109 + } else if (aActualError) { 1.110 + throw "Threw unexpected error: " + aActualError; 1.111 + } 1.112 +} 1.113 + 1.114 + 1.115 +function run_test() { 1.116 + 1.117 +try { 1.118 + 1.119 + 1.120 +/* ========== 0 ========== */ 1.121 +var testnum = 0; 1.122 +var testdesc = "imgITools setup"; 1.123 +var err = null; 1.124 + 1.125 +var imgTools = Cc["@mozilla.org/image/tools;1"]. 1.126 + getService(Ci.imgITools); 1.127 + 1.128 +if (!imgTools) 1.129 + throw "Couldn't get imgITools service" 1.130 + 1.131 +// Ugh, this is an ugly hack. The pixel values we get in Windows are sometimes 1.132 +// +/- 1 value compared to other platforms, so we need to compare against a 1.133 +// different set of reference images. nsIXULRuntime.OS doesn't seem to be 1.134 +// available in xpcshell, so we'll use this as a kludgy way to figure out if 1.135 +// we're running on Windows. 1.136 +var isWindows = ("@mozilla.org/windows-registry-key;1" in Cc); 1.137 + 1.138 + 1.139 +/* ========== 1 ========== */ 1.140 +testnum++; 1.141 +testdesc = "test decoding a PNG"; 1.142 + 1.143 +// 64x64 png, 8415 bytes. 1.144 +var imgName = "image1.png"; 1.145 +var inMimeType = "image/png"; 1.146 +var imgFile = do_get_file(imgName); 1.147 + 1.148 +var istream = getFileInputStream(imgFile); 1.149 +do_check_eq(istream.available(), 8415); 1.150 + 1.151 +// Use decodeImageData for this test even though it's deprecated to ensure that 1.152 +// it correctly forwards to decodeImage and continues to work. 1.153 +var outParam = { value: null }; 1.154 +imgTools.decodeImageData(istream, inMimeType, outParam); 1.155 +var container = outParam.value; 1.156 + 1.157 +// It's not easy to look at the pixel values from JS, so just 1.158 +// check the container's size. 1.159 +do_check_eq(container.width, 64); 1.160 +do_check_eq(container.height, 64); 1.161 + 1.162 + 1.163 +/* ========== 2 ========== */ 1.164 +testnum++; 1.165 +testdesc = "test encoding a scaled JPEG"; 1.166 + 1.167 +// we'll reuse the container from the previous test 1.168 +istream = imgTools.encodeScaledImage(container, "image/jpeg", 16, 16); 1.169 + 1.170 +var encodedBytes = streamToArray(istream); 1.171 +// Get bytes for exected result 1.172 +var refName = "image1png16x16.jpg"; 1.173 +var refFile = do_get_file(refName); 1.174 +istream = getFileInputStream(refFile); 1.175 +do_check_eq(istream.available(), 1078); 1.176 +var referenceBytes = streamToArray(istream); 1.177 + 1.178 +// compare the encoder's output to the reference file. 1.179 +compareArrays(encodedBytes, referenceBytes); 1.180 + 1.181 + 1.182 +/* ========== 3 ========== */ 1.183 +testnum++; 1.184 +testdesc = "test encoding an unscaled JPEG"; 1.185 + 1.186 +// we'll reuse the container from the previous test 1.187 +istream = imgTools.encodeImage(container, "image/jpeg"); 1.188 +encodedBytes = streamToArray(istream); 1.189 + 1.190 +// Get bytes for exected result 1.191 +refName = "image1png64x64.jpg"; 1.192 +refFile = do_get_file(refName); 1.193 +istream = getFileInputStream(refFile); 1.194 +do_check_eq(istream.available(), 4503); 1.195 +referenceBytes = streamToArray(istream); 1.196 + 1.197 +// compare the encoder's output to the reference file. 1.198 +compareArrays(encodedBytes, referenceBytes); 1.199 + 1.200 + 1.201 +/* ========== 4 ========== */ 1.202 +testnum++; 1.203 +testdesc = "test decoding a JPEG"; 1.204 + 1.205 +// 32x32 jpeg, 3494 bytes. 1.206 +imgName = "image2.jpg"; 1.207 +inMimeType = "image/jpeg"; 1.208 +imgFile = do_get_file(imgName); 1.209 + 1.210 +istream = getFileInputStream(imgFile); 1.211 +do_check_eq(istream.available(), 3494); 1.212 + 1.213 +container = imgTools.decodeImage(istream, inMimeType); 1.214 + 1.215 +// It's not easy to look at the pixel values from JS, so just 1.216 +// check the container's size. 1.217 +do_check_eq(container.width, 32); 1.218 +do_check_eq(container.height, 32); 1.219 + 1.220 + 1.221 +/* ========== 5 ========== */ 1.222 +testnum++; 1.223 +testdesc = "test encoding a scaled PNG"; 1.224 + 1.225 +if (!isWindows) { 1.226 +// we'll reuse the container from the previous test 1.227 +istream = imgTools.encodeScaledImage(container, "image/png", 16, 16); 1.228 + 1.229 +encodedBytes = streamToArray(istream); 1.230 +// Get bytes for exected result 1.231 +refName = isWindows ? "image2jpg16x16-win.png" : "image2jpg16x16.png"; 1.232 +refFile = do_get_file(refName); 1.233 +istream = getFileInputStream(refFile); 1.234 +do_check_eq(istream.available(), 948); 1.235 +referenceBytes = streamToArray(istream); 1.236 + 1.237 +// compare the encoder's output to the reference file. 1.238 +compareArrays(encodedBytes, referenceBytes); 1.239 +} 1.240 + 1.241 + 1.242 +/* ========== 6 ========== */ 1.243 +testnum++; 1.244 +testdesc = "test encoding an unscaled PNG"; 1.245 + 1.246 +if (!isWindows) { 1.247 +// we'll reuse the container from the previous test 1.248 +istream = imgTools.encodeImage(container, "image/png"); 1.249 +encodedBytes = streamToArray(istream); 1.250 + 1.251 +// Get bytes for exected result 1.252 +refName = isWindows ? "image2jpg32x32-win.png" : "image2jpg32x32.png"; 1.253 +refFile = do_get_file(refName); 1.254 +istream = getFileInputStream(refFile); 1.255 +do_check_eq(istream.available(), 3105); 1.256 +referenceBytes = streamToArray(istream); 1.257 + 1.258 +// compare the encoder's output to the reference file. 1.259 +compareArrays(encodedBytes, referenceBytes); 1.260 +} 1.261 + 1.262 + 1.263 +/* ========== 7 ========== */ 1.264 +testnum++; 1.265 +testdesc = "test decoding a ICO"; 1.266 + 1.267 +// 16x16 ico, 1406 bytes. 1.268 +imgName = "image3.ico"; 1.269 +inMimeType = "image/x-icon"; 1.270 +imgFile = do_get_file(imgName); 1.271 + 1.272 +istream = getFileInputStream(imgFile); 1.273 +do_check_eq(istream.available(), 1406); 1.274 + 1.275 +container = imgTools.decodeImage(istream, inMimeType); 1.276 + 1.277 +// It's not easy to look at the pixel values from JS, so just 1.278 +// check the container's size. 1.279 +do_check_eq(container.width, 16); 1.280 +do_check_eq(container.height, 16); 1.281 + 1.282 + 1.283 +/* ========== 8 ========== */ 1.284 +testnum++; 1.285 +testdesc = "test encoding a scaled PNG"; // note that we're scaling UP 1.286 + 1.287 +// we'll reuse the container from the previous test 1.288 +istream = imgTools.encodeScaledImage(container, "image/png", 32, 32); 1.289 +encodedBytes = streamToArray(istream); 1.290 + 1.291 +// Get bytes for exected result 1.292 +refName = "image3ico32x32.png"; 1.293 +refFile = do_get_file(refName); 1.294 +istream = getFileInputStream(refFile); 1.295 +do_check_eq(istream.available(), 2285); 1.296 +referenceBytes = streamToArray(istream); 1.297 + 1.298 +// compare the encoder's output to the reference file. 1.299 +compareArrays(encodedBytes, referenceBytes); 1.300 + 1.301 + 1.302 +/* ========== 9 ========== */ 1.303 +testnum++; 1.304 +testdesc = "test encoding an unscaled PNG"; 1.305 + 1.306 +// we'll reuse the container from the previous test 1.307 +istream = imgTools.encodeImage(container, "image/png"); 1.308 +encodedBytes = streamToArray(istream); 1.309 + 1.310 +// Get bytes for exected result 1.311 +refName = "image3ico16x16.png"; 1.312 +refFile = do_get_file(refName); 1.313 +istream = getFileInputStream(refFile); 1.314 +do_check_eq(istream.available(), 330); 1.315 +referenceBytes = streamToArray(istream); 1.316 + 1.317 +// compare the encoder's output to the reference file. 1.318 +compareArrays(encodedBytes, referenceBytes); 1.319 + 1.320 + 1.321 +/* ========== 10 ========== */ 1.322 +testnum++; 1.323 +testdesc = "test decoding a GIF"; 1.324 + 1.325 +// 32x32 gif, 1809 bytes. 1.326 +imgName = "image4.gif"; 1.327 +inMimeType = "image/gif"; 1.328 +imgFile = do_get_file(imgName); 1.329 + 1.330 +istream = getFileInputStream(imgFile); 1.331 +do_check_eq(istream.available(), 1809); 1.332 + 1.333 +container = imgTools.decodeImage(istream, inMimeType); 1.334 + 1.335 +// It's not easy to look at the pixel values from JS, so just 1.336 +// check the container's size. 1.337 +do_check_eq(container.width, 32); 1.338 +do_check_eq(container.height, 32); 1.339 + 1.340 +/* ========== 11 ========== */ 1.341 +testnum++; 1.342 +testdesc = "test encoding an unscaled ICO with format options " + 1.343 + "(format=bmp;bpp=32)"; 1.344 + 1.345 +// we'll reuse the container from the previous test 1.346 +istream = imgTools.encodeImage(container, 1.347 + "image/vnd.microsoft.icon", 1.348 + "format=bmp;bpp=32"); 1.349 +encodedBytes = streamToArray(istream); 1.350 + 1.351 +// Get bytes for exected result 1.352 +refName = "image4gif32x32bmp32bpp.ico"; 1.353 +refFile = do_get_file(refName); 1.354 +istream = getFileInputStream(refFile); 1.355 +do_check_eq(istream.available(), 4286); 1.356 +referenceBytes = streamToArray(istream); 1.357 + 1.358 +// compare the encoder's output to the reference file. 1.359 +compareArrays(encodedBytes, referenceBytes); 1.360 + 1.361 +/* ========== 12 ========== */ 1.362 +testnum++; 1.363 +testdesc = "test encoding a scaled ICO with format options " + 1.364 + "(format=bmp;bpp=32)"; 1.365 + 1.366 +// we'll reuse the container from the previous test 1.367 +istream = imgTools.encodeScaledImage(container, 1.368 + "image/vnd.microsoft.icon", 1.369 + 16, 1.370 + 16, 1.371 + "format=bmp;bpp=32"); 1.372 +encodedBytes = streamToArray(istream); 1.373 + 1.374 +// Get bytes for exected result 1.375 +refName = "image4gif16x16bmp32bpp.ico"; 1.376 +refFile = do_get_file(refName); 1.377 +istream = getFileInputStream(refFile); 1.378 +do_check_eq(istream.available(), 1150); 1.379 +referenceBytes = streamToArray(istream); 1.380 + 1.381 +// compare the encoder's output to the reference file. 1.382 +compareArrays(encodedBytes, referenceBytes); 1.383 + 1.384 +/* ========== 13 ========== */ 1.385 +testnum++; 1.386 +testdesc = "test encoding an unscaled ICO with format options " + 1.387 + "(format=bmp;bpp=24)"; 1.388 + 1.389 +// we'll reuse the container from the previous test 1.390 +istream = imgTools.encodeImage(container, 1.391 + "image/vnd.microsoft.icon", 1.392 + "format=bmp;bpp=24"); 1.393 +encodedBytes = streamToArray(istream); 1.394 + 1.395 +// Get bytes for exected result 1.396 +refName = "image4gif32x32bmp24bpp.ico"; 1.397 +refFile = do_get_file(refName); 1.398 +istream = getFileInputStream(refFile); 1.399 +do_check_eq(istream.available(), 3262); 1.400 +referenceBytes = streamToArray(istream); 1.401 + 1.402 +// compare the encoder's output to the reference file. 1.403 +compareArrays(encodedBytes, referenceBytes); 1.404 + 1.405 +/* ========== 14 ========== */ 1.406 +testnum++; 1.407 +testdesc = "test encoding a scaled ICO with format options " + 1.408 + "(format=bmp;bpp=24)"; 1.409 + 1.410 +// we'll reuse the container from the previous test 1.411 +istream = imgTools.encodeScaledImage(container, 1.412 + "image/vnd.microsoft.icon", 1.413 + 16, 1.414 + 16, 1.415 + "format=bmp;bpp=24"); 1.416 +encodedBytes = streamToArray(istream); 1.417 + 1.418 +// Get bytes for exected result 1.419 +refName = "image4gif16x16bmp24bpp.ico"; 1.420 +refFile = do_get_file(refName); 1.421 +istream = getFileInputStream(refFile); 1.422 +do_check_eq(istream.available(), 894); 1.423 +referenceBytes = streamToArray(istream); 1.424 + 1.425 +// compare the encoder's output to the reference file. 1.426 +compareArrays(encodedBytes, referenceBytes); 1.427 + 1.428 + 1.429 +/* ========== 15 ========== */ 1.430 +testnum++; 1.431 +testdesc = "test cropping a JPG"; 1.432 + 1.433 +// 32x32 jpeg, 3494 bytes. 1.434 +imgName = "image2.jpg"; 1.435 +inMimeType = "image/jpeg"; 1.436 +imgFile = do_get_file(imgName); 1.437 + 1.438 +istream = getFileInputStream(imgFile); 1.439 +do_check_eq(istream.available(), 3494); 1.440 + 1.441 +container = imgTools.decodeImage(istream, inMimeType); 1.442 + 1.443 +// It's not easy to look at the pixel values from JS, so just 1.444 +// check the container's size. 1.445 +do_check_eq(container.width, 32); 1.446 +do_check_eq(container.height, 32); 1.447 + 1.448 +// encode a cropped image 1.449 +istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 16, 16); 1.450 +encodedBytes = streamToArray(istream); 1.451 + 1.452 +// Get bytes for exected result 1.453 +refName = "image2jpg16x16cropped.jpg"; 1.454 +refFile = do_get_file(refName); 1.455 +istream = getFileInputStream(refFile); 1.456 +do_check_eq(istream.available(), 879); 1.457 +referenceBytes = streamToArray(istream); 1.458 + 1.459 +// compare the encoder's output to the reference file. 1.460 +compareArrays(encodedBytes, referenceBytes); 1.461 + 1.462 + 1.463 +/* ========== 16 ========== */ 1.464 +testnum++; 1.465 +testdesc = "test cropping a JPG with an offset"; 1.466 + 1.467 +// we'll reuse the container from the previous test 1.468 +istream = imgTools.encodeCroppedImage(container, "image/jpeg", 16, 16, 16, 16); 1.469 +encodedBytes = streamToArray(istream); 1.470 + 1.471 +// Get bytes for exected result 1.472 +refName = "image2jpg16x16cropped2.jpg"; 1.473 +refFile = do_get_file(refName); 1.474 +istream = getFileInputStream(refFile); 1.475 +do_check_eq(istream.available(), 878); 1.476 +referenceBytes = streamToArray(istream); 1.477 + 1.478 +// compare the encoder's output to the reference file. 1.479 +compareArrays(encodedBytes, referenceBytes); 1.480 + 1.481 + 1.482 +/* ========== 17 ========== */ 1.483 +testnum++; 1.484 +testdesc = "test cropping a JPG without a given height"; 1.485 + 1.486 +// we'll reuse the container from the previous test 1.487 +istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 16, 0); 1.488 +encodedBytes = streamToArray(istream); 1.489 + 1.490 +// Get bytes for exected result 1.491 +refName = "image2jpg16x32cropped3.jpg"; 1.492 +refFile = do_get_file(refName); 1.493 +istream = getFileInputStream(refFile); 1.494 +do_check_eq(istream.available(), 1127); 1.495 +referenceBytes = streamToArray(istream); 1.496 + 1.497 +// compare the encoder's output to the reference file. 1.498 +compareArrays(encodedBytes, referenceBytes); 1.499 + 1.500 + 1.501 +/* ========== 18 ========== */ 1.502 +testnum++; 1.503 +testdesc = "test cropping a JPG without a given width"; 1.504 + 1.505 +// we'll reuse the container from the previous test 1.506 +istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 0, 16); 1.507 +encodedBytes = streamToArray(istream); 1.508 + 1.509 +// Get bytes for exected result 1.510 +refName = "image2jpg32x16cropped4.jpg"; 1.511 +refFile = do_get_file(refName); 1.512 +istream = getFileInputStream(refFile); 1.513 +do_check_eq(istream.available(), 1135); 1.514 +referenceBytes = streamToArray(istream); 1.515 + 1.516 +// compare the encoder's output to the reference file. 1.517 +compareArrays(encodedBytes, referenceBytes); 1.518 + 1.519 + 1.520 +/* ========== 19 ========== */ 1.521 +testnum++; 1.522 +testdesc = "test cropping a JPG without a given width and height"; 1.523 + 1.524 +// we'll reuse the container from the previous test 1.525 +istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 0, 0); 1.526 +encodedBytes = streamToArray(istream); 1.527 + 1.528 +// Get bytes for exected result 1.529 +refName = "image2jpg32x32.jpg"; 1.530 +refFile = do_get_file(refName); 1.531 +istream = getFileInputStream(refFile); 1.532 +do_check_eq(istream.available(), 1634); 1.533 +referenceBytes = streamToArray(istream); 1.534 + 1.535 +// compare the encoder's output to the reference file. 1.536 +compareArrays(encodedBytes, referenceBytes); 1.537 + 1.538 + 1.539 +/* ========== 20 ========== */ 1.540 +testnum++; 1.541 +testdesc = "test scaling a JPG without a given width"; 1.542 + 1.543 +// we'll reuse the container from the previous test 1.544 +istream = imgTools.encodeScaledImage(container, "image/jpeg", 0, 16); 1.545 +encodedBytes = streamToArray(istream); 1.546 + 1.547 +// Get bytes for exected result 1.548 +refName = "image2jpg32x16scaled.jpg"; 1.549 +refFile = do_get_file(refName); 1.550 +istream = getFileInputStream(refFile); 1.551 +do_check_eq(istream.available(), 1227); 1.552 +referenceBytes = streamToArray(istream); 1.553 + 1.554 +// compare the encoder's output to the reference file. 1.555 +compareArrays(encodedBytes, referenceBytes); 1.556 + 1.557 + 1.558 +/* ========== 21 ========== */ 1.559 +testnum++; 1.560 +testdesc = "test scaling a JPG without a given height"; 1.561 + 1.562 +// we'll reuse the container from the previous test 1.563 +istream = imgTools.encodeScaledImage(container, "image/jpeg", 16, 0); 1.564 +encodedBytes = streamToArray(istream); 1.565 + 1.566 +// Get bytes for exected result 1.567 +refName = "image2jpg16x32scaled.jpg"; 1.568 +refFile = do_get_file(refName); 1.569 +istream = getFileInputStream(refFile); 1.570 +do_check_eq(istream.available(), 1219); 1.571 +referenceBytes = streamToArray(istream); 1.572 + 1.573 +// compare the encoder's output to the reference file. 1.574 +compareArrays(encodedBytes, referenceBytes); 1.575 + 1.576 + 1.577 +/* ========== 22 ========== */ 1.578 +testnum++; 1.579 +testdesc = "test scaling a JPG without a given width and height"; 1.580 + 1.581 +// we'll reuse the container from the previous test 1.582 +istream = imgTools.encodeScaledImage(container, "image/jpeg", 0, 0); 1.583 +encodedBytes = streamToArray(istream); 1.584 + 1.585 +// Get bytes for exected result 1.586 +refName = "image2jpg32x32.jpg"; 1.587 +refFile = do_get_file(refName); 1.588 +istream = getFileInputStream(refFile); 1.589 +do_check_eq(istream.available(), 1634); 1.590 +referenceBytes = streamToArray(istream); 1.591 + 1.592 +// compare the encoder's output to the reference file. 1.593 +compareArrays(encodedBytes, referenceBytes); 1.594 + 1.595 + 1.596 +/* ========== 22 ========== */ 1.597 +testnum++; 1.598 +testdesc = "test invalid arguments for cropping"; 1.599 + 1.600 +var numErrors = 0; 1.601 + 1.602 +try { 1.603 + // width/height can't be negative 1.604 + imgTools.encodeScaledImage(container, "image/jpeg", -1, -1); 1.605 +} catch (e) { numErrors++; } 1.606 + 1.607 +try { 1.608 + // offsets can't be negative 1.609 + imgTools.encodeCroppedImage(container, "image/jpeg", -1, -1, 16, 16); 1.610 +} catch (e) { numErrors++; } 1.611 + 1.612 +try { 1.613 + // width/height can't be negative 1.614 + imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, -1, -1); 1.615 +} catch (e) { numErrors++; } 1.616 + 1.617 +try { 1.618 + // out of bounds 1.619 + imgTools.encodeCroppedImage(container, "image/jpeg", 17, 17, 16, 16); 1.620 +} catch (e) { numErrors++; } 1.621 + 1.622 +try { 1.623 + // out of bounds 1.624 + imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 33, 33); 1.625 +} catch (e) { numErrors++; } 1.626 + 1.627 +try { 1.628 + // out of bounds 1.629 + imgTools.encodeCroppedImage(container, "image/jpeg", 1, 1, 0, 0); 1.630 +} catch (e) { numErrors++; } 1.631 + 1.632 +do_check_eq(numErrors, 6); 1.633 + 1.634 + 1.635 +/* ========== bug 363986 ========== */ 1.636 +testnum = 363986; 1.637 +testdesc = "test PNG and JPEG encoders' Read/ReadSegments methods"; 1.638 + 1.639 +var testData = 1.640 + [{preImage: "image3.ico", 1.641 + preImageMimeType: "image/x-icon", 1.642 + refImage: "image3ico16x16.png", 1.643 + refImageMimeType: "image/png"}, 1.644 + {preImage: "image1.png", 1.645 + preImageMimeType: "image/png", 1.646 + refImage: "image1png64x64.jpg", 1.647 + refImageMimeType: "image/jpeg"}]; 1.648 + 1.649 +for(var i=0; i<testData.length; ++i) { 1.650 + var dict = testData[i]; 1.651 + 1.652 + var imgFile = do_get_file(dict["refImage"]); 1.653 + var istream = getFileInputStream(imgFile); 1.654 + var refBytes = streamToArray(istream); 1.655 + 1.656 + imgFile = do_get_file(dict["preImage"]); 1.657 + istream = getFileInputStream(imgFile); 1.658 + 1.659 + var container = imgTools.decodeImage(istream, dict["preImageMimeType"]); 1.660 + 1.661 + istream = imgTools.encodeImage(container, dict["refImageMimeType"]); 1.662 + 1.663 + var sstream = Cc["@mozilla.org/storagestream;1"]. 1.664 + createInstance(Ci.nsIStorageStream); 1.665 + sstream.init(4096, 4294967295, null); 1.666 + var ostream = sstream.getOutputStream(0); 1.667 + var bostream = Cc["@mozilla.org/network/buffered-output-stream;1"]. 1.668 + createInstance(Ci.nsIBufferedOutputStream); 1.669 + 1.670 + //use a tiny buffer to make sure the image data doesn't fully fit in it 1.671 + bostream.init(ostream, 8); 1.672 + 1.673 + bostream.writeFrom(istream, istream.available()); 1.674 + bostream.flush(); bostream.close(); 1.675 + 1.676 + var encBytes = streamToArray(sstream.newInputStream(0)); 1.677 + 1.678 + compareArrays(refBytes, encBytes); 1.679 +} 1.680 + 1.681 + 1.682 +/* ========== bug 413512 ========== */ 1.683 +testnum = 413512; 1.684 +testdesc = "test decoding bad favicon (bug 413512)"; 1.685 + 1.686 +imgName = "bug413512.ico"; 1.687 +inMimeType = "image/x-icon"; 1.688 +imgFile = do_get_file(imgName); 1.689 + 1.690 +istream = getFileInputStream(imgFile); 1.691 +do_check_eq(istream.available(), 17759); 1.692 +var errsrc = "none"; 1.693 + 1.694 +try { 1.695 + container = imgTools.decodeImage(istream, inMimeType); 1.696 + 1.697 + // We should never hit this - decodeImage throws an assertion because the 1.698 + // image decoded doesn't have enough frames. 1.699 + try { 1.700 + istream = imgTools.encodeImage(container, "image/png"); 1.701 + } catch (e) { 1.702 + err = e; 1.703 + errsrc = "encode"; 1.704 + } 1.705 +} catch (e) { 1.706 + err = e; 1.707 + errsrc = "decode"; 1.708 +} 1.709 + 1.710 +do_check_eq(errsrc, "decode"); 1.711 +checkExpectedError(/NS_ERROR_FAILURE/, err); 1.712 + 1.713 + 1.714 +/* ========== bug 815359 ========== */ 1.715 +testnum = 815359; 1.716 +testdesc = "test correct ico hotspots (bug 815359)"; 1.717 + 1.718 +imgName = "bug815359.ico"; 1.719 +inMimeType = "image/x-icon"; 1.720 +imgFile = do_get_file(imgName); 1.721 + 1.722 +istream = getFileInputStream(imgFile); 1.723 +do_check_eq(istream.available(), 4286); 1.724 + 1.725 +container = imgTools.decodeImage(istream, inMimeType); 1.726 + 1.727 +var props = container.QueryInterface(Ci.nsIProperties); 1.728 + 1.729 +do_check_eq(props.get("hotspotX", Ci.nsISupportsPRUint32).data, 10); 1.730 +do_check_eq(props.get("hotspotY", Ci.nsISupportsPRUint32).data, 9); 1.731 + 1.732 + 1.733 +/* ========== end ========== */ 1.734 + 1.735 +} catch (e) { 1.736 + throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e; 1.737 +} 1.738 +};