summaryrefslogtreecommitdiffstats
path: root/extensions/universalchardet/tests/CharsetDetectionTests.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /extensions/universalchardet/tests/CharsetDetectionTests.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'extensions/universalchardet/tests/CharsetDetectionTests.js')
-rw-r--r--extensions/universalchardet/tests/CharsetDetectionTests.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/extensions/universalchardet/tests/CharsetDetectionTests.js b/extensions/universalchardet/tests/CharsetDetectionTests.js
new file mode 100644
index 000000000..6ccc9c3f1
--- /dev/null
+++ b/extensions/universalchardet/tests/CharsetDetectionTests.js
@@ -0,0 +1,94 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
+/* vim: set ts=8 et sw=4 tw=80: */
+var gExpectedCharset;
+var gOldPref;
+var gDetectorList;
+var gTestIndex;
+var gLocalDir;
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+
+function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList)
+{
+ gExpectedCharset = aExpectedCharset;
+ gDetectorList = aDetectorList;
+
+ InitDetectorTests();
+
+ var fileURI = gLocalDir + aTestFile;
+ $("testframe").src = fileURI;
+
+ 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);
+ 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");
+
+ try {
+ gOldPref = prefService
+ .getComplexValue("intl.charset.detector",
+ Ci.nsIPrefLocalizedString).data;
+ } catch (e) {
+ gOldPref = "";
+ }
+ SetDetectorPref(gDetectorList[0]);
+ gTestIndex = 0;
+ $("testframe").onload = DoDetectionTest;
+
+ if (gExpectedCharset == "default") {
+ // No point trying to be generic here, because we have plenty of other
+ // unit tests that fail if run using a non-windows-1252 locale.
+ gExpectedCharset = "windows-1252";
+ }
+
+ // Get the local directory. This needs to be a file: URI because chrome:
+ // URIs are always UTF-8 (bug 617339) and we are testing decoding from other
+ // charsets.
+ var jar = getJar(getRootDirectory(window.location.href));
+ var dir = jar ?
+ extractJarToTmp(jar) :
+ getChromeDir(getResolvedURI(window.location.href));
+ gLocalDir = ioService.newFileURI(dir).spec;
+}
+
+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);
+ str.data = aPrefValue;
+ prefService.setComplexValue("intl.charset.detector",
+ Ci.nsISupportsString, str);
+ 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();
+ } else {
+ CleanUpDetectionTests();
+ }
+}
+
+function CleanUpDetectionTests() {
+ SetDetectorPref(gOldPref);
+ SimpleTest.finish();
+}
+