1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/favicons/test_favicons_conversions.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,131 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * This file tests the image conversions done by the favicon service. 1.9 + */ 1.10 + 1.11 +//////////////////////////////////////////////////////////////////////////////// 1.12 +/// Globals 1.13 + 1.14 +// The pixel values we get on Windows are sometimes +/- 1 value compared to 1.15 +// other platforms, so we need to skip some image content tests. 1.16 +let isWindows = ("@mozilla.org/windows-registry-key;1" in Cc); 1.17 + 1.18 +/** 1.19 + * Checks the conversion of the given test image file. 1.20 + * 1.21 + * @param aFileName 1.22 + * File that contains the favicon image, located in the test folder. 1.23 + * @param aFileMimeType 1.24 + * MIME type of the image contained in the file. 1.25 + * @param aFileLength 1.26 + * Expected length of the file. 1.27 + * @param aExpectConversion 1.28 + * If false, the icon should be stored as is. If true, the expected data 1.29 + * is loaded from a file named "expected-" + aFileName + ".png". 1.30 + * @param aVaryOnWindows 1.31 + * Indicates that the content of the converted image can be different on 1.32 + * Windows and should not be checked on that platform. 1.33 + * @param aCallback 1.34 + * This function is called after the check finished. 1.35 + */ 1.36 +function checkFaviconDataConversion(aFileName, aFileMimeType, aFileLength, 1.37 + aExpectConversion, aVaryOnWindows, 1.38 + aCallback) { 1.39 + let pageURI = NetUtil.newURI("http://places.test/page/" + aFileName); 1.40 + promiseAddVisits({ uri: pageURI, transition: TRANSITION_TYPED }).then( 1.41 + function () { 1.42 + let faviconURI = NetUtil.newURI("http://places.test/icon/" + aFileName); 1.43 + let fileData = readFileOfLength(aFileName, aFileLength); 1.44 + 1.45 + PlacesUtils.favicons.replaceFaviconData(faviconURI, fileData, fileData.length, 1.46 + aFileMimeType); 1.47 + PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, faviconURI, true, 1.48 + PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, 1.49 + function CFDC_verify(aURI, aDataLen, aData, aMimeType) { 1.50 + if (!aExpectConversion) { 1.51 + do_check_true(compareArrays(aData, fileData)); 1.52 + do_check_eq(aMimeType, aFileMimeType); 1.53 + } else { 1.54 + if (!aVaryOnWindows || !isWindows) { 1.55 + let expectedFile = do_get_file("expected-" + aFileName + ".png"); 1.56 + do_check_true(compareArrays(aData, readFileData(expectedFile))); 1.57 + } 1.58 + do_check_eq(aMimeType, "image/png"); 1.59 + } 1.60 + 1.61 + aCallback(); 1.62 + }); 1.63 + }); 1.64 +} 1.65 + 1.66 +//////////////////////////////////////////////////////////////////////////////// 1.67 +/// Tests 1.68 + 1.69 +function run_test() { 1.70 + run_next_test(); 1.71 +} 1.72 + 1.73 +add_test(function test_storing_a_normal_16x16_icon() { 1.74 + // 16x16 png, 286 bytes. 1.75 + checkFaviconDataConversion("favicon-normal16.png", "image/png", 286, 1.76 + false, false, run_next_test); 1.77 +}); 1.78 + 1.79 +add_test(function test_storing_a_normal_32x32_icon() { 1.80 + // 32x32 png, 344 bytes. 1.81 + checkFaviconDataConversion("favicon-normal32.png", "image/png", 344, 1.82 + false, false, run_next_test); 1.83 +}); 1.84 + 1.85 +add_test(function test_storing_an_oversize_16x16_icon() { 1.86 + // in: 16x16 ico, 1406 bytes. 1.87 + // out: 16x16 png 1.88 + checkFaviconDataConversion("favicon-big16.ico", "image/x-icon", 1406, 1.89 + true, false, run_next_test); 1.90 +}); 1.91 + 1.92 +add_test(function test_storing_an_oversize_4x4_icon() { 1.93 + // in: 4x4 jpg, 4751 bytes. 1.94 + // out: 16x16 png 1.95 + checkFaviconDataConversion("favicon-big4.jpg", "image/jpeg", 4751, 1.96 + true, false, run_next_test); 1.97 +}); 1.98 + 1.99 +add_test(function test_storing_an_oversize_32x32_icon() { 1.100 + // in: 32x32 jpg, 3494 bytes. 1.101 + // out: 16x16 png 1.102 + checkFaviconDataConversion("favicon-big32.jpg", "image/jpeg", 3494, 1.103 + true, true, run_next_test); 1.104 +}); 1.105 + 1.106 +add_test(function test_storing_an_oversize_48x48_icon() { 1.107 + // in: 48x48 ico, 56646 bytes. 1.108 + // (howstuffworks.com icon, contains 13 icons with sizes from 16x16 to 1.109 + // 48x48 in varying depths) 1.110 + // out: 16x16 png 1.111 + checkFaviconDataConversion("favicon-big48.ico", "image/x-icon", 56646, 1.112 + true, false, run_next_test); 1.113 +}); 1.114 + 1.115 +add_test(function test_storing_an_oversize_64x64_icon() { 1.116 + // in: 64x64 png, 10698 bytes. 1.117 + // out: 16x16 png 1.118 + checkFaviconDataConversion("favicon-big64.png", "image/png", 10698, 1.119 + true, false, run_next_test); 1.120 +}); 1.121 + 1.122 +add_test(function test_scaling_an_oversize_160x3_icon() { 1.123 + // in: 160x3 jpg, 5095 bytes. 1.124 + // out: 16x16 png 1.125 + checkFaviconDataConversion("favicon-scale160x3.jpg", "image/jpeg", 5095, 1.126 + true, false, run_next_test); 1.127 +}); 1.128 + 1.129 +add_test(function test_scaling_an_oversize_3x160_icon() { 1.130 + // in: 3x160 jpg, 5059 bytes. 1.131 + // out: 16x16 png 1.132 + checkFaviconDataConversion("favicon-scale3x160.jpg", "image/jpeg", 5059, 1.133 + true, false, run_next_test); 1.134 +});