browser_contentAltClick.js (6299B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Test for Bug 1109146. 7 * The tests opens a new tab and alt + clicks to download files 8 * and confirms those files are on the download list. 9 * 10 * The difference between this and the test "browser_contentAreaClick.js" is that 11 * the code path in e10s uses the ClickHandler actor instead of browser.js::contentAreaClick() util. 12 */ 13 "use strict"; 14 15 ChromeUtils.defineESModuleGetters(this, { 16 Downloads: "resource://gre/modules/Downloads.sys.mjs", 17 }); 18 19 function setup() { 20 Services.prefs.setBoolPref("browser.altClickSave", true); 21 22 let testPage = 23 "data:text/html," + 24 '<p><a id="commonlink" href="http://mochi.test/moz/">Common link</a></p>' + 25 '<p><math id="mathlink" xmlns="http://www.w3.org/1998/Math/MathML" href="http://mochi.test/moz/"><mtext>MathML XLink</mtext></math></p>' + 26 '<p><svg id="svgxlink" xmlns="http://www.w3.org/2000/svg" width="100px" height="50px" version="1.1"><a xlink:type="simple" xlink:href="http://mochi.test/moz/"><text transform="translate(10, 25)">SVG XLink</text></a></svg></p><br>' + 27 '<span id="host"></span><script>document.getElementById("host").attachShadow({mode: "closed"}).appendChild(document.getElementById("commonlink").cloneNode(true));</script>' + 28 '<iframe id="frame" src="https://test2.example.com:443/browser/browser/base/content/test/general/file_with_link_to_http.html"></iframe>'; 29 30 return BrowserTestUtils.openNewForegroundTab(gBrowser, testPage); 31 } 32 33 async function clean_up() { 34 // Remove downloads. 35 let downloadList = await Downloads.getList(Downloads.ALL); 36 let downloads = await downloadList.getAll(); 37 for (let download of downloads) { 38 await downloadList.remove(download); 39 await download.finalize(true); 40 } 41 // Remove download history. 42 await PlacesUtils.history.clear(); 43 44 Services.prefs.clearUserPref("browser.altClickSave"); 45 BrowserTestUtils.removeTab(gBrowser.selectedTab); 46 } 47 48 add_setup(async function () { 49 await SpecialPowers.pushPrefEnv({ 50 set: [["test.wait300msAfterTabSwitch", true]], 51 }); 52 }); 53 54 add_task(async function test_alt_click() { 55 await setup(); 56 57 let downloadList = await Downloads.getList(Downloads.ALL); 58 let downloads = []; 59 let downloadView; 60 // When 1 download has been attempted then resolve the promise. 61 let finishedAllDownloads = new Promise(resolve => { 62 downloadView = { 63 onDownloadAdded(aDownload) { 64 downloads.push(aDownload); 65 resolve(); 66 }, 67 }; 68 }); 69 await downloadList.addView(downloadView); 70 await BrowserTestUtils.synthesizeMouseAtCenter( 71 "#commonlink", 72 { altKey: true }, 73 gBrowser.selectedBrowser 74 ); 75 76 // Wait for all downloads to be added to the download list. 77 await finishedAllDownloads; 78 await downloadList.removeView(downloadView); 79 80 is(downloads.length, 1, "1 downloads"); 81 is( 82 downloads[0].source.url, 83 "http://mochi.test/moz/", 84 "Downloaded #commonlink element" 85 ); 86 87 await clean_up(); 88 }); 89 90 add_task(async function test_alt_click_shadow_dom() { 91 await setup(); 92 93 let downloadList = await Downloads.getList(Downloads.ALL); 94 let downloads = []; 95 let downloadView; 96 // When 1 download has been attempted then resolve the promise. 97 let finishedAllDownloads = new Promise(resolve => { 98 downloadView = { 99 onDownloadAdded(aDownload) { 100 downloads.push(aDownload); 101 resolve(); 102 }, 103 }; 104 }); 105 await downloadList.addView(downloadView); 106 await BrowserTestUtils.synthesizeMouseAtCenter( 107 "#host", 108 { altKey: true }, 109 gBrowser.selectedBrowser 110 ); 111 112 // Wait for all downloads to be added to the download list. 113 await finishedAllDownloads; 114 await downloadList.removeView(downloadView); 115 116 is(downloads.length, 1, "1 downloads"); 117 is( 118 downloads[0].source.url, 119 "http://mochi.test/moz/", 120 "Downloaded #commonlink element in shadow DOM." 121 ); 122 123 await clean_up(); 124 }); 125 126 add_task(async function test_alt_click_on_xlinks() { 127 await setup(); 128 129 let downloadList = await Downloads.getList(Downloads.ALL); 130 let downloads = []; 131 let downloadView; 132 // When all 2 downloads have been attempted then resolve the promise. 133 let finishedAllDownloads = new Promise(resolve => { 134 downloadView = { 135 onDownloadAdded(aDownload) { 136 downloads.push(aDownload); 137 if (downloads.length == 2) { 138 resolve(); 139 } 140 }, 141 }; 142 }); 143 await downloadList.addView(downloadView); 144 await BrowserTestUtils.synthesizeMouseAtCenter( 145 "#mathlink", 146 { altKey: true }, 147 gBrowser.selectedBrowser 148 ); 149 await BrowserTestUtils.synthesizeMouseAtCenter( 150 "#svgxlink", 151 { altKey: true }, 152 gBrowser.selectedBrowser 153 ); 154 155 // Wait for all downloads to be added to the download list. 156 await finishedAllDownloads; 157 await downloadList.removeView(downloadView); 158 159 is(downloads.length, 2, "2 downloads"); 160 is( 161 downloads[0].source.url, 162 "http://mochi.test/moz/", 163 "Downloaded #mathlink element" 164 ); 165 is( 166 downloads[1].source.url, 167 "http://mochi.test/moz/", 168 "Downloaded #svgxlink element" 169 ); 170 171 await clean_up(); 172 }); 173 174 // Alt+Click a link in a frame from another domain as the outer document. 175 add_task(async function test_alt_click_in_frame() { 176 await setup(); 177 178 let downloadList = await Downloads.getList(Downloads.ALL); 179 let downloads = []; 180 let downloadView; 181 // When the download has been attempted, resolve the promise. 182 let finishedAllDownloads = new Promise(resolve => { 183 downloadView = { 184 onDownloadAdded(aDownload) { 185 downloads.push(aDownload); 186 resolve(); 187 }, 188 }; 189 }); 190 191 await downloadList.addView(downloadView); 192 await BrowserTestUtils.synthesizeMouseAtCenter( 193 "#linkToExample", 194 { altKey: true }, 195 gBrowser.selectedBrowser.browsingContext.children[0] 196 ); 197 198 // Wait for all downloads to be added to the download list. 199 await finishedAllDownloads; 200 await downloadList.removeView(downloadView); 201 202 is(downloads.length, 1, "1 downloads"); 203 is( 204 downloads[0].source.url, 205 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 206 "http://example.org/", 207 "Downloaded link in iframe." 208 ); 209 210 await clean_up(); 211 });