summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/browser_connectToFrame.js
blob: 1047393470c8a5a02fd947b3cee95c445d10ed92 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test `connectToFrame` method
 */

"use strict";

const {
  connectToFrame,
} = require("resource://devtools/server/connectors/frame-connector.js");

add_task(async function () {
  // Create a minimal browser with a message manager
  const browser = document.createXULElement("browser");
  browser.setAttribute("type", "content");
  document.body.appendChild(browser);

  await TestUtils.waitForCondition(
    () => browser.browsingContext.currentWindowGlobal,
    "browser has no window global"
  );

  // Register a test actor in the child process so that we can know if and when
  // this fake actor is destroyed.
  await SpecialPowers.spawn(browser, [], () => {
    const { require } = ChromeUtils.importESModule(
      "resource://devtools/shared/loader/Loader.sys.mjs"
    );
    const {
      DevToolsServer,
    } = require("resource://devtools/server/devtools-server.js");
    const {
      ActorRegistry,
    } = require("resource://devtools/server/actors/utils/actor-registry.js");

    DevToolsServer.init();

    const { Actor } = require("resource://devtools/shared/protocol/Actor.js");
    class ConnectToFrameTestActor extends Actor {
      constructor(conn) {
        super(conn, { typeName: "connectToFrameTest", methods: [] });
        dump("instantiate test actor\n");
        this.requestTypes = {
          hello: this.hello,
        };
      }
      hello() {
        return { msg: "world" };
      }

      destroy() {
        SpecialPowers.notifyObserversInParentProcess(
          null,
          "devtools-test-actor-destroyed",
          ""
        );
      }
    }

    ActorRegistry.addTargetScopedActor(
      {
        constructorName: "ConnectToFrameTestActor",
        constructorFun: ConnectToFrameTestActor,
      },
      "connectToFrameTestActor"
    );
  });

  // Instantiate a minimal server
  DevToolsServer.init();
  if (!DevToolsServer.createRootActor) {
    DevToolsServer.registerAllActors();
  }

  async function initAndCloseFirstClient() {
    // Fake a first connection to a browser
    const transport = DevToolsServer.connectPipe();
    const conn = transport._serverConnection;
    const client = new DevToolsClient(transport);
    const actor = await connectToFrame(conn, browser);
    ok(actor.connectToFrameTestActor, "Got the test actor");

    // Ensure sending at least one request to our actor,
    // otherwise it won't be instantiated, nor be destroyed...
    await client.request({
      to: actor.connectToFrameTestActor,
      type: "hello",
    });

    // Connect a second client in parallel to assert that it received a distinct set of
    // target actors
    await initAndCloseSecondClient(actor.connectToFrameTestActor);

    ok(
      DevToolsServer.initialized,
      "DevToolsServer isn't destroyed until all clients are disconnected"
    );

    // Ensure that our test actor got cleaned up;
    // its destroy method should be called
    const onActorDestroyed = TestUtils.topicObserved(
      "devtools-test-actor-destroyed"
    );

    // Then close the client. That should end up cleaning our test actor
    await client.close();

    await onActorDestroyed;

    // This test loads a frame in the parent process, so that we end up sharing the same
    // DevToolsServer instance
    ok(
      !DevToolsServer.initialized,
      "DevToolsServer is destroyed when all clients are disconnected"
    );
  }

  async function initAndCloseSecondClient(firstActor) {
    // Then fake a second one, that should spawn a new set of target-scoped actors
    const transport = DevToolsServer.connectPipe();
    const conn = transport._serverConnection;
    const client = new DevToolsClient(transport);
    const actor = await connectToFrame(conn, browser);
    ok(
      actor.connectToFrameTestActor,
      "Got a test actor for the second connection"
    );
    isnot(
      actor.connectToFrameTestActor,
      firstActor,
      "We get different actor instances between two connections"
    );
    return client.close();
  }

  await initAndCloseFirstClient();

  DevToolsServer.destroy();
  browser.remove();
});