Bug 903058 - rewrite SMS test cases for filtering with single number. r=gene

This commit is contained in:
Vicamo Yang 2013-08-23 14:45:16 +08:00
parent d11ffa96cf
commit 55af35fdac
4 changed files with 202 additions and 458 deletions

View File

@ -16,8 +16,7 @@ qemu = true
[test_getmessages.js]
[test_filter_date.js]
[test_filter_date_notfound.js]
[test_filter_number_single.js]
[test_filter_number_multiple.js]
[test_filter_number.js]
[test_filter_received.js]
[test_filter_sent.js]
[test_filter_read.js]

View File

@ -0,0 +1,201 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
const NUM_THREADS = 10;
const REMOTE_NATIONAL_NUMBER = "555531555";
const REMOTE_INTERNATIONAL_NUMBER = "+1" + REMOTE_NATIONAL_NUMBER;
SpecialPowers.addPermission("sms", true, document);
SpecialPowers.setBoolPref("dom.sms.enabled", true);
let pendingEmulatorCmdCount = 0;
function sendSmsToEmulator(from, text) {
++pendingEmulatorCmdCount;
let cmd = "sms send " + from + " " + text;
runEmulatorCmd(cmd, function (result) {
--pendingEmulatorCmdCount;
is(result[0], "OK", "Emulator response");
});
}
let 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 push(func) {
this._tasks.push(func);
},
next: function next() {
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 finish() {
this._tasks[this._tasks.length - 1]();
},
run: function run() {
this.next();
}
};
let manager;
function getAllMessages(callback, filter, reverse) {
if (!filter) {
filter = new MozSmsFilter;
}
let messages = [];
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() {
log("Deleting all messages.");
getAllMessages(function deleteAll(messages) {
let message = messages.shift();
if (!message) {
ok(true, "all messages deleted");
tasks.next();
return;
}
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 checkMessage(needle, secondary) {
log(" Verifying " + needle);
let filter = new MozSmsFilter();
filter.numbers = [needle];
getAllMessages(function (messages) {
is(messages.length, 2, "should have exactly 2 messages");
// Check the messages are sent to/received from either 'needle' or
// 'secondary' number.
let validNumbers = [needle, secondary];
for (let message of messages) {
let number = (message.delivery === "received") ? message.sender
: message.receiver;
let index = validNumbers.indexOf(number);
ok(index >= 0, "message.number");
validNumbers.splice(index, 1); // Remove from validNumbers.
}
tasks.next();
}, filter);
}
tasks.push(function verifyInitialState() {
log("Verifying initial state.");
manager = window.navigator.mozMobileMessage;
ok(manager instanceof MozMobileMessageManager,
"manager is instance of " + manager.constructor);
tasks.next();
});
tasks.push(deleteAllMessages);
/**
* Populate database with messages to being tests. We'll have NUM_THREADS
* sent and received messages.
*
* send to "+15555315550"
* receive from "5555315550", count = 1
*
* send to "+15555315551"
* receive from "5555315551", count = 2
* ...
* send to "+15555315559"
* receive from "5555315559", count = 10
*/
tasks.push(function populateMessages() {
log("Populating messages.");
let count = 0;
function sendMessage(iter) {
let request = manager.send(REMOTE_INTERNATIONAL_NUMBER + iter,
"Nice to meet you");
request.onsuccess = function onRequestSuccess(event) {
sendSmsToEmulator(REMOTE_NATIONAL_NUMBER + iter,
"Nice to meet you, too");
}
request.onerror = function onRequestError(event) {
tasks.finish();
}
}
manager.addEventListener("received", function onReceived(event) {
++count;
if (count < NUM_THREADS) {
sendMessage(count);
} else {
manager.removeEventListener("received", onReceived);
tasks.next();
}
});
sendMessage(count);
});
tasks.push(function () {
log("Verifying number of messages in database");
getAllMessages(function (messages) {
is(messages.length, NUM_THREADS * 2,
"should have exactly " + (NUM_THREADS * 2) + " messages");
tasks.next();
});
});
for (let iter = 0; iter < NUM_THREADS; iter++) {
let national = REMOTE_NATIONAL_NUMBER + iter;
let international = REMOTE_INTERNATIONAL_NUMBER + iter;
tasks.push(checkMessage.bind(null, national, international));
tasks.push(checkMessage.bind(null, international, national));
}
tasks.push(deleteAllMessages);
// WARNING: All tasks should be pushed before this!!!
tasks.push(function cleanUp() {
if (pendingEmulatorCmdCount) {
window.setTimeout(cleanUp, 100);
return;
}
SpecialPowers.removePermission("sms", document);
SpecialPowers.clearUserPref("dom.sms.enabled");
finish();
});
tasks.run();

View File

@ -1,249 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
SpecialPowers.addPermission("sms", true, document);
SpecialPowers.setBoolPref("dom.sms.enabled", true);
let manager = window.navigator.mozMobileMessage;
let numberMsgs = 10;
let smsList = new Array();
let defaultRemoteNumber = "+15552227777";
function verifyInitialState() {
log("Verifying initial state.");
ok(manager instanceof MozMobileMessageManager,
"manager is instance of " + manager.constructor);
// Ensure test is starting clean with no existing sms messages
deleteAllMsgs(sendSms);
}
function deleteAllMsgs(nextFunction) {
// Check for any existing SMS messages, if any are found delete them
let msgList = new Array();
let filter = new MozSmsFilter;
let cursor = manager.getMessages(filter, false);
ok(cursor instanceof DOMCursor,
"cursor is instanceof " + cursor.constructor);
cursor.onsuccess = function(event) {
// Check if message was found
if (cursor.result) {
msgList.push(cursor.result.id);
// Now get next message in the list
cursor.continue();
} else {
// No (more) messages found
if (msgList.length) {
log("Found " + msgList.length + " SMS messages to delete.");
deleteMsgs(msgList, nextFunction);
} else {
log("No SMS messages found.");
nextFunction();
}
}
};
cursor.onerror = function(event) {
log("Received 'onerror' event.");
ok(event.target.error, "domerror obj");
log("manager.getMessages error: " + event.target.error.name);
ok(false,"Could not get SMS messages");
cleanUp();
};
}
function deleteMsgs(msgList, nextFunction) {
// Delete the SMS messages specified in the given list
let smsId = msgList.shift();
log("Deleting SMS (id: " + smsId + ").");
let request = manager.delete(smsId);
ok(request instanceof DOMRequest,
"request is instanceof " + request.constructor);
request.onsuccess = function(event) {
log("Received 'onsuccess' smsrequest event.");
if (event.target.result) {
// Message deleted, continue until none are left
if (msgList.length) {
deleteMsgs(msgList, nextFunction);
} else {
log("Finished deleting SMS messages.");
nextFunction();
}
} else {
log("SMS delete failed.");
ok(false,"manager.delete request returned false");
cleanUp();
}
};
request.onerror = function(event) {
log("Received 'onerror' smsrequest event.");
ok(event.target.error, "domerror obj");
ok(false, "manager.delete request returned unexpected error: "
+ event.target.error.name );
cleanUp();
};
}
function sendSms() {
// Send an SMS to a unique number that will fall outside of the filter
let gotSmsSent = false;
let gotRequestSuccess = false;
let remoteNumber = "+15558120649";
let text = "Outgoing SMS brought to you by Firefox OS!";
log("Sending an SMS.");
manager.onsent = function(event) {
log("Received 'onsent' event.");
gotSmsSent = true;
log("Sent SMS (id: " + event.message.id + ").");
if (gotSmsSent && gotRequestSuccess) {
simulateIncomingSms();
}
};
let request = manager.send(remoteNumber, text);
ok(request instanceof DOMRequest,
"request is instanceof " + request.constructor);
request.onsuccess = function(event) {
log("Received 'onsuccess' smsrequest event.");
if (event.target.result) {
gotRequestSuccess = true;
if (gotSmsSent && gotRequestSuccess) {
simulateIncomingSms();
}
} else {
log("smsrequest returned false for manager.send");
ok(false,"SMS send failed");
cleanUp();
}
};
request.onerror = function(event) {
log("Received 'onerror' smsrequest event.");
ok(event.target.error, "domerror obj");
ok(false, "manager.send request returned unexpected error: "
+ event.target.error.name );
cleanUp();
};
}
function simulateIncomingSms(remoteNumber) {
// Simulate incoming SMS messages from specified (or default) remote number
let text = "Incoming SMS number " + (smsList.length + 1);
remoteNumber = typeof remoteNumber !== 'undefined'
? remoteNumber : defaultRemoteNumber;
log("Simulating incoming SMS number " + (smsList.length + 1) + " of "
+ (numberMsgs - 1) + ".");
// Simulate incoming SMS sent from remoteNumber to our emulator
rcvdEmulatorCallback = false;
runEmulatorCmd("sms send " + remoteNumber + " " + text, function(result) {
is(result[0], "OK", "emulator callback");
rcvdEmulatorCallback = true;
});
}
manager.onreceived = function onreceived(event) {
// Callback for incoming SMS
log("Received 'onreceived' sms event.");
let incomingSms = event.message;
log("Received SMS (id: " + incomingSms.id + ").");
smsList.push(incomingSms);
// Wait for emulator to catch up before continuing
waitFor(nextRep,function() {
return(rcvdEmulatorCallback);
});
};
function nextRep() {
// Keep simulating incoming messages until have received specified number
let secondNumber = "+15559990000";
if (smsList.length < (numberMsgs - 1)) {
// Have every other SMS be from different number, so filter won't find all
if (smsList.length % 2) {
simulateIncomingSms(secondNumber);
} else {
simulateIncomingSms();
}
} else {
getMsgs(secondNumber);
}
}
function getMsgs(secondNumber) {
// Set the filter and test it via getMessages
var filter = new MozSmsFilter();
let foundSmsList = new Array();
// Set filter for default and second number
filter.numbers = new Array(defaultRemoteNumber, secondNumber);
log("Getting the SMS messages with numbers " + defaultRemoteNumber + " and "
+ secondNumber + ".");
let cursor = manager.getMessages(filter, false);
ok(cursor instanceof DOMCursor,
"cursor is instanceof " + cursor.constructor);
cursor.onsuccess = function(event) {
log("Received 'onsuccess' event.");
if (cursor.result) {
// Another message found
log("Got SMS (id: " + cursor.result.id + ").");
// Store found message
foundSmsList.push(cursor.result);
// Now get next message in the list
cursor.continue();
} else {
// No more messages; ensure correct number of SMS messages were found
if (foundSmsList.length == smsList.length) {
log("SMS getMessages returned " + foundSmsList.length +
" messages as expected.");
verifyFoundMsgs(foundSmsList);
} else {
log("SMS getMessages returned " + foundSmsList.length +
" messages, but expected " + smsList.length + ".");
ok(false, "Incorrect number of messages returned by manager.getMessages");
deleteAllMsgs(cleanUp);
}
}
};
cursor.onerror = function(event) {
log("Received 'onerror' event.");
ok(event.target.error, "domerror obj");
log("manager.getMessages error: " + event.target.error.name);
ok(false,"Could not get SMS messages");
cleanUp();
};
}
function verifyFoundMsgs(foundSmsList) {
// Verify the SMS messages returned by getMessages are the correct ones
for (var x = 0; x < foundSmsList.length; x++) {
is(foundSmsList[x].id, smsList[x].id, "id");
is(foundSmsList[x].sender, smsList[x].sender, "number");
}
deleteAllMsgs(cleanUp);
}
function cleanUp() {
manager.onreceived = null;
SpecialPowers.removePermission("sms", document);
SpecialPowers.clearUserPref("dom.sms.enabled");
finish();
}
// Start the test
verifyInitialState();

View File

@ -1,207 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
MARIONETTE_TIMEOUT = 60000;
SpecialPowers.addPermission("sms", true, document);
SpecialPowers.setBoolPref("dom.sms.enabled", true);
let manager = window.navigator.mozMobileMessage;
let numberMsgs = 10;
let smsList = new Array();
let defaultRemoteNumber = "+15552227777";
function verifyInitialState() {
log("Verifying initial state.");
ok(manager instanceof MozMobileMessageManager,
"manager is instance of " + manager.constructor);
// Ensure test is starting clean with no existing sms messages
deleteAllMsgs(simulateIncomingSms);
}
function deleteAllMsgs(nextFunction) {
// Check for any existing SMS messages, if any are found delete them
let msgList = new Array();
let filter = new MozSmsFilter;
let cursor = manager.getMessages(filter, false);
ok(cursor instanceof DOMCursor,
"cursor is instanceof " + cursor.constructor);
cursor.onsuccess = function(event) {
// Check if message was found
if (cursor.result) {
msgList.push(cursor.result.id);
// Now get next message in the list
cursor.continue();
} else {
// No (more) messages found
if (msgList.length) {
log("Found " + msgList.length + " SMS messages to delete.");
deleteMsgs(msgList, nextFunction);
} else {
log("No SMS messages found.");
nextFunction();
}
}
};
cursor.onerror = function(event) {
log("Received 'onerror' event.");
ok(event.target.error, "domerror obj");
log("manager.getMessages error: " + event.target.error.name);
ok(false,"Could not get SMS messages");
cleanUp();
};
}
function deleteMsgs(msgList, nextFunction) {
// Delete the SMS messages specified in the given list
let smsId = msgList.shift();
log("Deleting SMS (id: " + smsId + ").");
let request = manager.delete(smsId);
ok(request instanceof DOMRequest,
"request is instanceof " + request.constructor);
request.onsuccess = function(event) {
log("Received 'onsuccess' smsrequest event.");
if (event.target.result) {
// Message deleted, continue until none are left
if (msgList.length) {
deleteMsgs(msgList, nextFunction);
} else {
log("Finished deleting SMS messages.");
nextFunction();
}
} else {
log("SMS delete failed.");
ok(false,"manager.delete request returned false");
cleanUp();
}
};
request.onerror = function(event) {
log("Received 'onerror' smsrequest event.");
ok(event.target.error, "domerror obj");
ok(false, "manager.delete request returned unexpected error: "
+ event.target.error.name );
cleanUp();
};
}
function simulateIncomingSms(remoteNumber) {
// Simulate incoming SMS messages from specified (or default) remote number
let text = "Incoming SMS number " + (smsList.length + 1);
remoteNumber = typeof remoteNumber !== 'undefined'
? remoteNumber : defaultRemoteNumber;
log("Simulating incoming SMS number " + (smsList.length + 1) + " of "
+ numberMsgs + ".");
// Simulate incoming sms sent from remoteNumber to our emulator
rcvdEmulatorCallback = false;
runEmulatorCmd("sms send " + remoteNumber + " " + text, function(result) {
is(result[0], "OK", "emulator callback");
rcvdEmulatorCallback = true;
});
}
manager.onreceived = function onreceived(event) {
// Callback for incoming SMS
log("Received 'onreceived' sms event.");
let incomingSms = event.message;
log("Received SMS (id: " + incomingSms.id + ").");
smsList.push(incomingSms);
// Wait for emulator to catch up before continuing
waitFor(nextRep,function() {
return(rcvdEmulatorCallback);
});
};
function nextRep() {
// Keep simulating incoming messages until have received specified number
if (smsList.length < numberMsgs) {
// Have every other sms be from different number, so filter won't find all
if (smsList.length % 2) {
simulateIncomingSms("+15559990000");
} else {
simulateIncomingSms();
}
} else {
getMsgs();
}
}
function getMsgs() {
// Set the filter and test it via getMessages
var filter = new MozSmsFilter();
let foundSmsList = new Array();
// Going to filter for one number only, so set our expected SMS array
smsList = smsList.filter(function(i) {
return i.sender != defaultRemoteNumber ? false: true;
});
// Set filter for default remote number
filter.numbers = new Array(defaultRemoteNumber);
log("Getting the SMS messages from sender " + defaultRemoteNumber + ".");
let cursor = manager.getMessages(filter, false);
ok(cursor instanceof DOMCursor,
"cursor is instanceof " + cursor.constructor);
cursor.onsuccess = function(event) {
log("Received 'onsuccess' event.");
if (cursor.result) {
// Another message found
log("Got SMS (id: " + cursor.result.id + ").");
// Store found message
foundSmsList.push(cursor.result);
// Now get next message in the list
cursor.continue();
} else {
// No more messages; ensure correct number of SMS messages were found
if (foundSmsList.length == smsList.length) {
log("SMS getMessages returned " + foundSmsList.length +
" messages as expected.");
verifyFoundMsgs(foundSmsList);
} else {
log("SMS getMessages returned " + foundSmsList.length +
" messages, but expected " + smsList.length + ".");
ok(false, "Incorrect number of messages returned by manager.getMessages");
deleteAllMsgs(cleanUp);
}
}
};
cursor.onerror = function(event) {
log("Received 'onerror' event.");
ok(event.target.error, "domerror obj");
log("manager.getMessages error: " + event.target.error.name);
ok(false,"Could not get SMS messages");
cleanUp();
};
}
function verifyFoundMsgs(foundSmsList) {
// Verify the SMS messages returned by getMessages are the correct ones
for (var x = 0; x < foundSmsList.length; x++) {
is(foundSmsList[x].id, smsList[x].id, "id");
is(foundSmsList[x].sender, smsList[x].sender, "number");
}
deleteAllMsgs(cleanUp);
}
function cleanUp() {
manager.onreceived = null;
SpecialPowers.removePermission("sms", document);
SpecialPowers.clearUserPref("dom.sms.enabled");
finish();
}
// Start the test
verifyInitialState();