gecko/extensions/universalchardet/tests/CharsetDetectionTests.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 et sw=4 tw=80: */
2008-04-21 03:50:45 -07:00
var gExpectedCharset;
var gOldPref;
var gDetectorList;
var gTestIndex;
const Cc = Components.classes;
const Ci = Components.interfaces;
2008-04-21 03:50:45 -07:00
function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList)
{
gExpectedCharset = aExpectedCharset;
gDetectorList = aDetectorList;
InitDetectorTests();
// convert aTestFile to a file:// URI
var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
var ioService = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);
loader.loadSubScript("chrome://mochikit/content/chrome-harness.js");
var jar = getJar(getRootDirectory(window.location.href));
var dir = jar ?
extractJarToTmp(jar) :
getChromeDir(getResolvedURI(window.location.href));
var fileURI = ioService.newFileURI(dir).spec + aTestFile;
$("testframe").src = fileURI;
2008-04-21 03:50:45 -07:00
SimpleTest.waitForExplicitFinish();
}
function InitDetectorTests()
{
var prefService = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefBranch);
var str = Cc["@mozilla.org/supports-string;1"]
.createInstance(Ci.nsISupportsString);
2008-04-21 03:50:45 -07:00
try {
gOldPref = prefService
.getComplexValue("intl.charset.detector",
Ci.nsIPrefLocalizedString).data;
2008-04-21 03:50:45 -07:00
} catch (e) {
gOldPref = "";
2008-04-21 03:50:45 -07:00
}
SetDetectorPref(gDetectorList[0]);
gTestIndex = 0;
$("testframe").onload = DoDetectionTest;
if (gExpectedCharset == "default") {
try {
gExpectedCharset = prefService
.getComplexValue("intl.charset.default",
Ci.nsIPrefLocalizedString)
.data;
} catch (e) {
gExpectedCharset = "ISO-8859-8";
}
}
2008-04-21 03:50:45 -07:00
}
function SetDetectorPref(aPrefValue)
{
var prefService = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefBranch);
var str = Cc["@mozilla.org/supports-string;1"]
.createInstance(Ci.nsISupportsString);
2008-04-21 03:50:45 -07:00
str.data = aPrefValue;
prefService.setComplexValue("intl.charset.detector",
Ci.nsISupportsString, str);
2008-04-21 03:50:45 -07:00
gCurrentDetector = aPrefValue;
}
function DoDetectionTest() {
var iframeDoc = $("testframe").contentDocument;
var charset = iframeDoc.characterSet;
is(charset, gExpectedCharset,
"decoded as " + gExpectedCharset + " by " + gDetectorList[gTestIndex]);
if (++gTestIndex < gDetectorList.length) {
SetDetectorPref(gDetectorList[gTestIndex]);
iframeDoc.location.reload();
2008-04-21 03:50:45 -07:00
} else {
CleanUpDetectionTests();
2008-04-21 03:50:45 -07:00
}
}
function CleanUpDetectionTests() {
SetDetectorPref(gOldPref);
SimpleTest.finish();
}