summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_getAllResources.js
blob: 10bc8390d9e519fe7db2ec9425e55341ede07f57 (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 getAllResources function of the ResourceCommand.

const TEST_URI = "data:text/html;charset=utf-8,getAllResources test";

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

  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab
  );

  info("Check the resources gotten from getAllResources at initial");
  is(
    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE)
      .length,
    0,
    "There is no resources at initial"
  );

  info(
    "Start to watch the available resources in order to compare with resources gotten from getAllResources"
  );
  const availableResources = [];
  const onAvailable = resources => availableResources.push(...resources);
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    { onAvailable }
  );

  info("Check the resources after some resources are available");
  const messages = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, messages);

  try {
    await waitFor(() => availableResources.length === messages.length);
  } catch (e) {
    ok(
      false,
      `Didn't receive the expected number of resources. Got ${
        availableResources.length
      }, expected ${messages.length} - ${availableResources
        .map(r => r.message.arguments[0])
        .join(" - ")}`
    );
  }

  assertResources(
    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
    availableResources
  );
  assertResources(
    resourceCommand.getAllResources(resourceCommand.TYPES.STYLESHEET),
    []
  );

  info("Check the resources after reloading");
  await BrowserTestUtils.reloadTab(tab);
  assertResources(
    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
    []
  );

  info("Append some resources again to test unwatching");
  const newMessages = ["d", "e", "f"];
  await logMessages(tab.linkedBrowser, messages);
  try {
    await waitFor(
      () =>
        resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE)
          .length === newMessages.length
    );
  } catch (e) {
    const resources = resourceCommand.getAllResources(
      resourceCommand.TYPES.CONSOLE_MESSAGE
    );
    ok(
      false,
      `Didn't receive the expected number of resources. Got ${
        resources.length
      }, expected ${messages.length} - ${resources
        .map(r => r.message.arguments.join(" | "))
        .join(" - ")}`
    );
  }

  info("Check the resources after unwatching");
  resourceCommand.unwatchResources([resourceCommand.TYPES.CONSOLE_MESSAGE], {
    onAvailable,
  });
  assertResources(
    resourceCommand.getAllResources(resourceCommand.TYPES.CONSOLE_MESSAGE),
    []
  );

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

function assertResources(resources, expectedResources) {
  is(
    resources.length,
    expectedResources.length,
    "Number of the resources is correct"
  );

  for (let i = 0; i < resources.length; i++) {
    const resource = resources[i];
    const expectedResource = expectedResources[i];
    Assert.strictEqual(
      resource,
      expectedResource,
      `The ${i}th resource is correct`
    );
  }
}

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