Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../../../resources/js-test.js"></script> | 2 <title>Encoding API: Basics</title> |
| 3 <script src="../../../resources/testharness.js"></script> | |
| 4 <script src="../../../resources/testharnessreport.js"></script> | |
| 3 <script> | 5 <script> |
| 4 | 6 |
| 5 description("This tests the basics of the Encoding API."); | 7 test(function() { |
| 6 | 8 |
| 7 shouldBeTrue("'TextEncoder' in window"); | 9 assert_true('TextEncoder' in window); |
| 8 shouldBeTrue("'TextDecoder' in window"); | 10 assert_true('TextDecoder' in window); |
| 9 | 11 |
| 10 shouldBeTrue("'encoding' in new TextEncoder"); | 12 assert_true('encoding' in new TextEncoder); |
| 11 shouldBeTrue("'encoding' in new TextDecoder"); | 13 assert_true('encoding' in new TextDecoder); |
| 14 assert_equals(typeof (new TextEncoder).encoding, 'string'); | |
| 15 assert_equals(typeof (new TextDecoder).encoding, 'string'); | |
| 12 | 16 |
| 13 shouldBeEqualToString("typeof (new TextEncoder).encoding", "string"); | 17 assert_true('encode' in new TextEncoder); |
| 14 shouldBeEqualToString("typeof (new TextDecoder).encoding", "string"); | 18 assert_true('decode' in new TextDecoder); |
| 15 | 19 |
| 16 shouldBeTrue("'encode' in new TextEncoder"); | 20 assert_equals(typeof (new TextEncoder).encode, 'function'); |
| 17 shouldBeTrue("'decode' in new TextDecoder"); | 21 assert_equals(typeof (new TextDecoder).decode, 'function'); |
| 18 | 22 |
| 19 shouldBeEqualToString("typeof (new TextEncoder).encode", "function"); | 23 assert_equals((new TextEncoder).encoding, 'utf-8', 'default encoding is utf- 8'); |
| 20 shouldBeEqualToString("typeof (new TextDecoder).decode", "function"); | 24 assert_equals((new TextDecoder).encoding, 'utf-8', 'default encoding is utf- 8'); |
| 21 | 25 |
| 26 function testEncodeDecodeSample(encoding, string, bytes) { | |
| 27 var encoded = new TextEncoder(encoding).encode(string); | |
| 28 assert_array_equals([].slice.call(encoded), bytes); | |
| 29 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), s tring); | |
| 30 } | |
| 22 | 31 |
| 23 shouldBeEqualToString("(new TextEncoder).encoding", "utf-8"); | 32 testEncodeDecodeSample( |
| 24 shouldBeEqualToString("(new TextDecoder).encoding", "utf-8"); | 33 'utf-8', |
| 25 | 34 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD', // z, cent, CJK water, G-Clef, Pr ivate-use character |
|
jungshik at Google
2014/04/29 16:51:04
Shouldn't this sample include a BMP PUA code point
jsbell
2014/04/29 21:41:18
Good idea. (I inherited this sequence from some ot
| |
| 26 function toArray(arrayLike) { | 35 [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4, 0x8F, 0xBF, 0xBD] |
| 27 return [].map.call(arrayLike, function(x) { return x; }); | 36 ); |
| 28 } | 37 testEncodeDecodeSample( |
| 29 | 38 'utf-16le', |
| 30 function testEncodeDecodeSample(encoding, string, bytes) { | 39 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD', // z, cent, CJK water, G-Clef, Pr ivate-use character |
| 31 debug(""); | 40 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF] |
| 32 debug("test encode/decode sample - " + encoding); | 41 ); |
| 33 | 42 testEncodeDecodeSample( |
| 34 evalAndLog("encoded = new TextEncoder('" + encoding + "').encode(" + JSON.st ringify(string) + ")"); | 43 'utf-16be', |
| 35 shouldBeEqualToString("JSON.stringify(toArray(encoded))", JSON.stringify(byt es)); | 44 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD', // z, cent, CJK water, G-Clef, Pr ivate-use character |
| 36 shouldBeEqualToString("new TextDecoder('" + encoding + "').decode(new Uint8A rray(" + JSON.stringify(bytes) + "))", string); | 45 [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB, 0xFF, 0xDF, 0xFD] |
| 37 } | 46 ); |
| 38 | 47 testEncodeDecodeSample( |
| 39 testEncodeDecodeSample( | 48 'utf-16', |
| 40 "utf-8", | 49 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD', // z, cent, CJK water, G-Clef, Pr ivate-use character |
| 41 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat e-use character | 50 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF] |
| 42 [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4, 0x8F, 0xB F, 0xBD] | 51 ); |
| 43 ); | 52 }, 'Encoding API basics'); |
| 44 testEncodeDecodeSample( | |
| 45 "utf-16le", | |
| 46 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat e-use character | |
| 47 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xF D, 0xDF] | |
| 48 ); | |
| 49 testEncodeDecodeSample( | |
| 50 "utf-16be", | |
| 51 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat e-use character | |
| 52 [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB, 0xFF, 0xD F, 0xFD] | |
| 53 ); | |
| 54 testEncodeDecodeSample( | |
| 55 "utf-16", | |
| 56 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat e-use character | |
| 57 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xF D, 0xDF] | |
| 58 ); | |
| 59 | 53 |
| 60 </script> | 54 </script> |
| OLD | NEW |