1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/header_helpers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.10 + 1.11 + 1.12 +let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.13 + .getService(Ci.mozIJSSubScriptLoader); 1.14 + 1.15 +/** 1.16 + * Test whether specified function throws exception with expected 1.17 + * result. 1.18 + * 1.19 + * @param func 1.20 + * Function to be tested. 1.21 + * @param exception 1.22 + * Expected class name of thrown exception. Use null for no throws. 1.23 + * @param stack 1.24 + * Optional stack object to be printed. null for Components#stack#caller. 1.25 + */ 1.26 +function do_check_throws(func, result, stack) 1.27 +{ 1.28 + if (!stack) 1.29 + stack = Components.stack.caller; 1.30 + 1.31 + try { 1.32 + func(); 1.33 + } catch (ex) { 1.34 + if (ex.name == result) { 1.35 + return; 1.36 + } 1.37 + do_throw("expected result " + result + ", caught " + ex, stack); 1.38 + } 1.39 + 1.40 + if (result) { 1.41 + do_throw("expected result " + result + ", none thrown", stack); 1.42 + } 1.43 +} 1.44 + 1.45 +/** 1.46 + * Internal test function for comparing results. 1.47 + * 1.48 + * @param func 1.49 + * A function under test. It should accept an arguement and return the 1.50 + * result. 1.51 + * @param data 1.52 + * Input data for `func`. 1.53 + * @param expect 1.54 + * Expected result. 1.55 + */ 1.56 +function wsp_test_func(func, data, expect) { 1.57 + let result_str = JSON.stringify(func(data)); 1.58 + let expect_str = JSON.stringify(expect); 1.59 + do_check_eq(result_str, expect_str); 1.60 +} 1.61 + 1.62 +/** 1.63 + * Test customized WSP PDU decoding. 1.64 + * 1.65 + * @param func 1.66 + * Decoding func under test. It should return a decoded value if invoked. 1.67 + * @param input 1.68 + * Array of octets as test data. 1.69 + * @param expect 1.70 + * Expected decoded value, use null if expecting errors instead. 1.71 + * @param exception 1.72 + * Expected class name of thrown exception. Use null for no throws. 1.73 + */ 1.74 +function wsp_decode_test_ex(func, input, expect, exception) { 1.75 + let data = {array: input, offset: 0}; 1.76 + do_check_throws(wsp_test_func.bind(null, func, data, expect), exception); 1.77 +} 1.78 + 1.79 +/** 1.80 + * Test default WSP PDU decoding. 1.81 + * 1.82 + * @param target 1.83 + * Target decoding object, ie. TextValue. 1.84 + * @param input 1.85 + * Array of octets as test data. 1.86 + * @param expect 1.87 + * Expected decoded value, use null if expecting errors instead. 1.88 + * @param exception 1.89 + * Expected class name of thrown exception. Use null for no throws. 1.90 + */ 1.91 +function wsp_decode_test(target, input, expect, exception) { 1.92 + let func = function decode_func(data) { 1.93 + return target.decode(data); 1.94 + }; 1.95 + 1.96 + wsp_decode_test_ex(func, input, expect, exception); 1.97 +} 1.98 + 1.99 +/** 1.100 + * Test customized WSP PDU encoding. 1.101 + * 1.102 + * @param func 1.103 + * Encoding func under test. It should return an encoded octet array if 1.104 + * invoked. 1.105 + * @param input 1.106 + * An object to be encoded. 1.107 + * @param expect 1.108 + * Expected encoded octet array, use null if expecting errors instead. 1.109 + * @param exception 1.110 + * Expected class name of thrown exception. Use null for no throws. 1.111 + */ 1.112 +function wsp_encode_test_ex(func, input, expect, exception) { 1.113 + let data = {array: [], offset: 0}; 1.114 + do_check_throws(wsp_test_func.bind(null, func.bind(null, data), input, 1.115 + expect), exception); 1.116 +} 1.117 + 1.118 +/** 1.119 + * Test default WSP PDU encoding. 1.120 + * 1.121 + * @param target 1.122 + * Target decoding object, ie. TextValue. 1.123 + * @param input 1.124 + * An object to be encoded. 1.125 + * @param expect 1.126 + * Expected encoded octet array, use null if expecting errors instead. 1.127 + * @param exception 1.128 + * Expected class name of thrown exception. Use null for no throws. 1.129 + */ 1.130 +function wsp_encode_test(target, input, expect, exception) { 1.131 + let func = function encode_func(data, input) { 1.132 + target.encode(data, input); 1.133 + 1.134 + // Remove extra space consumed during encoding. 1.135 + while (data.array.length > data.offset) { 1.136 + data.array.pop(); 1.137 + } 1.138 + 1.139 + return data.array; 1.140 + } 1.141 + 1.142 + wsp_encode_test_ex(func, input, expect, exception); 1.143 +} 1.144 + 1.145 +/** 1.146 + * @param str 1.147 + * A string. 1.148 + * @param noAppendNull 1.149 + * True to omit terminating NUL octet. Default false. 1.150 + * 1.151 + * @return A number array of char codes of characters in `str`. 1.152 + */ 1.153 +function strToCharCodeArray(str, noAppendNull) { 1.154 + let result = []; 1.155 + 1.156 + for (let i = 0; i < str.length; i++) { 1.157 + result.push(str.charCodeAt(i)); 1.158 + } 1.159 + if (!noAppendNull) { 1.160 + result.push(0); 1.161 + } 1.162 + 1.163 + return result; 1.164 +} 1.165 +