Bug 980354 - 2/2: add more test cases for storage full event. r=hsinyi

This commit is contained in:
Vicamo Yang 2014-03-13 13:44:53 +09:00
Родитель 89cc629659
Коммит 7286272afa
4 изменённых файлов: 338 добавлений и 80 удалений

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

@ -71,6 +71,241 @@ function closeMobileMessageDB(aMmdb) {
return aMmdb;
}
/**
* Utility function for calling MMDB methods that takes either a
* nsIRilMobileMessageDatabaseCallback or a
* nsIRilMobileMessageDatabaseRecordCallback.
*
* Resolve when the target method notifies us with a successful result code;
* reject otherwise. In either case, the arguments passed are packed into an
* array and propagated to next action.
*
* Fulfill params: an array whose elements are the arguments of the original
* callback.
* Reject params: same as fulfill params.
*
* @param aMmdb
* A MobileMessageDB instance.
* @param aMethodName
* A string name for that target method.
* @param ...
* Extra arguments to pass to that target method. The last callback
* argument should always be excluded.
*
* @return A deferred promise.
*/
function callMmdbMethod(aMmdb, aMethodName) {
let deferred = Promise.defer();
let args = Array.slice(arguments, 2);
args.push({
notify: function(aRv) {
if (!Components.isSuccessCode(aRv)) {
ok(true, aMethodName + " returns a unsuccessful code: " + aRv);
deferred.reject(Array.slice(arguments));
} else {
ok(true, aMethodName + " returns a successful code: " + aRv);
deferred.resolve(Array.slice(arguments));
}
}
});
aMmdb[aMethodName].apply(aMmdb, args);
return deferred.promise;
}
/**
* A convenient function for calling |mmdb.saveSendingMessage(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function saveSendingMessage(aMmdb, aMessage) {
return callMmdbMethod(aMmdb, "saveSendingMessage", aMessage);
}
/**
* A convenient function for calling |mmdb.saveReceivedMessage(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function saveReceivedMessage(aMmdb, aMessage) {
return callMmdbMethod(aMmdb, "saveReceivedMessage", aMessage);
}
/**
* A convenient function for calling |mmdb.setMessageDeliveryByMessageId(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function setMessageDeliveryByMessageId(aMmdb, aMessageId, aReceiver, aDelivery,
aDeliveryStatus, aEnvelopeId) {
return callMmdbMethod(aMmdb, "setMessageDeliveryByMessageId", aMessageId,
aReceiver, aDelivery, aDeliveryStatus, aEnvelopeId);
}
/**
* A convenient function for calling
* |mmdb.setMessageDeliveryStatusByEnvelopeId(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function setMessageDeliveryStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver,
aDeliveryStatus) {
return callMmdbMethod(aMmdb, "setMessageDeliveryStatusByEnvelopeId",
aMmdb, aEnvelopeId, aReceiver, aDeliveryStatus);
}
/**
* A convenient function for calling
* |mmdb.setMessageReadStatusByEnvelopeId(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function setMessageReadStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver,
aReadStatus) {
return callMmdbMethod(aMmdb, "setMessageReadStatusByEnvelopeId",
aEnvelopeId, aReceiver, aReadStatus);
}
/**
* A convenient function for calling
* |mmdb.getMessageRecordByTransactionId(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function getMessageRecordByTransactionId(aMmdb, aTransactionId) {
return callMmdbMethod(aMmdb, "getMessageRecordByTransactionId",
aTransactionId);
}
/**
* A convenient function for calling |mmdb.getMessageRecordById(...)|.
*
* Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function getMessageRecordById(aMmdb, aMessageId) {
return callMmdbMethod(aMmdb, "getMessageRecordById", aMessageId);
}
/**
* A convenient function for calling |mmdb.markMessageRead(...)|.
*
* Fulfill params: Ci.nsIMobileMessageCallback.FOO.
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function markMessageRead(aMmdb, aMessageId, aRead) {
let deferred = Promise.defer();
aMmdb.markMessageRead(aMessageId, aRead, false, {
notifyMarkMessageReadFailed: function(aRv) {
ok(true, "markMessageRead returns a unsuccessful code: " + aRv);
deferred.reject(aRv);
},
notifyMessageMarkedRead: function(aRead) {
ok(true, "markMessageRead returns a successful code: " + Cr.NS_OK);
deferred.resolve(Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR);
}
});
return deferred.promise;
}
/**
* Utility function for calling cursor-based MMDB methods.
*
* Resolve when the target method notifies us with |notifyCursorDone|,
* reject otherwise.
*
* Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message/thread>]]
* Reject params: same as fulfill params.
*
* @param aMmdb
* A MobileMessageDB instance.
* @param aMethodName
* A string name for that target method.
* @param ...
* Extra arguments to pass to that target method. The last callback
* argument should always be excluded.
*
* @return A deferred promise.
*/
function createMmdbCursor(aMmdb, aMethodName) {
let deferred = Promise.defer();
let cursor;
let results = [];
let args = Array.slice(arguments, 2);
args.push({
notifyCursorError: function(aRv) {
ok(true, "notifyCursorError: " + aRv);
deferred.reject([aRv, results]);
},
notifyCursorResult: function(aResult) {
ok(true, "notifyCursorResult: " + aResult.id);
results.push(aResult);
cursor.handleContinue();
},
notifyCursorDone: function() {
ok(true, "notifyCursorDone");
deferred.resolve([Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR, results]);
}
});
cursor = aMmdb[aMethodName].apply(aMmdb, args);
return deferred.promise;
}
/**
* A convenient function for calling |mmdb.createMessageCursor(...)|.
*
* Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message>]].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function createMessageCursor(aMmdb, aFilter, aReverse) {
return createMmdbCursor(aMmdb, "createMessageCursor", aFilter, aReverse);
}
/**
* A convenient function for calling |mmdb.createThreadCursor(...)|.
*
* Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM thread>]].
* Reject params: same as fulfill params.
*
* @return A deferred promise.
*/
function createThreadCursor(aMmdb) {
return createMmdbCursor(aMmdb, "createThreadCursor");
}
// A reference to a nsIUUIDGenerator service.
let _uuidGenerator;

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

