Mochitests for charset autodetection
authorsmontagu@smontagu.org
Mon, 21 Apr 2008 03:50:45 -0700
changeset 14511 5b21581e6f3cbe35f289d9674d47808f14701a40
parent 14510 559263457a69023273f7065736b36ca1257e296c
child 14512 057dd829939cf6b1023a9d706cf502c5310c14b8
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
milestone1.9pre
Mochitests for charset autodetection
extensions/universalchardet/tests/CharsetDetectionTests.js
extensions/universalchardet/tests/Makefile.in
extensions/universalchardet/tests/bug171813_text.html
extensions/universalchardet/tests/bug306272_text.html
extensions/universalchardet/tests/bug421271_text.html
extensions/universalchardet/tests/bug426271_text-euc-jp.html
extensions/universalchardet/tests/bug426271_text-utf-8.html
extensions/universalchardet/tests/test_bug171813.html
extensions/universalchardet/tests/test_bug306272.html
extensions/universalchardet/tests/test_bug421271.html
extensions/universalchardet/tests/test_bug426271-euc-jp.html
extensions/universalchardet/tests/test_bug426271-utf-8.html
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/CharsetDetectionTests.js
@@ -0,0 +1,70 @@
+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()
+{
+    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+    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);
+
+    try {
+	gOldPref = prefService
+	    .getComplexValue("intl.charset.detector",
+			     Components.interfaces.nsIPrefLocalizedString).data;
+    } catch (e) {
+	gOldPref = "";
+    }
+    SetDetectorPref(gDetectorList[0]);
+    gTestIndex = 0;
+    $("testframe").onload = DoDetectionTest;
+}
+
+function SetDetectorPref(aPrefValue)
+{
+    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+    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);
+    str.data = aPrefValue;
+    prefService.setComplexValue("intl.charset.detector",
+				Components.interfaces.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();
+}
+
--- a/extensions/universalchardet/tests/Makefile.in
+++ b/extensions/universalchardet/tests/Makefile.in
@@ -66,18 +66,25 @@ LIBS		+= \
 		$(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
 ifdef MOZ_MOCHITEST
 relativesrcdir = extensions/universalchardet/tests
 
 _TEST_FILES =	\
-		test_bug426271-euc-jp.html \
+		CharsetDetectionTests.js \
+		bug171813_text.html \
+		test_bug171813.html \
+		bug306272_text.html \
+		test_bug306272.html \
+		bug421271_text.html \
+		test_bug421271.html \
 		bug426271_text-euc-jp.html \
+		test_bug426271-euc-jp.html \
+		bug426271_text-utf-8.html \
 		test_bug426271-utf-8.html \
-		bug426271_text-utf-8.html \
 		$(NULL)
 
 libs:: $(_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
 
 endif # MOZ_MOCHITEST
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/bug171813_text.html
@@ -0,0 +1,36 @@
+<body leftmargin="0" topmargin="0" bgcolor="#fffeeb">
+<table border=0 cellpadding=0 cellspacing=0 height=22 width=160>
+  <tbody> 
+  <tr bgcolor=C40001> 
+    <td align=middle class=cn 
+        height=22 > 
+      <div align="center"><a href="http://www.sol.sohu.com" class="white" target="_blank"><font color="#ffffff"><b>搜狐在线</b></font></a></div>
+    </td>
+  </tr>
+  </tbody> 
+</table>
+<table border=0 cellpadding=0 cellspacing=2 width=160>
+  <tr> 
+    <td class=sca valign=top> 
+      <div align="center">
+      </div>
+    </td>
+  </tr>
+  <tr> 
+    <td class=sca valign=top> 
+      <div align="center"><a href="http://www.sol.sohu.com/promote/nd.htm" target="_blank">搜狐在线国庆免费大酬宾</a></div>
+    </td>
+  </tr>
+  <tr> 
+    <td class=sca valign=top> 
+      <p align="center"><a href="http://www.sol.sohu.com/game/crossgate/"><font color="red">拨95933上网魔力宝贝免费玩</font></a></p>
+    </td>
+  </tr>
+  <tr> 
+    <td class=sca valign=top> 
+      <div align="center"><a href="http://www.sol.sohu.com/promote/95933/" target="_blank">95933网费天天最低6毛/小时</a></div>
+    </td>
+  </tr>
+  <tbody> </tbody> 
+</table>
+
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/bug306272_text.html
@@ -0,0 +1,9 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html><head>
+
+  <title>306272</title>
+
+  
+</head><body>
+<a href="mailto:Antti.Nayha@somewhere.fi">Antti N盲yh盲 &lt;Antti.Nayha@somewhere.fi&gt;</a>
+</body></html>
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/bug421271_text.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html><head>
+<meta http-equiv="Content-Type" content="text/html"><title>BBC - 606 - A Forum Conversation</title>
+</head>
+<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0"><p class="posted">posted 5 Weeks Ago</p><p>if rangers draw and marseille and benfica win i stand to lift 825. not bad for a 2 bet.<br>50p on 3 homes<br>Man Utd/Marseille/Benfica<br>50p on 3 Draws<br>Rangers/Halifax/Bristol City<br>50p on 3 Aways<br>Doncaster/Stranraer/Rushden &amp; Diamonds<br>and 50p on all nine results.<br>GET IN THERE.</p></body></html>
\ No newline at end of file
--- a/extensions/universalchardet/tests/bug426271_text-euc-jp.html
+++ b/extensions/universalchardet/tests/bug426271_text-euc-jp.html
@@ -1,11 +1,11 @@
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html lang="ja">
 <head>
 <title>泣塑胳エンコ〖ドテスト</title>
 </head>
 <body>
-<span id="testtext">これはEUC-JPです牢」、</span>ある疥に灰丁のいない戏勺韶が交んでいた。ある泣、お翘さんが李で丽迈をしていると、络きな彭が萎れて丸たので、お天さんと咯べようと积ち耽った。企客で彭を充ると面から盟の灰が栏まれたので、≈彭吕虾∽と叹烧けて络祸に伴てた。
+これはEUC-JPです牢」、ある疥に灰丁のいない戏勺韶が交んでいた。ある泣、お翘さんが李で丽迈をしていると、络きな彭が萎れて丸たので、お天さんと咯べようと积ち耽った。企客で彭を充ると面から盟の灰が栏まれたので、≈彭吕虾∽と叹烧けて络祸に伴てた。
 
 喇墓した彭吕虾は、荡ヶ喷の荡が客」を鹅しめていることを梦り、荡锣迹を疯罢する。尉科から敌媚灰を翊侍に蹄い、苹面にそれを尸け涂えてイヌ、サル、キジを踩丸に骄える。荡ヶ喷で荡と里い、斧祸に尽网を箭め、荡が数」から氓っていった衡术を积ち耽り、お天さんˇお翘さんの傅に手り、宫せに孰らしたという。叫诺: フリ〖纱彩祸诺∝ウィキペディア∈Wikipedia∷≠
 </body>
 </html>
--- a/extensions/universalchardet/tests/bug426271_text-utf-8.html
+++ b/extensions/universalchardet/tests/bug426271_text-utf-8.html
@@ -1,11 +1,11 @@
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html lang="ja">
 <head>
 <title>鏃ユ湰瑾炪偍銉炽偝銉笺儔銉嗐偣銉</title>
 </head>
 <body>
-<span id="testtext">銇撱倢銇疷TF-8銇с仚鏄斻呫</span>銇傘倠鎵銇瓙渚涖伄銇勩仾銇勮佸か濠︺亴浣忋倱銇с亜銇熴傘亗銈嬫棩銆併亰濠嗐仌銈撱亴宸濄仹娲楁刊銈掋仐銇︺亜銈嬨仺銆佸ぇ銇嶃仾妗冦亴娴併倢銇︽潵銇熴伄銇с併亰鐖恒仌銈撱仺椋熴伖銈堛亞銇ㄦ寔銇″赴銇c仧銆備簩浜恒仹妗冦倰鍓层倠銇ㄤ腑銇嬨倝鐢枫伄瀛愩亴鐢熴伨銈屻仧銇仹銆併屾澶儙銆嶃仺鍚嶄粯銇戙仸澶т簨銇偛銇︺仧銆
+銇撱倢銇疷TF-8銇с仚鏄斻呫併亗銈嬫墍銇瓙渚涖伄銇勩仾銇勮佸か濠︺亴浣忋倱銇с亜銇熴傘亗銈嬫棩銆併亰濠嗐仌銈撱亴宸濄仹娲楁刊銈掋仐銇︺亜銈嬨仺銆佸ぇ銇嶃仾妗冦亴娴併倢銇︽潵銇熴伄銇с併亰鐖恒仌銈撱仺椋熴伖銈堛亞銇ㄦ寔銇″赴銇c仧銆備簩浜恒仹妗冦倰鍓层倠銇ㄤ腑銇嬨倝鐢枫伄瀛愩亴鐢熴伨銈屻仧銇仹銆併屾澶儙銆嶃仺鍚嶄粯銇戙仸澶т簨銇偛銇︺仧銆
 
 鎴愰暦銇椼仧妗冨お閮庛伅銆侀銉跺扯銇銇屼汉銆呫倰鑻︺仐銈併仸銇勩倠銇撱仺銈掔煡銈娿侀閫娌汇倰姹烘剰銇欍倠銆備浮瑕亱銈夐粛鍥e瓙銈掗鍒ャ伀璨般亜銆侀亾涓伀銇濄倢銈掑垎銇戜笌銇堛仸銈ゃ儗銆併偟銉併偔銈搞倰瀹舵潵銇緭銇堛倠銆傞銉跺扯銇ч銇ㄦ垿銇勩佽浜嬨伀鍕濆埄銈掑弾銈併侀銇屾柟銆呫亱銈夊オ銇c仸銇勩仯銇熻病瀹濄倰鎸併仭甯般倞銆併亰鐖恒仌銈撱兓銇婂﹩銇曘倱銇厓銇繑銈娿佸垢銇涖伀鏆倝銇椼仧銇ㄣ亜銇嗐傚嚭鍏: 銉曘儶銉肩櫨绉戜簨鍏搞庛偊銈c偔銉氥儑銈c偄锛圵ikipedia锛夈
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/test_bug171813.html
@@ -0,0 +1,32 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=171813
+-->
+<head>
+  <title>Test for Bug 171813</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="CharsetDetectionTests.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=171813">Mozilla Bug 171813</a>
+<p id="display"></p>
+<div id="content" style="display: none">  
+</div>
+<iframe id="testframe"></iframe>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+/** Test for Bug 171813 **/
+CharsetDetectionTests("bug171813_text.html",
+		      "gb18030",
+		      new Array("zh_parallel_state_machine",
+				"zhcn_parallel_state_machine",
+				"cjk_parallel_state_machine",
+				"universal_charset_detector"));
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/test_bug306272.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=306272
+-->
+<head>
+  <title>Test for Bug 306272</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="CharsetDetectionTests.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=306272">Mozilla Bug 306272</a>
+<p id="display"></p>
+<div id="content" style="display: none">  
+</div>
+<iframe id="testframe"></iframe>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+/** Test for Bug 306272 **/
+CharsetDetectionTests("bug306272_text.html",
+		      "UTF-8",
+		      new Array("ja_parallel_state_machine",
+				"ko_parallel_state_machine",
+				"zh_parallel_state_machine",
+				"zhtw_parallel_state_machine",
+				"zhcn_parallel_state_machine",
+				"cjk_parallel_state_machine",
+				"universal_charset_detector"));
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/extensions/universalchardet/tests/test_bug421271.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=421271
+-->
+<head>
+  <title>Test for Bug 421271</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="CharsetDetectionTests.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=421271">Mozilla Bug 421271</a>
+<p id="display"></p>
+<div id="content" style="display: none">  
+</div>
+<iframe id="testframe"></iframe>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+/** Test for Bug 421271 **/
+CharsetDetectionTests("bug421271_text.html",
+		      "windows-1252",
+		      new Array("ja_parallel_state_machine",
+				"ko_parallel_state_machine",
+				"zh_parallel_state_machine",
+				"zhtw_parallel_state_machine",
+				"zhcn_parallel_state_machine",
+				"cjk_parallel_state_machine",
+				"universal_charset_detector"));
+</script>
+</pre>
+</body>
+</html>
+
--- a/extensions/universalchardet/tests/test_bug426271-euc-jp.html
+++ b/extensions/universalchardet/tests/test_bug426271-euc-jp.html
@@ -2,64 +2,30 @@
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=426271
 -->
 <head>
   <title>Test for Bug 426271</title>
   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="CharsetDetectionTests.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=426271">Mozilla Bug 426271</a>
 <p id="display"></p>
 <div id="content" style="display: none">  
 </div>
 <iframe id="testframe"></iframe>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 /** Test for Bug 426271 **/
-const expectedText = "\u3053\u308C\u306FEUC-JP\u3067\u3059\u6614\u3005\u3001";
-netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-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);
-var oldPref;
-
-try {
-    oldPref = prefService
-     .getComplexValue("intl.charset.detector",
-		      Components.interfaces.nsIPrefLocalizedString).data;
-} catch (e) {
-    oldPref = "";
-}
-str.data = "ja_parallel_state_machine";
-prefService.setComplexValue("intl.charset.detector",
-			    Components.interfaces.nsISupportsString, str);
-
-function afterLoad() {
-    var iframeDoc = $("testframe").contentDocument;
-
-    is(iframeDoc.getElementById("testtext").innerHTML, expectedText,
-       "decoded as EUC-JP");
-
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-    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);
-    str.data = oldPref;
-    prefService.setComplexValue("intl.charset.detector",
-				Components.interfaces.nsISupportsString, str);
-    SimpleTest.finish();
-}
-
-$("testframe").onload= afterLoad;
-$("testframe").src="bug426271_text-euc-jp.html";
-SimpleTest.waitForExplicitFinish();
-
+CharsetDetectionTests("bug426271_text-euc-jp.html",
+		      "EUC-JP",
+		      new Array("ja_parallel_state_machine",
+				"cjk_parallel_state_machine",
+				"universal_charset_detector"));
 </script>
 </pre>
 </body>
 </html>
 
