summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_client_caching.js
blob: ae398f73cca7ff75352456792a79f2c9f46d132b (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the cache mechanism of the ResourceCommand.

const TEST_URI = "data:text/html;charset=utf-8,<!DOCTYPE html>Cache Test";

add_task(async function () {
  info("Test whether multiple listener can get same cached resources");

  const tab = await addTab(TEST_URI);

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

  info("Add messages as existing resources");
  const messages = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, messages);

  info("Register first listener");
  const cachedResources1 = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable(resources, { areExistingResources }) {
        ok(areExistingResources, "All resources are already existing ones");
        cachedResources1.push(...resources);
      },
    }
  );

  info("Register second listener");
  const cachedResources2 = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable(resources, { areExistingResources }) {
        ok(areExistingResources, "All resources are already existing ones");
        cachedResources2.push(...resources);
      },
    }
  );

  assertContents(cachedResources1, messages);
  assertResources(cachedResources2, cachedResources1);

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

add_task(async function () {
  info(
    "Test whether the cache is reflecting existing resources and additional resources"
  );

  const tab = await addTab(TEST_URI);

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

  info("Add messages as existing resources");
  const existingMessages = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, existingMessages);

  info("Register first listener to get all available resources");
  const availableResources = [];
  // We first get notified about existing resources
  let shouldBeExistingResources = true;
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable(resources, { areExistingResources }) {
        is(
          areExistingResources,
          shouldBeExistingResources,
          "areExistingResources flag is correct"
        );
        availableResources.push(...resources);
      },
    }
  );
  // Then, we are notified about, new, live ones
  shouldBeExistingResources = false;

  info("Add messages as additional resources");
  const additionalMessages = ["d", "e"];
  await logMessages(tab.linkedBrowser, additionalMessages);

  info("Wait until onAvailable is called expected times");
  const allMessages = [...existingMessages, ...additionalMessages];
  await waitUntil(() => availableResources.length === allMessages.length);

  info("Register second listener to get the cached resources");
  const cachedResources = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable(resources, { areExistingResources }) {
        ok(areExistingResources, "All resources are already existing ones");
        cachedResources.push(...resources);
      },
    }
  );

  assertContents(availableResources, allMessages);
  assertResources(cachedResources, availableResources);

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

add_task(async function () {
  info("Test whether the cache is cleared when navigation");

  const tab = await addTab(TEST_URI);

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

  info("Add messages as existing resources");
  const existingMessages = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, existingMessages);

  info("Register first listener");
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: () => {},
    }
  );

  info("Reload the page");
  await BrowserTestUtils.reloadTab(tab);

  info("Register second listener");
  const cachedResources = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: resources => cachedResources.push(...resources),
    }
  );

  is(cachedResources.length, 0, "The cache in ResourceCommand is cleared");

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

