browser_strip_on_share_link.js (7724B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 let listService; 7 8 let url = 9 "https://example.com/browser/browser/base/content/test/general/dummy_page.html"; 10 11 add_setup(async function () { 12 await SpecialPowers.pushPrefEnv({ 13 set: [ 14 ["test.wait300msAfterTabSwitch", true], 15 ["privacy.query_stripping.strip_list", "stripParam"], 16 ], 17 }); 18 19 // Get the list service so we can wait for it to be fully initialized before running tests. 20 listService = Cc["@mozilla.org/query-stripping-list-service;1"].getService( 21 Ci.nsIURLQueryStrippingListService 22 ); 23 24 await listService.testWaitForInit(); 25 }); 26 27 /* 28 Tests the strip-on-share feature for in-content links 29 */ 30 31 // Tests that the menu item does not show if the pref is disabled 32 add_task(async function testPrefDisabled() { 33 let validUrl = "https://www.example.com/"; 34 let shortenedUrl = "https://www.example.com/"; 35 await testStripOnShare({ 36 originalURI: validUrl, 37 strippedURI: shortenedUrl, 38 prefEnabled: false, 39 useTestList: false, 40 expectedDisabled: true, 41 }); 42 }); 43 44 // Menu item should be visible, url should be stripped. 45 add_task(async function testQueryParamIsStrippedSelectURL() { 46 let validUrl = "https://www.example.com/?stripParam=1234"; 47 let shortenedUrl = "https://www.example.com/"; 48 await testStripOnShare({ 49 originalURI: validUrl, 50 strippedURI: shortenedUrl, 51 prefEnabled: true, 52 useTestList: false, 53 expectedDisabled: false, 54 }); 55 }); 56 57 // Menu item should be visible, ensuring only parameters on the list are stripped 58 add_task(async function testQueryParamIsStripped() { 59 let validUrl = "https://www.example.com/?stripParam=1234&otherParam=1234"; 60 let shortenedUrl = "https://www.example.com/?otherParam=1234"; 61 await testStripOnShare({ 62 originalURI: validUrl, 63 strippedURI: shortenedUrl, 64 prefEnabled: true, 65 useTestList: false, 66 expectedDisabled: false, 67 }); 68 }); 69 70 // Menu item should be disabled if the url remains the same. 71 add_task(async function testURLIsCopiedWithNoParams() { 72 let validUrl = "https://www.example.com/"; 73 let shortenedUrl = "https://www.example.com/"; 74 await testStripOnShare({ 75 originalURI: validUrl, 76 strippedURI: shortenedUrl, 77 prefEnabled: true, 78 useTestList: false, 79 expectedDisabled: true, 80 }); 81 }); 82 83 // Testing site specific parameter stripping 84 add_task(async function testQueryParamIsStrippedForSiteSpecific() { 85 let validUrl = "https://www.example.com/?test_2=1234"; 86 let shortenedUrl = "https://www.example.com/"; 87 await testStripOnShare({ 88 originalURI: validUrl, 89 strippedURI: shortenedUrl, 90 prefEnabled: true, 91 useTestList: true, 92 expectedDisabled: false, 93 }); 94 }); 95 96 // Ensuring site specific parameters are not stripped for other sites 97 add_task(async function testQueryParamIsNotStrippedForWrongSiteSpecific() { 98 let validUrl = "https://www.example.com/?test_3=1234"; 99 let shortenedUrl = "https://www.example.com/?test_3=1234"; 100 await testStripOnShare({ 101 originalURI: validUrl, 102 strippedURI: shortenedUrl, 103 prefEnabled: true, 104 useTestList: true, 105 expectedDisabled: true, 106 }); 107 }); 108 109 // Ensuring clean copy option is disabled on magnet links 110 add_task(async function testMagneticLinks() { 111 let validUrl = "magnet:?xt=urn:btih:somesha1hash"; 112 let shortenedUrl = "magnet:?xt=urn:btih:somesha1hash"; 113 await testStripOnShare({ 114 originalURI: validUrl, 115 strippedURI: shortenedUrl, 116 prefEnabled: true, 117 useTestList: true, 118 expectedDisabled: true, 119 }); 120 }); 121 122 // Ensuring clean copy is disabled on about links 123 add_task(async function testAboutLinks() { 124 let validUrl = "about:blank"; 125 let shortenedUrl = "about:blank"; 126 await testStripOnShare({ 127 originalURI: validUrl, 128 strippedURI: shortenedUrl, 129 prefEnabled: true, 130 useTestList: true, 131 expectedDisabled: true, 132 }); 133 }); 134 135 // Ensure clean copy is disabled when nothing can be stripped. 136 add_task(async function testStripNothingDisabled() { 137 let validUrl = "https://example.com"; 138 let shortenedUrl = "https://example.com/"; 139 await testStripOnShare({ 140 originalURI: validUrl, 141 strippedURI: shortenedUrl, 142 prefEnabled: true, 143 useTestList: true, 144 expectedDisabled: true, 145 }); 146 }); 147 148 // Ensuring clean copy does works correctly when encountering a nested link that throws causes an error to 149 // occur. In this case, a nested magnetic link was used as it causes an error to be thrown. 150 add_task(async function testErrorHandlingForNestedLinks() { 151 let validUrl = 152 "https://www.example.com/?test_3=magnet%3A%3Fxt%3Durn%3Abtih%3Asomesha1hash&test_4=1234&test_2=4321"; 153 let shortenedUrl = 154 "https://www.example.com/?test_3=magnet%3A%3Fxt%3Durn%3Abtih%3Asomesha1hash&test_4=1234"; 155 await testStripOnShare({ 156 originalURI: validUrl, 157 strippedURI: shortenedUrl, 158 prefEnabled: true, 159 useTestList: true, 160 expectedDisabled: false, 161 }); 162 }); 163 164 /** 165 * Opens a new tab, opens the context menu and checks the menu item. 166 * Checks that the stripped version of the url is copied to the clipboard. 167 * 168 * @param {object} options 169 * @param {string} options.originalURI - The orginal url before stripping. 170 * @param {string} options.strippedURI - The expected url after stripping. 171 * @param {boolean} options.prefEnabled - If true, enable strip_on_share pref. 172 * @param {boolean} options.useTestList - If true, use test mode pref and list. 173 * @param {boolean} options.expectedDisabled - The expected item disabled state. 174 */ 175 async function testStripOnShare({ 176 originalURI, 177 strippedURI, 178 prefEnabled, 179 useTestList, 180 expectedDisabled, 181 }) { 182 await SpecialPowers.pushPrefEnv({ 183 set: [ 184 ["privacy.query_stripping.strip_on_share.enabled", prefEnabled], 185 ["privacy.query_stripping.strip_on_share.enableTestMode", useTestList], 186 ], 187 }); 188 189 if (useTestList) { 190 let testJson = { 191 global: { 192 queryParams: ["utm_ad"], 193 topLevelSites: ["*"], 194 }, 195 example: { 196 queryParams: ["test_2", "test_1"], 197 topLevelSites: ["www.example.com"], 198 }, 199 exampleNet: { 200 queryParams: ["test_3", "test_4"], 201 topLevelSites: ["www.example.net"], 202 }, 203 }; 204 205 await listService.testSetList(testJson); 206 } 207 208 await BrowserTestUtils.withNewTab(url, async function (browser) { 209 // Prepare a link 210 await SpecialPowers.spawn( 211 browser, 212 [originalURI], 213 async function (startingURI) { 214 let link = content.document.createElement("a"); 215 link.href = startingURI; 216 link.textContent = "link with query param"; 217 link.id = "link"; 218 content.document.body.appendChild(link); 219 } 220 ); 221 let contextMenu = document.getElementById("contentAreaContextMenu"); 222 // Open the context menu 223 let awaitPopupShown = BrowserTestUtils.waitForEvent( 224 contextMenu, 225 "popupshown" 226 ); 227 await BrowserTestUtils.synthesizeMouseAtCenter( 228 "#link", 229 { type: "contextmenu", button: 2 }, 230 browser 231 ); 232 await awaitPopupShown; 233 234 let menuItem = contextMenu.querySelector("#context-stripOnShareLink"); 235 Assert.equal( 236 BrowserTestUtils.isVisible(menuItem), 237 prefEnabled, 238 "Menu item is visible" 239 ); 240 Assert.equal(menuItem.disabled, expectedDisabled, "Menu item is disabled"); 241 242 let awaitPopupHidden = BrowserTestUtils.waitForEvent( 243 contextMenu, 244 "popuphidden" 245 ); 246 if (prefEnabled) { 247 // Make sure the stripped link will be copied to the clipboard 248 await SimpleTest.promiseClipboardChange(strippedURI, () => { 249 contextMenu.activateItem(menuItem); 250 }); 251 } else { 252 contextMenu.hidePopup(); 253 } 254 await awaitPopupHidden; 255 }); 256 }