dom/mobilemessage/tests/marionette/test_invalid_address.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:68fd161284f9
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 MARIONETTE_TIMEOUT = 60000;
5
6 const MMS_MAX_LENGTH_SUBJECT = 40;
7
8 SpecialPowers.addPermission("sms", true, document);
9 SpecialPowers.setBoolPref("dom.sms.enabled", true);
10
11 let tasks = {
12 // List of test fuctions. Each of them should call |tasks.next()| when
13 // completed or |tasks.finish()| to jump to the last one.
14 _tasks: [],
15 _nextTaskIndex: 0,
16
17 push: function(func) {
18 this._tasks.push(func);
19 },
20
21 next: function() {
22 let index = this._nextTaskIndex++;
23 let task = this._tasks[index];
24 try {
25 task();
26 } catch (ex) {
27 ok(false, "test task[" + index + "] throws: " + ex);
28 // Run last task as clean up if possible.
29 if (index != this._tasks.length - 1) {
30 this.finish();
31 }
32 }
33 },
34
35 finish: function() {
36 this._tasks[this._tasks.length - 1]();
37 },
38
39 run: function() {
40 this.next();
41 }
42 };
43
44 let manager;
45
46 function getAllMessages(callback, filter, reverse) {
47 if (!filter) {
48 filter = new MozSmsFilter;
49 }
50 let messages = [];
51 let request = manager.getMessages(filter, reverse || false);
52 request.onsuccess = function(event) {
53 if (request.result) {
54 messages.push(request.result);
55 request.continue();
56 return;
57 }
58
59 window.setTimeout(callback.bind(null, messages), 0);
60 }
61 }
62
63 function deleteAllMessages() {
64 getAllMessages(function deleteAll(messages) {
65 let message = messages.shift();
66 if (!message) {
67 ok(true, "all messages deleted");
68 tasks.next();
69 return;
70 }
71
72 let request = manager.delete(message.id);
73 request.onsuccess = deleteAll.bind(null, messages);
74 request.onerror = function(event) {
75 ok(false, "failed to delete all messages");
76 tasks.finish();
77 }
78 });
79 }
80
81 function testInvalidAddressForSMS(aInvalidAddr) {
82 log("manager.send(...) should get 'InvalidAddressError' error " +
83 "when attempting to send SMS to: " + aInvalidAddr);
84
85 let request = manager.send(aInvalidAddr, "Test");
86
87 request.onerror = function(event) {
88 log("Received 'onerror' DOMRequest event.");
89 let error = event.target.error;
90 ok(error instanceof DOMError, "should be a valid DOMError object");
91 ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
92 tasks.next();
93 };
94 }
95
96 function testInvalidAddressForMMS(aInvalidAddrs) {
97 log("manager.sendMMS(...) should get 'InvalidAddressError' error " +
98 "when attempting to send MMS to: " + aInvalidAddrs);
99
100 let request = manager.sendMMS({
101 subject: "Test",
102 receivers: aInvalidAddrs,
103 attachments: [],
104 });
105
106 request.onerror = function(event) {
107 log("Received 'onerror' DOMRequest event.");
108 let error = event.target.error;
109 ok(error instanceof DOMError, "should be a valid DOMError object");
110 ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
111 tasks.next();
112 };
113 }
114
115 tasks.push(function() {
116 log("Verifying initial state.");
117
118 manager = window.navigator.mozMobileMessage;
119 ok(manager instanceof MozMobileMessageManager,
120 "manager is instance of " + manager.constructor);
121
122 tasks.next();
123 });
124
125 // Test sending SMS to invalid addresses.
126 tasks.push(testInvalidAddressForSMS.bind(this, "&%&"));
127 tasks.push(testInvalidAddressForSMS.bind(this, ""));
128
129 // Test sending MMS to invalid addresses.
130 tasks.push(testInvalidAddressForMMS.bind(this, ["&%&"]));
131 tasks.push(testInvalidAddressForMMS.bind(this, [""]));
132 tasks.push(testInvalidAddressForMMS.bind(this, ["123", "&%&", "456"]));
133
134 tasks.push(deleteAllMessages);
135
136 // WARNING: All tasks should be pushed before this!!!
137 tasks.push(function cleanUp() {
138 SpecialPowers.removePermission("sms", document);
139 SpecialPowers.clearUserPref("dom.sms.enabled");
140 finish();
141 });
142
143 tasks.run();

mercurial