summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_prefs_tracker.js
blob: 17ccaa43e30f5718d1bbc83eccf2a572b258be4a (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Cu.import("resource://gre/modules/Preferences.jsm");
Cu.import("resource://services-common/utils.js");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/engines/prefs.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");

function run_test() {
  let engine = Service.engineManager.get("prefs");
  let tracker = engine._tracker;

  // Don't write out by default.
  tracker.persistChangedIDs = false;

  let prefs = new Preferences();

  try {

    _("tracker.modified corresponds to preference.");
    do_check_eq(Svc.Prefs.get("engine.prefs.modified"), undefined);
    do_check_false(tracker.modified);

    tracker.modified = true;
    do_check_eq(Svc.Prefs.get("engine.prefs.modified"), true);
    do_check_true(tracker.modified);

    _("Engine's getChangedID() just returns the one GUID we have.");
    let changedIDs = engine.getChangedIDs();
    let ids = Object.keys(changedIDs);
    do_check_eq(ids.length, 1);
    do_check_eq(ids[0], CommonUtils.encodeBase64URL(Services.appinfo.ID));

    Svc.Prefs.set("engine.prefs.modified", false);
    do_check_false(tracker.modified);

    _("No modified state, so no changed IDs.");
    do_check_empty(engine.getChangedIDs());

    _("Initial score is 0");
    do_check_eq(tracker.score, 0);

    _("Test fixtures.");
    Svc.Prefs.set("prefs.sync.testing.int", true);

    _("Test fixtures haven't upped the tracker score yet because it hasn't started tracking yet.");
    do_check_eq(tracker.score, 0);

    _("Tell the tracker to start tracking changes.");
    Svc.Obs.notify("weave:engine:start-tracking");
    prefs.set("testing.int", 23);
    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE);
    do_check_eq(tracker.modified, true);

    _("Clearing changed IDs reset modified status.");
    tracker.clearChangedIDs();
    do_check_eq(tracker.modified, false);

    _("Resetting a pref ups the score, too.");
    prefs.reset("testing.int");
    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 2);
    do_check_eq(tracker.modified, true);
    tracker.clearChangedIDs();

    _("So does changing a pref sync pref.");
    Svc.Prefs.set("prefs.sync.testing.int", false);
    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3);
    do_check_eq(tracker.modified, true);
    tracker.clearChangedIDs();

    _("Now that the pref sync pref has been flipped, changes to it won't be picked up.");
    prefs.set("testing.int", 42);
    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3);
    do_check_eq(tracker.modified, false);
    tracker.clearChangedIDs();

    _("Changing some other random pref won't do anything.");
    prefs.set("testing.other", "blergh");
    do_check_eq(tracker.score, SCORE_INCREMENT_XLARGE * 3);
    do_check_eq(tracker.modified, false);

  } finally {
    Svc.Obs.notify("weave:engine:stop-tracking");
    prefs.resetBranch("");
  }
}