1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/test/test_TextEncoder.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,277 @@ 1.4 +/* 1.5 + * test_TextEncoder.js 1.6 + * bug 764234 tests 1.7 +*/ 1.8 + 1.9 +function runTextEncoderTests() 1.10 +{ 1.11 + var data = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09" 1.12 + + "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" 1.13 + + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f" 1.14 + + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a" 1.15 + + "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35" 1.16 + + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44" 1.17 + + "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f" 1.18 + + "\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a" 1.19 + + "\u0e5b"; 1.20 + 1.21 + var expectedString = [0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0, 1.22 + 0xB8, 0x83, 0xE0, 0xB8, 0x84, 0xE0, 0xB8, 0x85, 0xE0, 1.23 + 0xB8, 0x86, 0xE0, 0xB8, 0x87, 0xE0, 0xB8, 0x88, 0xE0, 1.24 + 0xB8, 0x89, 0xE0, 0xB8, 0x8A, 0xE0, 0xB8, 0x8B, 0xE0, 1.25 + 0xB8, 0x8C, 0xE0, 0xB8, 0x8D, 0xE0, 0xB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 0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88, 0xE0, 1.44 + 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, 0xE0, 1.45 + 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, 0xE0, 1.46 + 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, 0xE0, 1.47 + 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, 0xE0, 1.48 + 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, 0xE0, 1.49 + 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, 0xE0, 1.50 + 0xB9, 0x9B]; 1.51 + 1.52 + test(testEncoderGetEncoding, "testEncoderGetEncoding"); 1.53 + test(testInvalidSequence, "testInvalidSequence"); 1.54 + test(testEncodeUTF16ToUTF16, "testEncodeUTF16ToUTF16"); 1.55 + test(function() { 1.56 + testConstructorEncodingOption(data, expectedString) 1.57 + }, "testConstructorEncodingOption"); 1.58 + test(function() { 1.59 + testEncodingValues(data, expectedString) 1.60 + }, "testEncodingValues"); 1.61 + test(function() { 1.62 + testInputString(data, expectedString) 1.63 + }, "testInputString"); 1.64 + test(testStreamingOptions, "testStreamingOptions"); 1.65 +} 1.66 + 1.67 +function testInvalidSequence() 1.68 +{ 1.69 + var data = "\u0e43\u0e44\ufffd\u0e45"; 1.70 + var expectedString = [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xEF, 0xBF, 0xBD, 1.71 + 0xE0, 0xB9, 0x85]; 1.72 + 1.73 + //Test null input string 1.74 + testSingleString({encoding: "utf-8", input: data, expected: expectedString, 1.75 + msg: "encoder with replacement character test."}); 1.76 +} 1.77 + 1.78 +function testEncodeUTF16ToUTF16() 1.79 +{ 1.80 + var data = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c" 1.81 + + "\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56" 1.82 + + "\u0e57\u0e58\u0e59\u0e5a\u0e5b"; 1.83 + 1.84 + var expected = [0x43, 0x0E, 0x44, 0x0E, 0x45, 0x0E, 0x46, 0x0E, 0x47, 0x0E, 1.85 + 0x48, 0x0E, 0x49, 0x0E, 0x4A, 0x0E, 0x4B, 0x0E, 0x4C, 0x0E, 1.86 + 0x4D, 0x0E, 0x4E, 0x0E, 0x4F, 0x0E, 0x50, 0x0E, 0x51, 0x0E, 1.87 + 0x52, 0x0E, 0x53, 0x0E, 0x54, 0x0E, 0x55, 0x0E, 0x56, 0x0E, 1.88 + 0x57, 0x0E, 0x58, 0x0E, 0x59, 0x0E, 0x5A, 0x0E, 0x5B, 0x0E]; 1.89 + 1.90 + testSingleString({encoding: "Utf-16", input: data, expected: expected, 1.91 + msg: "testing encoding from utf-16 to utf-16 zero."}); 1.92 +} 1.93 + 1.94 +function testConstructorEncodingOption(aData, aExpectedString) 1.95 +{ 1.96 + // valid encoding passed 1.97 + testSingleString({encoding: "UTF-8", input: aData, expected: aExpectedString, 1.98 + msg: "testing encoding with valid utf-8 encoding."}); 1.99 + 1.100 + // passing spaces for encoding 1.101 + testSingleString({encoding: " ", input: aData, error: "TypeError", 1.102 + msg: "constructor encoding, spaces encoding test."}); 1.103 + 1.104 + // invalid encoding passed 1.105 + testSingleString({encoding: "asdfasdf", input: aData, error: "TypeError", 1.106 + msg: "constructor encoding, invalid encoding test."}); 1.107 + 1.108 + // null encoding passed 1.109 + testSingleString({encoding: null, input: aData, error: "TypeError", 1.110 + msg: "constructor encoding, \"null\" encoding test."}); 1.111 + 1.112 + // null encoding passed 1.113 + testSingleString({encoding: "", input: aData, error: "TypeError", 1.114 + msg: "constructor encoding, empty encoding test."}); 1.115 +} 1.116 + 1.117 +function testEncodingValues(aData, aExpectedString) 1.118 +{ 1.119 + var encoding = "ISO-8859-11"; 1.120 + testSingleString({encoding: aData, input: encoding, error: "TypeError", 1.121 + msg: "encoder encoding values test."}); 1.122 +} 1.123 + 1.124 +function testInputString(aData, aExpectedString) 1.125 +{ 1.126 + //Test null input string 1.127 + testSingleString({encoding: "utf-8", input: "", expected: [], 1.128 + msg: "encoder null input string test."}); 1.129 + 1.130 + //Test spaces as input string 1.131 + testSingleString({encoding: "utf-8", input: " ", expected: [32, 32], 1.132 + msg: "spaces as input string."}); 1.133 +} 1.134 + 1.135 +function testSingleString(test) 1.136 +{ 1.137 + var outText; 1.138 + try { 1.139 + var stream = test.stream ? {stream: true} : null; 1.140 + outText = (new TextEncoder(test.encoding)).encode(test.input, stream); 1.141 + } catch (e) { 1.142 + assert_equals(e.name, test.error, test.msg); 1.143 + return; 1.144 + } 1.145 + assert_true(!test.error, test.msg); 1.146 + 1.147 + if (outText.length !== test.expected.length) { 1.148 + assert_equals(outText.length, test.expected.length, test.msg + " length mismatch"); 1.149 + return; 1.150 + } 1.151 + 1.152 + for (var i = 0; i < outText.length; i++) { 1.153 + if (outText[i] != test.expected[i]) { 1.154 + assert_equals(escape(stringFromArray(outText.buffer)), escape(stringFromArray(test.expected)), 1.155 + test.msg + " Bytes do not match expected bytes."); 1.156 + return; 1.157 + } 1.158 + } 1.159 +} 1.160 + 1.161 +function stringFromArray(a) { 1.162 + return Array.map.call(a, function(v){return String.fromCharCode(v)}).join(''); 1.163 +} 1.164 + 1.165 +function testStreamingOptions() 1.166 +{ 1.167 + var data = [ 1.168 + "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a" 1.169 + + "\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" 1.170 + + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d", 1.171 + "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28" 1.172 + + "\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32" 1.173 + + "\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40" 1.174 + + "\u0e41\u0e42", 1.175 + "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d" 1.176 + + "\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57" 1.177 + + "\u0e58\u0e59\u0e5a\u0e5b" 1.178 + ]; 1.179 + 1.180 + var expected = [[0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0, 1.181 + 0xB8, 0x83, 0xE0, 0xB8, 0x84, 0xE0, 0xB8, 0x85, 0xE0, 1.182 + 0xB8, 0x86, 0xE0, 0xB8, 0x87, 0xE0, 0xB8, 0x88, 0xE0, 1.183 + 0xB8, 0x89, 0xE0, 0xB8, 0x8A, 0xE0, 0xB8, 0x8B, 0xE0, 1.184 + 0xB8, 0x8C, 0xE0, 0xB8, 0x8D, 0xE0, 0xB8, 0x8E, 0xE0, 1.185 + 0xB8, 0x8F, 0xE0, 0xB8, 0x90, 0xE0, 0xB8, 0x91, 0xE0, 1.186 + 0xB8, 0x92, 0xE0, 0xB8, 0x93, 0xE0, 0xB8, 0x94, 0xE0, 1.187 + 0xB8, 0x95, 0xE0, 0xB8, 0x96, 0xE0, 0xB8, 0x97, 0xE0, 1.188 + 0xB8, 0x98, 0xE0, 0xB8, 0x99, 0xE0, 0xB8, 0x9A, 0xE0, 1.189 + 0xB8, 0x9B, 0xE0, 0xB8, 0x9C, 0xE0, 0xB8, 0x9D], 1.190 + 1.191 + [0xE0, 0xB8, 0x9E, 0xE0, 0xB8, 0x9F, 0xE0, 0xB8, 0xA0, 1.192 + 0xE0, 0xB8, 0xA1, 0xE0, 0xB8, 0xA2, 0xE0, 0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xE0, 0xB9, 0x9B]]; 1.212 + 1.213 + var expectedUTF16 = data.map(function(d) { 1.214 + return new Uint8Array(new Uint16Array(arrayFromString(d)).buffer); 1.215 + }); 1.216 + 1.217 + // STREAMING TEST ONE: test streaming three valid strings with stream option 1.218 + // set to true for all three. 1.219 + testArrayOfStrings({encoding: "utf-8", array: [ 1.220 + {input: data[0], stream: true, expected: expected[0]}, 1.221 + {input: data[1], stream: true, expected: expected[1]}, 1.222 + {input: data[2], stream: true, expected: expected[2]}, 1.223 + ], msg: "streaming test one."}); 1.224 + 1.225 + // STREAMING TEST TWO: test streaming valid strings with stream option 1.226 + // streaming option: false from constructor, string 1 stream: true, 1.227 + // string 2 stream: false, string 3 stream: false 1.228 + testArrayOfStrings({encoding: "utf-16", array: [ 1.229 + {input: data[0], stream: true, expected: expectedUTF16[0]}, 1.230 + {input: data[1], expected: expectedUTF16[1]}, 1.231 + {input: data[2], expected: expectedUTF16[2]}, 1.232 + ], msg: "streaming test two."}); 1.233 +} 1.234 + 1.235 +function arrayFromString(s) { 1.236 + return s.split('').map(function(c){return String.charCodeAt(c)}); 1.237 +} 1.238 + 1.239 +function testArrayOfStrings(test) 1.240 +{ 1.241 + var encoder; 1.242 + try { 1.243 + encoder = new TextEncoder(test.encoding); 1.244 + } catch (e) { 1.245 + assert_equals(e.name, test.error, test.msg); 1.246 + return; 1.247 + } 1.248 + assert_true(!test.error, test.msg); 1.249 + 1.250 + var array = test.array; 1.251 + for (var i = 0; i < array.length; i += 1) { 1.252 + var stream = array[i].stream ? {stream: true} : null; 1.253 + var view = encoder.encode(array[i].input, stream); 1.254 + 1.255 + var stringLen = view.length; 1.256 + var expected = array[i].expected; 1.257 + for (var j = 0; j < stringLen; j++) { 1.258 + if (view[j] !== expected[j]) { 1.259 + assert_equals(view[j], expected[j], msg + " Bytes do not match expected bytes."); 1.260 + return; 1.261 + } 1.262 + } 1.263 + } 1.264 +} 1.265 + 1.266 +function testEncoderGetEncoding() 1.267 +{ 1.268 + var labelEncodings = [ 1.269 + {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]}, 1.270 + {encoding: "utf-16le", labels: ["utf-16", "utf-16"]}, 1.271 + {encoding: "utf-16be", labels: ["utf-16be"]}, 1.272 + ]; 1.273 + 1.274 + for (var le of labelEncodings) { 1.275 + for (var label of le.labels) { 1.276 + var encoder = new TextEncoder(label); 1.277 + assert_equals(encoder.encoding, le.encoding, label + " label encoding test."); 1.278 + } 1.279 + } 1.280 +}