Bug 1237601 - Centralize storage xpcshell promise helpers (support patch). r=bkelly

This commit is contained in:
Andrew Sutherland 2016-01-12 17:27:33 -05:00
parent d663db527d
commit 70e2919f7f
3 changed files with 85 additions and 107 deletions

View File

@ -262,5 +262,89 @@ function getTableRowCount(aTableName)
return currentRows;
}
cleanup();
////////////////////////////////////////////////////////////////////////////////
//// Promise-Returning Functions
function asyncClone(db, readOnly) {
let deferred = Promise.defer();
db.asyncClone(readOnly, function (status, db2) {
if (Components.isSuccessCode(status)) {
deferred.resolve(db2);
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function asyncClose(db) {
let deferred = Promise.defer();
db.asyncClose(function (status) {
if (Components.isSuccessCode(status)) {
deferred.resolve();
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function openAsyncDatabase(file, options) {
let deferred = Promise.defer();
let properties;
if (options) {
properties = Cc["@mozilla.org/hash-property-bag;1"].
createInstance(Ci.nsIWritablePropertyBag);
for (let k in options) {
properties.setProperty(k, options[k]);
}
}
getService().openAsyncDatabase(file, properties, function (status, db) {
if (Components.isSuccessCode(status)) {
deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function executeAsync(statement, onResult) {
let deferred = Promise.defer();
statement.executeAsync({
handleError: function (error) {
deferred.reject(error);
},
handleResult: function (result) {
if (onResult) {
onResult(result);
}
},
handleCompletion: function (result) {
deferred.resolve(result);
}
});
return deferred.promise;
}
function executeSimpleSQLAsync(db, query, onResult) {
let deferred = Promise.defer();
db.executeSimpleSQLAsync(query, {
handleError(error) {
deferred.reject(error);
},
handleResult(result) {
if (onResult) {
onResult(result);
} else {
do_throw("No results were expected");
}
},
handleCompletion(result) {
deferred.resolve(result);
}
});
return deferred.promise;
}
cleanup();

View File

@ -11,50 +11,6 @@ const INTEGER = 1;
const TEXT = "this is test text";
const REAL = 3.23;
function asyncClose(db) {
let deferred = Promise.defer();
db.asyncClose(function (status) {
if (Components.isSuccessCode(status)) {
deferred.resolve();
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function openAsyncDatabase(file) {
let deferred = Promise.defer();
getService().openAsyncDatabase(file, null, function (status, db) {
if (Components.isSuccessCode(status)) {
deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function executeSimpleSQLAsync(db, query, onResult) {
let deferred = Promise.defer();
db.executeSimpleSQLAsync(query, {
handleError(error) {
deferred.reject(error);
},
handleResult(result) {
if (onResult) {
onResult(result);
} else {
do_throw("No results were expected");
}
},
handleCompletion(result) {
deferred.resolve(result);
}
});
return deferred.promise;
}
add_task(function* test_create_and_add() {
let adb = yield openAsyncDatabase(getTestDB());

View File

@ -7,68 +7,6 @@
////////////////////////////////////////////////////////////////////////////////
//// Test Functions
function asyncClone(db, readOnly) {
let deferred = Promise.defer();
db.asyncClone(readOnly, function (status, db2) {
if (Components.isSuccessCode(status)) {
deferred.resolve(db2);
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function asyncClose(db) {
let deferred = Promise.defer();
db.asyncClose(function (status) {
if (Components.isSuccessCode(status)) {
deferred.resolve();
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function openAsyncDatabase(file, options) {
let deferred = Promise.defer();
let properties;
if (options) {
properties = Cc["@mozilla.org/hash-property-bag;1"].
createInstance(Ci.nsIWritablePropertyBag);
for (let k in options) {
properties.setProperty(k, options[k]);
}
}
getService().openAsyncDatabase(file, properties, function (status, db) {
if (Components.isSuccessCode(status)) {
deferred.resolve(db.QueryInterface(Ci.mozIStorageAsyncConnection));
} else {
deferred.reject(status);
}
});
return deferred.promise;
}
function executeAsync(statement, onResult) {
let deferred = Promise.defer();
statement.executeAsync({
handleError: function (error) {
deferred.reject(error);
},
handleResult: function (result) {
if (onResult) {
onResult(result);
}
},
handleCompletion: function (result) {
deferred.resolve(result);
}
});
return deferred.promise;
}
add_task(function* test_connectionReady_open() {
// there doesn't seem to be a way for the connection to not be ready (unless
// we close it with mozIStorageConnection::Close(), but we don't for this).