browser_selection.js (9197B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests keyboard selection within UrlbarUtils.RESULT_TYPE.TIP results. 5 6 "use strict"; 7 8 const HELP_URL = "about:mozilla"; 9 const TIP_URL = "about:about"; 10 const LEARN_MORE_TOPIC = "test-learn-more"; 11 const LEARN_MORE_URL = 12 Services.urlFormatter.formatURLPref("app.support.baseURL") + LEARN_MORE_TOPIC; 13 14 add_task(async function tipIsSecondResult() { 15 let results = [ 16 new UrlbarResult({ 17 type: UrlbarUtils.RESULT_TYPE.URL, 18 source: UrlbarUtils.RESULT_SOURCE.HISTORY, 19 payload: { 20 url: "http://mozilla.org/a", 21 helpUrl: "http://example.com/", 22 isBlockable: true, 23 blockL10n: { id: "urlbar-result-menu-remove-from-history" }, 24 }, 25 }), 26 makeTipResult({ 27 buttonUrl: TIP_URL, 28 helpUrl: HELP_URL, 29 descriptionL10n: { 30 id: "urlbar-result-market-opt-in-description", 31 parseMarkup: true, 32 }, 33 descriptionLearnMoreTopic: LEARN_MORE_TOPIC, 34 }), 35 ]; 36 37 let provider = new UrlbarTestUtils.TestProvider({ results, priority: 1 }); 38 UrlbarProvidersManager.registerProvider(provider); 39 40 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 41 value: "test", 42 window, 43 }); 44 45 Assert.equal( 46 UrlbarTestUtils.getResultCount(window), 47 2, 48 "There should be two results in the view." 49 ); 50 let secondResult = await UrlbarTestUtils.getDetailsOfResultAt(window, 1); 51 Assert.equal( 52 secondResult.type, 53 UrlbarUtils.RESULT_TYPE.TIP, 54 "The second result should be a tip." 55 ); 56 57 EventUtils.synthesizeKey("KEY_ArrowDown"); 58 Assert.equal( 59 UrlbarTestUtils.getSelectedElementIndex(window), 60 0, 61 "The first result's row element should be selected." 62 ); 63 64 EventUtils.synthesizeKey("KEY_ArrowDown"); 65 Assert.ok( 66 UrlbarTestUtils.getSelectedElement(window).classList.contains( 67 "urlbarView-button-0" 68 ), 69 "The selected element should be the tip button." 70 ); 71 Assert.equal( 72 UrlbarTestUtils.getSelectedElementIndex(window), 73 2, 74 "Selected element index" 75 ); 76 Assert.equal( 77 gURLBar.value, 78 TIP_URL, 79 "Input value should be the button's URL" 80 ); 81 82 EventUtils.synthesizeKey("KEY_Tab"); 83 Assert.ok( 84 UrlbarTestUtils.getSelectedElement(window).classList.contains( 85 "urlbarView-button-result-menu" 86 ), 87 "The selected element should be the tip menu button." 88 ); 89 Assert.equal( 90 UrlbarTestUtils.getSelectedRowIndex(window), 91 1, 92 "getSelectedRowIndex should return 1 even though the menu button is selected." 93 ); 94 Assert.equal( 95 UrlbarTestUtils.getSelectedElementIndex(window), 96 3, 97 "Selected element index" 98 ); 99 100 EventUtils.synthesizeKey("KEY_Tab"); 101 Assert.equal( 102 UrlbarTestUtils.getSelectedElement(window).dataset.l10nName, 103 "learn-more-link", 104 "The selected element should be the learn-more link." 105 ); 106 Assert.equal( 107 UrlbarTestUtils.getSelectedRowIndex(window), 108 1, 109 "getSelectedRowIndex should return 1 when the learn-more link is selected." 110 ); 111 Assert.equal( 112 gURLBar.value, 113 LEARN_MORE_URL, 114 "Input value should be the learn-more URL" 115 ); 116 117 // Don't check `UrlbarTestUtils.getSelectedElementIndex(window)`. The link 118 // won't have an `elementIndex`, so `getSelectedElementIndex()` will return 119 // undefined. It won't have an `elementIndex` because the `<a>` is created 120 // lazily when the Fluent `descriptionL10n` string is fetched, and element 121 // indexes are assigned before that, when the view updates row indices. 122 123 EventUtils.synthesizeKey("KEY_ArrowDown"); 124 EventUtils.synthesizeKey("KEY_ArrowUp"); 125 Assert.ok( 126 UrlbarTestUtils.getSelectedElement(window).classList.contains( 127 "urlbarView-button-0" 128 ), 129 "The selected element should be the tip button." 130 ); 131 Assert.equal( 132 gURLBar.value, 133 TIP_URL, 134 "Input value should be the button's URL" 135 ); 136 137 await UrlbarTestUtils.promisePopupClose(window); 138 UrlbarProvidersManager.unregisterProvider(provider); 139 }); 140 141 add_task(async function tipIsOnlyResult() { 142 let results = [ 143 makeTipResult({ 144 buttonUrl: TIP_URL, 145 helpUrl: HELP_URL, 146 descriptionL10n: { 147 id: "urlbar-result-market-opt-in-description", 148 parseMarkup: true, 149 }, 150 descriptionLearnMoreTopic: LEARN_MORE_TOPIC, 151 }), 152 ]; 153 154 let provider = new UrlbarTestUtils.TestProvider({ results, priority: 1 }); 155 UrlbarProvidersManager.registerProvider(provider); 156 157 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 158 value: "test", 159 window, 160 }); 161 162 Assert.equal( 163 UrlbarTestUtils.getResultCount(window), 164 1, 165 "There should be one result in the view." 166 ); 167 let firstResult = await UrlbarTestUtils.getDetailsOfResultAt(window, 0); 168 Assert.equal( 169 firstResult.type, 170 UrlbarUtils.RESULT_TYPE.TIP, 171 "The first and only result should be a tip." 172 ); 173 174 EventUtils.synthesizeKey("KEY_Tab"); 175 Assert.ok( 176 UrlbarTestUtils.getSelectedElement(window).classList.contains( 177 "urlbarView-button-0" 178 ), 179 "The selected element should be the tip button." 180 ); 181 Assert.equal( 182 UrlbarTestUtils.getSelectedElementIndex(window), 183 0, 184 "The first element should be selected." 185 ); 186 Assert.equal( 187 gURLBar.value, 188 TIP_URL, 189 "Input value should be the button's URL" 190 ); 191 192 EventUtils.synthesizeKey("KEY_Tab"); 193 Assert.ok( 194 UrlbarTestUtils.getSelectedElement(window).classList.contains( 195 "urlbarView-button-result-menu" 196 ), 197 "The selected element should be the tip menu button." 198 ); 199 Assert.equal( 200 UrlbarTestUtils.getSelectedElementIndex(window), 201 1, 202 "The second element should be selected." 203 ); 204 205 EventUtils.synthesizeKey("KEY_Tab"); 206 Assert.equal( 207 UrlbarTestUtils.getSelectedElement(window).dataset.l10nName, 208 "learn-more-link", 209 "The selected element should be the learn-more link." 210 ); 211 Assert.equal( 212 gURLBar.value, 213 LEARN_MORE_URL, 214 "Input value should be the learn-more URL" 215 ); 216 217 EventUtils.synthesizeKey("KEY_ArrowDown"); 218 Assert.equal( 219 UrlbarTestUtils.getSelectedElementIndex(window), 220 -1, 221 "There should be no selection." 222 ); 223 224 EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true }); 225 Assert.equal( 226 UrlbarTestUtils.getSelectedElement(window).dataset.l10nName, 227 "learn-more-link", 228 "The selected element should be the learn-more link." 229 ); 230 Assert.equal( 231 gURLBar.value, 232 LEARN_MORE_URL, 233 "Input value should be the learn-more URL" 234 ); 235 236 await UrlbarTestUtils.promisePopupClose(window); 237 UrlbarProvidersManager.unregisterProvider(provider); 238 }); 239 240 add_task(async function tipHasNoResultMenuButton() { 241 let results = [ 242 new UrlbarResult({ 243 type: UrlbarUtils.RESULT_TYPE.URL, 244 source: UrlbarUtils.RESULT_SOURCE.HISTORY, 245 payload: { 246 url: "http://mozilla.org/a", 247 helpUrl: "http://example.com/", 248 isBlockable: true, 249 blockL10n: { id: "urlbar-result-menu-remove-from-history" }, 250 }, 251 }), 252 253 // No `helpUrl` means no result-menu button. 254 makeTipResult({ 255 buttonUrl: TIP_URL, 256 descriptionL10n: { 257 id: "urlbar-result-market-opt-in-description", 258 parseMarkup: true, 259 }, 260 descriptionLearnMoreTopic: LEARN_MORE_TOPIC, 261 }), 262 ]; 263 264 let provider = new UrlbarTestUtils.TestProvider({ results, priority: 1 }); 265 UrlbarProvidersManager.registerProvider(provider); 266 267 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 268 value: "test", 269 window, 270 }); 271 272 Assert.equal( 273 UrlbarTestUtils.getResultCount(window), 274 2, 275 "There should be two results in the view." 276 ); 277 let secondResult = await UrlbarTestUtils.getDetailsOfResultAt(window, 1); 278 Assert.equal( 279 secondResult.type, 280 UrlbarUtils.RESULT_TYPE.TIP, 281 "The second result should be a tip." 282 ); 283 284 EventUtils.synthesizeKey("KEY_ArrowDown"); 285 Assert.equal( 286 UrlbarTestUtils.getSelectedElementIndex(window), 287 0, 288 "The first result's row element should be selected." 289 ); 290 291 EventUtils.synthesizeKey("KEY_ArrowDown"); 292 Assert.ok( 293 UrlbarTestUtils.getSelectedElement(window).classList.contains( 294 "urlbarView-button-0" 295 ), 296 "The selected element should be the tip button." 297 ); 298 Assert.equal( 299 UrlbarTestUtils.getSelectedElementIndex(window), 300 2, 301 "Selected element index" 302 ); 303 Assert.equal( 304 gURLBar.value, 305 TIP_URL, 306 "Input value should be the button's URL" 307 ); 308 309 EventUtils.synthesizeKey("KEY_Tab"); 310 Assert.equal( 311 UrlbarTestUtils.getSelectedElement(window).dataset.l10nName, 312 "learn-more-link", 313 "The selected element should be the learn-more link." 314 ); 315 Assert.equal( 316 gURLBar.value, 317 LEARN_MORE_URL, 318 "Input value should be the learn-more URL" 319 ); 320 321 EventUtils.synthesizeKey("KEY_ArrowDown"); 322 EventUtils.synthesizeKey("KEY_ArrowUp"); 323 Assert.ok( 324 UrlbarTestUtils.getSelectedElement(window).classList.contains( 325 "urlbarView-button-0" 326 ), 327 "The selected element should be the tip button." 328 ); 329 Assert.equal( 330 gURLBar.value, 331 TIP_URL, 332 "Input value should be the button's URL" 333 ); 334 335 await UrlbarTestUtils.promisePopupClose(window); 336 UrlbarProvidersManager.unregisterProvider(provider); 337 });