|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 MARIONETTE_TIMEOUT = 60000; |
|
5 |
|
6 // Copied from dom/system/gonk/ril_consts.js. |
|
7 const PDU_MAX_USER_DATA_7BIT = 160; |
|
8 |
|
9 SpecialPowers.setBoolPref("dom.sms.enabled", true); |
|
10 SpecialPowers.addPermission("sms", true, document); |
|
11 |
|
12 let manager = window.navigator.mozMobileMessage; |
|
13 ok(manager instanceof MozMobileMessageManager, |
|
14 "manager is instance of " + manager.constructor); |
|
15 |
|
16 let tasks = { |
|
17 // List of test fuctions. Each of them should call |tasks.next()| when |
|
18 // completed or |tasks.finish()| to jump to the last one. |
|
19 _tasks: [], |
|
20 _nextTaskIndex: 0, |
|
21 |
|
22 push: function(func) { |
|
23 this._tasks.push(func); |
|
24 }, |
|
25 |
|
26 next: function() { |
|
27 let index = this._nextTaskIndex++; |
|
28 let task = this._tasks[index]; |
|
29 try { |
|
30 task(); |
|
31 } catch (ex) { |
|
32 ok(false, "test task[" + index + "] throws: " + ex); |
|
33 // Run last task as clean up if possible. |
|
34 if (index != this._tasks.length - 1) { |
|
35 this.finish(); |
|
36 } |
|
37 } |
|
38 }, |
|
39 |
|
40 finish: function() { |
|
41 this._tasks[this._tasks.length - 1](); |
|
42 }, |
|
43 |
|
44 run: function() { |
|
45 this.next(); |
|
46 } |
|
47 }; |
|
48 |
|
49 function addTest(text, segments, charsPerSegment, charsAvailableInLastSegment) { |
|
50 tasks.push(function() { |
|
51 log("Testing '" + text + "' ..."); |
|
52 let domRequest = manager.getSegmentInfoForText(text); |
|
53 ok(domRequest, "DOMRequest object returned."); |
|
54 |
|
55 domRequest.onsuccess = function(e) { |
|
56 log("Received 'onsuccess' DOMRequest event."); |
|
57 |
|
58 let result = e.target.result; |
|
59 if (!result) { |
|
60 ok(false, "getSegmentInfoForText() result is not valid."); |
|
61 tasks.finish(); |
|
62 return; |
|
63 } |
|
64 |
|
65 is(result.segments, segments, "info.segments"); |
|
66 is(result.charsPerSegment, charsPerSegment, "info.charsPerSegment"); |
|
67 is(result.charsAvailableInLastSegment, charsAvailableInLastSegment, |
|
68 "info.charsAvailableInLastSegment"); |
|
69 |
|
70 tasks.next(); |
|
71 }; |
|
72 |
|
73 domRequest.onerror = function(e) { |
|
74 ok(false, "Failed to call getSegmentInfoForText()."); |
|
75 tasks.finish(); |
|
76 }; |
|
77 }); |
|
78 } |
|
79 |
|
80 function addTestThrows(text) { |
|
81 tasks.push(function() { |
|
82 log("Testing '" + text + "' ..."); |
|
83 try { |
|
84 let domRequest = manager.getSegmentInfoForText(text); |
|
85 |
|
86 ok(false, "Not thrown."); |
|
87 tasks.finish(); |
|
88 } catch (e) { |
|
89 tasks.next(); |
|
90 } |
|
91 }); |
|
92 } |
|
93 |
|
94 addTestThrows(null); |
|
95 |
|
96 // Testing "undefined". |
|
97 addTest(undefined, 1, PDU_MAX_USER_DATA_7BIT, |
|
98 PDU_MAX_USER_DATA_7BIT - "undefined".length); |
|
99 |
|
100 // Testing numeric values. |
|
101 addTest(0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "0".length); |
|
102 addTest(1.0, 1, PDU_MAX_USER_DATA_7BIT, PDU_MAX_USER_DATA_7BIT - "1".length); |
|
103 |
|
104 // Testing empty object. The empty object extends to "[object Object]" and both |
|
105 // '[' and ']' are in default single shift table, so each of them takes two |
|
106 // septets. |
|
107 addTest({}, 1, PDU_MAX_USER_DATA_7BIT, |
|
108 PDU_MAX_USER_DATA_7BIT - (("" + {}).length + 2)); |
|
109 |
|
110 // Testing Date object. |
|
111 let date = new Date(); |
|
112 addTest(date, 1, PDU_MAX_USER_DATA_7BIT, |
|
113 PDU_MAX_USER_DATA_7BIT - ("" + date).length); |
|
114 |
|
115 addTest("", 1, PDU_MAX_USER_DATA_7BIT, |
|
116 PDU_MAX_USER_DATA_7BIT - "".length); |
|
117 |
|
118 // WARNING: All tasks should be pushed before this!!! |
|
119 tasks.push(function cleanUp() { |
|
120 SpecialPowers.removePermission("sms", document); |
|
121 SpecialPowers.clearUserPref("dom.sms.enabled"); |
|
122 finish(); |
|
123 }); |
|
124 |
|
125 tasks.run(); |