tor-browser

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

test_register_actor.js (2898B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 function run_test() {
      7  // Allow incoming connections.
      8  DevToolsServer.keepAlive = true;
      9  DevToolsServer.init();
     10  DevToolsServer.registerAllActors();
     11 
     12  add_test(test_lazy_api);
     13  add_test(manual_remove);
     14  add_test(cleanup);
     15  run_next_test();
     16 }
     17 
     18 // Bug 988237: Test the new lazy actor actor-register
     19 function test_lazy_api() {
     20  let isActorLoaded = false;
     21  let isActorInstantiated = false;
     22  function onActorEvent(subject, topic, data) {
     23    if (data == "loaded") {
     24      isActorLoaded = true;
     25    } else if (data == "instantiated") {
     26      isActorInstantiated = true;
     27    }
     28  }
     29  Services.obs.addObserver(onActorEvent, "actor");
     30  ActorRegistry.registerModule("xpcshell-test/registertestactors-lazy", {
     31    prefix: "lazy",
     32    constructor: "LazyActor",
     33    type: { global: true, target: true },
     34  });
     35  // The actor is immediatly registered, but not loaded
     36  Assert.ok(
     37    ActorRegistry.targetScopedActorFactories.hasOwnProperty("lazyActor")
     38  );
     39  Assert.ok(ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
     40  Assert.ok(!isActorLoaded);
     41  Assert.ok(!isActorInstantiated);
     42 
     43  const client = new DevToolsClient(DevToolsServer.connectPipe());
     44  client.connect().then(function onConnect() {
     45    client.mainRoot.rootForm.then(onRootForm);
     46  });
     47  function onRootForm(response) {
     48    // On rootForm, the actor is still not loaded,
     49    // but we can see its name in the list of available actors
     50    Assert.ok(!isActorLoaded);
     51    Assert.ok(!isActorInstantiated);
     52    Assert.ok("lazyActor" in response);
     53 
     54    const { LazyFront } = require("xpcshell-test/registertestactors-lazy");
     55    const front = new LazyFront(client);
     56    // As this Front isn't instantiated by protocol.js, we have to manually
     57    // set its actor ID and manage it:
     58    front.actorID = response.lazyActor;
     59    client.addActorPool(front);
     60    front.manage(front);
     61 
     62    front.hello().then(onRequest);
     63  }
     64  function onRequest(response) {
     65    Assert.equal(response, "world");
     66 
     67    // Finally, the actor is loaded on the first request being made to it
     68    Assert.ok(isActorLoaded);
     69    Assert.ok(isActorInstantiated);
     70 
     71    Services.obs.removeObserver(onActorEvent, "actor");
     72    client.close().then(() => run_next_test());
     73  }
     74 }
     75 
     76 function manual_remove() {
     77  Assert.ok(ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
     78  ActorRegistry.removeGlobalActor("lazyActor");
     79  Assert.ok(!ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
     80 
     81  run_next_test();
     82 }
     83 
     84 function cleanup() {
     85  DevToolsServer.destroy();
     86 
     87  // Check that all actors are unregistered on server destruction
     88  Assert.ok(
     89    !ActorRegistry.targetScopedActorFactories.hasOwnProperty("lazyActor")
     90  );
     91  Assert.ok(!ActorRegistry.globalActorFactories.hasOwnProperty("lazyActor"));
     92 
     93  run_next_test();
     94 }