[macOS debug] LayoutTest inspector/worker/resources-in-worker.html is a flaky timeout
[WebKit-https.git] / LayoutTests / inspector / dom-debugger / xhr-breakpoints.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script src="../debugger/resources/log-active-stack-trace.js"></script>
6 <script src="../worker/resources/resource-utilities.js"></script>
7 <script>
8 function test()
9 {
10     let suite = InspectorTest.createAsyncSuite("XHRBreakpoints");
11
12     function teardown(resolve) {
13         let breakpoints = WI.domDebuggerManager.xhrBreakpoints;
14         for (let breakpoint of breakpoints)
15             WI.domDebuggerManager.removeXHRBreakpoint(breakpoint);
16
17         WI.domDebuggerManager.allRequestsBreakpoint.disabled = true;
18         resolve();
19     }
20
21     function awaitEvaluateInPage(expression) {
22         return new Promise((resolve, reject) => {
23             InspectorTest.log("Wait for evaluate in page to return.");
24             InspectorTest.evaluateInPage(expression, (error) => {
25                 if (error)
26                     reject(error);
27
28                 resolve();
29             });
30         });
31     }
32
33     function rejectOnPause() {
34         return new Promise((resolve, reject) => {
35             WI.debuggerManager.awaitEvent(WI.DebuggerManager.Event.Paused)
36             .then((event) => {
37                 WI.debuggerManager.resume();
38                 InspectorTest.fail("Should not pause.");
39                 reject();
40             });
41         });
42     }
43
44     function addBreakpoint(type, url) {
45         return new Promise((resolve, reject) => {
46             let mainFrame = WI.frameResourceManager.mainFrame;
47             InspectorTest.assert(mainFrame, "Missing main frame.");
48             if (!mainFrame)
49                 reject();
50
51             let breakpoint = new WI.XHRBreakpoint(type, url);
52             WI.domDebuggerManager.awaitEvent(WI.DOMDebuggerManager.Event.XHRBreakpointAdded)
53             .then(() => {
54                 InspectorTest.pass("Added breakpoint for URL: " + url);
55                 InspectorTest.expectFalse(breakpoint.disabled, "Breakpoint should not be disabled initially.");
56                 resolve(breakpoint);
57             });
58
59             WI.domDebuggerManager.addXHRBreakpoint(breakpoint);
60         });
61     }
62
63     suite.addTestCase({
64         name: "BreakOnAllXMLHttpRequests",
65         description: "Check that debugger pauses for all XMLHttpRequests.",
66         teardown,
67         test(resolve, reject) {
68             InspectorTest.log("Set pause on all XMLHttpRequests.");
69             WI.domDebuggerManager.allRequestsBreakpoint.disabled = false;
70
71             InspectorTest.log("Sending XMLHttpRequest.");
72             InspectorTest.evaluateInPage(`loadResourceXHR("resources/dataXHR.json")`);
73
74             WI.debuggerManager.awaitEvent(WI.DebuggerManager.Event.Paused)
75             .then(() => {
76                 let targetData = WI.debuggerManager.dataForTarget(WI.debuggerManager.activeCallFrame.target);
77                 InspectorTest.log("PAUSED:");
78                 InspectorTest.expectEqual(targetData.pauseReason, WI.DebuggerManager.PauseReason.XHR, "Pause reason should be XHR.");
79                 InspectorTest.expectFalse(targetData.pauseData.breakpointURL, "Pause data URL should be empty.");
80
81                 logActiveStackTrace();
82
83                 return WI.debuggerManager.resume();
84             })
85             .then(resolve, reject);
86         }
87     });
88
89     suite.addTestCase({
90         name: "BreakOnXMLHttpRequestContainingText",
91         description: "Check that debugger pauses on a URL containing text.",
92         teardown,
93         test(resolve, reject) {
94             let expectedBreakpointURL;
95             addBreakpoint(WI.XHRBreakpoint.Type.Text, "data")
96             .then((breakpoint) => {
97                 expectedBreakpointURL = breakpoint.url;
98
99                 InspectorTest.log("Sending XMLHttpRequest.");
100                 InspectorTest.evaluateInPage(`loadResourceXHR("resources/dataXHR.json")`);
101                 return WI.debuggerManager.awaitEvent(WI.DebuggerManager.Event.Paused);
102             })
103             .then(() => {
104                 let targetData = WI.debuggerManager.dataForTarget(WI.debuggerManager.activeCallFrame.target);
105                 InspectorTest.log("PAUSED:");
106                 InspectorTest.expectEqual(targetData.pauseReason, WI.DebuggerManager.PauseReason.XHR, "Pause reason should be XHR.");
107                 InspectorTest.expectEqual(targetData.pauseData.breakpointURL, expectedBreakpointURL, "Pause data URL should match breakpoint URL.");
108
109                 logActiveStackTrace();
110
111                 return WI.debuggerManager.resume();
112             })
113             .then(resolve, reject);
114         }
115     });
116
117     suite.addTestCase({
118         name: "BreakOnXMLHttpRequestMatchingRegularExpression",
119         description: "Check that debugger pauses based on a URL matching a regex pattern.",
120         teardown,
121         test(resolve, reject) {
122             let expectedBreakpointURL;
123             addBreakpoint(WI.XHRBreakpoint.Type.RegularExpression, "data[A-Z]*\.(json|txt|png)")
124             .then((breakpoint) => {
125                 expectedBreakpointURL = breakpoint.url;
126
127                 InspectorTest.log("Sending XMLHttpRequest.");
128                 InspectorTest.evaluateInPage(`loadResourceXHR("resources/dataXHR.json")`);
129                 return WI.debuggerManager.awaitEvent(WI.DebuggerManager.Event.Paused);
130             })
131             .then(() => {
132                 let targetData = WI.debuggerManager.dataForTarget(WI.debuggerManager.activeCallFrame.target);
133                 InspectorTest.log("PAUSED:");
134                 InspectorTest.expectEqual(targetData.pauseReason, WI.DebuggerManager.PauseReason.XHR, "Pause reason should be XHR.");
135                 InspectorTest.expectEqual(targetData.pauseData.breakpointURL, expectedBreakpointURL, "Pause data URL should match breakpoint URL.");
136
137                 logActiveStackTrace();
138
139                 return WI.debuggerManager.resume();
140             })
141             .then(resolve, reject);
142         }
143     });
144
145     suite.addTestCase({
146         name: "ShouldNotPauseOnDisabledBreakpoint",
147         description: "Check that debugger does not pause for disabled breakpoint.",
148         teardown,
149         test(resolve, reject) {
150             addBreakpoint(WI.XHRBreakpoint.Type.Text, "data")
151             .then((breakpoint) => {
152                 breakpoint.disabled = true;
153                 InspectorTest.log("Breakpoint disabled.");
154
155                 InspectorTest.log("Sending XMLHttpRequest.");
156                 return Promise.race([awaitEvaluateInPage(`loadResourceXHR("resources/dataXHR.json")`), rejectOnPause()]);
157             })
158             .then(() => {
159                 InspectorTest.pass("Should not pause for disabled breakpoint.");
160                 resolve();
161             })
162             .catch(reject);
163         }
164     });
165
166     suite.addTestCase({
167         name: "ShouldNotPauseOnXMLHttpRequestNotContainingURL",
168         description: "Check that debugger does not pause for breakpoint with different URL.",
169         teardown,
170         test(resolve, reject) {
171             addBreakpoint(WI.XHRBreakpoint.Type.Text, "nonexistant-url")
172             .then((breakpoint) => {
173                 InspectorTest.log("Sending XMLHttpRequest.");
174                 return Promise.race([awaitEvaluateInPage(`loadResourceXHR("resources/dataXHR.json")`), rejectOnPause()]);
175             })
176             .then(() => {
177                 InspectorTest.pass("Should not pause for breakpoint with different URL.");
178                 resolve();
179             })
180             .catch(reject);
181         }
182     });
183
184     suite.addTestCase({
185         name: "RemoveBreakpoint",
186         description: "Check that debugger does not pause for removed breakpoint.",
187         teardown,
188         test(resolve, reject) {
189             addBreakpoint(WI.XHRBreakpoint.Type.Text, "data")
190             .then((breakpoint) => {
191                 WI.domDebuggerManager.removeXHRBreakpoint(breakpoint);
192                 InspectorTest.log("Breakpoint removed.");
193
194                 return Promise.race([awaitEvaluateInPage(`loadResourceXHR("resources/dataXHR.json")`), rejectOnPause()]);
195             })
196             .then(() => {
197                 InspectorTest.pass("Should not pause for removed breakpoint.");
198                 resolve();
199             })
200             .catch(reject);
201         }
202     });
203
204     suite.addTestCase({
205         name: "RemoveDisabledBreakpoint",
206         description: "Check that a disabled breakpoint can be removed.",
207         test(resolve, reject) {
208             addBreakpoint(WI.XHRBreakpoint.Type.Text, "data")
209             .then((breakpoint) => {
210                 breakpoint.disabled = true;
211                 InspectorTest.log("Breakpoint disabled.");
212
213                 WI.domDebuggerManager.removeXHRBreakpoint(breakpoint);
214                 InspectorTest.log("Breakpoint removed.");
215
216                 return Promise.race([awaitEvaluateInPage(`loadResourceXHR("resources/dataXHR.json")`), rejectOnPause()]);
217             })
218             .then(() => {
219                 InspectorTest.pass("Should not pause for removed disabled breakpoint.");
220                 resolve();
221             })
222             .catch(reject);
223         }
224     });
225
226     suite.runTestCasesAndFinish();
227 }
228 </script>
229 </head>
230 <body onload="runTest()">
231 <p>Tests for XHR breakpoints.</p>
232 </body>
233 </html>