gecko-dev/dom/mobilemessage/tests/marionette/test_invalid_address.js

141 строка
3.7 KiB
JavaScript
Исходник Обычный вид История

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
const MMS_MAX_LENGTH_SUBJECT = 40;
SpecialPowers.addPermission("sms", true, document);
SpecialPowers.setBoolPref("dom.sms.enabled", true);
var tasks = {
// List of test fuctions. Each of them should call |tasks.next()| when
// completed or |tasks.finish()| to jump to the last one.
_tasks: [],
_nextTaskIndex: 0,
push: function(func) {
this._tasks.push(func);
},
next: function() {
let index = this._nextTaskIndex++;
let task = this._tasks[index];
try {
task();
} catch (ex) {
ok(false, "test task[" + index + "] throws: " + ex);
// Run last task as clean up if possible.
if (index != this._tasks.length - 1) {
this.finish();
}
}
},
finish: function() {
this._tasks[this._tasks.length - 1]();
},
run: function() {
this.next();
}
};
var manager;
function getAllMessages(callback, filter, reverse) {
let messages = [];
2013-07-26 14:40:27 +04:00
let request = manager.getMessages(filter, reverse || false);
request.onsuccess = function(event) {
if (request.result) {
messages.push(request.result);
request.continue();
return;
}
window.setTimeout(callback.bind(null, messages), 0);
}
}
function deleteAllMessages() {
getAllMessages(function deleteAll(messages) {
let message = messages.shift();
if (!message) {
ok(true, "all messages deleted");
tasks.next();
return;
}
2013-07-26 14:40:27 +04:00
let request = manager.delete(message.id);
request.onsuccess = deleteAll.bind(null, messages);
request.onerror = function(event) {
ok(false, "failed to delete all messages");
tasks.finish();
}
});
}
function testInvalidAddressForSMS(aInvalidAddr) {
2013-07-26 14:40:27 +04:00
log("manager.send(...) should get 'InvalidAddressError' error " +
"when attempting to send SMS to: " + aInvalidAddr);
2013-07-26 14:40:27 +04:00
let request = manager.send(aInvalidAddr, "Test");
request.onerror = function(event) {
log("Received 'onerror' DOMRequest event.");
let error = event.target.error;
ok(error instanceof DOMError, "should be a valid DOMError object");
ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
tasks.next();
};
}
function testInvalidAddressForMMS(aInvalidAddrs) {
2013-07-26 14:40:27 +04:00
log("manager.sendMMS(...) should get 'InvalidAddressError' error " +
"when attempting to send MMS to: " + aInvalidAddrs);
2013-07-26 14:40:27 +04:00
let request = manager.sendMMS({
subject: "Test",
receivers: aInvalidAddrs,
attachments: [],
});
request.onerror = function(event) {
log("Received 'onerror' DOMRequest event.");
let error = event.target.error;
ok(error instanceof DOMError, "should be a valid DOMError object");
ok(error.name === "InvalidAddressError", "should be 'InvalidAddressError'");
tasks.next();
};
}
tasks.push(function() {
log("Verifying initial state.");
2013-07-26 14:40:27 +04:00
manager = window.navigator.mozMobileMessage;
ok(manager instanceof MozMobileMessageManager,
"manager is instance of " + manager.constructor);
tasks.next();
});
// Test sending SMS to invalid addresses.
tasks.push(testInvalidAddressForSMS.bind(this, "&%&"));
tasks.push(testInvalidAddressForSMS.bind(this, ""));
// Test sending MMS to invalid addresses.
tasks.push(testInvalidAddressForMMS.bind(this, ["&%&"]));
tasks.push(testInvalidAddressForMMS.bind(this, [""]));
tasks.push(testInvalidAddressForMMS.bind(this, ["123", "&%&", "456"]));
tasks.push(deleteAllMessages);
// WARNING: All tasks should be pushed before this!!!
tasks.push(function cleanUp() {
SpecialPowers.removePermission("sms", document);
SpecialPowers.clearUserPref("dom.sms.enabled");
finish();
});
tasks.run();