summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_forms_store.js
blob: 6963df1c0b00a4c9d00be40672a2f1fc8146b4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 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");
Cu.import("resource://gre/modules/Services.jsm");

function run_test() {
  let baseuri = "http://fake/uri/";
  let engine = new FormEngine(Service);
  let store = engine._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));

  _("Should be able to find this entry as a dupe");
  do_check_eq(engine._findDupe({name: "name!!", value: "value??"}), 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"
  }]);

  store.wipe();

  for (let id in store.getAllIDs()) {
    do_throw("Shouldn't get any ids!");
  }

  _("Ensure we work if formfill is disabled.");
  Services.prefs.setBoolPref("browser.formfill.enable", false);
  try {
    // a search
    for (let id in store.getAllIDs()) {
      do_throw("Shouldn't get any ids!");
    }
    // an update.
    applyEnsureNoFailures([{
      id: Utils.makeGUID(),
      name: "some",
      value: "entry"
    }]);
  } finally {
    Services.prefs.clearUserPref("browser.formfill.enable");
    store.wipe();
  }
}