test_bug415761.html (3598B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for icon filenames</title> 5 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 11 <pre id="test"> 12 <script class="testbody" type="text/javascript"> 13 14 SimpleTest.waitForExplicitFinish(); 15 16 // We want to make sure that moz-icon URIs with non-ascii characters work. To that 17 // end, we compare the rendering of an icon without non-ascii characters to that 18 // of one that does include such characters. 19 20 // First, obtain the file URI to the ourselves: 21 var chromeURI = location.href; 22 var io = Services.io; 23 chromeURI = io.newURI(chromeURI); 24 var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"] 25 .getService(Ci.nsIChromeRegistry); 26 var fileURI = chromeReg.convertChromeURL(chromeURI); 27 fileURI.QueryInterface(Ci.nsIFileURL); 28 var self = fileURI.file; 29 30 // Check if the ref or test icon are still hanging around from a previous test 31 var testDest = self.parent; 32 var refDest = self.parent; 33 testDest.append("\u263a.ico"); 34 refDest.append("bug415761-ref.ico"); 35 if (testDest.exists()) { 36 testDest.remove(false); 37 } 38 if (refDest.exists()) { 39 refDest.remove(false); 40 } 41 42 // Copy the source icon so that we have two identical icons with, one with 43 // non-ascii characters in its name. 44 var src = self.parent; 45 src.append("bug415761.ico"); 46 src.copyTo(null, testDest.leafName); 47 src.copyTo(null, refDest.leafName); 48 49 // Now load both icons in an Image() with a moz-icon URI 50 var testImage = new Image(); 51 var refImage = new Image(); 52 53 var loadedImages = 0; 54 testImage.onload = refImage.onload = function() { 55 loadedImages++; 56 if (loadedImages == 2) { 57 finishTest(); 58 } 59 }; 60 testImage.onerror = refImage.onerror = function() { 61 testImage.onload = refImage.onload = function() {}; 62 63 ok(false, "Icon did not load successfully"); 64 SimpleTest.finish(); 65 }; 66 67 function finishTest() { 68 ok(true, "Both icons loaded successfully"); 69 // Render the reference to a canvas 70 var refCanvas = document.createElement("canvas"); 71 refCanvas.setAttribute("height", 32); 72 refCanvas.setAttribute("width", 32); 73 refCanvas.getContext('2d').drawImage(refImage, 0, 0, 32, 32); 74 75 // A blank canvas to compare to to make sure we don't draw nothing. 76 var blankCanvas = document.createElement("canvas"); 77 blankCanvas.setAttribute("height", 32); 78 blankCanvas.setAttribute("width", 32); 79 80 // Assert that they should be the different. 81 if (!navigator.userAgent.includes("Windows NT 6.1")) { 82 // Fails on Windows 7 for some reason. 83 assertSnapshots(blankCanvas, refCanvas, false, 0, "blank", "reference icon"); 84 } 85 86 // Render the icon with a non-ascii character in its name to a canvas 87 var testCanvas = document.createElement("canvas"); 88 testCanvas.setAttribute("height", 32); 89 testCanvas.setAttribute("width", 32); 90 testCanvas.getContext('2d').drawImage(testImage, 0, 0, 32, 32); 91 92 // Assert that they should be the same. 93 assertSnapshots(testCanvas, refCanvas, true, 0, "icon", "reference icon"); 94 SimpleTest.finish(); 95 }; 96 97 var testURI = io.newFileURI(testDest).spec; 98 var refURI = io.newFileURI(refDest).spec; 99 testImage.src = "moz-icon:" + testURI; 100 refImage.src = "moz-icon:" + refURI; 101 102 SimpleTest.registerCleanupFunction(function() { 103 // Remove the copied files if they exist. 104 if (testDest.exists()) { 105 testDest.remove(false); 106 } 107 if (refDest.exists()) { 108 refDest.remove(false); 109 } 110 }); 111 112 </script> 113 </pre> 114 </body> 115 116 </html>