Bug 1152730 - Part 2: Add a marionette test case. r=btseng

This commit is contained in:
Samael Wang 2015-05-20 17:50:27 +08:00
Родитель bda8330654
Коммит cbcbfc54f7
3 изменённых файлов: 204 добавлений и 0 удалений

Просмотреть файл

@ -77,6 +77,49 @@ function ensureMobileMessage() {
return deferred.promise;
}
/**
* Push required permissions and test if |navigator.mozMobileConnections| exists.
* Resolve if it does, reject otherwise.
*
* Fulfill params:
* manager -- an reference to navigator.mozMobileConnections.
*
* Reject params: (none)
*
* @param aServiceId [optional]
* A numeric DSDS service id. Default: 0.
*
* @return A deferred promise.
*/
let mobileConnection;
function ensureMobileConnection(aServiceId) {
return new Promise(function(resolve, reject) {
let permissions = [{
"type": "mobileconnection",
"allow": 1,
"context": document,
}];
SpecialPowers.pushPermissions(permissions, function() {
ok(true, "permissions pushed: " + JSON.stringify(permissions));
let serviceId = aServiceId || 0;
mobileConnection = window.navigator.mozMobileConnections[serviceId];
if (mobileConnection) {
log("navigator.mozMobileConnections[" + serviceId + "] is instance of " +
mobileConnection.constructor);
} else {
log("navigator.mozMobileConnections[" + serviceId + "] is undefined");
}
if (mobileConnection instanceof MozMobileConnection) {
resolve(mobileConnection);
} else {
reject();
}
});
});
}
/**
* Wait for one named MobileMessageManager event.
*
@ -501,6 +544,28 @@ function sendMultipleRawSmsToEmulatorAndWait(aPdus) {
return Promise.all(promises);
}
/**
* Set voice state and wait for state change.
*
* @param aState
* "unregistered", "searching", "denied", "roaming", or "home".
*
* @return A deferred promise.
*/
function setEmulatorVoiceStateAndWait(aState) {
let promises = [];
promises.push(new Promise(function(resolve, reject) {
mobileConnection.addEventListener("voicechange", function onevent(aEvent) {
log("voicechange: connected=" + mobileConnection.voice.connected);
mobileConnection.removeEventListener("voicechange", onevent);
resolve(aEvent);
})
}));
promises.push(runEmulatorCmdSafe("gsm voice " + aState));
return Promise.all(promises);
}
/**
* Create a new array of id attribute of input messages.
*

Просмотреть файл

@ -53,3 +53,4 @@ qemu = true
[test_decode_spanish_fallback.js]
[test_update_gsm_nl_on_mcc_chanages.js]
[test_set_smsc_address.js]
[test_outgoing_unstable_voice_connection.js]

Просмотреть файл

@ -0,0 +1,138 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = 'head.js';
const RECEIVER = "15555215555";
// The Book of Mozilla, 15:1
const MESSAGES = [
"The twins of Mammon quarrelled.",
"Their warring plunged the world into a new darkness, and the beast abhorred the darkness.",
"So it began to move swiftly, and grew more powerful, and went forth and multiplied.",
"And the beasts brought fire and light to the darkness."
];
/**
* Send messages and verify that corresponding "sending" events are received.
*
* @return an array of message IDs.
*/
function sendMessagesAndVerifySending() {
return new Promise(function(resolve, reject) {
try {
let eventCount = 0;
let now = Date.now();
let messageIds = [];
manager.addEventListener("sending", function onevent(aEvent) {
log("onsending event received.");
let message = aEvent.message;
let expectedBody = MESSAGES[eventCount++];
messageIds.push(message.id);
is(message.delivery, "sending", "message.delivery");
is(message.deliveryStatus, "pending", "message.deliveryStatus");
is(message.body, expectedBody, "message.body: expected '" + expectedBody
+ "'' but got '" + message.body + "'");
// timestamp is in seconds.
ok(Math.floor(message.timestamp / 1000) >= Math.floor(now / 1000),
"expected " + message.timestamp + " >= " + now);
// resolve when all messages are appeared "sending"
if (eventCount == MESSAGES.length) {
manager.removeEventListener("sending", onevent);
resolve(messageIds);
}
});
// send messages
for (let body of MESSAGES) {
manager.send(RECEIVER, body);
}
} catch (err) {
log("Error: " + err);
reject(err);
}
});
}
/**
* Turn on voice connection, delete specified messages, and verify all the
* corresponding "sent" / "failed" events are received.
*
* @param aMessageIdsToDelete
* An array of message ids to delete.
* @param aExpectedSentMessages
* Expected successfully sent messages, ordered.
* @param aExpectedFailures
* Expected number of failures.
*/
function turnOnVoiceDeleteMessagesAndVerify(aMessageIdsToDelete,
aExpectedSentMessages, aExpectedFailures) {
let promises = [];
// Wait for "sent" and "failed" events.
promises.push(new Promise(function(resolve, reject) {
try {
let sentEventCount = 0;
let failedEventCount = 0;
let onSentHandler = function(aEvent) {
log("onsent event received.");
let message = aEvent.message;
let expectedBody = aExpectedSentMessages[sentEventCount++];
is(message.delivery, "sent", "message.delivery");
is(message.receiver, RECEIVER, "message.receiver");
is(message.body, expectedBody, "message.body: expected '" + expectedBody
+ "'' but got '" + message.body + "'");
tryResolve();
}
let onFailedHandler = function(aEvent) {
log("onfailed event received.");
failedEventCount++;
tryResolve();
}
let tryResolve = function() {
log("sentEventCount=" + sentEventCount + "; failedEventCount=" + failedEventCount);
if (sentEventCount === aExpectedSentMessages.length &&
failedEventCount === aExpectedFailures) {
manager.removeEventListener("sent", onSentHandler);
manager.removeEventListener("failed", onFailedHandler);
resolve();
}
}
manager.addEventListener("sent", onSentHandler);
manager.addEventListener("failed", onFailedHandler);
} catch (err) {
log("Error: " + err);
reject(err);
}
}));
// Delete messages with given ids.
promises.push(deleteMessagesById(aMessageIdsToDelete));
// wait for 3 seconds and turn on voice connection.
promises.push(new Promise(function(resolve, reject) {
setTimeout(() => resolve(), 3000);
}).then(() => setEmulatorVoiceStateAndWait("on")));
return Promise.all(promises);
}
startTestCommon(function testCaseMain() {
return pushPrefEnv({ set: [['dom.sms.requestStatusReport', true]] })
.then(() => ensureMobileConnection())
.then(() => setEmulatorVoiceStateAndWait("unregistered"))
.then(() => sendMessagesAndVerifySending())
// Delete the first message and wait for result.
.then((aMessageIds) => turnOnVoiceDeleteMessagesAndVerify([aMessageIds[0]],
MESSAGES.slice().splice(1, MESSAGES.length - 1), 1));
});