--- a/extensions/universalchardet/tests/test_bug426271-utf-8.html
+++ b/extensions/universalchardet/tests/test_bug426271-utf-8.html
@@ -2,64 +2,33 @@
 <html>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=426271
 -->
 <head>
   <title>Test for Bug 426271</title>
   <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="CharsetDetectionTests.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=426271">Mozilla Bug 426271</a>
 <p id="display"></p>
 <div id="content" style="display: none">  
 </div>
 <iframe id="testframe"></iframe>
 <pre id="test">
 <script class="testbody" type="text/javascript">
 /** Test for Bug 426271 **/
-const expectedText = "\u3053\u308C\u306FUTF-8\u3067\u3059\u6614\u3005\u3001";
-netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-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);
-var oldPref;
-
-try {
-    oldPref = prefService
-     .getComplexValue("intl.charset.detector",
-		      Components.interfaces.nsIPrefLocalizedString).data;
-} catch (e) {
-    oldPref = "";
-}
-str.data = "ja_parallel_state_machine";
-prefService.setComplexValue("intl.charset.detector",
-			    Components.interfaces.nsISupportsString, str);
-
-function afterLoad() {
-    var iframeDoc = $("testframe").contentDocument;
-
-    is(iframeDoc.getElementById("testtext").innerHTML, expectedText,
-       "decoded as UTF-8");
-
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-    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);
-    str.data = oldPref;
-    prefService.setComplexValue("intl.charset.detector",
-				Components.interfaces.nsISupportsString, str);
-    SimpleTest.finish();
-}
-
-$("testframe").onload= afterLoad;
-$("testframe").src="bug426271_text-utf-8.html";
-SimpleTest.waitForExplicitFinish();
-
+CharsetDetectionTests("bug426271_text-utf-8.html",
+		      "UTF-8",
+		      new Array("ja_parallel_state_machine",
+				"zhtw_parallel_state_machine",
+				"zhcn_parallel_state_machine",
+				"zh_parallel_state_machine",
+				"cjk_parallel_state_machine",
+				"universal_charset_detector"));
 </script>
 </pre>
 </body>
 </html>