test_imgtools.js 21 KB

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