michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * This file tests the image conversions done by the favicon service. michael@0: */ michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: /// Globals michael@0: michael@0: // The pixel values we get on Windows are sometimes +/- 1 value compared to michael@0: // other platforms, so we need to skip some image content tests. michael@0: let isWindows = ("@mozilla.org/windows-registry-key;1" in Cc); michael@0: michael@0: /** michael@0: * Checks the conversion of the given test image file. michael@0: * michael@0: * @param aFileName michael@0: * File that contains the favicon image, located in the test folder. michael@0: * @param aFileMimeType michael@0: * MIME type of the image contained in the file. michael@0: * @param aFileLength michael@0: * Expected length of the file. michael@0: * @param aExpectConversion michael@0: * If false, the icon should be stored as is. If true, the expected data michael@0: * is loaded from a file named "expected-" + aFileName + ".png". michael@0: * @param aVaryOnWindows michael@0: * Indicates that the content of the converted image can be different on michael@0: * Windows and should not be checked on that platform. michael@0: * @param aCallback michael@0: * This function is called after the check finished. michael@0: */ michael@0: function checkFaviconDataConversion(aFileName, aFileMimeType, aFileLength, michael@0: aExpectConversion, aVaryOnWindows, michael@0: aCallback) { michael@0: let pageURI = NetUtil.newURI("http://places.test/page/" + aFileName); michael@0: promiseAddVisits({ uri: pageURI, transition: TRANSITION_TYPED }).then( michael@0: function () { michael@0: let faviconURI = NetUtil.newURI("http://places.test/icon/" + aFileName); michael@0: let fileData = readFileOfLength(aFileName, aFileLength); michael@0: michael@0: PlacesUtils.favicons.replaceFaviconData(faviconURI, fileData, fileData.length, michael@0: aFileMimeType); michael@0: PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, faviconURI, true, michael@0: PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, michael@0: function CFDC_verify(aURI, aDataLen, aData, aMimeType) { michael@0: if (!aExpectConversion) { michael@0: do_check_true(compareArrays(aData, fileData)); michael@0: do_check_eq(aMimeType, aFileMimeType); michael@0: } else { michael@0: if (!aVaryOnWindows || !isWindows) { michael@0: let expectedFile = do_get_file("expected-" + aFileName + ".png"); michael@0: do_check_true(compareArrays(aData, readFileData(expectedFile))); michael@0: } michael@0: do_check_eq(aMimeType, "image/png"); michael@0: } michael@0: michael@0: aCallback(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: /// Tests michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_storing_a_normal_16x16_icon() { michael@0: // 16x16 png, 286 bytes. michael@0: checkFaviconDataConversion("favicon-normal16.png", "image/png", 286, michael@0: false, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_a_normal_32x32_icon() { michael@0: // 32x32 png, 344 bytes. michael@0: checkFaviconDataConversion("favicon-normal32.png", "image/png", 344, michael@0: false, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_an_oversize_16x16_icon() { michael@0: // in: 16x16 ico, 1406 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-big16.ico", "image/x-icon", 1406, michael@0: true, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_an_oversize_4x4_icon() { michael@0: // in: 4x4 jpg, 4751 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-big4.jpg", "image/jpeg", 4751, michael@0: true, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_an_oversize_32x32_icon() { michael@0: // in: 32x32 jpg, 3494 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-big32.jpg", "image/jpeg", 3494, michael@0: true, true, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_an_oversize_48x48_icon() { michael@0: // in: 48x48 ico, 56646 bytes. michael@0: // (howstuffworks.com icon, contains 13 icons with sizes from 16x16 to michael@0: // 48x48 in varying depths) michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-big48.ico", "image/x-icon", 56646, michael@0: true, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_storing_an_oversize_64x64_icon() { michael@0: // in: 64x64 png, 10698 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-big64.png", "image/png", 10698, michael@0: true, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_scaling_an_oversize_160x3_icon() { michael@0: // in: 160x3 jpg, 5095 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-scale160x3.jpg", "image/jpeg", 5095, michael@0: true, false, run_next_test); michael@0: }); michael@0: michael@0: add_test(function test_scaling_an_oversize_3x160_icon() { michael@0: // in: 3x160 jpg, 5059 bytes. michael@0: // out: 16x16 png michael@0: checkFaviconDataConversion("favicon-scale3x160.jpg", "image/jpeg", 5059, michael@0: true, false, run_next_test); michael@0: });