test_UrlbarController_integration.js (2893B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * These tests test the UrlbarController in association with the model. 6 */ 7 8 "use strict"; 9 10 const TEST_URL = "http://example.com"; 11 const match = new UrlbarResult({ 12 type: UrlbarUtils.RESULT_TYPE.TAB_SWITCH, 13 source: UrlbarUtils.RESULT_SOURCE.TABS, 14 payload: { url: TEST_URL }, 15 }); 16 let controller; 17 18 add_setup(async function () { 19 controller = UrlbarTestUtils.newMockController(); 20 }); 21 22 add_task(async function test_basic_search() { 23 let provider = registerBasicTestProvider([match]); 24 const context = createContext(TEST_URL, { providers: [provider.name] }); 25 26 let startedPromise = promiseControllerNotification( 27 controller, 28 "onQueryStarted" 29 ); 30 let resultsPromise = promiseControllerNotification( 31 controller, 32 "onQueryResults" 33 ); 34 35 controller.startQuery(context); 36 37 let params = await startedPromise; 38 39 Assert.equal(params[0], context); 40 41 params = await resultsPromise; 42 43 Assert.deepEqual( 44 params[0].results, 45 [match], 46 "Should have the expected match" 47 ); 48 }); 49 50 add_task(async function test_cancel_search() { 51 let providerCanceledDeferred = Promise.withResolvers(); 52 let provider = registerBasicTestProvider( 53 [match], 54 providerCanceledDeferred.resolve 55 ); 56 const context = createContext(TEST_URL, { providers: [provider.name] }); 57 58 let startedPromise = promiseControllerNotification( 59 controller, 60 "onQueryStarted" 61 ); 62 let cancelPromise = promiseControllerNotification( 63 controller, 64 "onQueryCancelled" 65 ); 66 67 let delayResultsPromise = new Promise(resolve => { 68 controller.addListener({ 69 async onQueryResults(queryContext) { 70 controller.removeListener(this); 71 controller.cancelQuery(queryContext); 72 resolve(); 73 }, 74 }); 75 }); 76 77 let result = new UrlbarResult({ 78 type: UrlbarUtils.RESULT_TYPE.URL, 79 source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, 80 payload: { url: "https://example.com/1", title: "example" }, 81 }); 82 83 // We are awaiting for asynchronous work on initialization. 84 // For this test, we need the query objects to be created. We ensure this by 85 // using a delayed Provider. We wait for onQueryResults, then cancel the 86 // query. By that time the query objects are created. Then we unblock the 87 // delayed provider. 88 let delayedProvider = new UrlbarTestUtils.TestProvider({ 89 delayResultsPromise, 90 results: [result], 91 type: UrlbarUtils.PROVIDER_TYPE.PROFILE, 92 }); 93 94 UrlbarProvidersManager.registerProvider(delayedProvider); 95 96 controller.startQuery(context); 97 98 let params = await startedPromise; 99 Assert.equal(params[0], context); 100 101 info("Should have notified the provider the query is canceled"); 102 await providerCanceledDeferred.promise; 103 104 params = await cancelPromise; 105 UrlbarProvidersManager.unregisterProvider(delayedProvider); 106 });