tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_enginemanager.js (7033B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { Service } = ChromeUtils.importESModule(
      5  "resource://services-sync/service.sys.mjs"
      6 );
      7 
      8 function PetrolEngine() {}
      9 PetrolEngine.prototype.name = "petrol";
     10 PetrolEngine.prototype.finalize = async function () {};
     11 
     12 function DieselEngine() {}
     13 DieselEngine.prototype.name = "diesel";
     14 DieselEngine.prototype.finalize = async function () {};
     15 
     16 function DummyEngine() {}
     17 DummyEngine.prototype.name = "dummy";
     18 DummyEngine.prototype.finalize = async function () {};
     19 
     20 class ActualEngine extends SyncEngine {
     21  constructor(service) {
     22    super("Actual", service);
     23  }
     24 }
     25 
     26 add_task(async function test_basics() {
     27  _("We start out with a clean slate");
     28 
     29  let manager = new EngineManager(Service);
     30 
     31  let engines = await manager.getAll();
     32  Assert.equal(engines.length, 0);
     33  Assert.equal(await manager.get("dummy"), undefined);
     34 
     35  _("Register an engine");
     36  await manager.register(DummyEngine);
     37  let dummy = await manager.get("dummy");
     38  Assert.ok(dummy instanceof DummyEngine);
     39 
     40  engines = await manager.getAll();
     41  Assert.equal(engines.length, 1);
     42  Assert.equal(engines[0], dummy);
     43 
     44  _("Register an already registered engine is ignored");
     45  await manager.register(DummyEngine);
     46  Assert.equal(await manager.get("dummy"), dummy);
     47 
     48  _("Register multiple engines in one go");
     49  await manager.register([PetrolEngine, DieselEngine]);
     50  let petrol = await manager.get("petrol");
     51  let diesel = await manager.get("diesel");
     52  Assert.ok(petrol instanceof PetrolEngine);
     53  Assert.ok(diesel instanceof DieselEngine);
     54 
     55  engines = await manager.getAll();
     56  Assert.equal(engines.length, 3);
     57  Assert.notEqual(engines.indexOf(petrol), -1);
     58  Assert.notEqual(engines.indexOf(diesel), -1);
     59 
     60  _("Retrieve multiple engines in one go");
     61  engines = await manager.get(["dummy", "diesel"]);
     62  Assert.equal(engines.length, 2);
     63  Assert.notEqual(engines.indexOf(dummy), -1);
     64  Assert.notEqual(engines.indexOf(diesel), -1);
     65 
     66  _("getEnabled() only returns enabled engines");
     67  engines = await manager.getEnabled();
     68  Assert.equal(engines.length, 0);
     69 
     70  petrol.enabled = true;
     71  engines = await manager.getEnabled();
     72  Assert.equal(engines.length, 1);
     73  Assert.equal(engines[0], petrol);
     74 
     75  dummy.enabled = true;
     76  diesel.enabled = true;
     77  engines = await manager.getEnabled();
     78  Assert.equal(engines.length, 3);
     79 
     80  _("getEnabled() returns enabled engines in sorted order");
     81  petrol.syncPriority = 1;
     82  dummy.syncPriority = 2;
     83  diesel.syncPriority = 3;
     84 
     85  engines = await manager.getEnabled();
     86 
     87  Assert.deepEqual(engines, [petrol, dummy, diesel]);
     88 
     89  _("Changing the priorities should change the order in getEnabled()");
     90 
     91  dummy.syncPriority = 4;
     92 
     93  engines = await manager.getEnabled();
     94 
     95  Assert.deepEqual(engines, [petrol, diesel, dummy]);
     96 
     97  _("Unregister an engine by name");
     98  await manager.unregister("dummy");
     99  Assert.equal(await manager.get("dummy"), undefined);
    100  engines = await manager.getAll();
    101  Assert.equal(engines.length, 2);
    102  Assert.equal(engines.indexOf(dummy), -1);
    103 
    104  _("Unregister an engine by value");
    105  // manager.unregister() checks for instanceof Engine, so let's make one:
    106  await manager.register(ActualEngine);
    107  let actual = await manager.get("actual");
    108  Assert.ok(actual instanceof ActualEngine);
    109  Assert.ok(actual instanceof SyncEngine);
    110 
    111  await manager.unregister(actual);
    112  Assert.equal(await manager.get("actual"), undefined);
    113 });
    114 
    115 class AutoEngine {
    116  constructor(type) {
    117    this.name = "automobile";
    118    this.type = type;
    119    this.initializeCalled = false;
    120    this.finalizeCalled = false;
    121    this.isActive = false;
    122  }
    123 
    124  async initialize() {
    125    Assert.ok(!this.initializeCalled);
    126    Assert.equal(AutoEngine.current, undefined);
    127    this.initializeCalled = true;
    128    this.isActive = true;
    129    AutoEngine.current = this;
    130  }
    131 
    132  async finalize() {
    133    Assert.equal(AutoEngine.current, this);
    134    Assert.ok(!this.finalizeCalled);
    135    Assert.ok(this.isActive);
    136    this.finalizeCalled = true;
    137    this.isActive = false;
    138    AutoEngine.current = undefined;
    139  }
    140 }
    141 
    142 class GasolineEngine extends AutoEngine {
    143  constructor() {
    144    super("gasoline");
    145  }
    146 }
    147 
    148 class ElectricEngine extends AutoEngine {
    149  constructor() {
    150    super("electric");
    151  }
    152 }
    153 
    154 add_task(async function test_alternates() {
    155  let manager = new EngineManager(Service);
    156  let engines = await manager.getAll();
    157  Assert.equal(engines.length, 0);
    158 
    159  const prefName = "services.sync.engines.automobile.electric";
    160  Services.prefs.clearUserPref(prefName);
    161 
    162  await manager.registerAlternatives(
    163    "automobile",
    164    prefName,
    165    ElectricEngine,
    166    GasolineEngine
    167  );
    168 
    169  let gasEngine = manager.get("automobile");
    170  Assert.equal(gasEngine.type, "gasoline");
    171 
    172  Assert.ok(gasEngine.isActive);
    173  Assert.ok(gasEngine.initializeCalled);
    174  Assert.ok(!gasEngine.finalizeCalled);
    175  Assert.equal(AutoEngine.current, gasEngine);
    176 
    177  _("Check that setting the controlling pref to false makes no difference");
    178  Services.prefs.setBoolPref(prefName, false);
    179  Assert.equal(manager.get("automobile"), gasEngine);
    180  Assert.ok(gasEngine.isActive);
    181  Assert.ok(gasEngine.initializeCalled);
    182  Assert.ok(!gasEngine.finalizeCalled);
    183 
    184  _("Even after the call to switchAlternatives");
    185  await manager.switchAlternatives();
    186  Assert.equal(manager.get("automobile"), gasEngine);
    187  Assert.ok(gasEngine.isActive);
    188  Assert.ok(gasEngine.initializeCalled);
    189  Assert.ok(!gasEngine.finalizeCalled);
    190 
    191  _("Set the pref to true, we still shouldn't switch yet");
    192  Services.prefs.setBoolPref(prefName, true);
    193  Assert.equal(manager.get("automobile"), gasEngine);
    194  Assert.ok(gasEngine.isActive);
    195  Assert.ok(gasEngine.initializeCalled);
    196  Assert.ok(!gasEngine.finalizeCalled);
    197 
    198  _("Now we expect to switch from gas to electric");
    199  await manager.switchAlternatives();
    200  let elecEngine = manager.get("automobile");
    201  Assert.equal(elecEngine.type, "electric");
    202  Assert.ok(elecEngine.isActive);
    203  Assert.ok(elecEngine.initializeCalled);
    204  Assert.ok(!elecEngine.finalizeCalled);
    205  Assert.equal(AutoEngine.current, elecEngine);
    206 
    207  Assert.ok(!gasEngine.isActive);
    208  Assert.ok(gasEngine.finalizeCalled);
    209 
    210  _("Switch back, and ensure we get a new instance that got initialized again");
    211  Services.prefs.setBoolPref(prefName, false);
    212  await manager.switchAlternatives();
    213 
    214  // First make sure we deactivated the electric engine as we should
    215  Assert.ok(!elecEngine.isActive);
    216  Assert.ok(elecEngine.initializeCalled);
    217  Assert.ok(elecEngine.finalizeCalled);
    218 
    219  let newGasEngine = manager.get("automobile");
    220  Assert.notEqual(newGasEngine, gasEngine);
    221  Assert.equal(newGasEngine.type, "gasoline");
    222 
    223  Assert.ok(newGasEngine.isActive);
    224  Assert.ok(newGasEngine.initializeCalled);
    225  Assert.ok(!newGasEngine.finalizeCalled);
    226 
    227  _("Make sure unregister removes the alt info too");
    228  await manager.unregister("automobile");
    229  Assert.equal(manager.get("automobile"), null);
    230  Assert.ok(newGasEngine.finalizeCalled);
    231  Assert.deepEqual(Object.keys(manager._altEngineInfo), []);
    232 });