services/sync/modules-testing/rotaryengine.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/modules-testing/rotaryengine.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [
    1.11 +  "RotaryEngine",
    1.12 +  "RotaryRecord",
    1.13 +  "RotaryStore",
    1.14 +  "RotaryTracker",
    1.15 +];
    1.16 +
    1.17 +const {utils: Cu} = Components;
    1.18 +
    1.19 +Cu.import("resource://services-sync/engines.js");
    1.20 +Cu.import("resource://services-sync/record.js");
    1.21 +Cu.import("resource://services-sync/util.js");
    1.22 +
    1.23 +/*
    1.24 + * A fake engine implementation.
    1.25 + * This is used all over the place.
    1.26 + *
    1.27 + * Complete with record, store, and tracker implementations.
    1.28 + */
    1.29 +
    1.30 +this.RotaryRecord = function RotaryRecord(collection, id) {
    1.31 +  CryptoWrapper.call(this, collection, id);
    1.32 +}
    1.33 +RotaryRecord.prototype = {
    1.34 +  __proto__: CryptoWrapper.prototype
    1.35 +};
    1.36 +Utils.deferGetSet(RotaryRecord, "cleartext", ["denomination"]);
    1.37 +
    1.38 +this.RotaryStore = function RotaryStore(engine) {
    1.39 +  Store.call(this, "Rotary", engine);
    1.40 +  this.items = {};
    1.41 +}
    1.42 +RotaryStore.prototype = {
    1.43 +  __proto__: Store.prototype,
    1.44 +
    1.45 +  create: function create(record) {
    1.46 +    this.items[record.id] = record.denomination;
    1.47 +  },
    1.48 +
    1.49 +  remove: function remove(record) {
    1.50 +    delete this.items[record.id];
    1.51 +  },
    1.52 +
    1.53 +  update: function update(record) {
    1.54 +    this.items[record.id] = record.denomination;
    1.55 +  },
    1.56 +
    1.57 +  itemExists: function itemExists(id) {
    1.58 +    return (id in this.items);
    1.59 +  },
    1.60 +
    1.61 +  createRecord: function createRecord(id, collection) {
    1.62 +    let record = new RotaryRecord(collection, id);
    1.63 +
    1.64 +    if (!(id in this.items)) {
    1.65 +      record.deleted = true;
    1.66 +      return record;
    1.67 +    }
    1.68 +
    1.69 +    record.denomination = this.items[id] || "Data for new record: " + id;
    1.70 +    return record;
    1.71 +  },
    1.72 +
    1.73 +  changeItemID: function changeItemID(oldID, newID) {
    1.74 +    if (oldID in this.items) {
    1.75 +      this.items[newID] = this.items[oldID];
    1.76 +    }
    1.77 +
    1.78 +    delete this.items[oldID];
    1.79 +  },
    1.80 +
    1.81 +  getAllIDs: function getAllIDs() {
    1.82 +    let ids = {};
    1.83 +    for (let id in this.items) {
    1.84 +      ids[id] = true;
    1.85 +    }
    1.86 +    return ids;
    1.87 +  },
    1.88 +
    1.89 +  wipe: function wipe() {
    1.90 +    this.items = {};
    1.91 +  }
    1.92 +};
    1.93 +
    1.94 +this.RotaryTracker = function RotaryTracker(engine) {
    1.95 +  Tracker.call(this, "Rotary", engine);
    1.96 +}
    1.97 +RotaryTracker.prototype = {
    1.98 +  __proto__: Tracker.prototype
    1.99 +};
   1.100 +
   1.101 +
   1.102 +this.RotaryEngine = function RotaryEngine(service) {
   1.103 +  SyncEngine.call(this, "Rotary", service);
   1.104 +  // Ensure that the engine starts with a clean slate.
   1.105 +  this.toFetch        = [];
   1.106 +  this.previousFailed = [];
   1.107 +}
   1.108 +RotaryEngine.prototype = {
   1.109 +  __proto__: SyncEngine.prototype,
   1.110 +  _storeObj: RotaryStore,
   1.111 +  _trackerObj: RotaryTracker,
   1.112 +  _recordObj: RotaryRecord,
   1.113 +
   1.114 +  _findDupe: function _findDupe(item) {
   1.115 +    // This is a semaphore used for testing proper reconciling on dupe
   1.116 +    // detection.
   1.117 +    if (item.id == "DUPE_INCOMING") {
   1.118 +      return "DUPE_LOCAL";
   1.119 +    }
   1.120 +
   1.121 +    for (let [id, value] in Iterator(this._store.items)) {
   1.122 +      if (item.denomination == value) {
   1.123 +        return id;
   1.124 +      }
   1.125 +    }
   1.126 +  }
   1.127 +};

mercurial