soft-navigation-test-helper.js (4029B)
1 /** 2 * @fileoverview Helper class for soft navigation tests. 3 * 4 * This class provides helper functions for soft navigation tests. It can be 5 * used to wait for performance entries and create promises with timeout 6 * messages. 7 */ 8 class SoftNavigationTestHelper { 9 /** 10 * Constructs a new instance of the helper class. 11 * @param {!Test} test The test object. See 12 * https://web-platform-tests.org/writing-tests/testharness-api.html#test-objects 13 */ 14 constructor(test) { 15 this.test_ = test; 16 } 17 18 /** 19 * Wraps a promise with a timeout message, so that it rejects with this 20 * message if it does not resolve within the given timeout. 21 * @param {!Promise} promise The promise to wait for. 22 * @param {string} message The message to use if the promise times out. 23 * @param {number=} timeout The timeout in milliseconds. Defaults to 1000. 24 * @return {!Promise} The promise with a timeout message. 25 */ 26 async withTimeoutMessage(promise, message, timeout = 1000) { 27 return Promise.race([ 28 promise, 29 new Promise((resolve, reject) => { 30 this.test_.step_timeout(() => { 31 reject(new Error(message)); 32 }, timeout); 33 }), 34 ]); 35 } 36 37 /** 38 * Creates a new promise with a timeout message, so that it rejects with this 39 * message if it does not resolve within the given timeout, and otherwise 40 * resolves. 41 * @param {!Function} executor The executor function to create the promise; 42 * see 43 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise#executor 44 * @param {string} message The message to use if the promise times out. 45 * @param {number=} timeout The timeout in milliseconds. Defaults to 1000. 46 * @return {!Promise} The promise with a timeout message. 47 */ 48 async newPromiseWithTimeoutMessage(executor, message, timeout = 1000) { 49 return this.withTimeoutMessage(new Promise(executor), message, timeout); 50 } 51 52 /** 53 * Waits for a number of buffered performance entries of a given type, 54 * optionally including soft navigation observations, with a timeout message. 55 * @param {string} type The type of the entries to wait for. 56 * @param {number} minNumEntries The minimum number of entries to wait for. 57 * Defaults to 1. 58 * @param {number=} timeout The timeout in milliseconds. Defaults to 1000. 59 * @return {!Promise} The promise, which either resolves with the entries or 60 * rejects with a timeout message. 61 */ 62 async getBufferedPerformanceEntriesWithTimeout( 63 type, minNumEntries = 1, timeout = 1000) { 64 let observer; 65 return this 66 .newPromiseWithTimeoutMessage( 67 (resolve) => { 68 const entries = []; 69 observer = new PerformanceObserver((list) => { 70 entries.push(...list.getEntries()); 71 if (entries.length >= minNumEntries) { 72 resolve(entries); 73 } 74 }) 75 observer.observe({ 76 type: type, 77 buffered: true, 78 }); 79 }, 80 `${minNumEntries} entries of type ${type} never arrived`, 81 timeout) 82 .finally(() => { 83 observer.disconnect(); 84 }); 85 } 86 87 /** 88 * Waits for a number of performance entries of a given type, 89 * optionally including soft navigation observations. 90 * @param {string} type The type of the entries to wait for. 91 * @param {number} minNumEntries The minimum number of entries to wait for. 92 * Defaults to 1. 93 * @return {!Promise} The promise, which resolves with the entries. 94 */ 95 static getPerformanceEntries(type, minNumEntries = 1) { 96 return new Promise((resolve) => { 97 const entries = []; 98 const observer = new PerformanceObserver((list) => { 99 entries.push(...list.getEntries()); 100 if (entries.length >= minNumEntries) { 101 resolve(entries); 102 observer.disconnect(); 103 } 104 }) 105 observer.observe({ 106 type: type, 107 }); 108 }); 109 } 110 }