PageRenderTime 40ms CodeModel.GetById 15ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_engine.js

http://github.com/zpao/v8monkey
JavaScript | 170 lines | 142 code | 25 blank | 3 comment | 0 complexity | 3db02783b3b002cf79391f0433830fb1 MD5 | raw file
  1Cu.import("resource://services-sync/engines.js");
  2Cu.import("resource://services-sync/ext/Observers.js");
  3Cu.import("resource://services-sync/util.js");
  4
  5
  6function SteamStore() {
  7  Store.call(this, "Steam");
  8  this.wasWiped = false;
  9}
 10SteamStore.prototype = {
 11  __proto__: Store.prototype,
 12
 13  wipe: function() {
 14    this.wasWiped = true;
 15  }
 16};
 17
 18function SteamTracker() {
 19  Tracker.call(this, "Steam");
 20}
 21SteamTracker.prototype = {
 22  __proto__: Tracker.prototype
 23};
 24
 25function SteamEngine() {
 26  Engine.call(this, "Steam");
 27  this.wasReset = false;
 28  this.wasSynced = false;
 29}
 30SteamEngine.prototype = {
 31  __proto__: Engine.prototype,
 32  _storeObj: SteamStore,
 33  _trackerObj: SteamTracker,
 34
 35  _resetClient: function () {
 36    this.wasReset = true;
 37  },
 38
 39  _sync: function () {
 40    this.wasSynced = true;
 41  }
 42};
 43
 44let engineObserver = {
 45  topics: [],
 46
 47  observe: function(subject, topic, data) {
 48    do_check_eq(data, "steam");
 49    this.topics.push(topic);
 50  },
 51
 52  reset: function() {
 53    this.topics = [];
 54  }
 55};
 56Observers.add("weave:engine:reset-client:start", engineObserver);
 57Observers.add("weave:engine:reset-client:finish", engineObserver);
 58Observers.add("weave:engine:wipe-client:start", engineObserver);
 59Observers.add("weave:engine:wipe-client:finish", engineObserver);
 60Observers.add("weave:engine:sync:start", engineObserver);
 61Observers.add("weave:engine:sync:finish", engineObserver);
 62
 63
 64function test_members() {
 65  _("Engine object members");
 66  let engine = new SteamEngine();
 67  do_check_eq(engine.Name, "Steam");
 68  do_check_eq(engine.prefName, "steam");
 69  do_check_true(engine._store instanceof SteamStore);
 70  do_check_true(engine._tracker instanceof SteamTracker);
 71}
 72
 73function test_score() {
 74  _("Engine.score corresponds to tracker.score and is readonly");
 75  let engine = new SteamEngine();
 76  do_check_eq(engine.score, 0);
 77  engine._tracker.score += 5;
 78  do_check_eq(engine.score, 5);
 79
 80  try {
 81    engine.score = 10;
 82  } catch(ex) {
 83    // Setting an attribute that has a getter produces an error in
 84    // Firefox <= 3.6 and is ignored in later versions.  Either way,
 85    // the attribute's value won't change.
 86  }
 87  do_check_eq(engine.score, 5);
 88}
 89
 90function test_resetClient() {
 91  _("Engine.resetClient calls _resetClient");
 92  let engine = new SteamEngine();
 93  do_check_false(engine.wasReset);
 94
 95  engine.resetClient();
 96  do_check_true(engine.wasReset);
 97  do_check_eq(engineObserver.topics[0], "weave:engine:reset-client:start");
 98  do_check_eq(engineObserver.topics[1], "weave:engine:reset-client:finish");
 99
100  engine.wasReset = false;
101  engineObserver.reset();
102}
103
104function test_wipeClient() {
105  _("Engine.wipeClient calls resetClient, wipes store, clears changed IDs");
106  let engine = new SteamEngine();
107  do_check_false(engine.wasReset);
108  do_check_false(engine._store.wasWiped);
109  do_check_true(engine._tracker.addChangedID("a-changed-id"));
110  do_check_true("a-changed-id" in engine._tracker.changedIDs);
111
112  engine.wipeClient();
113  do_check_true(engine.wasReset);
114  do_check_true(engine._store.wasWiped);
115  do_check_eq(JSON.stringify(engine._tracker.changedIDs), "{}");
116  do_check_eq(engineObserver.topics[0], "weave:engine:wipe-client:start");
117  do_check_eq(engineObserver.topics[1], "weave:engine:reset-client:start");
118  do_check_eq(engineObserver.topics[2], "weave:engine:reset-client:finish");
119  do_check_eq(engineObserver.topics[3], "weave:engine:wipe-client:finish");
120
121  engine.wasReset = false;
122  engine._store.wasWiped = false;
123  engineObserver.reset();
124}
125
126function test_enabled() {
127  _("Engine.enabled corresponds to preference");
128  let engine = new SteamEngine();
129  try {
130    do_check_false(engine.enabled);
131    Svc.Prefs.set("engine.steam", true);
132    do_check_true(engine.enabled);
133
134    engine.enabled = false;
135    do_check_false(Svc.Prefs.get("engine.steam"));
136  } finally {
137    Svc.Prefs.resetBranch("");
138  }
139}
140
141function test_sync() {
142  let engine = new SteamEngine();
143  try {
144    _("Engine.sync doesn't call _sync if it's not enabled");
145    do_check_false(engine.enabled);
146    do_check_false(engine.wasSynced);
147    engine.sync();
148    do_check_false(engine.wasSynced);
149
150    _("Engine.sync calls _sync if it's enabled");
151    engine.enabled = true;
152    engine.sync();
153    do_check_true(engine.wasSynced);
154    do_check_eq(engineObserver.topics[0], "weave:engine:sync:start");
155    do_check_eq(engineObserver.topics[1], "weave:engine:sync:finish");
156  } finally {
157    Svc.Prefs.resetBranch("");
158    engine.wasSynced = false;
159    engineObserver.reset();
160  }
161}
162
163function run_test() {
164  test_members();
165  test_score();
166  test_resetClient();
167  test_wipeClient();
168  test_enabled();
169  test_sync();
170}