summaryrefslogtreecommitdiffstats
path: root/devtools/client/jsonview/test/browser_jsonview_url_linkification.js
blob: 19d1f27f0c280e738d9889d23e49d1d62f34a96f (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";

const { ELLIPSIS } = require("resource://devtools/shared/l10n.js");

add_task(async function () {
  info("Test short URL linkification JSON started");

  const url = "https://example.com/";
  const tab = await addJsonViewTab(
    "data:application/json," + JSON.stringify([url])
  );
  await testLinkNavigation({ browser: tab.linkedBrowser, url });

  info("Switch back to the JSONViewer");
  await BrowserTestUtils.switchTab(gBrowser, tab);

  await testLinkNavigation({
    browser: tab.linkedBrowser,
    url,
    clickLabel: true,
  });
});

add_task(async function () {
  info("Test long URL linkification JSON started");

  const url = "https://example.com/" + "a".repeat(100);
  const tab = await addJsonViewTab(
    "data:application/json," + JSON.stringify([url])
  );

  await testLinkNavigation({ browser: tab.linkedBrowser, url });

  info("Switch back to the JSONViewer");
  await BrowserTestUtils.switchTab(gBrowser, tab);

  await testLinkNavigation({
    browser: tab.linkedBrowser,
    url,
    urlText: url.slice(0, 24) + ELLIPSIS + url.slice(-24),
    clickLabel: true,
  });
});

/**
 * Assert that the expected link is displayed and that clicking on it navigates to the
 * expected url.
 *
 * @param {Object} option object containing:
 *        - browser (mandatory): the browser the tab will be opened in.
 *        - url (mandatory): The url we should navigate to.
 *        - urlText: The expected displayed text of the url.
 *                   Falls back to `url` if not filled
 *        - clickLabel: Should we click the label before doing assertions.
 */
async function testLinkNavigation({
  browser,
  url,
  urlText,
  clickLabel = false,
}) {
  const onTabLoaded = BrowserTestUtils.waitForNewTab(gBrowser, url);

  SpecialPowers.spawn(browser, [[urlText || url, url, clickLabel]], args => {
    const [expectedURLText, expectedURL, shouldClickLabel] = args;
    const { document } = content;

    if (shouldClickLabel === true) {
      document.querySelector(".jsonPanelBox .treeTable .treeLabel").click();
    }

    const link = document.querySelector(
      ".jsonPanelBox .treeTable .treeValueCell a"
    );
    is(link.textContent, expectedURLText, "The expected URL is displayed.");
    is(link.href, expectedURL, "The URL was linkified.");

    link.click();
  });

  const newTab = await onTabLoaded;
  // We only need to check that newTab is truthy since
  // BrowserTestUtils.waitForNewTab checks the URL.
  ok(newTab, "The expected tab was opened.");
}