browser_keypressTelemetry.js (1881B)
1 /* vim:set ts=2 sw=2 sts=2 et: */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 var EventUtils = {}; 7 var PaintListener = {}; 8 Services.scriptloader.loadSubScript( 9 "chrome://mochikit/content/tests/SimpleTest/EventUtils.js", 10 EventUtils 11 ); 12 13 const { ContentTaskUtils } = ChromeUtils.importESModule( 14 "resource://testing-common/ContentTaskUtils.sys.mjs" 15 ); 16 17 function getRecordedKeypressCount() { 18 let snapshot = Services.telemetry.getSnapshotForHistograms("main", false); 19 20 var totalCount = 0; 21 for (var prop in snapshot) { 22 if (snapshot[prop].KEYPRESS_PRESENT_LATENCY) { 23 dump("found snapshot"); 24 totalCount += Object.values( 25 snapshot[prop].KEYPRESS_PRESENT_LATENCY.values 26 ).reduce((a, b) => a + b, 0); 27 } 28 } 29 30 return totalCount; 31 } 32 33 add_task(async function () { 34 await SpecialPowers.pushPrefEnv({ 35 set: [["toolkit.telemetry.ipcBatchTimeout", 10]], 36 }); 37 let histogram = Services.telemetry.getHistogramById( 38 "KEYPRESS_PRESENT_LATENCY" 39 ); 40 histogram.clear(); 41 42 waitForExplicitFinish(); 43 44 gURLBar.focus(); 45 await SimpleTest.promiseFocus(window); 46 EventUtils.sendChar("x"); 47 48 await ContentTaskUtils.waitForCondition( 49 () => { 50 return getRecordedKeypressCount() > 0; 51 }, 52 "waiting for telemetry", 53 200, 54 600 55 ); 56 let result = getRecordedKeypressCount(); 57 Assert.equal(result, 1, "One keypress recorded"); 58 59 gURLBar.focus(); 60 await SimpleTest.promiseFocus(window); 61 EventUtils.sendChar("x"); 62 63 await ContentTaskUtils.waitForCondition( 64 () => { 65 return getRecordedKeypressCount() > 1; 66 }, 67 "waiting for telemetry", 68 200, 69 600 70 ); 71 result = getRecordedKeypressCount(); 72 Assert.equal(result, 2, "Two keypresses recorded"); 73 });