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

"use strict";

// Test that the ResourceCommand clears its pending events for resources emitted from
// target destroyed when devtools.browsertoolbox.scope is updated.

const TEST_URL =
  "data:text/html;charset=utf-8," + encodeURIComponent(`<div id="test"></div>`);

add_task(async function () {
  // Do not run this test when both fission and EFT is disabled as it changes
  // the number of targets
  if (!isFissionEnabled() && !isEveryFrameTargetEnabled()) {
    ok(true, "Don't go further is both Fission and EFT are disabled");
    return;
  }

  // Disable the preloaded process as it gets created lazily and may interfere
  // with process count assertions
  await pushPref("dom.ipc.processPrelaunch.enabled", false);
  // This preference helps destroying the content process when we close the tab
  await pushPref("dom.ipc.keepProcessesAlive.web", 1);

  // Start with multiprocess debugging enabled
  await pushPref("devtools.browsertoolbox.scope", "everything");

  const commands = await CommandsFactory.forMainProcess();
  const targetCommand = commands.targetCommand;
  await targetCommand.startListening();
  const { TYPES } = targetCommand;

  const targets = new Set();
  const onAvailable = async ({ targetFront }) => {
    targets.add(targetFront);
  };
  const onDestroyed = () => {};
  await targetCommand.watchTargets({
    types: [TYPES.PROCESS, TYPES.FRAME],
    onAvailable,
    onDestroyed,
  });

  info("Open a tab in a new content process");
  const firstTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_URL,
    forceNewProcess: true,
  });

  const newTabInnerWindowId =
    firstTab.linkedBrowser.browsingContext.currentWindowGlobal.innerWindowId;

  info("Wait for the tab window global target");
  const windowGlobalTarget = await waitFor(() =>
    [...targets].find(
      target =>
        target.targetType == TYPES.FRAME &&
        target.innerWindowId == newTabInnerWindowId
    )
  );

  let gotTabResource = false;
  const onResourceAvailable = resources => {
    for (const resource of resources) {
      if (resource.targetFront == windowGlobalTarget) {
        gotTabResource = true;

        if (resource.targetFront.isDestroyed()) {
          ok(
            false,
            "we shouldn't get resources for the target that was destroyed when switching mode"
          );
        }
      }
    }
  };

  info("Start listening for resources");
  await commands.resourceCommand.watchResources(
    [commands.resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: onResourceAvailable,
      ignoreExistingResources: true,
    }
  );

  // Emit logs every ms to fill up the resourceCommand resource queue (pendingEvents)
  const intervalId = await SpecialPowers.spawn(
    firstTab.linkedBrowser,
    [],
    () => {
      let counter = 0;
      return content.wrappedJSObject.setInterval(() => {
        counter++;
        content.wrappedJSObject.console.log("STREAM_" + counter);
      }, 1);
    }
  );

  info("Wait until we get the first resource");
  await waitFor(() => gotTabResource);

  info("Disable multiprocess debugging");
  await pushPref("devtools.browsertoolbox.scope", "parent-process");

  info("Wait for the tab target to be destroyed");
  await waitFor(() => windowGlobalTarget.isDestroyed());

  info("Wait for a bit so any throttled action would have the time to occur");
  await wait(1000);

  // Stop listening for resources
  await commands.resourceCommand.unwatchResources(
    [commands.resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: onResourceAvailable,
    }
  );
  // And stop the interval
  await SpecialPowers.spawn(firstTab.linkedBrowser, [intervalId], id => {
    content.wrappedJSObject.clearInterval(id);
  });

  targetCommand.destroy();
  await commands.destroy();
});