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

"use strict";

// Test the resource command API around CONSOLE_MESSAGE when navigating
// tab and inner iframes to distinct origin/processes.

const TEST_URL = URL_ROOT_COM_SSL + "doc_console.html";
const TEST_IFRAME_URL = URL_ROOT_ORG_SSL + "doc_console_iframe.html";
const TEST_DOMAIN = "https://example.org";
add_task(async function () {
  const START_URL = "data:text/html;charset=utf-8,foo";
  const tab = await addTab(START_URL);

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

  await testCrossProcessTabNavigation(tab.linkedBrowser, resourceCommand);
  await testCrossProcessIframeNavigation(tab.linkedBrowser, resourceCommand);

  targetCommand.destroy();
  await client.close();
  BrowserTestUtils.removeTab(tab);
});

async function testCrossProcessTabNavigation(browser, resourceCommand) {
  info(
    "Navigate the top level document from data: URI to a https document including remote iframes"
  );

  let doneResolve;
  const messages = [];
  const onConsoleLogsComplete = new Promise(resolve => (doneResolve = resolve));

  const onAvailable = resources => {
    messages.push(...resources);
    if (messages.length == 2) {
      doneResolve();
    }
  };

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

  const onLoaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.startLoadingURIString(browser, TEST_URL);
  await onLoaded;

  info("Wait for log message");
  await onConsoleLogsComplete;

  // messages are coming from different targets so the order isn't guaranteed
  const topLevelMessageResource = messages.find(resource =>
    resource.message.filename.startsWith(URL_ROOT_COM_SSL)
  );
  const iframeMessage = messages.find(resource =>
    resource.message.filename.startsWith("data:")
  );

  assertConsoleMessage(resourceCommand, topLevelMessageResource, {
    targetFront: resourceCommand.targetCommand.targetFront,
    messageText: "top-level document log",
  });
  assertConsoleMessage(resourceCommand, iframeMessage, {
    targetFront: isEveryFrameTargetEnabled
      ? resourceCommand.targetCommand
          .getAllTargets([resourceCommand.targetCommand.TYPES.FRAME])
          .find(t => t.url.startsWith("data:"))
      : resourceCommand.targetCommand.targetFront,
    messageText: "data url data log",
  });

  resourceCommand.unwatchResources([resourceCommand.TYPES.CONSOLE_MESSAGE], {
    onAvailable,
  });
}

async function testCrossProcessIframeNavigation(browser, resourceCommand) {
  info("Navigate an inner iframe from data: URI to a https remote URL");

  let doneResolve;
  const messages = [];
  const onConsoleLogsComplete = new Promise(resolve => (doneResolve = resolve));

  const onAvailable = resources => {
    messages.push(
      ...resources.filter(
        r => !r.message.arguments[0].startsWith("[WORKER] started")
      )
    );
    if (messages.length == 3) {
      doneResolve();
    }
  };

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

  // messages are coming from different targets so the order isn't guaranteed
  const topLevelMessageResource = messages.find(resource =>
    resource.message.arguments[0].startsWith("top-level")
  );
  const dataUrlMessageResource = messages.find(resource =>
    resource.message.arguments[0].startsWith("data url")
  );

  // Assert cached messages from the previous top document
  assertConsoleMessage(resourceCommand, topLevelMessageResource, {
    messageText: "top-level document log",
  });
  assertConsoleMessage(resourceCommand, dataUrlMessageResource, {
    messageText: "data url data log",
  });

  // Navigate the iframe to another origin/process
  await SpecialPowers.spawn(browser, [TEST_IFRAME_URL], function (iframeUrl) {
    const iframe = content.document.querySelector("iframe");
    iframe.src = iframeUrl;
  });

  info("Wait for log message");
  await onConsoleLogsComplete;

  // iframeTarget will be different if Fission is on or off
  const iframeTarget = await getIframeTargetFront(
    resourceCommand.targetCommand
  );

  const iframeMessageResource = messages.find(resource =>
    resource.message.arguments[0].endsWith("iframe log")
  );
  assertConsoleMessage(resourceCommand, iframeMessageResource, {
    messageText: `${TEST_DOMAIN} iframe log`,
    targetFront: iframeTarget,
  });

  resourceCommand.unwatchResources([resourceCommand.TYPES.CONSOLE_MESSAGE], {
    onAvailable,
  });
}

function assertConsoleMessage(resourceCommand, messageResource, expected) {
  is(
    messageResource.resourceType,
    resourceCommand.TYPES.CONSOLE_MESSAGE,
    "Resource is a console message"
  );
  ok(messageResource.message, "message is wrapped into a message attribute");
  if (expected.targetFront) {
    is(
      messageResource.targetFront,
      expected.targetFront,
      "Message has the correct target front"
    );
  }
  is(
    messageResource.message.arguments[0],
    expected.messageText,
    "The correct type of message"
  );
}

async function getIframeTargetFront(targetCommand) {
  // If Fission/EFT is enabled, the iframe will have a dedicated target,
  // otherwise it will be debuggable via the top level target.
  if (!isFissionEnabled() && !isEveryFrameTargetEnabled()) {
    return targetCommand.targetFront;
  }
  const frameTargets = targetCommand.getAllTargets([targetCommand.TYPES.FRAME]);
  const browsingContextID = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    () => {
      return content.document.querySelector("iframe").browsingContext.id;
    }
  );
  const iframeTarget = frameTargets.find(target => {
    return target.browsingContextID == browsingContextID;
  });
  ok(iframeTarget, "Found the iframe target front");
  return iframeTarget;
}