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

"use strict";

// Test the clearResources function of the ResourceCommand

add_task(async () => {
  const tab = await addTab(`${URL_ROOT_SSL}empty.html`);
  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab
  );

  info("Assert the initial no of resources");
  assertNoOfResources(resourceCommand, 0, 0);

  const onAvailable = () => {};
  const onUpdated = () => {};

  await resourceCommand.watchResources(
    [
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      resourceCommand.TYPES.NETWORK_EVENT,
    ],
    { onAvailable, onUpdated }
  );

  info("Log some messages");
  await logConsoleMessages(tab.linkedBrowser, ["log1", "log2", "log3"]);

  info("Trigger some network requests");
  const EXAMPLE_DOMAIN = "https://example.com/";
  await triggerNetworkRequests(tab.linkedBrowser, [
    `await fetch("${EXAMPLE_DOMAIN}/request1.html", { method: "GET" });`,
    `await fetch("${EXAMPLE_DOMAIN}/request2.html", { method: "GET" });`,
  ]);

  assertNoOfResources(resourceCommand, 3, 2);

  info("Clear the network event resources");
  await resourceCommand.clearResources([resourceCommand.TYPES.NETWORK_EVENT]);
  assertNoOfResources(resourceCommand, 3, 0);

  info("Clear the console message resources");
  await resourceCommand.clearResources([resourceCommand.TYPES.CONSOLE_MESSAGE]);
  assertNoOfResources(resourceCommand, 0, 0);

  resourceCommand.unwatchResources(
    [
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      resourceCommand.TYPES.NETWORK_EVENT,
    ],
    { onAvailable, onUpdated, ignoreExistingResources: true }
  );

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

function assertNoOfResources(
  resourceCommand,
  expectedNoOfConsoleMessageResources,
  expectedNoOfNetworkEventResources
) {
  const actualNoOfConsoleMessageResources = resourceCommand.getAllResources(
    resourceCommand.TYPES.CONSOLE_MESSAGE
  ).length;
  is(
    actualNoOfConsoleMessageResources,
    expectedNoOfConsoleMessageResources,
    `There are ${actualNoOfConsoleMessageResources} console messages resources`
  );

  const actualNoOfNetworkEventResources = resourceCommand.getAllResources(
    resourceCommand.TYPES.NETWORK_EVENT
  ).length;
  is(
    actualNoOfNetworkEventResources,
    expectedNoOfNetworkEventResources,
    `There are ${actualNoOfNetworkEventResources} network event resources`
  );
}

function logConsoleMessages(browser, messages) {
  return SpecialPowers.spawn(browser, [messages], innerMessages => {
    for (const message of innerMessages) {
      content.console.log(message);
    }
  });
}