browser_jsonview_url_linkification.js (2622B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { ELLIPSIS } = require("resource://devtools/shared/l10n.js"); 7 8 add_task(async function () { 9 info("Test short URL linkification JSON started"); 10 11 const url = "https://example.com/"; 12 const tab = await addJsonViewTab( 13 "data:application/json," + JSON.stringify([url]) 14 ); 15 await testLinkNavigation({ browser: tab.linkedBrowser, url }); 16 17 info("Switch back to the JSONViewer"); 18 await BrowserTestUtils.switchTab(gBrowser, tab); 19 20 await testLinkNavigation({ 21 browser: tab.linkedBrowser, 22 url, 23 clickLabel: true, 24 }); 25 }); 26 27 add_task(async function () { 28 info("Test long URL linkification JSON started"); 29 30 const url = "https://example.com/" + "a".repeat(250); 31 const tab = await addJsonViewTab( 32 "data:application/json," + JSON.stringify([url]) 33 ); 34 35 await testLinkNavigation({ browser: tab.linkedBrowser, url }); 36 37 info("Switch back to the JSONViewer"); 38 await BrowserTestUtils.switchTab(gBrowser, tab); 39 40 await testLinkNavigation({ 41 browser: tab.linkedBrowser, 42 url, 43 urlText: url.slice(0, 124) + ELLIPSIS + url.slice(-124), 44 clickLabel: true, 45 }); 46 }); 47 48 /** 49 * Assert that the expected link is displayed and that clicking on it navigates to the 50 * expected url. 51 * 52 * @param {object} option object containing: 53 * - browser (mandatory): the browser the tab will be opened in. 54 * - url (mandatory): The url we should navigate to. 55 * - urlText: The expected displayed text of the url. 56 * Falls back to `url` if not filled 57 * - clickLabel: Should we click the label before doing assertions. 58 */ 59 async function testLinkNavigation({ 60 browser, 61 url, 62 urlText, 63 clickLabel = false, 64 }) { 65 const onTabLoaded = BrowserTestUtils.waitForNewTab(gBrowser, url); 66 67 SpecialPowers.spawn(browser, [[urlText || url, url, clickLabel]], args => { 68 const [expectedURLText, expectedURL, shouldClickLabel] = args; 69 const { document } = content; 70 71 if (shouldClickLabel === true) { 72 document.querySelector(".jsonPanelBox .treeTable .treeLabel").click(); 73 } 74 75 const link = document.querySelector( 76 ".jsonPanelBox .treeTable .treeValueCell a" 77 ); 78 is(link.textContent, expectedURLText, "The expected URL is displayed."); 79 is(link.href, expectedURL, "The URL was linkified."); 80 81 link.click(); 82 }); 83 84 const newTab = await onTabLoaded; 85 // We only need to check that newTab is truthy since 86 // BrowserTestUtils.waitForNewTab checks the URL. 87 ok(newTab, "The expected tab was opened."); 88 }