extensions/universalchardet/tests/CharsetDetectionTests.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim: set ts=8 et sw=4 tw=80: */
michael@0 3 var gExpectedCharset;
michael@0 4 var gOldPref;
michael@0 5 var gDetectorList;
michael@0 6 var gTestIndex;
michael@0 7 var gLocalDir;
michael@0 8 const Cc = Components.classes;
michael@0 9 const Ci = Components.interfaces;
michael@0 10
michael@0 11 function CharsetDetectionTests(aTestFile, aExpectedCharset, aDetectorList)
michael@0 12 {
michael@0 13 gExpectedCharset = aExpectedCharset;
michael@0 14 gDetectorList = aDetectorList;
michael@0 15
michael@0 16 InitDetectorTests();
michael@0 17
michael@0 18 var fileURI = gLocalDir + aTestFile;
michael@0 19 $("testframe").src = fileURI;
michael@0 20
michael@0 21 SimpleTest.waitForExplicitFinish();
michael@0 22 }
michael@0 23
michael@0 24 function InitDetectorTests()
michael@0 25 {
michael@0 26 var prefService = Cc["@mozilla.org/preferences-service;1"]
michael@0 27 .getService(Ci.nsIPrefBranch);
michael@0 28 var str = Cc["@mozilla.org/supports-string;1"]
michael@0 29 .createInstance(Ci.nsISupportsString);
michael@0 30 var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 31 .getService(Ci.mozIJSSubScriptLoader);
michael@0 32 var ioService = Cc['@mozilla.org/network/io-service;1']
michael@0 33 .getService(Ci.nsIIOService);
michael@0 34 loader.loadSubScript("chrome://mochikit/content/chrome-harness.js");
michael@0 35
michael@0 36 try {
michael@0 37 gOldPref = prefService
michael@0 38 .getComplexValue("intl.charset.detector",
michael@0 39 Ci.nsIPrefLocalizedString).data;
michael@0 40 } catch (e) {
michael@0 41 gOldPref = "";
michael@0 42 }
michael@0 43 SetDetectorPref(gDetectorList[0]);
michael@0 44 gTestIndex = 0;
michael@0 45 $("testframe").onload = DoDetectionTest;
michael@0 46
michael@0 47 if (gExpectedCharset == "default") {
michael@0 48 // No point trying to be generic here, because we have plenty of other
michael@0 49 // unit tests that fail if run using a non-windows-1252 locale.
michael@0 50 gExpectedCharset = "windows-1252";
michael@0 51 }
michael@0 52
michael@0 53 // Get the local directory. This needs to be a file: URI because chrome:
michael@0 54 // URIs are always UTF-8 (bug 617339) and we are testing decoding from other
michael@0 55 // charsets.
michael@0 56 var jar = getJar(getRootDirectory(window.location.href));
michael@0 57 var dir = jar ?
michael@0 58 extractJarToTmp(jar) :
michael@0 59 getChromeDir(getResolvedURI(window.location.href));
michael@0 60 gLocalDir = ioService.newFileURI(dir).spec;
michael@0 61 }
michael@0 62
michael@0 63 function SetDetectorPref(aPrefValue)
michael@0 64 {
michael@0 65 var prefService = Cc["@mozilla.org/preferences-service;1"]
michael@0 66 .getService(Ci.nsIPrefBranch);
michael@0 67 var str = Cc["@mozilla.org/supports-string;1"]
michael@0 68 .createInstance(Ci.nsISupportsString);
michael@0 69 str.data = aPrefValue;
michael@0 70 prefService.setComplexValue("intl.charset.detector",
michael@0 71 Ci.nsISupportsString, str);
michael@0 72 gCurrentDetector = aPrefValue;
michael@0 73 }
michael@0 74
michael@0 75 function DoDetectionTest() {
michael@0 76 var iframeDoc = $("testframe").contentDocument;
michael@0 77 var charset = iframeDoc.characterSet;
michael@0 78
michael@0 79 is(charset, gExpectedCharset,
michael@0 80 "decoded as " + gExpectedCharset + " by " + gDetectorList[gTestIndex]);
michael@0 81
michael@0 82 if (++gTestIndex < gDetectorList.length) {
michael@0 83 SetDetectorPref(gDetectorList[gTestIndex]);
michael@0 84 iframeDoc.location.reload();
michael@0 85 } else {
michael@0 86 CleanUpDetectionTests();
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 function CleanUpDetectionTests() {
michael@0 91 SetDetectorPref(gOldPref);
michael@0 92 SimpleTest.finish();
michael@0 93 }
michael@0 94

mercurial