gecko/services/sync/tests/unit/test_forms_store.js
Gregory Szorc 45d3f90cf3 Bug 785225 - Part 9: Refactor engines to not use singletons; r=rnewman
Engines now maintain a reference to the service they belong to. This
allows them to obtain references to other engine instances belonging to
that service and that service only.

Stores and trackers now maintain a reference to the engine they belong
to.

Engine managers now maintain a reference back to a service.

The clients singleton has been removed. It now exists as an instance
variable on Service. Parts of ClientsEngine do behave as singletons
(e.g. commands). This will be addressed in future refactoring.
2012-08-29 14:43:41 -07:00

131 lines
3.1 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
_("Make sure the form store follows the Store api and correctly accesses the backend form storage");
Cu.import("resource://services-sync/engines/forms.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
function run_test() {
let baseuri = "http://fake/uri/";
let store = new FormEngine(Service)._store;
function applyEnsureNoFailures(records) {
do_check_eq(store.applyIncomingBatch(records).length, 0);
}
_("Remove any existing entries");
store.wipe();
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
_("Add a form entry");
applyEnsureNoFailures([{
id: Utils.makeGUID(),
name: "name!!",
value: "value??"
}]);
_("Should have 1 entry now");
let id = "";
for (let _id in store.getAllIDs()) {
if (id == "")
id = _id;
else
do_throw("Should have only gotten one!");
}
do_check_true(store.itemExists(id));
let rec = store.createRecord(id);
_("Got record for id", id, rec);
do_check_eq(rec.name, "name!!");
do_check_eq(rec.value, "value??");
_("Create a non-existent id for delete");
do_check_true(store.createRecord("deleted!!").deleted);
_("Try updating.. doesn't do anything yet");
store.update({});
_("Remove all entries");
store.wipe();
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
_("Add another entry");
applyEnsureNoFailures([{
id: Utils.makeGUID(),
name: "another",
value: "entry"
}]);
id = "";
for (let _id in store.getAllIDs()) {
if (id == "")
id = _id;
else
do_throw("Should have only gotten one!");
}
_("Change the id of the new entry to something else");
store.changeItemID(id, "newid");
_("Make sure it's there");
do_check_true(store.itemExists("newid"));
_("Remove the entry");
store.remove({
id: "newid"
});
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
_("Removing the entry again shouldn't matter");
store.remove({
id: "newid"
});
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
_("Add another entry to delete using applyIncomingBatch");
let toDelete = {
id: Utils.makeGUID(),
name: "todelete",
value: "entry"
};
applyEnsureNoFailures([toDelete]);
id = "";
for (let _id in store.getAllIDs()) {
if (id == "")
id = _id;
else
do_throw("Should have only gotten one!");
}
do_check_true(store.itemExists(id));
// mark entry as deleted
toDelete.id = id;
toDelete.deleted = true;
applyEnsureNoFailures([toDelete]);
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
_("Add an entry to wipe");
applyEnsureNoFailures([{
id: Utils.makeGUID(),
name: "towipe",
value: "entry"
}]);
Utils.runInTransaction(Svc.Form.DBConnection, function() {
store.wipe();
});
for (let id in store.getAllIDs()) {
do_throw("Shouldn't get any ids!");
}
}