browser_picks.js (5197B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests clicks and enter key presses on UrlbarUtils.RESULT_TYPE.TIP results. 5 6 "use strict"; 7 8 const TIP_URL = "http://example.com/tip"; 9 const HELP_URL = "http://example.com/help"; 10 11 add_setup(async function () { 12 window.windowUtils.disableNonTestMouseEvents(true); 13 registerCleanupFunction(() => { 14 window.windowUtils.disableNonTestMouseEvents(false); 15 }); 16 }); 17 18 add_task(async function enter_mainButton_url() { 19 await doTest({ click: false, buttonUrl: TIP_URL }); 20 }); 21 22 add_task(async function enter_mainButton_noURL() { 23 await doTest({ click: false }); 24 }); 25 26 add_task(async function enter_help() { 27 await doTest({ click: false, helpUrl: HELP_URL }); 28 }); 29 30 add_task(async function mouse_mainButton_url() { 31 await doTest({ click: true, buttonUrl: TIP_URL }); 32 }); 33 34 add_task(async function mouse_mainButton_noURL() { 35 await doTest({ click: true }); 36 }); 37 38 add_task(async function mouse_help() { 39 await doTest({ click: true, helpUrl: HELP_URL }); 40 }); 41 42 // Clicks inside a tip but not on any button. 43 add_task(async function mouse_insideTipButNotOnButtons() { 44 // Click inside the tip but outside the buttons. Nothing should happen. Make 45 // the result the heuristic to check that the selection on the main button 46 // isn't lost. 47 let results = [ 48 makeTipResult({ buttonUrl: TIP_URL, helpUrl: HELP_URL, heuristic: true }), 49 ]; 50 let provider = new UrlbarTestUtils.TestProvider({ results, priority: 1 }); 51 UrlbarProvidersManager.registerProvider(provider); 52 53 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 54 value: "test", 55 window, 56 fireInputEvent: true, 57 }); 58 let row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0); 59 Assert.equal( 60 UrlbarTestUtils.getSelectedElementIndex(window), 61 0, 62 "The main button's index should be selected initially" 63 ); 64 Assert.equal( 65 UrlbarTestUtils.getSelectedElement(window), 66 row._buttons.get("0"), 67 "The main button element should be selected initially" 68 ); 69 EventUtils.synthesizeMouseAtCenter(row, {}); 70 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 71 await new Promise(r => setTimeout(r, 500)); 72 Assert.ok(gURLBar.view.isOpen, "The view should remain open"); 73 Assert.equal( 74 UrlbarTestUtils.getSelectedElementIndex(window), 75 0, 76 "The main button's index should remain selected" 77 ); 78 Assert.equal( 79 UrlbarTestUtils.getSelectedElement(window), 80 row._buttons.get("0"), 81 "The main button element should remain selected" 82 ); 83 84 await UrlbarTestUtils.promisePopupClose(window); 85 UrlbarProvidersManager.unregisterProvider(provider); 86 }); 87 88 /** 89 * Runs this test's main checks. 90 * 91 * @param {object} options 92 * Options for the test. 93 * @param {boolean} options.click 94 * Pass true to trigger a click, false to trigger an enter key. 95 * @param {string} [options.buttonUrl] 96 * Pass a URL if picking the main button should open a URL. Pass nothing if 97 * a URL shouldn't be opened or if you want to pick the help button instead of 98 * the main button. 99 * @param {string} [options.helpUrl] 100 * Pass a URL if you want to pick the help button. Pass nothing if you want 101 * to pick the main button instead. 102 */ 103 async function doTest({ click, buttonUrl = undefined, helpUrl = undefined }) { 104 // Open a new tab for the test if we expect to load a URL. 105 let tab; 106 if (buttonUrl || helpUrl) { 107 tab = await BrowserTestUtils.openNewForegroundTab({ 108 gBrowser, 109 url: "about:blank", 110 }); 111 } 112 113 const deferred = Promise.withResolvers(); 114 115 // Add our test provider. 116 let provider = new UrlbarTestUtils.TestProvider({ 117 results: [makeTipResult({ buttonUrl, helpUrl })], 118 priority: 1, 119 onEngagement: () => deferred.resolve(), 120 }); 121 UrlbarProvidersManager.registerProvider(provider); 122 123 // Do a search to show our tip result. 124 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 125 value: "test", 126 window, 127 fireInputEvent: true, 128 }); 129 let row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0); 130 let mainButton = row._buttons.get("0"); 131 let target = helpUrl ? row._buttons.get("result-menu") : mainButton; 132 133 // If we're picking the tip with the keyboard, TAB to select the proper 134 // target. 135 if (!click) { 136 EventUtils.synthesizeKey("KEY_Tab", { repeat: helpUrl ? 2 : 1 }); 137 Assert.equal( 138 UrlbarTestUtils.getSelectedElement(window), 139 target, 140 `${target.className} should be selected.` 141 ); 142 } 143 144 // Done. 145 await UrlbarTestUtils.promisePopupClose(window); 146 UrlbarProvidersManager.unregisterProvider(provider); 147 if (tab) { 148 BrowserTestUtils.removeTab(tab); 149 } 150 } 151 152 function makeTipResult({ buttonUrl, helpUrl, heuristic }) { 153 return new UrlbarResult({ 154 type: UrlbarUtils.RESULT_TYPE.TIP, 155 source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, 156 heuristic, 157 payload: { 158 type: "test", 159 titleL10n: { id: "urlbar-search-tips-confirm" }, 160 buttons: [ 161 { 162 url: buttonUrl, 163 l10n: { id: "urlbar-search-tips-confirm" }, 164 }, 165 ], 166 helpUrl, 167 helpL10n: { 168 id: "urlbar-result-menu-tip-get-help", 169 }, 170 }, 171 }); 172 }