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

Cu.import("resource://services-sync/engines.js");
Cu.import("resource://services-sync/service.js");

function run_test() {
  run_next_test();
}

function PetrolEngine() {}
PetrolEngine.prototype.name = "petrol";

function DieselEngine() {}
DieselEngine.prototype.name = "diesel";

function DummyEngine() {}
DummyEngine.prototype.name = "dummy";

function ActualEngine() {}
ActualEngine.prototype = {__proto__: Engine.prototype,
                          name: 'actual'};

add_test(function test_basics() {
  _("We start out with a clean slate");

  let manager = new EngineManager(Service);

  let engines = manager.getAll();
  do_check_eq(engines.length, 0);
  do_check_eq(manager.get('dummy'), undefined);

  _("Register an engine");
  manager.register(DummyEngine);
  let dummy = manager.get('dummy');
  do_check_true(dummy instanceof DummyEngine);

  engines = manager.getAll();
  do_check_eq(engines.length, 1);
  do_check_eq(engines[0], dummy);

  _("Register an already registered engine is ignored");
  manager.register(DummyEngine);
  do_check_eq(manager.get('dummy'), dummy);

  _("Register multiple engines in one go");
  manager.register([PetrolEngine, DieselEngine]);
  let petrol = manager.get('petrol');
  let diesel = manager.get('diesel');
  do_check_true(petrol instanceof PetrolEngine);
  do_check_true(diesel instanceof DieselEngine);

  engines = manager.getAll();
  do_check_eq(engines.length, 3);
  do_check_neq(engines.indexOf(petrol), -1);
  do_check_neq(engines.indexOf(diesel), -1);

  _("Retrieve multiple engines in one go");
  engines = manager.get(["dummy", "diesel"]);
  do_check_eq(engines.length, 2);
  do_check_neq(engines.indexOf(dummy), -1);
  do_check_neq(engines.indexOf(diesel), -1);

  _("getEnabled() only returns enabled engines");
  engines = manager.getEnabled();
  do_check_eq(engines.length, 0);

  petrol.enabled = true;
  engines = manager.getEnabled();
  do_check_eq(engines.length, 1);
  do_check_eq(engines[0], petrol);

  dummy.enabled = true;
  diesel.enabled = true;
  engines = manager.getEnabled();
  do_check_eq(engines.length, 3);

  _("getEnabled() returns enabled engines in sorted order");
  petrol.syncPriority = 1;
  dummy.syncPriority = 2;
  diesel.syncPriority = 3;

  engines = manager.getEnabled();

  do_check_array_eq(engines, [petrol, dummy, diesel]);

  _("Changing the priorities should change the order in getEnabled()");

  dummy.syncPriority = 4;

  engines = manager.getEnabled();

  do_check_array_eq(engines, [petrol, diesel, dummy]);

  _("Unregister an engine by name");
  manager.unregister('dummy');
  do_check_eq(manager.get('dummy'), undefined);
  engines = manager.getAll();
  do_check_eq(engines.length, 2);
  do_check_eq(engines.indexOf(dummy), -1);

  _("Unregister an engine by value");
  // manager.unregister() checks for instanceof Engine, so let's make one:
  manager.register(ActualEngine);
  let actual = manager.get('actual');
  do_check_true(actual instanceof ActualEngine);
  do_check_true(actual instanceof Engine);

  manager.unregister(actual);
  do_check_eq(manager.get('actual'), undefined);

  run_next_test();
});