test_UrlbarController_telemetry.js (6591B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * These tests unit test the functionality of UrlbarController by stubbing out the 6 * model and providing stubs to be called. 7 */ 8 9 "use strict"; 10 11 const TEST_URL = "http://example.com"; 12 const MATCH = new UrlbarResult({ 13 type: UrlbarUtils.RESULT_TYPE.TAB_SWITCH, 14 source: UrlbarUtils.RESULT_SOURCE.TABS, 15 payload: { url: TEST_URL }, 16 }); 17 const TELEMETRY_1ST_RESULT = "PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS"; 18 const TELEMETRY_6_FIRST_RESULTS = "PLACES_AUTOCOMPLETE_6_FIRST_RESULTS_TIME_MS"; 19 20 let controller; 21 let firstHistogram; 22 let sixthHistogram; 23 24 /** 25 * A delayed test provider, allowing the query to be delayed for an amount of time. 26 */ 27 class DelayedProvider extends UrlbarTestUtils.TestProvider { 28 async startQuery(context, add) { 29 Assert.ok(context, "context is passed-in"); 30 Assert.equal(typeof add, "function", "add is a callback"); 31 this._add = add; 32 await new Promise(resolve => { 33 this._resultsAdded = resolve; 34 }); 35 } 36 async addResults(matches, finish = true) { 37 // startQuery may have not been invoked yet, so wait for it 38 await TestUtils.waitForCondition( 39 () => !!this._add, 40 "Waiting for the _add callback" 41 ); 42 for (const match of matches) { 43 this._add(this, match); 44 } 45 if (finish) { 46 this._add = null; 47 this._resultsAdded(); 48 } 49 } 50 } 51 52 /** 53 * Returns the number of reports sent recorded within the histogram results. 54 * 55 * @param {object} results a snapshot of histogram results to check. 56 * @returns {number} The count of reports recorded in the histogram. 57 */ 58 function getHistogramReportsCount(results) { 59 let sum = 0; 60 for (let [, value] of Object.entries(results.values)) { 61 sum += value; 62 } 63 return sum; 64 } 65 66 add_setup(function () { 67 controller = UrlbarTestUtils.newMockController(); 68 69 firstHistogram = Services.telemetry.getHistogramById(TELEMETRY_1ST_RESULT); 70 sixthHistogram = Services.telemetry.getHistogramById( 71 TELEMETRY_6_FIRST_RESULTS 72 ); 73 }); 74 75 add_task(async function test_n_autocomplete_cancel() { 76 firstHistogram.clear(); 77 sixthHistogram.clear(); 78 79 let provider = new UrlbarTestUtils.TestProvider({ 80 results: [], 81 }); 82 UrlbarProvidersManager.registerProvider(provider); 83 const context = createContext(TEST_URL, { providers: [provider.name] }); 84 85 Assert.ok( 86 !context.firstTimerId, 87 "Should not have started first result stopwatch" 88 ); 89 Assert.ok( 90 !context.sixthTimerId, 91 "Should not have started first 6 results stopwatch" 92 ); 93 94 let startQueryPromise = controller.startQuery(context); 95 96 Assert.ok( 97 !!context.firstTimerId, 98 "Should have started first result stopwatch" 99 ); 100 Assert.ok( 101 !!context.sixthTimerId, 102 "Should have started first 6 results stopwatch" 103 ); 104 105 controller.cancelQuery(context); 106 await startQueryPromise; 107 108 Assert.ok( 109 !context.firstTimerId, 110 "Should have canceled first result stopwatch" 111 ); 112 Assert.ok( 113 !context.sixthTimerId, 114 "Should have canceled first 6 results stopwatch" 115 ); 116 117 let results = firstHistogram.snapshot(); 118 Assert.equal( 119 results.sum, 120 0, 121 "Should not have recorded any times (first result)" 122 ); 123 results = sixthHistogram.snapshot(); 124 Assert.equal( 125 results.sum, 126 0, 127 "Should not have recorded any times (first 6 results)" 128 ); 129 }); 130 131 add_task(async function test_n_autocomplete_results() { 132 firstHistogram.clear(); 133 sixthHistogram.clear(); 134 135 let provider = new DelayedProvider(); 136 UrlbarProvidersManager.registerProvider(provider); 137 const context = createContext(TEST_URL, { providers: [provider.name] }); 138 139 let resultsPromise = promiseControllerNotification( 140 controller, 141 "onQueryResults" 142 ); 143 144 Assert.ok( 145 !context.firstTimerId, 146 "Should not have started first result stopwatch" 147 ); 148 Assert.ok( 149 !context.sixthTimerId, 150 "Should not have started first 6 results stopwatch" 151 ); 152 153 controller.startQuery(context); 154 155 Assert.ok( 156 !!context.firstTimerId, 157 "Should have started first result stopwatch" 158 ); 159 Assert.ok( 160 !!context.sixthTimerId, 161 "Should have started first 6 results stopwatch" 162 ); 163 164 await provider.addResults([MATCH], false); 165 await resultsPromise; 166 167 Assert.ok(!context.firstTimerId, "Should have stopped the first stopwatch"); 168 Assert.ok( 169 !!context.sixthTimerId, 170 "Should have kept the first 6 results stopwatch running" 171 ); 172 173 let firstResults = firstHistogram.snapshot(); 174 let first6Results = sixthHistogram.snapshot(); 175 Assert.equal( 176 getHistogramReportsCount(firstResults), 177 1, 178 "Should have recorded one time for the first result" 179 ); 180 Assert.equal( 181 getHistogramReportsCount(first6Results), 182 0, 183 "Should not have recorded any times (first 6 results)" 184 ); 185 186 // Now add 5 more results, so that the first 6 results is triggered. 187 for (let i = 0; i < 5; i++) { 188 resultsPromise = promiseControllerNotification( 189 controller, 190 "onQueryResults" 191 ); 192 await provider.addResults( 193 [ 194 new UrlbarResult({ 195 type: UrlbarUtils.RESULT_TYPE.TAB_SWITCH, 196 source: UrlbarUtils.RESULT_SOURCE.TABS, 197 payload: { url: TEST_URL + "/" + i }, 198 }), 199 ], 200 false 201 ); 202 await resultsPromise; 203 } 204 205 Assert.ok(!context.firstTimerId, "Should have stopped the first stopwatch"); 206 Assert.ok( 207 !context.sixthTimerId, 208 "Should have stopped the first 6 results stopwatch" 209 ); 210 211 let updatedResults = firstHistogram.snapshot(); 212 let updated6Results = sixthHistogram.snapshot(); 213 Assert.deepEqual( 214 updatedResults, 215 firstResults, 216 "Should not have changed the histogram for the first result" 217 ); 218 Assert.equal( 219 getHistogramReportsCount(updated6Results), 220 1, 221 "Should have recorded one time for the first 6 results" 222 ); 223 224 // Add one more, to check neither are updated. 225 resultsPromise = promiseControllerNotification(controller, "onQueryResults"); 226 await provider.addResults([ 227 new UrlbarResult({ 228 type: UrlbarUtils.RESULT_TYPE.TAB_SWITCH, 229 source: UrlbarUtils.RESULT_SOURCE.TABS, 230 payload: { url: TEST_URL + "/6" }, 231 }), 232 ]); 233 await resultsPromise; 234 235 let secondUpdateResults = firstHistogram.snapshot(); 236 let secondUpdate6Results = sixthHistogram.snapshot(); 237 Assert.deepEqual( 238 secondUpdateResults, 239 firstResults, 240 "Should not have changed the histogram for the first result" 241 ); 242 Assert.equal( 243 getHistogramReportsCount(secondUpdate6Results), 244 1, 245 "Should not have changed the histogram for the first 6 results" 246 ); 247 });