Web Inspector: Timelines: can't reliably stop/start a recording
[WebKit-https.git] / LayoutTests / inspector / timeline / resources / timeline-event-utilities.js
1 function savePageData(data) {
2     TestPage.dispatchEventToFrontend("SavePageData", data);
3 }
4
5 TestPage.registerInitializer(() => {
6     InspectorTest.TimelineEvent = {};
7
8     InspectorTest.TimelineEvent.captureTimelineWithScript = function({expression, eventType}) {
9         let pageRecordingData = null;
10
11         let promise = new WI.WrappedPromise;
12
13         let listener = WI.timelineManager.addEventListener(WI.TimelineManager.Event.CapturingStateChanged, (event) => {
14             if (WI.timelineManager.capturingState === WI.TimelineManager.CapturingState.Active) {
15                 let recording = WI.timelineManager.activeRecording;
16                 let scriptTimeline = recording.timelines.get(WI.TimelineRecord.Type.Script);
17
18                 let recordAddedListener = scriptTimeline.addEventListener(WI.Timeline.Event.RecordAdded, (recordAddedEvent) => {
19                     let {record} = recordAddedEvent.data;
20                     if (record.eventType !== eventType)
21                         return;
22
23                     scriptTimeline.removeEventListener(WI.Timeline.Event.RecordAdded, recordAddedListener);
24
25                     InspectorTest.log("Stopping Capture...");
26                     WI.timelineManager.stopCapturing();
27                 });
28
29                 InspectorTest.log("Evaluating...");
30                 InspectorTest.evaluateInPage(expression)
31                 .catch(promise.reject);
32                 return;
33             }
34
35             if (WI.timelineManager.capturingState === WI.TimelineManager.CapturingState.Inactive) {
36                 WI.timelineManager.removeEventListener(WI.TimelineManager.Event.CapturingStateChanged, listener);
37                 InspectorTest.assert(pageRecordingData, "savePageData should have been called in the page before capturing was stopped.");
38                 promise.resolve(pageRecordingData);
39                 return;
40             }
41         });
42
43         InspectorTest.awaitEvent("SavePageData").then((event) => {
44             pageRecordingData = event.data;
45         });
46
47         InspectorTest.log("Starting Capture...");
48         const newRecording = true;
49         WI.timelineManager.startCapturing(newRecording);
50
51         return promise.promise;
52     }
53 });