@ -52,8 +52,6 @@ function testNotFound(aMmdb) {
doTest(aMmdb, PHONE_0, function(aElement) {
ok(false, "Should never have a match");
}, 0);
return aMmdb;
}
function testDirectMatch(aMmdb) {
@ -67,8 +65,6 @@ function testDirectMatch(aMmdb) {
is(aElement.receiver, needle, "element.receiver");
}, count);
}
return aMmdb;
}
function testPhoneMatch(aMmdb) {
@ -87,15 +83,13 @@ function testPhoneMatch(aMmdb) {
doTest(aMmdb, item, verifyFunc.bind(null, group), group.length);
}
}
return aMmdb;
}
startTestBase(function testCaseMain() {
let mmdb = newMobileMessageDB();
return initMobileMessageDB(mmdb, DBNAME, 0)
.then(testNotFound)
.then(testDirectMatch)
.then(testPhoneMatch)
.then(closeMobileMessageDB);
.then(() => testNotFound(mmdb))
.then(() => testDirectMatch(mmdb))
.then(() => testPhoneMatch(mmdb))
.then(() => closeMobileMessageDB(mmdb));
});

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

@ -8,82 +8,112 @@ const DBNAME = "test_mmdb_full_storage:" + newUUID();
let gIsDiskFull = true;
let gMessageToSave = {
type: "sms",
sender: "+0987654321",
receiver: "+1234567890",
body: "quick fox jump over the lazy dog",
deliveryStatusRequested: false,
timestamp: Date.now(),
iccId: "1029384756"
};
function newSavableMessage() {
return {
type: "sms",
sender: "+0987654321",
receiver: "+1234567890",
body: "quick fox jump over the lazy dog",
deliveryStatusRequested: false,
messageClass: "normal",
timestamp: Date.now(),
iccId: "1029384756"
};
}
function isFileNoDeviceSpaceError(aErrorResult) {
is(aErrorResult, Cr.NS_ERROR_FILE_NO_DEVICE_SPACE, "Database error code");
}
function isCallbackStorageFullError(aErrorCode) {
is(aErrorCode, Ci.nsIMobileMessageCallback.STORAGE_FULL_ERROR,
"nsIMobileMessageCallback error code");
}
function testSaveSendingMessage(aMmdb) {
log("testSaveSendingMessage()");
let deferred = Promise.defer();
aMmdb.saveSendingMessage(gMessageToSave,
{ notify : function(aRv, aDomMessage) {
if (aRv === Cr.NS_ERROR_FILE_NO_DEVICE_SPACE) {
ok(true, "Forbidden due to storage full.");
deferred.resolve(aMmdb);
} else {
ok(false, "Unexpected result: " + aRv);
deferred.reject(aRv);
}
}});
return deferred.promise;
gIsDiskFull = true;
return saveSendingMessage(aMmdb, newSavableMessage())
// Resolved/rejected results are both [<Cr.NS_ERROR_FOO>, <DOM message>],
// and we need only the error code in both cases.
.then((aValue) => aValue[0],
(aValue) => aValue[0])
.then(isFileNoDeviceSpaceError);
}
function testSaveReceivingMessage(aMmdb) {
log("testSaveReceivingMessage()");
function testSaveReceivedMessage(aMmdb) {
log("testSaveReceivedMessage()");
let deferred = Promise.defer();
aMmdb.saveReceivedMessage(gMessageToSave,
{ notify : function(aRv, aDomMessage) {
if (aRv === Cr.NS_ERROR_FILE_NO_DEVICE_SPACE) {
ok(true, "Forbidden due to storage full.");
deferred.resolve(aMmdb);
} else {
ok(false, "Unexpected result: " + aRv);
deferred.reject(aRv);
}
}});
return deferred.promise;
gIsDiskFull = true;
return saveReceivedMessage(aMmdb, newSavableMessage())
// Resolved/rejected results are both [<Cr.NS_ERROR_FOO>, <DOM message>],
// and we need only the error code in both cases.
.then((aValue) => aValue[0],
(aValue) => aValue[0])
.then(isFileNoDeviceSpaceError);
}
function testGetMessage(aMmdb) {
log("testGetMessage()");
function testGetMessageRecordById(aMmdb) {
log("testGetMessageRecordById()");
let deferred = Promise.defer();
gIsDiskFull = false;
return saveReceivedMessage(aMmdb, newSavableMessage())
// Resolved result is [<Cr.NS_ERROR_FOO>, <DOM message>],
.then(function(aValue) {
let domMessage = aValue[1];
aMmdb.getMessage(1,
{ notifyGetMessageFailed : function(aRv) {
if (aRv === Ci.nsIMobileMessageCallback.NOT_FOUND_ERROR) {
ok(true, "Getting message successfully!");
deferred.resolve(aMmdb);
} else {
ok(false, "Unexpected result: " + aRv);
deferred.reject(aRv);
}
}});
gIsDiskFull = true;
return getMessageRecordById(aMmdb, domMessage.id);
});
}
return deferred.promise;
function testMarkMessageRead(aMmdb) {
log("testMarkMessageRead()");
gIsDiskFull = false;
return saveReceivedMessage(aMmdb, newSavableMessage())
// Resolved/rejected results are both [<Cr.NS_ERROR_FOO>, <DOM message>].
.then(function(aValue) {
let domMessage = aValue[1];
gIsDiskFull = true;
return markMessageRead(aMmdb, domMessage.id, true)
.then(null, (aValue) => aValue)
.then(isCallbackStorageFullError);
});
}
function testCreateMessageCursor(aMmdb) {
log("testCreateMessageCursor()");
gIsDiskFull = true;
return createMessageCursor(aMmdb, {}, false);
}
function testCreateThreadCursor(aMmdb) {
log("testCreateThreadCursor()");
gIsDiskFull = true;
return createThreadCursor(aMmdb);
}
startTestBase(function testCaseMain() {
let mmdb = newMobileMessageDB();
mmdb.isDiskFull = function() {
return gIsDiskFull;
};
return initMobileMessageDB(mmdb, DBNAME, 0)
.then(testSaveSendingMessage)
.then(testSaveReceivingMessage)
.then(testGetMessage)
.then(closeMobileMessageDB);
.then(function() {
mmdb.isDiskFull = function() {
return gIsDiskFull;
};
})
.then(() => testSaveSendingMessage(mmdb))
.then(() => testSaveReceivedMessage(mmdb))
.then(() => testGetMessageRecordById(mmdb))
.then(() => testMarkMessageRead(mmdb))
.then(() => testCreateMessageCursor(mmdb))
.then(() => testCreateThreadCursor(mmdb))
.then(() => closeMobileMessageDB(mmdb));
});

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

@ -15,8 +15,6 @@ function check(aMmdb) {
} else {
is(aMmdb.dbVersion, dbVersion, "dbVersion");
}
return aMmdb;
}
startTestBase(function testCaseMain() {
@ -24,14 +22,15 @@ startTestBase(function testCaseMain() {
let mmdb = newMobileMessageDB();
return initMobileMessageDB(mmdb, DBNAME, dbVersion)
.then(check)
.then(closeMobileMessageDB)
.then(check)
.then(function(aMmdb) {
.then(() => check(mmdb))
.then(() => closeMobileMessageDB(mmdb))
.then(() => check(mmdb))
.then(function() {
log("Test re-init and close.");
return initMobileMessageDB(aMmdb, DBNAME, dbVersion);
return initMobileMessageDB(mmdb, DBNAME, dbVersion);
})
.then(check)
.then(closeMobileMessageDB)
.then(check);
.then(() => check(mmdb))
.then(() => closeMobileMessageDB(mmdb))
.then(() => check(mmdb));
});