browser_net_throttling_profiles.js (2536B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test all the network throttling profiles 5 6 "use strict"; 7 8 requestLongerTimeout(2); 9 10 const { 11 profiles, 12 } = require("resource://devtools/client/shared/components/throttling/profiles.js"); 13 14 const httpServer = createTestHTTPServer(); 15 httpServer.registerPathHandler(`/`, function (request, response) { 16 response.setStatusLine(request.httpVersion, 200, "OK"); 17 response.write(`<meta charset=utf8><h1>Test throttling profiles</h1>`); 18 }); 19 20 // The "data" path takes a size query parameter and will return a body of the 21 // requested size. 22 httpServer.registerPathHandler("/data", function (request, response) { 23 const size = request.queryString.match(/size=(\d+)/)[1]; 24 response.setHeader("Content-Type", "text/plain"); 25 26 response.setStatusLine(request.httpVersion, 200, "OK"); 27 const body = new Array(size * 1).join("a"); 28 response.bodyOutputStream.write(body, body.length); 29 }); 30 31 const TEST_URI = `http://localhost:${httpServer.identity.primaryPort}/`; 32 33 add_task(async function () { 34 await pushPref("devtools.cache.disabled", true); 35 36 const { monitor } = await initNetMonitor(TEST_URI, { requestCount: 1 }); 37 const { store, connector, windowRequire } = monitor.panelWin; 38 const { updateNetworkThrottling } = connector; 39 40 const { getSortedRequests } = windowRequire( 41 "devtools/client/netmonitor/src/selectors/index" 42 ); 43 44 for (const profile of profiles) { 45 info(`Starting test for throttling profile ${JSON.stringify(profile)}`); 46 47 info("sending throttle request"); 48 await updateNetworkThrottling(true, profile); 49 50 const onRequest = waitForNetworkEvents(monitor, 1); 51 await SpecialPowers.spawn(gBrowser.selectedBrowser, [profile], _profile => { 52 // Size must be greater than the profile download cap. 53 const size = _profile.download * 2; 54 content.fetch("data?size=" + size); 55 }); 56 await onRequest; 57 const requestItem = getSortedRequests(store.getState()).at(-1); 58 59 if (profile.id !== "Offline") { 60 // There will be no event timing data for the Offline profile. 61 info(`Wait for eventTimings for throttling profile ${profile.id}`); 62 await waitForRequestData(store, ["eventTimings"], requestItem.id); 63 64 if (requestItem.eventTimings) { 65 Assert.greater( 66 requestItem.eventTimings.timings.receive, 67 1000, 68 `Request was properly throttled for profile ${profile.id}` 69 ); 70 } 71 } 72 } 73 74 await teardown(monitor); 75 });