image/test/unit/test_imgtools.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Tests for imgITools
michael@0 3 */
michael@0 4
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cc = Components.classes;
michael@0 7
michael@0 8
michael@0 9 /*
michael@0 10 * dumpToFile()
michael@0 11 *
michael@0 12 * For test development, dumps the specified array to a file.
michael@0 13 * Call |dumpToFile(outData);| in a test to file to a file.
michael@0 14 */
michael@0 15 function dumpToFile(aData) {
michael@0 16 var outputFile = do_get_tempdir();
michael@0 17 outputFile.append("testdump.png");
michael@0 18
michael@0 19 var outputStream = Cc["@mozilla.org/network/file-output-stream;1"].
michael@0 20 createInstance(Ci.nsIFileOutputStream);
michael@0 21 // WR_ONLY|CREAT|TRUNC
michael@0 22 outputStream.init(outputFile, 0x02 | 0x08 | 0x20, 0644, null);
michael@0 23
michael@0 24 var bos = Cc["@mozilla.org/binaryoutputstream;1"].
michael@0 25 createInstance(Ci.nsIBinaryOutputStream);
michael@0 26 bos.setOutputStream(outputStream);
michael@0 27
michael@0 28 bos.writeByteArray(aData, aData.length);
michael@0 29
michael@0 30 outputStream.close();
michael@0 31 }
michael@0 32
michael@0 33
michael@0 34 /*
michael@0 35 * getFileInputStream()
michael@0 36 *
michael@0 37 * Returns an input stream for the specified file.
michael@0 38 */
michael@0 39 function getFileInputStream(aFile) {
michael@0 40 var inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 41 createInstance(Ci.nsIFileInputStream);
michael@0 42 // init the stream as RD_ONLY, -1 == default permissions.
michael@0 43 inputStream.init(aFile, 0x01, -1, null);
michael@0 44
michael@0 45 // Blah. The image decoders use ReadSegments, which isn't implemented on
michael@0 46 // file input streams. Use a buffered stream to make it work.
michael@0 47 var bis = Cc["@mozilla.org/network/buffered-input-stream;1"].
michael@0 48 createInstance(Ci.nsIBufferedInputStream);
michael@0 49 bis.init(inputStream, 1024);
michael@0 50
michael@0 51 return bis;
michael@0 52 }
michael@0 53
michael@0 54
michael@0 55 /*
michael@0 56 * streamToArray()
michael@0 57 *
michael@0 58 * Consumes an input stream, and returns its bytes as an array.
michael@0 59 */
michael@0 60 function streamToArray(aStream) {
michael@0 61 var size = aStream.available();
michael@0 62
michael@0 63 // use a binary input stream to grab the bytes.
michael@0 64 var bis = Cc["@mozilla.org/binaryinputstream;1"].
michael@0 65 createInstance(Ci.nsIBinaryInputStream);
michael@0 66 bis.setInputStream(aStream);
michael@0 67
michael@0 68 var bytes = bis.readByteArray(size);
michael@0 69 if (size != bytes.length)
michael@0 70 throw "Didn't read expected number of bytes";
michael@0 71
michael@0 72 return bytes;
michael@0 73 }
michael@0 74
michael@0 75
michael@0 76 /*
michael@0 77 * compareArrays
michael@0 78 *
michael@0 79 * Compares two arrays, and throws if there's a difference.
michael@0 80 */
michael@0 81 function compareArrays(aArray1, aArray2) {
michael@0 82 do_check_eq(aArray1.length, aArray2.length);
michael@0 83
michael@0 84 for (var i = 0; i < aArray1.length; i++)
michael@0 85 if (aArray1[i] != aArray2[i])
michael@0 86 throw "arrays differ at index " + i;
michael@0 87 }
michael@0 88
michael@0 89
michael@0 90 /*
michael@0 91 * checkExpectedError
michael@0 92 *
michael@0 93 * Checks to see if a thrown error was expected or not, and if it
michael@0 94 * matches the expected value.
michael@0 95 */
michael@0 96 function checkExpectedError (aExpectedError, aActualError) {
michael@0 97 if (aExpectedError) {
michael@0 98 if (!aActualError)
michael@0 99 throw "Didn't throw as expected (" + aExpectedError + ")";
michael@0 100
michael@0 101 if (!aExpectedError.test(aActualError))
michael@0 102 throw "Threw (" + aActualError + "), not (" + aExpectedError;
michael@0 103
michael@0 104 // We got the expected error, so make a note in the test log.
michael@0 105 dump("...that error was expected.\n\n");
michael@0 106 } else if (aActualError) {
michael@0 107 throw "Threw unexpected error: " + aActualError;
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111
michael@0 112 function run_test() {
michael@0 113
michael@0 114 try {
michael@0 115
michael@0 116
michael@0 117 /* ========== 0 ========== */
michael@0 118 var testnum = 0;
michael@0 119 var testdesc = "imgITools setup";
michael@0 120 var err = null;
michael@0 121
michael@0 122 var imgTools = Cc["@mozilla.org/image/tools;1"].
michael@0 123 getService(Ci.imgITools);
michael@0 124
michael@0 125 if (!imgTools)
michael@0 126 throw "Couldn't get imgITools service"
michael@0 127
michael@0 128 // Ugh, this is an ugly hack. The pixel values we get in Windows are sometimes
michael@0 129 // +/- 1 value compared to other platforms, so we need to compare against a
michael@0 130 // different set of reference images. nsIXULRuntime.OS doesn't seem to be
michael@0 131 // available in xpcshell, so we'll use this as a kludgy way to figure out if
michael@0 132 // we're running on Windows.
michael@0 133 var isWindows = ("@mozilla.org/windows-registry-key;1" in Cc);
michael@0 134
michael@0 135
michael@0 136 /* ========== 1 ========== */
michael@0 137 testnum++;
michael@0 138 testdesc = "test decoding a PNG";
michael@0 139
michael@0 140 // 64x64 png, 8415 bytes.
michael@0 141 var imgName = "image1.png";
michael@0 142 var inMimeType = "image/png";
michael@0 143 var imgFile = do_get_file(imgName);
michael@0 144
michael@0 145 var istream = getFileInputStream(imgFile);
michael@0 146 do_check_eq(istream.available(), 8415);
michael@0 147
michael@0 148 // Use decodeImageData for this test even though it's deprecated to ensure that
michael@0 149 // it correctly forwards to decodeImage and continues to work.
michael@0 150 var outParam = { value: null };
michael@0 151 imgTools.decodeImageData(istream, inMimeType, outParam);
michael@0 152 var container = outParam.value;
michael@0 153
michael@0 154 // It's not easy to look at the pixel values from JS, so just
michael@0 155 // check the container's size.
michael@0 156 do_check_eq(container.width, 64);
michael@0 157 do_check_eq(container.height, 64);
michael@0 158
michael@0 159
michael@0 160 /* ========== 2 ========== */
michael@0 161 testnum++;
michael@0 162 testdesc = "test encoding a scaled JPEG";
michael@0 163
michael@0 164 // we'll reuse the container from the previous test
michael@0 165 istream = imgTools.encodeScaledImage(container, "image/jpeg", 16, 16);
michael@0 166
michael@0 167 var encodedBytes = streamToArray(istream);
michael@0 168 // Get bytes for exected result
michael@0 169 var refName = "image1png16x16.jpg";
michael@0 170 var refFile = do_get_file(refName);
michael@0 171 istream = getFileInputStream(refFile);
michael@0 172 do_check_eq(istream.available(), 1078);
michael@0 173 var referenceBytes = streamToArray(istream);
michael@0 174
michael@0 175 // compare the encoder's output to the reference file.
michael@0 176 compareArrays(encodedBytes, referenceBytes);
michael@0 177
michael@0 178
michael@0 179 /* ========== 3 ========== */
michael@0 180 testnum++;
michael@0 181 testdesc = "test encoding an unscaled JPEG";
michael@0 182
michael@0 183 // we'll reuse the container from the previous test
michael@0 184 istream = imgTools.encodeImage(container, "image/jpeg");
michael@0 185 encodedBytes = streamToArray(istream);
michael@0 186
michael@0 187 // Get bytes for exected result
michael@0 188 refName = "image1png64x64.jpg";
michael@0 189 refFile = do_get_file(refName);
michael@0 190 istream = getFileInputStream(refFile);
michael@0 191 do_check_eq(istream.available(), 4503);
michael@0 192 referenceBytes = streamToArray(istream);
michael@0 193
michael@0 194 // compare the encoder's output to the reference file.
michael@0 195 compareArrays(encodedBytes, referenceBytes);
michael@0 196
michael@0 197
michael@0 198 /* ========== 4 ========== */
michael@0 199 testnum++;
michael@0 200 testdesc = "test decoding a JPEG";
michael@0 201
michael@0 202 // 32x32 jpeg, 3494 bytes.
michael@0 203 imgName = "image2.jpg";
michael@0 204 inMimeType = "image/jpeg";
michael@0 205 imgFile = do_get_file(imgName);
michael@0 206
michael@0 207 istream = getFileInputStream(imgFile);
michael@0 208 do_check_eq(istream.available(), 3494);
michael@0 209
michael@0 210 container = imgTools.decodeImage(istream, inMimeType);
michael@0 211
michael@0 212 // It's not easy to look at the pixel values from JS, so just
michael@0 213 // check the container's size.
michael@0 214 do_check_eq(container.width, 32);
michael@0 215 do_check_eq(container.height, 32);
michael@0 216
michael@0 217
michael@0 218 /* ========== 5 ========== */
michael@0 219 testnum++;
michael@0 220 testdesc = "test encoding a scaled PNG";
michael@0 221
michael@0 222 if (!isWindows) {
michael@0 223 // we'll reuse the container from the previous test
michael@0 224 istream = imgTools.encodeScaledImage(container, "image/png", 16, 16);
michael@0 225
michael@0 226 encodedBytes = streamToArray(istream);
michael@0 227 // Get bytes for exected result
michael@0 228 refName = isWindows ? "image2jpg16x16-win.png" : "image2jpg16x16.png";
michael@0 229 refFile = do_get_file(refName);
michael@0 230 istream = getFileInputStream(refFile);
michael@0 231 do_check_eq(istream.available(), 948);
michael@0 232 referenceBytes = streamToArray(istream);
michael@0 233
michael@0 234 // compare the encoder's output to the reference file.
michael@0 235 compareArrays(encodedBytes, referenceBytes);
michael@0 236 }
michael@0 237
michael@0 238
michael@0 239 /* ========== 6 ========== */
michael@0 240 testnum++;
michael@0 241 testdesc = "test encoding an unscaled PNG";
michael@0 242
michael@0 243 if (!isWindows) {
michael@0 244 // we'll reuse the container from the previous test
michael@0 245 istream = imgTools.encodeImage(container, "image/png");
michael@0 246 encodedBytes = streamToArray(istream);
michael@0 247
michael@0 248 // Get bytes for exected result
michael@0 249 refName = isWindows ? "image2jpg32x32-win.png" : "image2jpg32x32.png";
michael@0 250 refFile = do_get_file(refName);
michael@0 251 istream = getFileInputStream(refFile);
michael@0 252 do_check_eq(istream.available(), 3105);
michael@0 253 referenceBytes = streamToArray(istream);
michael@0 254
michael@0 255 // compare the encoder's output to the reference file.
michael@0 256 compareArrays(encodedBytes, referenceBytes);
michael@0 257 }
michael@0 258
michael@0 259
michael@0 260 /* ========== 7 ========== */
michael@0 261 testnum++;
michael@0 262 testdesc = "test decoding a ICO";
michael@0 263
michael@0 264 // 16x16 ico, 1406 bytes.
michael@0 265 imgName = "image3.ico";
michael@0 266 inMimeType = "image/x-icon";
michael@0 267 imgFile = do_get_file(imgName);
michael@0 268
michael@0 269 istream = getFileInputStream(imgFile);
michael@0 270 do_check_eq(istream.available(), 1406);
michael@0 271
michael@0 272 container = imgTools.decodeImage(istream, inMimeType);
michael@0 273
michael@0 274 // It's not easy to look at the pixel values from JS, so just
michael@0 275 // check the container's size.
michael@0 276 do_check_eq(container.width, 16);
michael@0 277 do_check_eq(container.height, 16);
michael@0 278
michael@0 279
michael@0 280 /* ========== 8 ========== */
michael@0 281 testnum++;
michael@0 282 testdesc = "test encoding a scaled PNG"; // note that we're scaling UP
michael@0 283
michael@0 284 // we'll reuse the container from the previous test
michael@0 285 istream = imgTools.encodeScaledImage(container, "image/png", 32, 32);
michael@0 286 encodedBytes = streamToArray(istream);
michael@0 287
michael@0 288 // Get bytes for exected result
michael@0 289 refName = "image3ico32x32.png";
michael@0 290 refFile = do_get_file(refName);
michael@0 291 istream = getFileInputStream(refFile);
michael@0 292 do_check_eq(istream.available(), 2285);
michael@0 293 referenceBytes = streamToArray(istream);
michael@0 294
michael@0 295 // compare the encoder's output to the reference file.
michael@0 296 compareArrays(encodedBytes, referenceBytes);
michael@0 297
michael@0 298
michael@0 299 /* ========== 9 ========== */
michael@0 300 testnum++;
michael@0 301 testdesc = "test encoding an unscaled PNG";
michael@0 302
michael@0 303 // we'll reuse the container from the previous test
michael@0 304 istream = imgTools.encodeImage(container, "image/png");
michael@0 305 encodedBytes = streamToArray(istream);
michael@0 306
michael@0 307 // Get bytes for exected result
michael@0 308 refName = "image3ico16x16.png";
michael@0 309 refFile = do_get_file(refName);
michael@0 310 istream = getFileInputStream(refFile);
michael@0 311 do_check_eq(istream.available(), 330);
michael@0 312 referenceBytes = streamToArray(istream);
michael@0 313
michael@0 314 // compare the encoder's output to the reference file.
michael@0 315 compareArrays(encodedBytes, referenceBytes);
michael@0 316
michael@0 317
michael@0 318 /* ========== 10 ========== */
michael@0 319 testnum++;
michael@0 320 testdesc = "test decoding a GIF";
michael@0 321
michael@0 322 // 32x32 gif, 1809 bytes.
michael@0 323 imgName = "image4.gif";
michael@0 324 inMimeType = "image/gif";
michael@0 325 imgFile = do_get_file(imgName);
michael@0 326
michael@0 327 istream = getFileInputStream(imgFile);
michael@0 328 do_check_eq(istream.available(), 1809);
michael@0 329
michael@0 330 container = imgTools.decodeImage(istream, inMimeType);
michael@0 331
michael@0 332 // It's not easy to look at the pixel values from JS, so just
michael@0 333 // check the container's size.
michael@0 334 do_check_eq(container.width, 32);
michael@0 335 do_check_eq(container.height, 32);
michael@0 336
michael@0 337 /* ========== 11 ========== */
michael@0 338 testnum++;
michael@0 339 testdesc = "test encoding an unscaled ICO with format options " +
michael@0 340 "(format=bmp;bpp=32)";
michael@0 341
michael@0 342 // we'll reuse the container from the previous test
michael@0 343 istream = imgTools.encodeImage(container,
michael@0 344 "image/vnd.microsoft.icon",
michael@0 345 "format=bmp;bpp=32");
michael@0 346 encodedBytes = streamToArray(istream);
michael@0 347
michael@0 348 // Get bytes for exected result
michael@0 349 refName = "image4gif32x32bmp32bpp.ico";
michael@0 350 refFile = do_get_file(refName);
michael@0 351 istream = getFileInputStream(refFile);
michael@0 352 do_check_eq(istream.available(), 4286);
michael@0 353 referenceBytes = streamToArray(istream);
michael@0 354
michael@0 355 // compare the encoder's output to the reference file.
michael@0 356 compareArrays(encodedBytes, referenceBytes);
michael@0 357
michael@0 358 /* ========== 12 ========== */
michael@0 359 testnum++;
michael@0 360 testdesc = "test encoding a scaled ICO with format options " +
michael@0 361 "(format=bmp;bpp=32)";
michael@0 362
michael@0 363 // we'll reuse the container from the previous test
michael@0 364 istream = imgTools.encodeScaledImage(container,
michael@0 365 "image/vnd.microsoft.icon",
michael@0 366 16,
michael@0 367 16,
michael@0 368 "format=bmp;bpp=32");
michael@0 369 encodedBytes = streamToArray(istream);
michael@0 370
michael@0 371 // Get bytes for exected result
michael@0 372 refName = "image4gif16x16bmp32bpp.ico";
michael@0 373 refFile = do_get_file(refName);
michael@0 374 istream = getFileInputStream(refFile);
michael@0 375 do_check_eq(istream.available(), 1150);
michael@0 376 referenceBytes = streamToArray(istream);
michael@0 377
michael@0 378 // compare the encoder's output to the reference file.
michael@0 379 compareArrays(encodedBytes, referenceBytes);
michael@0 380
michael@0 381 /* ========== 13 ========== */
michael@0 382 testnum++;
michael@0 383 testdesc = "test encoding an unscaled ICO with format options " +
michael@0 384 "(format=bmp;bpp=24)";
michael@0 385
michael@0 386 // we'll reuse the container from the previous test
michael@0 387 istream = imgTools.encodeImage(container,
michael@0 388 "image/vnd.microsoft.icon",
michael@0 389 "format=bmp;bpp=24");
michael@0 390 encodedBytes = streamToArray(istream);
michael@0 391
michael@0 392 // Get bytes for exected result
michael@0 393 refName = "image4gif32x32bmp24bpp.ico";
michael@0 394 refFile = do_get_file(refName);
michael@0 395 istream = getFileInputStream(refFile);
michael@0 396 do_check_eq(istream.available(), 3262);
michael@0 397 referenceBytes = streamToArray(istream);
michael@0 398
michael@0 399 // compare the encoder's output to the reference file.
michael@0 400 compareArrays(encodedBytes, referenceBytes);
michael@0 401
michael@0 402 /* ========== 14 ========== */
michael@0 403 testnum++;
michael@0 404 testdesc = "test encoding a scaled ICO with format options " +
michael@0 405 "(format=bmp;bpp=24)";
michael@0 406
michael@0 407 // we'll reuse the container from the previous test
michael@0 408 istream = imgTools.encodeScaledImage(container,
michael@0 409 "image/vnd.microsoft.icon",
michael@0 410 16,
michael@0 411 16,
michael@0 412 "format=bmp;bpp=24");
michael@0 413 encodedBytes = streamToArray(istream);
michael@0 414
michael@0 415 // Get bytes for exected result
michael@0 416 refName = "image4gif16x16bmp24bpp.ico";
michael@0 417 refFile = do_get_file(refName);
michael@0 418 istream = getFileInputStream(refFile);
michael@0 419 do_check_eq(istream.available(), 894);
michael@0 420 referenceBytes = streamToArray(istream);
michael@0 421
michael@0 422 // compare the encoder's output to the reference file.
michael@0 423 compareArrays(encodedBytes, referenceBytes);
michael@0 424
michael@0 425
michael@0 426 /* ========== 15 ========== */
michael@0 427 testnum++;
michael@0 428 testdesc = "test cropping a JPG";
michael@0 429
michael@0 430 // 32x32 jpeg, 3494 bytes.
michael@0 431 imgName = "image2.jpg";
michael@0 432 inMimeType = "image/jpeg";
michael@0 433 imgFile = do_get_file(imgName);
michael@0 434
michael@0 435 istream = getFileInputStream(imgFile);
michael@0 436 do_check_eq(istream.available(), 3494);
michael@0 437
michael@0 438 container = imgTools.decodeImage(istream, inMimeType);
michael@0 439
michael@0 440 // It's not easy to look at the pixel values from JS, so just
michael@0 441 // check the container's size.
michael@0 442 do_check_eq(container.width, 32);
michael@0 443 do_check_eq(container.height, 32);
michael@0 444
michael@0 445 // encode a cropped image
michael@0 446 istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 16, 16);
michael@0 447 encodedBytes = streamToArray(istream);
michael@0 448
michael@0 449 // Get bytes for exected result
michael@0 450 refName = "image2jpg16x16cropped.jpg";
michael@0 451 refFile = do_get_file(refName);
michael@0 452 istream = getFileInputStream(refFile);
michael@0 453 do_check_eq(istream.available(), 879);
michael@0 454 referenceBytes = streamToArray(istream);
michael@0 455
michael@0 456 // compare the encoder's output to the reference file.
michael@0 457 compareArrays(encodedBytes, referenceBytes);
michael@0 458
michael@0 459
michael@0 460 /* ========== 16 ========== */
michael@0 461 testnum++;
michael@0 462 testdesc = "test cropping a JPG with an offset";
michael@0 463
michael@0 464 // we'll reuse the container from the previous test
michael@0 465 istream = imgTools.encodeCroppedImage(container, "image/jpeg", 16, 16, 16, 16);
michael@0 466 encodedBytes = streamToArray(istream);
michael@0 467
michael@0 468 // Get bytes for exected result
michael@0 469 refName = "image2jpg16x16cropped2.jpg";
michael@0 470 refFile = do_get_file(refName);
michael@0 471 istream = getFileInputStream(refFile);
michael@0 472 do_check_eq(istream.available(), 878);
michael@0 473 referenceBytes = streamToArray(istream);
michael@0 474
michael@0 475 // compare the encoder's output to the reference file.
michael@0 476 compareArrays(encodedBytes, referenceBytes);
michael@0 477
michael@0 478
michael@0 479 /* ========== 17 ========== */
michael@0 480 testnum++;
michael@0 481 testdesc = "test cropping a JPG without a given height";
michael@0 482
michael@0 483 // we'll reuse the container from the previous test
michael@0 484 istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 16, 0);
michael@0 485 encodedBytes = streamToArray(istream);
michael@0 486
michael@0 487 // Get bytes for exected result
michael@0 488 refName = "image2jpg16x32cropped3.jpg";
michael@0 489 refFile = do_get_file(refName);
michael@0 490 istream = getFileInputStream(refFile);
michael@0 491 do_check_eq(istream.available(), 1127);
michael@0 492 referenceBytes = streamToArray(istream);
michael@0 493
michael@0 494 // compare the encoder's output to the reference file.
michael@0 495 compareArrays(encodedBytes, referenceBytes);
michael@0 496
michael@0 497
michael@0 498 /* ========== 18 ========== */
michael@0 499 testnum++;
michael@0 500 testdesc = "test cropping a JPG without a given width";
michael@0 501
michael@0 502 // we'll reuse the container from the previous test
michael@0 503 istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 0, 16);
michael@0 504 encodedBytes = streamToArray(istream);
michael@0 505
michael@0 506 // Get bytes for exected result
michael@0 507 refName = "image2jpg32x16cropped4.jpg";
michael@0 508 refFile = do_get_file(refName);
michael@0 509 istream = getFileInputStream(refFile);
michael@0 510 do_check_eq(istream.available(), 1135);
michael@0 511 referenceBytes = streamToArray(istream);
michael@0 512
michael@0 513 // compare the encoder's output to the reference file.
michael@0 514 compareArrays(encodedBytes, referenceBytes);
michael@0 515
michael@0 516
michael@0 517 /* ========== 19 ========== */
michael@0 518 testnum++;
michael@0 519 testdesc = "test cropping a JPG without a given width and height";
michael@0 520
michael@0 521 // we'll reuse the container from the previous test
michael@0 522 istream = imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 0, 0);
michael@0 523 encodedBytes = streamToArray(istream);
michael@0 524
michael@0 525 // Get bytes for exected result
michael@0 526 refName = "image2jpg32x32.jpg";
michael@0 527 refFile = do_get_file(refName);
michael@0 528 istream = getFileInputStream(refFile);
michael@0 529 do_check_eq(istream.available(), 1634);
michael@0 530 referenceBytes = streamToArray(istream);
michael@0 531
michael@0 532 // compare the encoder's output to the reference file.
michael@0 533 compareArrays(encodedBytes, referenceBytes);
michael@0 534
michael@0 535
michael@0 536 /* ========== 20 ========== */
michael@0 537 testnum++;
michael@0 538 testdesc = "test scaling a JPG without a given width";
michael@0 539
michael@0 540 // we'll reuse the container from the previous test
michael@0 541 istream = imgTools.encodeScaledImage(container, "image/jpeg", 0, 16);
michael@0 542 encodedBytes = streamToArray(istream);
michael@0 543
michael@0 544 // Get bytes for exected result
michael@0 545 refName = "image2jpg32x16scaled.jpg";
michael@0 546 refFile = do_get_file(refName);
michael@0 547 istream = getFileInputStream(refFile);
michael@0 548 do_check_eq(istream.available(), 1227);
michael@0 549 referenceBytes = streamToArray(istream);
michael@0 550
michael@0 551 // compare the encoder's output to the reference file.
michael@0 552 compareArrays(encodedBytes, referenceBytes);
michael@0 553
michael@0 554
michael@0 555 /* ========== 21 ========== */
michael@0 556 testnum++;
michael@0 557 testdesc = "test scaling a JPG without a given height";
michael@0 558
michael@0 559 // we'll reuse the container from the previous test
michael@0 560 istream = imgTools.encodeScaledImage(container, "image/jpeg", 16, 0);
michael@0 561 encodedBytes = streamToArray(istream);
michael@0 562
michael@0 563 // Get bytes for exected result
michael@0 564 refName = "image2jpg16x32scaled.jpg";
michael@0 565 refFile = do_get_file(refName);
michael@0 566 istream = getFileInputStream(refFile);
michael@0 567 do_check_eq(istream.available(), 1219);
michael@0 568 referenceBytes = streamToArray(istream);
michael@0 569
michael@0 570 // compare the encoder's output to the reference file.
michael@0 571 compareArrays(encodedBytes, referenceBytes);
michael@0 572
michael@0 573
michael@0 574 /* ========== 22 ========== */
michael@0 575 testnum++;
michael@0 576 testdesc = "test scaling a JPG without a given width and height";
michael@0 577
michael@0 578 // we'll reuse the container from the previous test
michael@0 579 istream = imgTools.encodeScaledImage(container, "image/jpeg", 0, 0);
michael@0 580 encodedBytes = streamToArray(istream);
michael@0 581
michael@0 582 // Get bytes for exected result
michael@0 583 refName = "image2jpg32x32.jpg";
michael@0 584 refFile = do_get_file(refName);
michael@0 585 istream = getFileInputStream(refFile);
michael@0 586 do_check_eq(istream.available(), 1634);
michael@0 587 referenceBytes = streamToArray(istream);
michael@0 588
michael@0 589 // compare the encoder's output to the reference file.
michael@0 590 compareArrays(encodedBytes, referenceBytes);
michael@0 591
michael@0 592
michael@0 593 /* ========== 22 ========== */
michael@0 594 testnum++;
michael@0 595 testdesc = "test invalid arguments for cropping";
michael@0 596
michael@0 597 var numErrors = 0;
michael@0 598
michael@0 599 try {
michael@0 600 // width/height can't be negative
michael@0 601 imgTools.encodeScaledImage(container, "image/jpeg", -1, -1);
michael@0 602 } catch (e) { numErrors++; }
michael@0 603
michael@0 604 try {
michael@0 605 // offsets can't be negative
michael@0 606 imgTools.encodeCroppedImage(container, "image/jpeg", -1, -1, 16, 16);
michael@0 607 } catch (e) { numErrors++; }
michael@0 608
michael@0 609 try {
michael@0 610 // width/height can't be negative
michael@0 611 imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, -1, -1);
michael@0 612 } catch (e) { numErrors++; }
michael@0 613
michael@0 614 try {
michael@0 615 // out of bounds
michael@0 616 imgTools.encodeCroppedImage(container, "image/jpeg", 17, 17, 16, 16);
michael@0 617 } catch (e) { numErrors++; }
michael@0 618
michael@0 619 try {
michael@0 620 // out of bounds
michael@0 621 imgTools.encodeCroppedImage(container, "image/jpeg", 0, 0, 33, 33);
michael@0 622 } catch (e) { numErrors++; }
michael@0 623
michael@0 624 try {
michael@0 625 // out of bounds
michael@0 626 imgTools.encodeCroppedImage(container, "image/jpeg", 1, 1, 0, 0);
michael@0 627 } catch (e) { numErrors++; }
michael@0 628
michael@0 629 do_check_eq(numErrors, 6);
michael@0 630
michael@0 631
michael@0 632 /* ========== bug 363986 ========== */
michael@0 633 testnum = 363986;
michael@0 634 testdesc = "test PNG and JPEG encoders' Read/ReadSegments methods";
michael@0 635
michael@0 636 var testData =
michael@0 637 [{preImage: "image3.ico",
michael@0 638 preImageMimeType: "image/x-icon",
michael@0 639 refImage: "image3ico16x16.png",
michael@0 640 refImageMimeType: "image/png"},
michael@0 641 {preImage: "image1.png",
michael@0 642 preImageMimeType: "image/png",
michael@0 643 refImage: "image1png64x64.jpg",
michael@0 644 refImageMimeType: "image/jpeg"}];
michael@0 645
michael@0 646 for(var i=0; i<testData.length; ++i) {
michael@0 647 var dict = testData[i];
michael@0 648
michael@0 649 var imgFile = do_get_file(dict["refImage"]);
michael@0 650 var istream = getFileInputStream(imgFile);
michael@0 651 var refBytes = streamToArray(istream);
michael@0 652
michael@0 653 imgFile = do_get_file(dict["preImage"]);
michael@0 654 istream = getFileInputStream(imgFile);
michael@0 655
michael@0 656 var container = imgTools.decodeImage(istream, dict["preImageMimeType"]);
michael@0 657
michael@0 658 istream = imgTools.encodeImage(container, dict["refImageMimeType"]);
michael@0 659
michael@0 660 var sstream = Cc["@mozilla.org/storagestream;1"].
michael@0 661 createInstance(Ci.nsIStorageStream);
michael@0 662 sstream.init(4096, 4294967295, null);
michael@0 663 var ostream = sstream.getOutputStream(0);
michael@0 664 var bostream = Cc["@mozilla.org/network/buffered-output-stream;1"].
michael@0 665 createInstance(Ci.nsIBufferedOutputStream);
michael@0 666
michael@0 667 //use a tiny buffer to make sure the image data doesn't fully fit in it
michael@0 668 bostream.init(ostream, 8);
michael@0 669
michael@0 670 bostream.writeFrom(istream, istream.available());
michael@0 671 bostream.flush(); bostream.close();
michael@0 672
michael@0 673 var encBytes = streamToArray(sstream.newInputStream(0));
michael@0 674
michael@0 675 compareArrays(refBytes, encBytes);
michael@0 676 }
michael@0 677
michael@0 678
michael@0 679 /* ========== bug 413512 ========== */
michael@0 680 testnum = 413512;
michael@0 681 testdesc = "test decoding bad favicon (bug 413512)";
michael@0 682
michael@0 683 imgName = "bug413512.ico";
michael@0 684 inMimeType = "image/x-icon";
michael@0 685 imgFile = do_get_file(imgName);
michael@0 686
michael@0 687 istream = getFileInputStream(imgFile);
michael@0 688 do_check_eq(istream.available(), 17759);
michael@0 689 var errsrc = "none";
michael@0 690
michael@0 691 try {
michael@0 692 container = imgTools.decodeImage(istream, inMimeType);
michael@0 693
michael@0 694 // We should never hit this - decodeImage throws an assertion because the
michael@0 695 // image decoded doesn't have enough frames.
michael@0 696 try {
michael@0 697 istream = imgTools.encodeImage(container, "image/png");
michael@0 698 } catch (e) {
michael@0 699 err = e;
michael@0 700 errsrc = "encode";
michael@0 701 }
michael@0 702 } catch (e) {
michael@0 703 err = e;
michael@0 704 errsrc = "decode";
michael@0 705 }
michael@0 706
michael@0 707 do_check_eq(errsrc, "decode");
michael@0 708 checkExpectedError(/NS_ERROR_FAILURE/, err);
michael@0 709
michael@0 710
michael@0 711 /* ========== bug 815359 ========== */
michael@0 712 testnum = 815359;
michael@0 713 testdesc = "test correct ico hotspots (bug 815359)";
michael@0 714
michael@0 715 imgName = "bug815359.ico";
michael@0 716 inMimeType = "image/x-icon";
michael@0 717 imgFile = do_get_file(imgName);
michael@0 718
michael@0 719 istream = getFileInputStream(imgFile);
michael@0 720 do_check_eq(istream.available(), 4286);
michael@0 721
michael@0 722 container = imgTools.decodeImage(istream, inMimeType);
michael@0 723
michael@0 724 var props = container.QueryInterface(Ci.nsIProperties);
michael@0 725
michael@0 726 do_check_eq(props.get("hotspotX", Ci.nsISupportsPRUint32).data, 10);
michael@0 727 do_check_eq(props.get("hotspotY", Ci.nsISupportsPRUint32).data, 9);
michael@0 728
michael@0 729
michael@0 730 /* ========== end ========== */
michael@0 731
michael@0 732 } catch (e) {
michael@0 733 throw "FAILED in test #" + testnum + " -- " + testdesc + ": " + e;
michael@0 734 }
michael@0 735 };

mercurial