1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,275 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 60000; 1.8 + 1.9 +// Copied from ril_consts.js. Some entries are commented out in ril_const.js, 1.10 +// but we still want to test them here. 1.11 +const GSM_SMS_STRICT_7BIT_CHARMAP = { 1.12 + "\u0024": "\u0024", // "$" => "$", already in default alphabet 1.13 + "\u00a5": "\u00a5", // "¥" => "¥", already in default alphabet 1.14 + "\u00c0": "\u0041", // "À" => "A" 1.15 + "\u00c1": "\u0041", // "Á" => "A" 1.16 + "\u00c2": "\u0041", // "Â" => "A" 1.17 + "\u00c3": "\u0041", // "Ã" => "A" 1.18 + "\u00c4": "\u00c4", // "Ä" => "Ä", already in default alphabet 1.19 + "\u00c5": "\u00c5", // "Å" => "Å", already in default alphabet 1.20 + "\u00c6": "\u00c6", // "Æ" => "Æ", already in default alphabet 1.21 + "\u00c7": "\u00c7", // "Ç" => "Ç", already in default alphabet 1.22 + "\u00c8": "\u0045", // "È" => "E" 1.23 + "\u00c9": "\u00c9", // "É" => "É", already in default alphabet 1.24 + "\u00ca": "\u0045", // "Ê" => "E" 1.25 + "\u00cb": "\u0045", // "Ë" => "E" 1.26 + "\u00cc": "\u0049", // "Ì" => "I" 1.27 + "\u00cd": "\u0049", // "Í" => "I" 1.28 + "\u00ce": "\u0049", // "Î" => "I" 1.29 + "\u00cf": "\u0049", // "Ï" => "I" 1.30 + "\u00d1": "\u00d1", // "Ñ" => "Ñ", already in default alphabet 1.31 + "\u00d2": "\u004f", // "Ò" => "O" 1.32 + "\u00d3": "\u004f", // "Ó" => "O" 1.33 + "\u00d4": "\u004f", // "Ô" => "O" 1.34 + "\u00d5": "\u004f", // "Õ" => "O" 1.35 + "\u00d6": "\u00d6", // "Ö" => "Ö", already in default alphabet 1.36 + "\u00d9": "\u0055", // "Ù" => "U" 1.37 + "\u00da": "\u0055", // "Ú" => "U" 1.38 + "\u00db": "\u0055", // "Û" => "U" 1.39 + "\u00dc": "\u00dc", // "Ü" => "Ü", already in default alphabet 1.40 + "\u00df": "\u00df", // "ß" => "ß", already in default alphabet 1.41 + "\u00e0": "\u00e0", // "à" => "à", already in default alphabet 1.42 + "\u00e1": "\u0061", // "á" => "a" 1.43 + "\u00e2": "\u0061", // "â" => "a" 1.44 + "\u00e3": "\u0061", // "ã" => "a" 1.45 + "\u00e4": "\u00e4", // "ä" => "ä", already in default alphabet 1.46 + "\u00e5": "\u00e5", // "å" => "å", already in default alphabet 1.47 + "\u00e6": "\u00e6", // "æ" => "æ", already in default alphabet 1.48 + "\u00e7": "\u00c7", // "ç" => "Ç" 1.49 + "\u00e8": "\u00e8", // "è" => "è", already in default alphabet 1.50 + "\u00e9": "\u00e9", // "é" => "é", already in default alphabet 1.51 + "\u00ea": "\u0065", // "ê" => "e" 1.52 + "\u00eb": "\u0065", // "ë" => "e" 1.53 + "\u00ec": "\u00ec", // "ì" => "ì", already in default alphabet 1.54 + "\u00ed": "\u0069", // "í" => "i" 1.55 + "\u00ee": "\u0069", // "î" => "i" 1.56 + "\u00ef": "\u0069", // "ï" => "i" 1.57 + "\u00f1": "\u00f1", // "ñ" => "ñ", already in default alphabet 1.58 + "\u00f2": "\u00f2", // "ò" => "ò", already in default alphabet 1.59 + "\u00f3": "\u006f", // "ó" => "o" 1.60 + "\u00f4": "\u006f", // "ô" => "o" 1.61 + "\u00f5": "\u006f", // "õ" => "o" 1.62 + "\u00f6": "\u00f6", // "ö" => "ö", already in default alphabet 1.63 + "\u00f8": "\u00f8", // "ø" => "ø", already in default alphabet 1.64 + "\u00f9": "\u00f9", // "ù" => "ù", already in default alphabet 1.65 + "\u00fa": "\u0075", // "ú" => "u" 1.66 + "\u00fb": "\u0075", // "û" => "u" 1.67 + "\u00fc": "\u00fc", // "ü" => "ü", already in default alphabet 1.68 + "\u00fe": "\u0074", // "þ" => "t" 1.69 + "\u0100": "\u0041", // "Ā" => "A" 1.70 + "\u0101": "\u0061", // "ā" => "a" 1.71 + "\u0106": "\u0043", // "Ć" => "C" 1.72 + "\u0107": "\u0063", // "ć" => "c" 1.73 + "\u010c": "\u0043", // "Č" => "C" 1.74 + "\u010d": "\u0063", // "č" => "c" 1.75 + "\u010f": "\u0064", // "ď" => "d" 1.76 + "\u0110": "\u0044", // "Đ" => "D" 1.77 + "\u0111": "\u0064", // "đ" => "d" 1.78 + "\u0112": "\u0045", // "Ē" => "E" 1.79 + "\u0113": "\u0065", // "ē" => "e" 1.80 + "\u0118": "\u0045", // "Ę" => "E" 1.81 + "\u0119": "\u0065", // "ę" => "e" 1.82 + "\u0128": "\u0049", // "Ĩ" => "I" 1.83 + "\u0129": "\u0069", // "ĩ" => "i" 1.84 + "\u012a": "\u0049", // "Ī" => "I" 1.85 + "\u012b": "\u0069", // "ī" => "i" 1.86 + "\u012e": "\u0049", // "Į" => "I" 1.87 + "\u012f": "\u0069", // "į" => "i" 1.88 + "\u0141": "\u004c", // "Ł" => "L" 1.89 + "\u0142": "\u006c", // "ł" => "l" 1.90 + "\u0143": "\u004e", // "Ń" => "N" 1.91 + "\u0144": "\u006e", // "ń" => "n" 1.92 + "\u0147": "\u004e", // "Ň" => "N" 1.93 + "\u0148": "\u006e", // "ň" => "n" 1.94 + "\u014c": "\u004f", // "Ō" => "O" 1.95 + "\u014d": "\u006f", // "ō" => "o" 1.96 + "\u0152": "\u004f", // "Œ" => "O" 1.97 + "\u0153": "\u006f", // "œ" => "o" 1.98 + "\u0158": "\u0052", // "Ř" => "R" 1.99 + "\u0159": "\u0072", // "ř" => "r" 1.100 + "\u0160": "\u0053", // "Š" => "S" 1.101 + "\u0161": "\u0073", // "š" => "s" 1.102 + "\u0165": "\u0074", // "ť" => "t" 1.103 + "\u0168": "\u0055", // "Ū" => "U" 1.104 + "\u0169": "\u0075", // "ū" => "u" 1.105 + "\u016a": "\u0055", // "Ū" => "U" 1.106 + "\u016b": "\u0075", // "ū" => "u" 1.107 + "\u0178": "\u0059", // "Ÿ" => "Y" 1.108 + "\u0179": "\u005a", // "Ź" => "Z" 1.109 + "\u017a": "\u007a", // "ź" => "z" 1.110 + "\u017b": "\u005a", // "Ż" => "Z" 1.111 + "\u017c": "\u007a", // "ż" => "z" 1.112 + "\u017d": "\u005a", // "Ž" => "Z" 1.113 + "\u017e": "\u007a", // "ž" => "z" 1.114 + "\u025b": "\u0045", // "ɛ" => "E" 1.115 + "\u0398": "\u0398", // "Θ" => "Θ", already in default alphabet 1.116 + "\u1e7c": "\u0056", // "Ṽ" => "V" 1.117 + "\u1e7d": "\u0076", // "ṽ" => "v" 1.118 + "\u1ebc": "\u0045", // "Ẽ" => "E" 1.119 + "\u1ebd": "\u0065", // "ẽ" => "e" 1.120 + "\u1ef8": "\u0059", // "Ỹ" => "Y" 1.121 + "\u1ef9": "\u0079", // "ỹ" => "y" 1.122 + "\u20a4": "\u00a3", // "₤" => "£" 1.123 + "\u20ac": "\u20ac", // "€" => "€", already in default alphabet 1.124 +}; 1.125 + 1.126 +// Emulator will loop back the outgoing SMS if the phone number equals to its 1.127 +// control port, which is 5554 for the first emulator instance. 1.128 +const SELF = "5554"; 1.129 + 1.130 +SpecialPowers.setBoolPref("dom.sms.enabled", true); 1.131 +SpecialPowers.addPermission("sms", true, document); 1.132 + 1.133 +let manager = window.navigator.mozMobileMessage; 1.134 +ok(manager instanceof MozMobileMessageManager, 1.135 + "manager is instance of " + manager.constructor); 1.136 + 1.137 +let tasks = { 1.138 + // List of test fuctions. Each of them should call |tasks.next()| when 1.139 + // completed or |tasks.finish()| to jump to the last one. 1.140 + _tasks: [], 1.141 + _nextTaskIndex: 0, 1.142 + 1.143 + push: function(func) { 1.144 + this._tasks.push(func); 1.145 + }, 1.146 + 1.147 + next: function() { 1.148 + let index = this._nextTaskIndex++; 1.149 + let task = this._tasks[index]; 1.150 + try { 1.151 + task(); 1.152 + } catch (ex) { 1.153 + ok(false, "test task[" + index + "] throws: " + ex); 1.154 + // Run last task as clean up if possible. 1.155 + if (index != this._tasks.length - 1) { 1.156 + this.finish(); 1.157 + } 1.158 + } 1.159 + }, 1.160 + 1.161 + finish: function() { 1.162 + this._tasks[this._tasks.length - 1](); 1.163 + }, 1.164 + 1.165 + run: function() { 1.166 + this.next(); 1.167 + } 1.168 +}; 1.169 + 1.170 +function testStrict7BitEncodingHelper(sent, received) { 1.171 + // The log message contains unicode and Marionette seems unable to process 1.172 + // it and throws: |UnicodeEncodeError: 'ascii' codec can't encode character 1.173 + // u'\xa5' in position 14: ordinal not in range(128)|. 1.174 + // 1.175 + //log("Testing '" + sent + "' => '" + received + "'"); 1.176 + 1.177 + let count = 0; 1.178 + function done(step) { 1.179 + count += step; 1.180 + if (count >= 2) { 1.181 + window.setTimeout(tasks.next.bind(tasks), 0); 1.182 + } 1.183 + } 1.184 + 1.185 + manager.addEventListener("received", function onReceived(event) { 1.186 + event.target.removeEventListener("received", onReceived); 1.187 + 1.188 + let message = event.message; 1.189 + is(message.body, received, "received message.body"); 1.190 + 1.191 + done(1); 1.192 + }); 1.193 + 1.194 + let request = manager.send(SELF, sent); 1.195 + request.addEventListener("success", function onRequestSuccess(event) { 1.196 + let message = event.target.result; 1.197 + is(message.body, sent, "sent message.body"); 1.198 + 1.199 + done(1); 1.200 + }); 1.201 + request.addEventListener("error", function onRequestError(event) { 1.202 + ok(false, "Can't send message out!!!"); 1.203 + done(2); 1.204 + }); 1.205 +} 1.206 + 1.207 +// Bug 877141 - If you send several spaces together in a sms, the other 1.208 +// dipositive receives a "*" for each space. 1.209 +// 1.210 +// This function is called twice, with strict 7bit encoding enabled or 1.211 +// disabled. Expect the same result in both sent and received text and with 1.212 +// either strict 7bit encoding enabled or disabled. 1.213 +function testBug877141() { 1.214 + log("Testing bug 877141"); 1.215 + let sent = "1 2 3"; 1.216 + testStrict7BitEncodingHelper(sent, sent); 1.217 +} 1.218 + 1.219 +tasks.push(function() { 1.220 + log("Testing with dom.sms.strict7BitEncoding enabled"); 1.221 + SpecialPowers.setBoolPref("dom.sms.strict7BitEncoding", true); 1.222 + tasks.next(); 1.223 +}); 1.224 + 1.225 + 1.226 +// Test for combined string. 1.227 +tasks.push(function() { 1.228 + let sent = "", received = ""; 1.229 + for (let c in GSM_SMS_STRICT_7BIT_CHARMAP) { 1.230 + sent += c; 1.231 + received += GSM_SMS_STRICT_7BIT_CHARMAP[c]; 1.232 + } 1.233 + testStrict7BitEncodingHelper(sent, received); 1.234 +}); 1.235 + 1.236 +// When strict7BitEncoding is enabled, we should replace characters that 1.237 +// can't be encoded with GSM 7-Bit alphabets with '*'. 1.238 +tasks.push(function() { 1.239 + // "Happy New Year" in Chinese. 1.240 + let sent = "\u65b0\u5e74\u5feb\u6a02", received = "****"; 1.241 + testStrict7BitEncodingHelper(sent, received); 1.242 +}); 1.243 + 1.244 +tasks.push(testBug877141); 1.245 + 1.246 +tasks.push(function() { 1.247 + log("Testing with dom.sms.strict7BitEncoding disabled"); 1.248 + SpecialPowers.setBoolPref("dom.sms.strict7BitEncoding", false); 1.249 + tasks.next(); 1.250 +}); 1.251 + 1.252 +// Test for combined string. 1.253 +tasks.push(function() { 1.254 + let sent = ""; 1.255 + for (let c in GSM_SMS_STRICT_7BIT_CHARMAP) { 1.256 + sent += c; 1.257 + } 1.258 + testStrict7BitEncodingHelper(sent, sent); 1.259 +}); 1.260 + 1.261 +tasks.push(function() { 1.262 + // "Happy New Year" in Chinese. 1.263 + let sent = "\u65b0\u5e74\u5feb\u6a02"; 1.264 + testStrict7BitEncodingHelper(sent, sent); 1.265 +}); 1.266 + 1.267 +tasks.push(testBug877141); 1.268 + 1.269 +// WARNING: All tasks should be pushed before this!!! 1.270 +tasks.push(function cleanUp() { 1.271 + SpecialPowers.removePermission("sms", document); 1.272 + SpecialPowers.clearUserPref("dom.sms.enabled"); 1.273 + SpecialPowers.clearUserPref("dom.sms.strict7BitEncoding"); 1.274 + 1.275 + finish(); 1.276 +}); 1.277 + 1.278 +tasks.run();