michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 et sw=4 tw=80: */ michael@0: var gExpectedCharset; michael@0: var gOldPref; michael@0: var gDetectorList; michael@0: var gTestIndex; michael@0: var gLocalDir; michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList) michael@0: { michael@0: gExpectedCharset = aExpectedCharset; michael@0: gDetectorList = aDetectorList; michael@0: michael@0: InitDetectorTests(); michael@0: michael@0: var fileURI = gLocalDir + aTestFile; michael@0: $("testframe").src = fileURI; michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: } michael@0: michael@0: function InitDetectorTests() michael@0: { michael@0: var prefService = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefBranch); michael@0: var str = Cc["@mozilla.org/supports-string;1"] michael@0: .createInstance(Ci.nsISupportsString); michael@0: var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader); michael@0: var ioService = Cc['@mozilla.org/network/io-service;1'] michael@0: .getService(Ci.nsIIOService); michael@0: loader.loadSubScript("chrome://mochikit/content/chrome-harness.js"); michael@0: michael@0: try { michael@0: gOldPref = prefService michael@0: .getComplexValue("intl.charset.detector", michael@0: Ci.nsIPrefLocalizedString).data; michael@0: } catch (e) { michael@0: gOldPref = ""; michael@0: } michael@0: SetDetectorPref(gDetectorList[0]); michael@0: gTestIndex = 0; michael@0: $("testframe").onload = DoDetectionTest; michael@0: michael@0: if (gExpectedCharset == "default") { michael@0: // No point trying to be generic here, because we have plenty of other michael@0: // unit tests that fail if run using a non-windows-1252 locale. michael@0: gExpectedCharset = "windows-1252"; michael@0: } michael@0: michael@0: // Get the local directory. This needs to be a file: URI because chrome: michael@0: // URIs are always UTF-8 (bug 617339) and we are testing decoding from other michael@0: // charsets. michael@0: var jar = getJar(getRootDirectory(window.location.href)); michael@0: var dir = jar ? michael@0: extractJarToTmp(jar) : michael@0: getChromeDir(getResolvedURI(window.location.href)); michael@0: gLocalDir = ioService.newFileURI(dir).spec; michael@0: } michael@0: michael@0: function SetDetectorPref(aPrefValue) michael@0: { michael@0: var prefService = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefBranch); michael@0: var str = Cc["@mozilla.org/supports-string;1"] michael@0: .createInstance(Ci.nsISupportsString); michael@0: str.data = aPrefValue; michael@0: prefService.setComplexValue("intl.charset.detector", michael@0: Ci.nsISupportsString, str); michael@0: gCurrentDetector = aPrefValue; michael@0: } michael@0: michael@0: function DoDetectionTest() { michael@0: var iframeDoc = $("testframe").contentDocument; michael@0: var charset = iframeDoc.characterSet; michael@0: michael@0: is(charset, gExpectedCharset, michael@0: "decoded as " + gExpectedCharset + " by " + gDetectorList[gTestIndex]); michael@0: michael@0: if (++gTestIndex < gDetectorList.length) { michael@0: SetDetectorPref(gDetectorList[gTestIndex]); michael@0: iframeDoc.location.reload(); michael@0: } else { michael@0: CleanUpDetectionTests(); michael@0: } michael@0: } michael@0: michael@0: function CleanUpDetectionTests() { michael@0: SetDetectorPref(gOldPref); michael@0: SimpleTest.finish(); michael@0: } michael@0: