summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_platform_messages.js
blob: 4e74a97e38d92a2bd141ad8669a1dd3822ff0595 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the ResourceCommand API around PLATFORM_MESSAGE
// Reproduces assertions from: devtools/shared/webconsole/test/chrome/test_nsiconsolemessage.html

add_task(async function () {
  // Disable the preloaded process as it creates processes intermittently
  // which forces the emission of RDP requests we aren't correctly waiting for.
  await pushPref("dom.ipc.processPrelaunch.enabled", false);

  await testPlatformMessagesResources();
  await testPlatformMessagesResourcesWithIgnoreExistingResources();
});

async function testPlatformMessagesResources() {
  const { client, resourceCommand, targetCommand } =
    await initMultiProcessResourceCommand();

  const cachedMessages = [
    "This is a cached message",
    "This is another cached message",
  ];
  const liveMessages = [
    "This is a live message",
    "This is another live message",
  ];
  const expectedMessages = [...cachedMessages, ...liveMessages];
  const receivedMessages = [];

  info(
    "Log some messages *before* calling ResourceCommand.watchResources in order to assert the behavior of already existing messages."
  );
  Services.console.logStringMessage(expectedMessages[0]);
  Services.console.logStringMessage(expectedMessages[1]);

  let done;
  const onAllMessagesReceived = new Promise(resolve => (done = resolve));
  const onAvailable = resources => {
    for (const resource of resources) {
      if (!expectedMessages.includes(resource.message)) {
        continue;
      }

      is(
        resource.targetFront,
        targetCommand.targetFront,
        "The targetFront property is the expected one"
      );

      receivedMessages.push(resource.message);
      is(
        resource.message,
        expectedMessages[receivedMessages.length - 1],
        `Received the expected «${resource.message}» message, in the expected order`
      );

      // timeStamp are the result of a number in microsecond divided by 1000.
      // so we can't expect a precise number of decimals, or even if there would
      // be decimals at all.
      ok(
        resource.timeStamp.toString().match(/^\d+(\.\d{1,3})?$/),
        `The resource has a timeStamp property ${resource.timeStamp}`
      );

      const isCachedMessage = receivedMessages.length <= cachedMessages.length;
      is(
        resource.isAlreadyExistingResource,
        isCachedMessage,
        "isAlreadyExistingResource has the expected value"
      );

      if (receivedMessages.length == expectedMessages.length) {
        done();
      }
    }
  };

  await resourceCommand.watchResources(
    [resourceCommand.TYPES.PLATFORM_MESSAGE],
    {
      onAvailable,
    }
  );

  info(
    "Now log messages *after* the call to ResourceCommand.watchResources and after having received all existing messages"
  );
  Services.console.logStringMessage(expectedMessages[2]);
  Services.console.logStringMessage(expectedMessages[3]);

  info("Waiting for all expected messages to be received");
  await onAllMessagesReceived;
  ok(true, "All the expected messages were received");

  Services.console.reset();
  targetCommand.destroy();
  await client.close();
}

async function testPlatformMessagesResourcesWithIgnoreExistingResources() {
  const { client, resourceCommand, targetCommand } =
    await initMultiProcessResourceCommand();

  info(
    "Check whether onAvailable will not be called with existing platform messages"
  );
  const expectedMessages = ["This is 1st message", "This is 2nd message"];
  Services.console.logStringMessage(expectedMessages[0]);
  Services.console.logStringMessage(expectedMessages[1]);

  const availableResources = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.PLATFORM_MESSAGE],
    {
      onAvailable: resources => {
        for (const resource of resources) {
          if (!expectedMessages.includes(resource.message)) {
            continue;
          }

          availableResources.push(resource);
        }
      },
      ignoreExistingResources: true,
    }
  );
  is(
    availableResources.length,
    0,
    "onAvailable wasn't called for existing platform messages"
  );

  info(
    "Check whether onAvailable will be called with the future platform messages"
  );
  Services.console.logStringMessage(expectedMessages[0]);
  Services.console.logStringMessage(expectedMessages[1]);

  await waitUntil(() => availableResources.length === expectedMessages.length);
  for (let i = 0; i < expectedMessages.length; i++) {
    const resource = availableResources[i];
    const { message } = resource;
    const expected = expectedMessages[i];
    is(message, expected, `Message[${i}] is correct`);
    is(
      resource.isAlreadyExistingResource,
      false,
      "isAlreadyExistingResource is false since we ignore existing resources"
    );
  }

  Services.console.reset();
  targetCommand.destroy();
  await client.close();
}