summaryrefslogtreecommitdiffstats
path: root/devtools/startup/tests/browser/browser_command_line_urls.js
blob: 4494d635c672b3f7331784ea22ed27fdc3443440 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test command line processing of URLs meant to be intepreted by DevTools.
 */

/* eslint-env browser */

const { DevToolsStartup } = ChromeUtils.importESModule(
  "resource:///modules/DevToolsStartup.sys.mjs"
);

const { require } = ChromeUtils.importESModule(
  "resource://devtools/shared/loader/Loader.sys.mjs"
);
const { gDevTools } = require("devtools/client/framework/devtools");

const URL_ROOT = "https://example.org/browser/devtools/startup/tests/browser/";

const startup = new DevToolsStartup();
// The feature covered here only work when calling firefox from command line
// while it is already opened. So fake Firefox being already opened:
startup.initialized = true;

add_task(async function ignoredUrls() {
  const tabCount = gBrowser.tabs.length;

  // We explicitely try to ignore these URL which looks like line, but are passwords
  sendUrlViaCommandLine("https://foo@user:123");
  sendUrlViaCommandLine("https://foo@user:123");
  sendUrlViaCommandLine("https://foo@123:456");

  // The following is an invalid URL (domain with space)
  sendUrlViaCommandLine("https://foo /index.html:123:456");

  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 1000));

  is(tabCount, gBrowser.tabs.length);
});

/**
 * With DevTools closed, but DevToolsStartup "initialized",
 * the url will be ignored
 */
add_task(async function openingWithDevToolsClosed() {
  const url = URL_ROOT + "command-line.html:5:2";

  const tabCount = gBrowser.tabs.length;
  const ignoredUrl = sendUrlViaCommandLine(url);
  ok(ignoredUrl, "The url is ignored when no devtools are opened");

  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 1000));

  is(tabCount, gBrowser.tabs.length);
});

/**
 * With DevTools opened, but the source isn't in the debugged tab,
 * the url will also be opened via view-source
 */
add_task(async function openingWithDevToolsButUnknownSource() {
  const url = URL_ROOT + "command-line.html:5:2";

  const tab = BrowserTestUtils.addTab(
    gBrowser,
    "data:text/html;charset=utf-8,<title>foo</title>"
  );

  const toolbox = await gDevTools.showToolboxForTab(gBrowser.selectedTab, {
    toolId: "jsdebugger",
  });

  const newTabOpened = BrowserTestUtils.waitForNewTab(gBrowser);
  sendUrlViaCommandLine(url);
  const newTab = await newTabOpened;
  is(
    newTab.linkedBrowser.documentURI.spec,
    "view-source:" + URL_ROOT + "command-line.html"
  );

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    const selection = content.getSelection();
    Assert.equal(
      selection.toString(),
      "  <title>Command line test page</title>",
      "The 5th line is selected in view-source"
    );
  });
  await gBrowser.removeTab(newTab);

  await toolbox.destroy();
  await gBrowser.removeTab(tab);
});

/**
 * With DevTools opened, and the source is debugged by the debugger,
 * the url will be opened in the debugger.
 */
add_task(async function openingWithDevToolsAndKnownSource() {
  const url = URL_ROOT + "command-line.js:5:2";

  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    URL_ROOT + "command-line.html"
  );
  const toolbox = await gDevTools.showToolboxForTab(tab, {
    toolId: "jsdebugger",
  });

  info("Open a first URL with line and column");
  sendUrlViaCommandLine(url);

  const dbg = toolbox.getPanel("jsdebugger");
  const selectedLocation = await BrowserTestUtils.waitForCondition(() => {
    return dbg._selectors.getSelectedLocation(dbg._getState());
  });
  is(selectedLocation.source.url, URL_ROOT + "command-line.js");
  is(selectedLocation.line, 5);
  is(selectedLocation.column, 1);

  info("Open another URL with only a line");
  const url2 = URL_ROOT + "command-line.js:6";
  sendUrlViaCommandLine(url2);
  const selectedLocation2 = await BrowserTestUtils.waitForCondition(() => {
    const location = dbg._selectors.getSelectedLocation(dbg._getState());
    return location.line == 6 ? location : false;
  });
  is(selectedLocation2.source.url, URL_ROOT + "command-line.js");
  is(selectedLocation2.line, 6);
  is(selectedLocation2.column, 0);

  await toolbox.destroy();
  await gBrowser.removeTab(tab);
});

// Fake opening an existing firefox instance with a URL
// passed via `-url` command line argument
function sendUrlViaCommandLine(url) {
  const cmdLine = Cu.createCommandLine(
    ["-url", url],
    null,
    Ci.nsICommandLine.STATE_REMOTE_EXPLICIT
  );
  startup.handle(cmdLine);

  // Return true if DevToolsStartup ignored the url
  // and let it be in the command line object.
  return cmdLine.findFlag("url", false) != -1;
}