1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/universalchardet/tests/CharsetDetectionTests.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim: set ts=8 et sw=4 tw=80: */ 1.6 +var gExpectedCharset; 1.7 +var gOldPref; 1.8 +var gDetectorList; 1.9 +var gTestIndex; 1.10 +var gLocalDir; 1.11 +const Cc = Components.classes; 1.12 +const Ci = Components.interfaces; 1.13 + 1.14 +function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList) 1.15 +{ 1.16 + gExpectedCharset = aExpectedCharset; 1.17 + gDetectorList = aDetectorList; 1.18 + 1.19 + InitDetectorTests(); 1.20 + 1.21 + var fileURI = gLocalDir + aTestFile; 1.22 + $("testframe").src = fileURI; 1.23 + 1.24 + SimpleTest.waitForExplicitFinish(); 1.25 +} 1.26 + 1.27 +function InitDetectorTests() 1.28 +{ 1.29 + var prefService = Cc["@mozilla.org/preferences-service;1"] 1.30 + .getService(Ci.nsIPrefBranch); 1.31 + var str = Cc["@mozilla.org/supports-string;1"] 1.32 + .createInstance(Ci.nsISupportsString); 1.33 + var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.34 + .getService(Ci.mozIJSSubScriptLoader); 1.35 + var ioService = Cc['@mozilla.org/network/io-service;1'] 1.36 + .getService(Ci.nsIIOService); 1.37 + loader.loadSubScript("chrome://mochikit/content/chrome-harness.js"); 1.38 + 1.39 + try { 1.40 + gOldPref = prefService 1.41 + .getComplexValue("intl.charset.detector", 1.42 + Ci.nsIPrefLocalizedString).data; 1.43 + } catch (e) { 1.44 + gOldPref = ""; 1.45 + } 1.46 + SetDetectorPref(gDetectorList[0]); 1.47 + gTestIndex = 0; 1.48 + $("testframe").onload = DoDetectionTest; 1.49 + 1.50 + if (gExpectedCharset == "default") { 1.51 + // No point trying to be generic here, because we have plenty of other 1.52 + // unit tests that fail if run using a non-windows-1252 locale. 1.53 + gExpectedCharset = "windows-1252"; 1.54 + } 1.55 + 1.56 + // Get the local directory. This needs to be a file: URI because chrome: 1.57 + // URIs are always UTF-8 (bug 617339) and we are testing decoding from other 1.58 + // charsets. 1.59 + var jar = getJar(getRootDirectory(window.location.href)); 1.60 + var dir = jar ? 1.61 + extractJarToTmp(jar) : 1.62 + getChromeDir(getResolvedURI(window.location.href)); 1.63 + gLocalDir = ioService.newFileURI(dir).spec; 1.64 +} 1.65 + 1.66 +function SetDetectorPref(aPrefValue) 1.67 +{ 1.68 + var prefService = Cc["@mozilla.org/preferences-service;1"] 1.69 + .getService(Ci.nsIPrefBranch); 1.70 + var str = Cc["@mozilla.org/supports-string;1"] 1.71 + .createInstance(Ci.nsISupportsString); 1.72 + str.data = aPrefValue; 1.73 + prefService.setComplexValue("intl.charset.detector", 1.74 + Ci.nsISupportsString, str); 1.75 + gCurrentDetector = aPrefValue; 1.76 +} 1.77 + 1.78 +function DoDetectionTest() { 1.79 + var iframeDoc = $("testframe").contentDocument; 1.80 + var charset = iframeDoc.characterSet; 1.81 + 1.82 + is(charset, gExpectedCharset, 1.83 + "decoded as " + gExpectedCharset + " by " + gDetectorList[gTestIndex]); 1.84 + 1.85 + if (++gTestIndex < gDetectorList.length) { 1.86 + SetDetectorPref(gDetectorList[gTestIndex]); 1.87 + iframeDoc.location.reload(); 1.88 + } else { 1.89 + CleanUpDetectionTests(); 1.90 + } 1.91 +} 1.92 + 1.93 +function CleanUpDetectionTests() { 1.94 + SetDetectorPref(gOldPref); 1.95 + SimpleTest.finish(); 1.96 +} 1.97 +