toolkit/devtools/server/tests/unit/testactors.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/tests/unit/testactors.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +var gTestGlobals = [];
     1.8 +DebuggerServer.addTestGlobal = function(aGlobal) {
     1.9 +  gTestGlobals.push(aGlobal);
    1.10 +};
    1.11 +
    1.12 +// A mock tab list, for use by tests. This simply presents each global in
    1.13 +// gTestGlobals as a tab, and the list is fixed: it never calls its
    1.14 +// onListChanged handler.
    1.15 +//
    1.16 +// As implemented now, we consult gTestGlobals when we're constructed, not
    1.17 +// when we're iterated over, so tests have to add their globals before the
    1.18 +// root actor is created.
    1.19 +function TestTabList(aConnection) {
    1.20 +  this.conn = aConnection;
    1.21 +
    1.22 +  // An array of actors for each global added with
    1.23 +  // DebuggerServer.addTestGlobal.
    1.24 +  this._tabActors = [];
    1.25 +
    1.26 +  // A pool mapping those actors' names to the actors.
    1.27 +  this._tabActorPool = new ActorPool(aConnection);
    1.28 +
    1.29 +  for (let global of gTestGlobals) {
    1.30 +    let actor = new TestTabActor(aConnection, global);
    1.31 +    actor.selected = false;
    1.32 +    this._tabActors.push(actor);
    1.33 +    this._tabActorPool.addActor(actor);
    1.34 +  }
    1.35 +  if (this._tabActors.length > 0) {
    1.36 +    this._tabActors[0].selected = true;
    1.37 +  }
    1.38 +
    1.39 +  aConnection.addActorPool(this._tabActorPool);
    1.40 +}
    1.41 +
    1.42 +TestTabList.prototype = {
    1.43 +  constructor: TestTabList,
    1.44 +  getList: function () {
    1.45 +    return promise.resolve([tabActor for (tabActor of this._tabActors)]);
    1.46 +  }
    1.47 +};
    1.48 +
    1.49 +function createRootActor(aConnection)
    1.50 +{
    1.51 +  let root = new RootActor(aConnection,
    1.52 +                           { tabList: new TestTabList(aConnection) });
    1.53 +  root.applicationType = "xpcshell-tests";
    1.54 +  return root;
    1.55 +}
    1.56 +
    1.57 +function TestTabActor(aConnection, aGlobal)
    1.58 +{
    1.59 +  this.conn = aConnection;
    1.60 +  this._global = aGlobal;
    1.61 +  this._global.wrappedJSObject = aGlobal;
    1.62 +  this._threadActor = new ThreadActor(this, this._global);
    1.63 +  this.conn.addActor(this._threadActor);
    1.64 +  this._attached = false;
    1.65 +  this._extraActors = {};
    1.66 +}
    1.67 +
    1.68 +TestTabActor.prototype = {
    1.69 +  constructor: TestTabActor,
    1.70 +  actorPrefix: "TestTabActor",
    1.71 +
    1.72 +  get window() {
    1.73 +    return { wrappedJSObject: this._global };
    1.74 +  },
    1.75 +
    1.76 +  get url() {
    1.77 +    return this._global.__name;
    1.78 +  },
    1.79 +
    1.80 +  form: function() {
    1.81 +    let response = { actor: this.actorID, title: this._global.__name };
    1.82 +
    1.83 +    // Walk over tab actors added by extensions and add them to a new ActorPool.
    1.84 +    let actorPool = new ActorPool(this.conn);
    1.85 +    this._createExtraActors(DebuggerServer.tabActorFactories, actorPool);
    1.86 +    if (!actorPool.isEmpty()) {
    1.87 +      this._tabActorPool = actorPool;
    1.88 +      this.conn.addActorPool(this._tabActorPool);
    1.89 +    }
    1.90 +
    1.91 +    this._appendExtraActors(response);
    1.92 +
    1.93 +    return response;
    1.94 +  },
    1.95 +
    1.96 +  onAttach: function(aRequest) {
    1.97 +    this._attached = true;
    1.98 +
    1.99 +    let response = { type: "tabAttached", threadActor: this._threadActor.actorID };
   1.100 +    this._appendExtraActors(response);
   1.101 +
   1.102 +    return response;
   1.103 +  },
   1.104 +
   1.105 +  onDetach: function(aRequest) {
   1.106 +    if (!this._attached) {
   1.107 +      return { "error":"wrongState" };
   1.108 +    }
   1.109 +    return { type: "detached" };
   1.110 +  },
   1.111 +
   1.112 +  /* Support for DebuggerServer.addTabActor. */
   1.113 +  _createExtraActors: createExtraActors,
   1.114 +  _appendExtraActors: appendExtraActors
   1.115 +};
   1.116 +
   1.117 +TestTabActor.prototype.requestTypes = {
   1.118 +  "attach": TestTabActor.prototype.onAttach,
   1.119 +  "detach": TestTabActor.prototype.onDetach
   1.120 +};

mercurial