browser_devtools-record-capture.js (6883B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const FRONTEND_BASE_HOST = "https://example.com"; 8 const FRONTEND_BASE_PATH = 9 "/browser/devtools/client/performance-new/test/browser/fake-frontend.html"; 10 const FRONTEND_BASE_URL = FRONTEND_BASE_HOST + FRONTEND_BASE_PATH; 11 12 add_setup(async function setup() { 13 // The active tab view isn't enabled in all configurations. Let's make sure 14 // it's enabled in these tests. 15 SpecialPowers.pushPrefEnv({ 16 set: [["devtools.performance.recording.active-tab-view.enabled", true]], 17 }); 18 }); 19 20 add_task(async function test() { 21 info( 22 "Test that DevTools can capture profiles. This function also unit tests the " + 23 "internal RecordingState of the client." 24 ); 25 26 // This test assumes that the Web Developer preset is set by default, which is 27 // not the case on Nightly and custom builds. 28 PrefsPresets.changePreset( 29 "aboutprofiling", 30 "web-developer", 31 Services.profiler.GetFeatures() 32 ); 33 34 await setProfilerFrontendUrl(FRONTEND_BASE_HOST, FRONTEND_BASE_PATH); 35 36 await withDevToolsPanel(async document => { 37 const getRecordingState = setupGetRecordingState(document); 38 39 // The initial state of the profiler UI is racy, as it calls out to the PerfFront 40 // to get the status of the profiler. This can race with the initialization of 41 // the test. Most of the the time the result is "not-yet-known", but rarely 42 // the PerfFront will win this race. Allow for both outcomes of the race in this 43 // test. 44 ok( 45 getRecordingState() === "not-yet-known" || 46 getRecordingState() === "available-to-record", 47 "The component starts out in an unknown state or is already available to record." 48 ); 49 50 // First check for "firefox-platform" preset which will have no "view" query 51 // string because this is where our traditional "full" view opens up. 52 await setPresetCaptureAndAssertUrl({ 53 document, 54 preset: "firefox-platform", 55 expectedUrl: FRONTEND_BASE_URL, 56 getRecordingState, 57 }); 58 59 // Now, let's check for "web-developer" preset. This will open up the frontend 60 // with "active-tab" view query string. Frontend will understand and open the active tab view for it. 61 await setPresetCaptureAndAssertUrl({ 62 document, 63 preset: "web-developer", 64 expectedUrl: FRONTEND_BASE_URL + "?view=active-tab&implementation=js", 65 getRecordingState, 66 }); 67 }); 68 }); 69 70 add_task(async function test_in_private_window() { 71 info("Test that DevTools can capture profiles in a private window."); 72 73 // This test assumes that the Web Developer preset is set by default, which is 74 // not the case on Nightly and custom builds. 75 PrefsPresets.changePreset( 76 "aboutprofiling", 77 "web-developer", 78 Services.profiler.GetFeatures() 79 ); 80 81 await setProfilerFrontendUrl(FRONTEND_BASE_HOST, FRONTEND_BASE_PATH); 82 83 info("Open a private window."); 84 const privateWindow = await BrowserTestUtils.openNewBrowserWindow({ 85 private: true, 86 }); 87 88 await withDevToolsPanel(async document => { 89 const getRecordingState = setupGetRecordingState(document); 90 91 // The initial state of the profiler UI is racy, as it calls out to the PerfFront 92 // to get the status of the profiler. This can race with the initialization of 93 // the test. Most of the the time the result is "not-yet-known", but rarely 94 // the PerfFront will win this race. Allow for both outcomes of the race in this 95 // test. 96 ok( 97 getRecordingState() === "not-yet-known" || 98 getRecordingState() === "available-to-record", 99 "The component starts out in an unknown state or is already available to record." 100 ); 101 102 // First check for "firefox-platform" preset which will have no "view" query 103 // string because this is where our traditional "full" view opens up. 104 // Note that this utility will check for a new tab in the main non-private 105 // window, which is exactly what we want here. 106 await setPresetCaptureAndAssertUrl({ 107 document, 108 preset: "firefox-platform", 109 expectedUrl: FRONTEND_BASE_URL, 110 getRecordingState, 111 }); 112 113 // Now, let's check for "web-developer" preset. This will open up the frontend 114 // with "active-tab" view query string. Frontend will understand and open the active tab view for it. 115 await setPresetCaptureAndAssertUrl({ 116 document, 117 preset: "web-developer", 118 expectedUrl: FRONTEND_BASE_URL + "?view=active-tab&implementation=js", 119 getRecordingState, 120 }); 121 }, privateWindow); 122 123 await BrowserTestUtils.closeWindow(privateWindow); 124 }); 125 126 async function setPresetCaptureAndAssertUrl({ 127 document, 128 preset, 129 expectedUrl, 130 getRecordingState, 131 }) { 132 const presetsInDevtools = await getNearestInputFromText(document, "Settings"); 133 setReactFriendlyInputValue(presetsInDevtools, preset); 134 135 const startRecording = await getActiveButtonFromText( 136 document, 137 "Start recording" 138 ); 139 140 is( 141 getRecordingState(), 142 "available-to-record", 143 "After talking to the actor, we're ready to record." 144 ); 145 146 info("Click the button to start recording"); 147 startRecording.click(); 148 149 is( 150 getRecordingState(), 151 "request-to-start-recording", 152 "Clicking the start recording button sends in a request to start recording." 153 ); 154 155 is( 156 document.defaultView.gToolbox.isHighlighted("performance"), 157 false, 158 "The Performance panel in not highlighted yet." 159 ); 160 161 const captureRecording = await getActiveButtonFromText( 162 document, 163 "Capture recording" 164 ); 165 166 is( 167 getRecordingState(), 168 "recording", 169 "Once the Capture recording button is available, the actor has started " + 170 "its recording" 171 ); 172 173 is( 174 document.defaultView.gToolbox.isHighlighted("performance"), 175 true, 176 "The Performance Panel in the Devtools Tab is highlighted when the profiler " + 177 "is recording" 178 ); 179 180 info("Click the button to capture the recording."); 181 captureRecording.click(); 182 183 is( 184 getRecordingState(), 185 "request-to-get-profile-and-stop-profiler", 186 "We have requested to stop the profiler." 187 ); 188 189 await getActiveButtonFromText(document, "Start recording"); 190 is( 191 getRecordingState(), 192 "available-to-record", 193 "The profiler is available to record again." 194 ); 195 196 is( 197 document.defaultView.gToolbox.isHighlighted("performance"), 198 false, 199 "The Performance panel in not highlighted anymore when the profiler is stopped" 200 ); 201 202 info( 203 "If the DevTools successfully injects a profile into the page, then the " + 204 "fake frontend will rename the title of the page." 205 ); 206 207 await waitForTabUrl({ 208 initialTitle: "Waiting on the profile", 209 successTitle: "Profile received", 210 errorTitle: "Error", 211 expectedUrl, 212 }); 213 }