summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_target_destroy.js
blob: 287572c5c5f9859868cb24f2938b4d03efa3a128 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that the server ResourceCommand are destroyed when the associated target actors
// are destroyed.

add_task(async function() {
  const tab = await addTab("data:text/html,Test");
  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab
  );

  // Start watching for console messages. We don't care about messages here, only the
  // registration/destroy mechanism, so we make onAvailable a no-op function.
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: () => {},
    }
  );

  info(
    "Spawn a content task in order to be able to manipulate actors and resource watchers directly"
  );
  const connectionPrefix = targetCommand.watcherFront.actorID.replace(
    /watcher\d+$/,
    ""
  );
  await ContentTask.spawn(tab.linkedBrowser, [connectionPrefix], function(
    _connectionPrefix
  ) {
    const { require } = ChromeUtils.importESModule(
      "resource://devtools/shared/loader/Loader.sys.mjs"
    );
    const { TargetActorRegistry } = ChromeUtils.import(
      "resource://devtools/server/actors/targets/target-actor-registry.jsm"
    );
    const {
      getResourceWatcher,
      TYPES,
    } = require("resource://devtools/server/actors/resources/index.js");

    // Retrieve the target actor instance and its watcher for console messages
    const targetActor = TargetActorRegistry.getTopLevelTargetActorForContext(
      {
        type: "browser-element",
        browserId: content.browsingContext.browserId,
      },
      _connectionPrefix
    );
    ok(targetActor, "Got the top level target actor from the content process");
    const watcher = getResourceWatcher(targetActor, TYPES.CONSOLE_MESSAGE);

    // Storing the target actor in the global so we can retrieve it later, even if it
    // was destroyed
    content._testTargetActor = targetActor;

    is(!!watcher, true, "The console message resource watcher was created");
  });

  info("Close the client, which will destroy the target");
  targetCommand.destroy();
  await client.close();

  info(
    "Spawn a content task in order to run some assertions on actors and resource watchers directly"
  );
  await ContentTask.spawn(tab.linkedBrowser, [], function() {
    const { require } = ChromeUtils.importESModule(
      "resource://devtools/shared/loader/Loader.sys.mjs"
    );
    const {
      getResourceWatcher,
      TYPES,
    } = require("resource://devtools/server/actors/resources/index.js");

    ok(
      content._testTargetActor && !content._testTargetActor.actorID,
      "The target was destroyed when the client was closed"
    );

    // Retrieve the console message resource watcher
    const watcher = getResourceWatcher(
      content._testTargetActor,
      TYPES.CONSOLE_MESSAGE
    );

    is(
      !!watcher,
      false,
      "The console message resource watcher isn't registered anymore after the target was destroyed"
    );

    // Cleanup work variable
    delete content._testTargetActor;
  });
});