1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/mobilemessage/tests/marionette/test_getsegmentinfofortext.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 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 dom/system/gonk/ril_consts.js. 1.10 +const PDU_MAX_USER_DATA_7BIT = 160; 1.11 + 1.12 +SpecialPowers.setBoolPref("dom.sms.enabled", true); 1.13 +SpecialPowers.addPermission("sms", true, document); 1.14 + 1.15 +let manager = window.navigator.mozMobileMessage; 1.16 +ok(manager instanceof MozMobileMessageManager, 1.17 + "manager is instance of " + manager.constructor); 1.18 + 1.19 +let tasks = { 1.20 + // List of test fuctions. Each of them should call |tasks.next()| when 1.21 + // completed or |tasks.finish()| to jump to the last one. 1.22 + _tasks: [], 1.23 + _nextTaskIndex: 0, 1.24 + 1.25 + push: function(func) { 1.26 + this._tasks.push(func); 1.27 + }, 1.28 + 1.29 + next: function() { 1.30 + let index = this._nextTaskIndex++; 1.31 + let task = this._tasks[index]; 1.32 + try { 1.33 + task(); 1.34 + } catch (ex) { 1.35 + ok(false, "test task[" + index + "] throws: " + ex); 1.36 + // Run last task as clean up if possible. 1.37 + if (index != this._tasks.length - 1) { 1.38 + this.finish(); 1.39 + } 1.40 + } 1.41 + }, 1.42 + 1.43 + finish: function() { 1.44 + this._tasks[this._tasks.length - 1](); 1.45 + }, 1.46 + 1.47 + run: function() { 1.48 + this.next(); 1.49 + } 1.50 +}; 1.51 + 1.52 +function addTest(text, segments, charsPerSegment, charsAvailableInLastSegment) { 1.53 + tasks.push(function() { 1.54 + log("Testing '" + text + "' ..."); 1.55 + let domRequest = manager.getSegmentInfoForText(text); 1.56 + ok(domRequest, "DOMRequest object returned."); 1.57 + 1.58 + domRequest.onsuccess = function(e) { 1.59 + log("Received 'onsuccess' DOMRequest event."); 1.60 + 1.61 + let result = e.target.result; 1.62 + if (!result) { 1.63 + ok(false, "getSegmentInfoForText() result is not valid."); 1.64 + tasks.finish(); 1.65 + return; 1.66 + } 1.67 + 1.68 + is(result.segments, segments, "info.segments"); 1.69 + is(result.charsPerSegment, charsPerSegment, "info.charsPerSegment"); 1.70 + is(result.charsAvailableInLastSegment, charsAvailableInLastSegment, 1.71 + "info.charsAvailableInLastSegment"); 1.72 + 1.73 + tasks.next(); 1.74 + }; 1.75 + 1.76 + domRequest.onerror = function(e) { 1.77 + ok(false, "Failed to call getSegmentInfoForText()."); 1.78 + tasks.finish(); 1.79 + }; 1.80 + }); 1.81 +} 1.82 + 1.83 +function addTestThrows(text) { 1.84 + tasks.push(function() { 1.85 + log("Testing '" + text + "' ..."); 1.86 + try { 1.87 + let domRequest = manager.getSegmentInfoForText(text); 1.88 + 1.89 + ok(false, "Not thrown."); 1.90 + tasks.finish(); 1.91 + } catch (e) { 1.92 + tasks.next(); 1.93 + } 1.94 + }); 1.95 +} 1.96 + 1.97 +addTestThrows(null); 1.98 + 1.99 +// Testing "undefined". 1.100 +addTest(undefined, 1, PDU_MAX_USER_DATA_7BIT, 1.101 + PDU_MAX_USER_DATA_7BIT - "undefined".length); 1.102 + 1.103 +// Testing numeric values. 1.104 +addTest(0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "0".length); 1.105 +addTest(1.0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "1".length); 1.106 + 1.107 +// Testing empty object. The empty object extends to "[object Object]" and both 1.108 +// '[' and ']' are in default single shift table, so each of them takes two 1.109 +// septets. 1.110 +addTest({}, 1, PDU_MAX_USER_DATA_7BIT, 1.111 + PDU_MAX_USER_DATA_7BIT - (("" + {}).length + 2)); 1.112 + 1.113 +// Testing Date object. 1.114 +let date = new Date(); 1.115 +addTest(date, 1, PDU_MAX_USER_DATA_7BIT, 1.116 + PDU_MAX_USER_DATA_7BIT - ("" + date).length); 1.117 + 1.118 +addTest("", 1, PDU_MAX_USER_DATA_7BIT, 1.119 + PDU_MAX_USER_DATA_7BIT - "".length); 1.120 + 1.121 +// WARNING: All tasks should be pushed before this!!! 1.122 +tasks.push(function cleanUp() { 1.123 + SpecialPowers.removePermission("sms", document); 1.124 + SpecialPowers.clearUserPref("dom.sms.enabled"); 1.125 + finish(); 1.126 +}); 1.127 + 1.128 +tasks.run();