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

"use strict";

// Check that watching/unwatching multiple times works as expected

add_task(async function () {
  const TEST_URL = "data:text/html;charset=utf-8,<!DOCTYPE html>foo";
  const tab = await addTab(TEST_URL);

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

  let resources = [];
  const onAvailable = _resources => {
    resources.push(..._resources);
  };

  info("Watch for error messages resources");
  await resourceCommand.watchResources([resourceCommand.TYPES.ERROR_MESSAGE], {
    onAvailable,
  });

  ok(
    resourceCommand.isResourceWatched(resourceCommand.TYPES.ERROR_MESSAGE),
    "The error message resource is currently been watched."
  );

  is(
    resources.length,
    0,
    "no resources were received after the first watchResources call"
  );

  info("Trigger an error in the page");
  await ContentTask.spawn(tab.linkedBrowser, [], function frameScript() {
    const document = content.document;
    const scriptEl = document.createElement("script");
    scriptEl.textContent = `document.unknownFunction()`;
    document.body.appendChild(scriptEl);
  });

  await waitFor(() => resources.length === 1);
  const EXPECTED_ERROR_MESSAGE =
    "TypeError: document.unknownFunction is not a function";
  is(
    resources[0].pageError.errorMessage,
    EXPECTED_ERROR_MESSAGE,
    "The resource was received"
  );

  info("Unwatching resources…");
  resourceCommand.unwatchResources([resourceCommand.TYPES.ERROR_MESSAGE], {
    onAvailable,
  });

  ok(
    !resourceCommand.isResourceWatched(resourceCommand.TYPES.ERROR_MESSAGE),
    "The error message resource is no longer been watched."
  );
  // clearing resources
  resources = [];

  info("…and watching again");
  await resourceCommand.watchResources([resourceCommand.TYPES.ERROR_MESSAGE], {
    onAvailable,
  });

  ok(
    resourceCommand.isResourceWatched(resourceCommand.TYPES.ERROR_MESSAGE),
    "The error message resource is been watched again."
  );
  is(
    resources.length,
    1,
    "we retrieve the expected number of existing resources"
  );
  is(
    resources[0].pageError.errorMessage,
    EXPECTED_ERROR_MESSAGE,
    "The resource is the expected one"
  );

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