summaryrefslogtreecommitdiffstats
path: root/extensions/universalchardet/tests/CharsetDetectionTests.js
blob: a92b808e4502f831a06d84ef127f06b7cf5aeae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
/* vim: set ts=8 et sw=4 tw=80: */
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var gExpectedCharset;
var gLocalDir;

function CharsetDetectionTests(aTestFile, aExpectedCharset) {
  gExpectedCharset = aExpectedCharset;

  InitDetectorTests();

  var fileURI = gLocalDir + aTestFile;
  $("testframe").src = fileURI;

  SimpleTest.waitForExplicitFinish();
}

function InitDetectorTests() {
  var loader = Services.scriptloader;
  var ioService = Services.io;
  loader.loadSubScript("chrome://mochikit/content/chrome-harness.js");

  $("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 DoDetectionTest() {
  var iframeDoc = $("testframe").contentDocument;
  var charset = iframeDoc.characterSet;

  is(charset, gExpectedCharset, "decoded as " + gExpectedCharset);

  SimpleTest.finish();
}