add_task(async function () {
  info("Test with multiple resource types");

  const tab = await addTab(TEST_URI);

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

  info("Register first listener to get all available resources");
  const availableResources = [];
  await resourceCommand.watchResources(
    [
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      resourceCommand.TYPES.ERROR_MESSAGE,
    ],
    {
      onAvailable: resources => availableResources.push(...resources),
    }
  );

  info("Add messages as console message");
  const consoleMessages1 = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, consoleMessages1);

  info("Add message as error message");
  const errorMessages = ["document.doTheImpossible();"];
  await triggerErrors(tab.linkedBrowser, errorMessages);

  info("Add messages as console message again");
  const consoleMessages2 = ["d", "e"];
  await logMessages(tab.linkedBrowser, consoleMessages2);

  info("Wait until the getting all available resources");
  const totalResourceCount =
    consoleMessages1.length + errorMessages.length + consoleMessages2.length;
  await waitUntil(() => {
    return availableResources.length === totalResourceCount;
  });

  info("Register listener to get the cached resources");
  const cachedResources = [];
  await resourceCommand.watchResources(
    [
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      resourceCommand.TYPES.ERROR_MESSAGE,
    ],
    {
      onAvailable: resources => cachedResources.push(...resources),
    }
  );

  assertResources(cachedResources, availableResources);

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

add_task(async function () {
  info("Test multiple listeners with/without ignoreExistingResources");
  await testIgnoreExistingResources(true);
  await testIgnoreExistingResources(false);
});

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

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

  info("Add messages as existing resources");
  const existingMessages = ["a", "b", "c"];
  await logMessages(tab.linkedBrowser, existingMessages);

  info("Register first listener");
  const cachedResources1 = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: resources => cachedResources1.push(...resources),
      ignoreExistingResources: isFirstListenerIgnoreExisting,
    }
  );

  info("Register second listener");
  const cachedResources2 = [];
  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    {
      onAvailable: resources => cachedResources2.push(...resources),
      ignoreExistingResources: !isFirstListenerIgnoreExisting,
    }
  );

  const cachedResourcesWithFlag = isFirstListenerIgnoreExisting
    ? cachedResources1
    : cachedResources2;
  const cachedResourcesWithoutFlag = isFirstListenerIgnoreExisting
    ? cachedResources2
    : cachedResources1;

  info("Check the existing resources both listeners got");
  assertContents(cachedResourcesWithFlag, []);
  assertContents(cachedResourcesWithoutFlag, existingMessages);

  info("Add messages as additional resources");
  const additionalMessages = ["d", "e"];
  await logMessages(tab.linkedBrowser, additionalMessages);

  info("Wait until onAvailable is called expected times");
  await waitUntil(
    () => cachedResourcesWithFlag.length === additionalMessages.length
  );
  const allMessages = [...existingMessages, ...additionalMessages];
  await waitUntil(
    () => cachedResourcesWithoutFlag.length === allMessages.length
  );

  info("Check the resources after adding messages");
  assertContents(cachedResourcesWithFlag, additionalMessages);
  assertContents(cachedResourcesWithoutFlag, allMessages);

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

add_task(async function () {
  info("Test that onAvailable is not called with an empty resources array");

  const tab = await addTab(TEST_URI);

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

  info("Register first listener to get all available resources");
  const availableResources = [];
  let onAvailableCallCount = 0;
  const onAvailable = resources => {
    ok(
      !!resources.length,
      "onAvailable is called with a non empty resources array"
    );
    availableResources.push(...resources);
    onAvailableCallCount++;
  };

  await resourceCommand.watchResources(
    [resourceCommand.TYPES.CONSOLE_MESSAGE],
    { onAvailable }
  );
  is(availableResources.length, 0, "availableResources array is empty");
  is(onAvailableCallCount, 0, "onAvailable was never called");

  info("Add messages as console message");
  await logMessages(tab.linkedBrowser, ["expected message"]);

  await waitUntil(() => availableResources.length === 1);
  is(availableResources.length, 1, "availableResources array has one item");
  is(onAvailableCallCount, 1, "onAvailable was called only once");
  is(
    availableResources[0].message.arguments[0],
    "expected message",
    "onAvailable was called with the expected resource"
  );

  resourceCommand.unwatchResources([resourceCommand.TYPES.CONSOLE_MESSAGE], {
    onAvailable,
  });
  targetCommand.destroy();
  await client.close();
});

function assertContents(resources, expectedMessages) {
  is(
    resources.length,
    expectedMessages.length,
    "Number of the resources is correct"
  );

  for (let i = 0; i < expectedMessages.length; i++) {
    const resource = resources[i];
    const message = resource.message.arguments[0];
    const expectedMessage = expectedMessages[i];
    is(message, expectedMessage, `The ${i}th content is correct`);
  }
}

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 ContentTask.spawn(browser, { messages }, args => {
    for (const message of args.messages) {
      content.console.log(message);
    }
  });
}

async function triggerErrors(browser, errorScripts) {
  for (const errorScript of errorScripts) {
    await ContentTask.spawn(browser, errorScript, expr => {
      const document = content.document;
      const container = document.createElement("script");
      document.body.appendChild(container);
      container.textContent = expr;
      container.remove();
    });
  }
}