summaryrefslogtreecommitdiffstats
path: root/devtools/shared/resources/tests/browser_resources_target_switching.js
blob: 1388970dcd5d49e0d25d10e44bc48a2e24da692b (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the behavior of ResourceWatcher when the top level target changes

const {
  ResourceWatcher,
} = require("devtools/shared/resources/resource-watcher");
const { CONSOLE_MESSAGE, SOURCE } = ResourceWatcher.TYPES;

const TEST_URI =
  "data:text/html;charset=utf-8,<script>console.log('foo');</script>";

add_task(async function() {
  const tab = await addTab(TEST_URI);

  const { client, resourceWatcher, targetList } = await initResourceWatcher(
    tab
  );

  info("Check the resources gotten from getAllResources at initial");
  is(
    resourceWatcher.getAllResources(CONSOLE_MESSAGE).length,
    0,
    "There is no resources before calling watchResources"
  );

  info(
    "Start to watch the available resources in order to compare with resources gotten from getAllResources"
  );
  const availableResources = [];
  const onAvailable = resources => {
    // Ignore message coming from shared worker started by previous tests and
    // logging late a console message
    resources
      .filter(r => {
        return !r.message.arguments[0].startsWith("[WORKER] started");
      })
      .map(r => availableResources.push(r));
  };
  await resourceWatcher.watchResources([CONSOLE_MESSAGE], { onAvailable });

  is(availableResources.length, 1, "Got the page message");
  is(
    availableResources[0].message.arguments[0],
    "foo",
    "Got the expected page message"
  );

  // Register another listener before unregistering the console listener
  // otherwise the resource watcher stop watching for targets
  const onSourceAvailable = () => {};
  await resourceWatcher.watchResources([SOURCE], {
    onAvailable: onSourceAvailable,
  });

  info(
    "Unregister the console listener and check that we no longer listen for console messages"
  );
  resourceWatcher.unwatchResources([CONSOLE_MESSAGE], {
    onAvailable,
  });

  let onSwitched = targetList.once("switched-target");
  info("Navigate to another process");
  BrowserTestUtils.loadURI(gBrowser.selectedBrowser, "about:robots");
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  await onSwitched;

  is(
    availableResources.length,
    1,
    "about:robots doesn't fire any new message, so we should have a new one"
  );

  info("Navigate back to data: URI");
  onSwitched = targetList.once("switched-target");
  BrowserTestUtils.loadURI(gBrowser.selectedBrowser, TEST_URI);
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  await onSwitched;

  is(
    availableResources.length,
    1,
    "the data:URI fired a message, but we are no longer listening to it, so no new one should be notified"
  );
  is(
    resourceWatcher.getAllResources(CONSOLE_MESSAGE).length,
    0,
    "As we are no longer listening to CONSOLE message, we should not collect any"
  );

  resourceWatcher.unwatchResources([SOURCE], {
    onAvailable: onSourceAvailable,
  });

  await targetList.destroy();
  await client.close();
});