test_forms_store.js (4200B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 _( 5 "Make sure the form store follows the Store api and correctly accesses the backend form storage" 6 ); 7 const { FormEngine } = ChromeUtils.importESModule( 8 "resource://services-sync/engines/forms.sys.mjs" 9 ); 10 const { Service } = ChromeUtils.importESModule( 11 "resource://services-sync/service.sys.mjs" 12 ); 13 const { SyncedRecordsTelemetry } = ChromeUtils.importESModule( 14 "resource://services-sync/telemetry.sys.mjs" 15 ); 16 17 add_task(async function run_test() { 18 let engine = new FormEngine(Service); 19 await engine.initialize(); 20 let store = engine._store; 21 22 async function applyEnsureNoFailures(records) { 23 let countTelemetry = new SyncedRecordsTelemetry(); 24 Assert.equal( 25 (await store.applyIncomingBatch(records, countTelemetry)).length, 26 0 27 ); 28 } 29 30 _("Remove any existing entries"); 31 await store.wipe(); 32 if ((await store.getAllIDs()).length) { 33 do_throw("Shouldn't get any ids!"); 34 } 35 36 _("Add a form entry"); 37 await applyEnsureNoFailures([ 38 { 39 id: Utils.makeGUID(), 40 name: "name!!", 41 value: "value??", 42 }, 43 ]); 44 45 _("Should have 1 entry now"); 46 let id = ""; 47 for (let _id in await store.getAllIDs()) { 48 if (id == "") { 49 id = _id; 50 } else { 51 do_throw("Should have only gotten one!"); 52 } 53 } 54 Assert.ok(store.itemExists(id)); 55 56 _("Should be able to find this entry as a dupe"); 57 Assert.equal( 58 await engine._findDupe({ name: "name!!", value: "value??" }), 59 id 60 ); 61 62 let rec = await store.createRecord(id); 63 _("Got record for id", id, rec); 64 Assert.equal(rec.name, "name!!"); 65 Assert.equal(rec.value, "value??"); 66 67 _("Create a non-existent id for delete"); 68 Assert.ok((await store.createRecord("deleted!!")).deleted); 69 70 _("Try updating.. doesn't do anything yet"); 71 await store.update({}); 72 73 _("Remove all entries"); 74 await store.wipe(); 75 if ((await store.getAllIDs()).length) { 76 do_throw("Shouldn't get any ids!"); 77 } 78 79 _("Add another entry"); 80 await applyEnsureNoFailures([ 81 { 82 id: Utils.makeGUID(), 83 name: "another", 84 value: "entry", 85 }, 86 ]); 87 id = ""; 88 for (let _id in await store.getAllIDs()) { 89 if (id == "") { 90 id = _id; 91 } else { 92 do_throw("Should have only gotten one!"); 93 } 94 } 95 96 _("Change the id of the new entry to something else"); 97 await store.changeItemID(id, "newid"); 98 99 _("Make sure it's there"); 100 Assert.ok(store.itemExists("newid")); 101 102 _("Remove the entry"); 103 await store.remove({ 104 id: "newid", 105 }); 106 if ((await store.getAllIDs()).length) { 107 do_throw("Shouldn't get any ids!"); 108 } 109 110 _("Removing the entry again shouldn't matter"); 111 await store.remove({ 112 id: "newid", 113 }); 114 if ((await store.getAllIDs()).length) { 115 do_throw("Shouldn't get any ids!"); 116 } 117 118 _("Add another entry to delete using applyIncomingBatch"); 119 let toDelete = { 120 id: Utils.makeGUID(), 121 name: "todelete", 122 value: "entry", 123 }; 124 await applyEnsureNoFailures([toDelete]); 125 id = ""; 126 for (let _id in await store.getAllIDs()) { 127 if (id == "") { 128 id = _id; 129 } else { 130 do_throw("Should have only gotten one!"); 131 } 132 } 133 Assert.ok(store.itemExists(id)); 134 // mark entry as deleted 135 toDelete.id = id; 136 toDelete.deleted = true; 137 await applyEnsureNoFailures([toDelete]); 138 if ((await store.getAllIDs()).length) { 139 do_throw("Shouldn't get any ids!"); 140 } 141 142 _("Add an entry to wipe"); 143 await applyEnsureNoFailures([ 144 { 145 id: Utils.makeGUID(), 146 name: "towipe", 147 value: "entry", 148 }, 149 ]); 150 151 await store.wipe(); 152 153 if ((await store.getAllIDs()).length) { 154 do_throw("Shouldn't get any ids!"); 155 } 156 157 _("Ensure we work if formfill is disabled."); 158 Services.prefs.setBoolPref("browser.formfill.enable", false); 159 try { 160 // a search 161 if ((await store.getAllIDs()).length) { 162 do_throw("Shouldn't get any ids!"); 163 } 164 // an update. 165 await applyEnsureNoFailures([ 166 { 167 id: Utils.makeGUID(), 168 name: "some", 169 value: "entry", 170 }, 171 ]); 172 } finally { 173 Services.prefs.clearUserPref("browser.formfill.enable"); 174 await store.wipe(); 175 } 176 });