1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_forms_store.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +_("Make sure the form store follows the Store api and correctly accesses the backend form storage"); 1.8 +Cu.import("resource://services-sync/engines/forms.js"); 1.9 +Cu.import("resource://services-sync/service.js"); 1.10 +Cu.import("resource://services-sync/util.js"); 1.11 +Cu.import("resource://gre/modules/Services.jsm"); 1.12 + 1.13 +function run_test() { 1.14 + let baseuri = "http://fake/uri/"; 1.15 + let engine = new FormEngine(Service); 1.16 + let store = engine._store; 1.17 + 1.18 + function applyEnsureNoFailures(records) { 1.19 + do_check_eq(store.applyIncomingBatch(records).length, 0); 1.20 + } 1.21 + 1.22 + _("Remove any existing entries"); 1.23 + store.wipe(); 1.24 + for (let id in store.getAllIDs()) { 1.25 + do_throw("Shouldn't get any ids!"); 1.26 + } 1.27 + 1.28 + _("Add a form entry"); 1.29 + applyEnsureNoFailures([{ 1.30 + id: Utils.makeGUID(), 1.31 + name: "name!!", 1.32 + value: "value??" 1.33 + }]); 1.34 + 1.35 + _("Should have 1 entry now"); 1.36 + let id = ""; 1.37 + for (let _id in store.getAllIDs()) { 1.38 + if (id == "") 1.39 + id = _id; 1.40 + else 1.41 + do_throw("Should have only gotten one!"); 1.42 + } 1.43 + do_check_true(store.itemExists(id)); 1.44 + 1.45 + _("Should be able to find this entry as a dupe"); 1.46 + do_check_eq(engine._findDupe({name: "name!!", value: "value??"}), id); 1.47 + 1.48 + let rec = store.createRecord(id); 1.49 + _("Got record for id", id, rec); 1.50 + do_check_eq(rec.name, "name!!"); 1.51 + do_check_eq(rec.value, "value??"); 1.52 + 1.53 + _("Create a non-existent id for delete"); 1.54 + do_check_true(store.createRecord("deleted!!").deleted); 1.55 + 1.56 + _("Try updating.. doesn't do anything yet"); 1.57 + store.update({}); 1.58 + 1.59 + _("Remove all entries"); 1.60 + store.wipe(); 1.61 + for (let id in store.getAllIDs()) { 1.62 + do_throw("Shouldn't get any ids!"); 1.63 + } 1.64 + 1.65 + _("Add another entry"); 1.66 + applyEnsureNoFailures([{ 1.67 + id: Utils.makeGUID(), 1.68 + name: "another", 1.69 + value: "entry" 1.70 + }]); 1.71 + id = ""; 1.72 + for (let _id in store.getAllIDs()) { 1.73 + if (id == "") 1.74 + id = _id; 1.75 + else 1.76 + do_throw("Should have only gotten one!"); 1.77 + } 1.78 + 1.79 + _("Change the id of the new entry to something else"); 1.80 + store.changeItemID(id, "newid"); 1.81 + 1.82 + _("Make sure it's there"); 1.83 + do_check_true(store.itemExists("newid")); 1.84 + 1.85 + _("Remove the entry"); 1.86 + store.remove({ 1.87 + id: "newid" 1.88 + }); 1.89 + for (let id in store.getAllIDs()) { 1.90 + do_throw("Shouldn't get any ids!"); 1.91 + } 1.92 + 1.93 + _("Removing the entry again shouldn't matter"); 1.94 + store.remove({ 1.95 + id: "newid" 1.96 + }); 1.97 + for (let id in store.getAllIDs()) { 1.98 + do_throw("Shouldn't get any ids!"); 1.99 + } 1.100 + 1.101 + _("Add another entry to delete using applyIncomingBatch"); 1.102 + let toDelete = { 1.103 + id: Utils.makeGUID(), 1.104 + name: "todelete", 1.105 + value: "entry" 1.106 + }; 1.107 + applyEnsureNoFailures([toDelete]); 1.108 + id = ""; 1.109 + for (let _id in store.getAllIDs()) { 1.110 + if (id == "") 1.111 + id = _id; 1.112 + else 1.113 + do_throw("Should have only gotten one!"); 1.114 + } 1.115 + do_check_true(store.itemExists(id)); 1.116 + // mark entry as deleted 1.117 + toDelete.id = id; 1.118 + toDelete.deleted = true; 1.119 + applyEnsureNoFailures([toDelete]); 1.120 + for (let id in store.getAllIDs()) { 1.121 + do_throw("Shouldn't get any ids!"); 1.122 + } 1.123 + 1.124 + _("Add an entry to wipe"); 1.125 + applyEnsureNoFailures([{ 1.126 + id: Utils.makeGUID(), 1.127 + name: "towipe", 1.128 + value: "entry" 1.129 + }]); 1.130 + 1.131 + store.wipe(); 1.132 + 1.133 + for (let id in store.getAllIDs()) { 1.134 + do_throw("Shouldn't get any ids!"); 1.135 + } 1.136 + 1.137 + _("Ensure we work if formfill is disabled."); 1.138 + Services.prefs.setBoolPref("browser.formfill.enable", false); 1.139 + try { 1.140 + // a search 1.141 + for (let id in store.getAllIDs()) { 1.142 + do_throw("Shouldn't get any ids!"); 1.143 + } 1.144 + // an update. 1.145 + applyEnsureNoFailures([{ 1.146 + id: Utils.makeGUID(), 1.147 + name: "some", 1.148 + value: "entry" 1.149 + }]); 1.150 + } finally { 1.151 + Services.prefs.clearUserPref("browser.formfill.enable"); 1.152 + store.wipe(); 1.153 + } 1.154 +}