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