Web Inspector: Timelines: can't reliably stop/start a recording
[WebKit-https.git] / LayoutTests / inspector / timeline / exception-in-injected-script-while-recording.html
1 <!doctype html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'">
5 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
6 <script src="./resources/timeline-helper.js"></script>
7 <script>
8 function installTimer()
9 {
10     setTimeout(function() {
11         callFunction(mul, add(1, 3), 3);
12         hook();
13     });
14 }
15
16 function add(a, b)
17 {
18     TestPage.addResult("Calling add(): " + a + " + " + b);
19     return a + b;
20 }
21
22 function mul(a, b)
23 {
24     TestPage.addResult("Calling mul(): " + a + " * " + b);
25     return a * b;
26 }
27
28 function test()
29 {
30     // First, set up the breakpoint, start timeline capturing, and trigger execution of installTimer().
31     WI.debuggerManager.addEventListener(WI.DebuggerManager.Event.ScriptAdded, function(event) {
32         var scriptObject = event.data.script;
33
34         if (!/timeline-helper\.js$/.test(scriptObject.url))
35             return;
36
37         var location = scriptObject.createSourceCodeLocation(17, 0);  // Inside timeline-helper.js:hook()
38         var breakpoint = new WI.Breakpoint(location);
39         WI.debuggerManager.addBreakpoint(breakpoint);
40         InspectorTest.addResult("Added a breakpoint inside hook().")
41
42         WI.timelineManager.startCapturing();
43         InspectorTest.evaluateInPage("installTimer()");
44     });
45
46     // Second, the debugger will pause during timeline capturing. Resume, then stop timeline capturing.
47     WI.debuggerManager.addEventListener(WI.DebuggerManager.Event.Paused, function(event) {
48         InspectorTest.addResult("Debugger paused;");
49         checkIfExceptionLoopsForever();
50     });
51
52     function checkIfExceptionLoopsForever() {
53         WI.runtimeManager.evaluateInInspectedWindow("({}).x.x", {objectGroup: "test", includeCommandLineAPI: true, doNotPauseOnExceptionsAndMuteConsole: true}, function(result, wasThrown) {
54             InspectorTest.addResult("An exception was " + (wasThrown ? "" : "not ") + "thrown from the injected script.");
55             WI.debuggerManager.resume().then(function() {
56                 InspectorTest.addResult("Debugger resumed; stopping timeline capture.");
57                 WI.timelineManager.stopCapturing();
58             });
59         });
60     }
61
62     // When timeline capturing stops, inspect the resulting timeline records for a profile.
63     WI.timelineManager.addEventListener(WI.TimelineManager.Event.CapturingStateChanged, (event) => {
64         if (WI.timelineManager.capturingState !== WI.TimelineManager.CapturingState.Inactive)
65             return;
66
67         var recording = WI.timelineManager.activeRecording;
68         var scriptTimeline = recording.timelines.get(WI.TimelineRecord.Type.Script);
69         console.assert(scriptTimeline);
70
71         InspectorTest.addResult("Timeline capturing stopped. Inspecting the active recording....");
72
73         for (var record of scriptTimeline.records) {
74             if (record.eventType !== WI.ScriptTimelineRecord.EventType.TimerFired)
75                 continue;
76
77             var result = record.profile ? "TRUE" : "FALSE";
78             InspectorTest.addResult("TimerFired timeline record has profile attached: " + result);
79         }
80
81         InspectorTest.completeTest();
82     });
83
84     InspectorTest.reloadPage();
85 }
86 </script>
87 </head>
88 <body onload="runTest()">
89     <p>Testing that an injected script, run while the debugger is paused and the timeline profiler is enabled, should not loop forever if it has an exception thrown.</p>
90 </body>
91 </html>