michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 60000; michael@0: michael@0: // Copied from dom/system/gonk/ril_consts.js. michael@0: const PDU_MAX_USER_DATA_7BIT = 160; michael@0: michael@0: SpecialPowers.setBoolPref("dom.sms.enabled", true); michael@0: SpecialPowers.addPermission("sms", true, document); michael@0: michael@0: let manager = window.navigator.mozMobileMessage; michael@0: ok(manager instanceof MozMobileMessageManager, michael@0: "manager is instance of " + manager.constructor); michael@0: michael@0: let tasks = { michael@0: // List of test fuctions. Each of them should call |tasks.next()| when michael@0: // completed or |tasks.finish()| to jump to the last one. michael@0: _tasks: [], michael@0: _nextTaskIndex: 0, michael@0: michael@0: push: function(func) { michael@0: this._tasks.push(func); michael@0: }, michael@0: michael@0: next: function() { michael@0: let index = this._nextTaskIndex++; michael@0: let task = this._tasks[index]; michael@0: try { michael@0: task(); michael@0: } catch (ex) { michael@0: ok(false, "test task[" + index + "] throws: " + ex); michael@0: // Run last task as clean up if possible. michael@0: if (index != this._tasks.length - 1) { michael@0: this.finish(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: finish: function() { michael@0: this._tasks[this._tasks.length - 1](); michael@0: }, michael@0: michael@0: run: function() { michael@0: this.next(); michael@0: } michael@0: }; michael@0: michael@0: function addTest(text, segments, charsPerSegment, charsAvailableInLastSegment) { michael@0: tasks.push(function() { michael@0: log("Testing '" + text + "' ..."); michael@0: let domRequest = manager.getSegmentInfoForText(text); michael@0: ok(domRequest, "DOMRequest object returned."); michael@0: michael@0: domRequest.onsuccess = function(e) { michael@0: log("Received 'onsuccess' DOMRequest event."); michael@0: michael@0: let result = e.target.result; michael@0: if (!result) { michael@0: ok(false, "getSegmentInfoForText() result is not valid."); michael@0: tasks.finish(); michael@0: return; michael@0: } michael@0: michael@0: is(result.segments, segments, "info.segments"); michael@0: is(result.charsPerSegment, charsPerSegment, "info.charsPerSegment"); michael@0: is(result.charsAvailableInLastSegment, charsAvailableInLastSegment, michael@0: "info.charsAvailableInLastSegment"); michael@0: michael@0: tasks.next(); michael@0: }; michael@0: michael@0: domRequest.onerror = function(e) { michael@0: ok(false, "Failed to call getSegmentInfoForText()."); michael@0: tasks.finish(); michael@0: }; michael@0: }); michael@0: } michael@0: michael@0: function addTestThrows(text) { michael@0: tasks.push(function() { michael@0: log("Testing '" + text + "' ..."); michael@0: try { michael@0: let domRequest = manager.getSegmentInfoForText(text); michael@0: michael@0: ok(false, "Not thrown."); michael@0: tasks.finish(); michael@0: } catch (e) { michael@0: tasks.next(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: addTestThrows(null); michael@0: michael@0: // Testing "undefined". michael@0: addTest(undefined, 1, PDU_MAX_USER_DATA_7BIT, michael@0: PDU_MAX_USER_DATA_7BIT - "undefined".length); michael@0: michael@0: // Testing numeric values. michael@0: addTest(0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "0".length); michael@0: addTest(1.0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "1".length); michael@0: michael@0: // Testing empty object. The empty object extends to "[object Object]" and both michael@0: // '[' and ']' are in default single shift table, so each of them takes two michael@0: // septets. michael@0: addTest({}, 1, PDU_MAX_USER_DATA_7BIT, michael@0: PDU_MAX_USER_DATA_7BIT - (("" + {}).length + 2)); michael@0: michael@0: // Testing Date object. michael@0: let date = new Date(); michael@0: addTest(date, 1, PDU_MAX_USER_DATA_7BIT, michael@0: PDU_MAX_USER_DATA_7BIT - ("" + date).length); michael@0: michael@0: addTest("", 1, PDU_MAX_USER_DATA_7BIT, michael@0: PDU_MAX_USER_DATA_7BIT - "".length); michael@0: michael@0: // WARNING: All tasks should be pushed before this!!! michael@0: tasks.push(function cleanUp() { michael@0: SpecialPowers.removePermission("sms", document); michael@0: SpecialPowers.clearUserPref("dom.sms.enabled"); michael@0: finish(); michael@0: }); michael@0: michael@0: tasks.run();