browser_contextmenu_loadblobinnewtab.js (4821B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const RESOURCE_LINK = 7 getRootDirectory(gTestPath).replace( 8 "chrome://mochitests/content", 9 "https://example.com" 10 ) + "browser_contextmenu_loadblobinnewtab.html"; 11 12 const blobDataAsString = `!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ`; 13 14 // Helper method to right click on the provided link (selector as id), 15 // open in new tab and return the content of the first <pre> under the 16 // <body> of the new tab's document. 17 async function rightClickOpenInNewTabAndReturnContent(selector) { 18 const loaded = BrowserTestUtils.browserLoaded( 19 gBrowser.selectedBrowser, 20 false, 21 RESOURCE_LINK 22 ); 23 BrowserTestUtils.startLoadingURIString( 24 gBrowser.selectedBrowser, 25 RESOURCE_LINK 26 ); 27 await loaded; 28 29 const generatedBlobURL = await ContentTask.spawn( 30 gBrowser.selectedBrowser, 31 { selector }, 32 async args => { 33 return content.document.getElementById(args.selector).href; 34 } 35 ); 36 37 const contextMenu = document.getElementById("contentAreaContextMenu"); 38 is(contextMenu.state, "closed", "checking if context menu is closed"); 39 40 let awaitPopupShown = BrowserTestUtils.waitForEvent( 41 contextMenu, 42 "popupshown" 43 ); 44 45 await BrowserTestUtils.synthesizeMouseAtCenter( 46 "#" + selector, 47 { type: "contextmenu", button: 2 }, 48 gBrowser.selectedBrowser 49 ); 50 await awaitPopupShown; 51 52 let awaitPopupHidden = BrowserTestUtils.waitForEvent( 53 contextMenu, 54 "popuphidden" 55 ); 56 57 const openPromise = BrowserTestUtils.waitForNewTab( 58 gBrowser, 59 generatedBlobURL, 60 false 61 ); 62 63 document.getElementById("context-openlinkintab").doCommand(); 64 65 contextMenu.hidePopup(); 66 await awaitPopupHidden; 67 68 let openTab = await openPromise; 69 70 await BrowserTestUtils.switchTab(gBrowser, gBrowser.tabs[1]); 71 72 let blobDataFromContent = await ContentTask.spawn( 73 gBrowser.selectedBrowser, 74 null, 75 async function () { 76 while (!content.document.querySelector("body pre")) { 77 await new Promise(resolve => 78 content.setTimeout(() => { 79 resolve(); 80 }, 100) 81 ); 82 } 83 return content.document.body.firstElementChild.innerText.trim(); 84 } 85 ); 86 87 let tabClosed = BrowserTestUtils.waitForTabClosing(openTab); 88 await BrowserTestUtils.removeTab(openTab); 89 await tabClosed; 90 91 return blobDataFromContent; 92 } 93 94 // Helper method to open selected link in new tab (selector as id), 95 // and return the content of the first <pre> under the <body> of 96 // the new tab's document. 97 async function openInNewTabAndReturnContent(selector) { 98 const loaded = BrowserTestUtils.browserLoaded( 99 gBrowser.selectedBrowser, 100 false, 101 RESOURCE_LINK 102 ); 103 BrowserTestUtils.startLoadingURIString( 104 gBrowser.selectedBrowser, 105 RESOURCE_LINK 106 ); 107 await loaded; 108 109 const generatedBlobURL = await ContentTask.spawn( 110 gBrowser.selectedBrowser, 111 { selector }, 112 async args => { 113 return content.document.getElementById(args.selector).href; 114 } 115 ); 116 117 let openTab = await BrowserTestUtils.openNewForegroundTab( 118 gBrowser, 119 generatedBlobURL 120 ); 121 122 let blobDataFromContent = await ContentTask.spawn( 123 gBrowser.selectedBrowser, 124 null, 125 async function () { 126 while (!content.document.querySelector("body pre")) { 127 await new Promise(resolve => 128 content.setTimeout(() => { 129 resolve(); 130 }, 100) 131 ); 132 } 133 return content.document.body.firstElementChild.innerText.trim(); 134 } 135 ); 136 137 let tabClosed = BrowserTestUtils.waitForTabClosing(openTab); 138 await BrowserTestUtils.removeTab(openTab); 139 await tabClosed; 140 141 return blobDataFromContent; 142 } 143 144 add_task(async function test_rightclick_open_bloburl_in_new_tab() { 145 let blobDataFromLoadedPage = 146 await rightClickOpenInNewTabAndReturnContent("blob-url-link"); 147 is( 148 blobDataFromLoadedPage, 149 blobDataAsString, 150 "The blobURL is correctly loaded" 151 ); 152 }); 153 154 add_task(async function test_rightclick_open_bloburl_referrer_in_new_tab() { 155 let blobDataFromLoadedPage = await rightClickOpenInNewTabAndReturnContent( 156 "blob-url-referrer-link" 157 ); 158 is( 159 blobDataFromLoadedPage, 160 blobDataAsString, 161 "The blobURL is correctly loaded" 162 ); 163 }); 164 165 add_task(async function test_open_bloburl_in_new_tab() { 166 let blobDataFromLoadedPage = 167 await openInNewTabAndReturnContent("blob-url-link"); 168 is( 169 blobDataFromLoadedPage, 170 blobDataAsString, 171 "The blobURL is correctly loaded" 172 ); 173 }); 174 175 add_task(async function test_open_bloburl_referrer_in_new_tab() { 176 let blobDataFromLoadedPage = await openInNewTabAndReturnContent( 177 "blob-url-referrer-link" 178 ); 179 is( 180 blobDataFromLoadedPage, 181 blobDataAsString, 182 "The blobURL is correctly loaded" 183 ); 184 });