summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_addons_tracker.js
blob: 01bf37ab9eee7bd111bf2410040723ce6ee216a9 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://services-sync/engines/addons.js");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");

loadAddonTestFunctions();
startupManager();
Svc.Prefs.set("engine.addons", true);

Service.engineManager.register(AddonsEngine);
var engine     = Service.engineManager.get("addons");
var reconciler = engine._reconciler;
var store      = engine._store;
var tracker    = engine._tracker;

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

const addon1ID = "addon1@tests.mozilla.org";

function cleanup_and_advance() {
  Svc.Obs.notify("weave:engine:stop-tracking");
  tracker.stopTracking();

  tracker.resetScore();
  tracker.clearChangedIDs();

  reconciler._addons = {};
  reconciler._changes = [];
  let cb = Async.makeSpinningCallback();
  reconciler.saveState(null, cb);
  cb.wait();

  run_next_test();
}

function run_test() {
  initTestLogging("Trace");
  Log.repository.getLogger("Sync.Engine.Addons").level = Log.Level.Trace;
  Log.repository.getLogger("Sync.AddonsReconciler").level =
    Log.Level.Trace;

  cleanup_and_advance();
}

add_test(function test_empty() {
  _("Verify the tracker is empty to start with.");

  do_check_eq(0, Object.keys(tracker.changedIDs).length);
  do_check_eq(0, tracker.score);

  cleanup_and_advance();
});

add_test(function test_not_tracking() {
  _("Ensures the tracker doesn't do anything when it isn't tracking.");

  let addon = installAddon("test_bootstrap1_1");
  uninstallAddon(addon);

  do_check_eq(0, Object.keys(tracker.changedIDs).length);
  do_check_eq(0, tracker.score);

  cleanup_and_advance();
});

add_test(function test_track_install() {
  _("Ensure that installing an add-on notifies tracker.");

  reconciler.startListening();

  Svc.Obs.notify("weave:engine:start-tracking");

  do_check_eq(0, tracker.score);
  let addon = installAddon("test_bootstrap1_1");
  let changed = tracker.changedIDs;

  do_check_eq(1, Object.keys(changed).length);
  do_check_true(addon.syncGUID in changed);
  do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);

  uninstallAddon(addon);
  cleanup_and_advance();
});

add_test(function test_track_uninstall() {
  _("Ensure that uninstalling an add-on notifies tracker.");

  reconciler.startListening();

  let addon = installAddon("test_bootstrap1_1");
  let guid = addon.syncGUID;
  do_check_eq(0, tracker.score);

  Svc.Obs.notify("weave:engine:start-tracking");

  uninstallAddon(addon);
  let changed = tracker.changedIDs;
  do_check_eq(1, Object.keys(changed).length);
  do_check_true(guid in changed);
  do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);

  cleanup_and_advance();
});

add_test(function test_track_user_disable() {
  _("Ensure that tracker sees disabling of add-on");

  reconciler.startListening();

  let addon = installAddon("test_bootstrap1_1");
  do_check_false(addon.userDisabled);
  do_check_false(addon.appDisabled);
  do_check_true(addon.isActive);

  Svc.Obs.notify("weave:engine:start-tracking");
  do_check_eq(0, tracker.score);

  let cb = Async.makeSyncCallback();

  let listener = {
    onDisabled: function(disabled) {
      _("onDisabled");
      if (disabled.id == addon.id) {
        AddonManager.removeAddonListener(listener);
        cb();
      }
    },
    onDisabling: function(disabling) {
      _("onDisabling add-on");
    }
  };
  AddonManager.addAddonListener(listener);

  _("Disabling add-on");
  addon.userDisabled = true;
  _("Disabling started...");
  Async.waitForSyncCallback(cb);

  let changed = tracker.changedIDs;
  do_check_eq(1, Object.keys(changed).length);
  do_check_true(addon.syncGUID in changed);
  do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);

  uninstallAddon(addon);
  cleanup_and_advance();
});

add_test(function test_track_enable() {
  _("Ensure that enabling a disabled add-on notifies tracker.");

  reconciler.startListening();

  let addon = installAddon("test_bootstrap1_1");
  addon.userDisabled = true;
  store._sleep(0);

  do_check_eq(0, tracker.score);

  Svc.Obs.notify("weave:engine:start-tracking");
  addon.userDisabled = false;
  store._sleep(0);

  let changed = tracker.changedIDs;
  do_check_eq(1, Object.keys(changed).length);
  do_check_true(addon.syncGUID in changed);
  do_check_eq(SCORE_INCREMENT_XLARGE, tracker.score);

  uninstallAddon(addon);
  cleanup_and_advance();
});