gecko/extensions/universalchardet/tests/CharsetDetectionTests.js

82 lines
2.4 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;
function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList)
{
gExpectedCharset = aExpectedCharset;
gDetectorList = aDetectorList;
InitDetectorTests();
$("testframe").src = aTestFile;
SimpleTest.waitForExplicitFinish();
}
function InitDetectorTests()
{
var prefService = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
2008-04-21 03:50:45 -07:00
try {
gOldPref = prefService
.getComplexValue("intl.charset.detector",
Components.interfaces.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",
Components.interfaces.nsIPrefLocalizedString)
.data;
} catch (e) {
gExpectedCharset = "ISO-8859-8";
}
}
2008-04-21 03:50:45 -07:00
}
function SetDetectorPref(aPrefValue)
{
var prefService = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var str = Components.classes["@mozilla.org/supports-string;1"]
2008-04-21 03:50:45 -07:00
.createInstance(Components.interfaces.nsISupportsString);
str.data = aPrefValue;
prefService.setComplexValue("intl.charset.detector",
Components.interfaces.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();
}