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