Unreviewed, rolling out r143100.
[WebKit-https.git] / LayoutTests / inspector / debugger / live-edit-breakpoints.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4 <script src="../../http/tests/inspector/debugger-test.js"></script>
5 <script src="../../http/tests/inspector/live-edit-test.js"></script>
6 <script src="resources/edit-me-breakpoints.js"></script>
7 <script>
8 function loadDynamicAnonymousScript()
9 {
10     function testFunction()
11     {
12         debugger;
13     }
14     var scriptElement = document.createElement("script");
15     scriptElement.textContent = String(testFunction);
16     docuemnt.head.appendChild(scriptElement);
17 }
18
19 function test()
20 {
21     var panel = WebInspector.showPanel("scripts");  
22
23     function pathToFileName(path)
24     {
25         return path.substring(path.lastIndexOf("/") + 1);
26     }
27
28     function dumpBreakpointStorageAndLocations()
29     {
30         var breakpointManager = WebInspector.breakpointManager;
31         var breakpoints = breakpointManager._storage._setting.get();
32         InspectorTest.addResult("    Dumping breakpoint storage");
33         for (var i = 0; i < breakpoints.length; ++i)
34             InspectorTest.addResult("        " + pathToFileName(breakpoints[i].sourceFileId) + ":" + breakpoints[i].lineNumber + ", enabled:" + breakpoints[i].enabled);
35
36         var breakpointManager = WebInspector.breakpointManager;
37         locations = breakpointManager.allBreakpointLocations();
38         InspectorTest.addResult("    Dumping breakpoint locations");
39         for (var i = 0; i < locations.length; ++i) {
40             var uiLocation = locations[i].uiLocation;
41             var uiSourceCode = uiLocation.uiSourceCode;
42             var project = WebInspector.workspace.projectForUISourceCode(uiSourceCode);
43             InspectorTest.addResult("        url: " + pathToFileName(uiSourceCode.originURL()) + ", lineNumber: " + uiLocation.lineNumber + ", project name: " + project.id());
44         }
45     }
46
47     function addBreakpointSniffer(lineNumber, disabled)
48     {
49         var prefix = this.main ? "" : "Original";
50         InspectorTest.addResult("    " + prefix + "TextEditor.addBreakpoint(lineNumber = " + lineNumber + ", disabled = " + disabled + ")");
51     }
52
53     function removeBreakpointSniffer(lineNumber)
54     {
55         var prefix = this.main ? "" : "Original";
56         InspectorTest.addResult("    " + prefix + "TextEditor.removeBreakpoint(lineNumber = " + lineNumber + ")");
57     }
58
59     WebInspector.breakpointManager._storage._breakpoints = {};
60     InspectorTest.addSniffer(WebInspector.DefaultTextEditor.prototype, "addBreakpoint", addBreakpointSniffer, true);
61     InspectorTest.addSniffer(WebInspector.DefaultTextEditor.prototype, "removeBreakpoint", removeBreakpointSniffer, true);
62
63     InspectorTest.runDebuggerTestSuite([
64         function testEditUndo(next)
65         {
66             var javaScriptSourceFrame, uiSourceCode, script, originalJavaScriptSourceFrame, originalUISourceCode;
67
68             InspectorTest.showScriptSource("edit-me-breakpoints.js", didShowScriptSource);
69
70             function didShowScriptSource(sourceFrame)
71             {
72                 javaScriptSourceFrame = sourceFrame;
73                 uiSourceCode = sourceFrame._uiSourceCode;
74                 javaScriptSourceFrame._textEditor.main = true;
75                 
76                 InspectorTest.addResult("Setting breakpoint:");
77                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
78                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
79             }
80
81             function breakpointResolved(location)
82             {
83                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
84
85                 dumpBreakpointStorageAndLocations();
86                 InspectorTest.addResult("Editing source:");
87                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "}//");
88
89                 originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
90                 InspectorTest.showUISourceCode(originalUISourceCode, didShowOriginalUISourceCode);
91             }
92
93             function didShowOriginalUISourceCode(sourceFrame)
94             {
95                 originalJavaScriptSourceFrame = sourceFrame;
96                 InspectorTest.assertTrue(originalJavaScriptSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
97                 InspectorTest.assertTrue(originalUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
98
99                 dumpBreakpointStorageAndLocations();
100                 InspectorTest.addResult("Undoing source editing:");
101                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolvedAgain);
102                 InspectorTest.undoSourceEditing(javaScriptSourceFrame);
103             }
104
105             function breakpointResolvedAgain()
106             {
107                 dumpBreakpointStorageAndLocations();
108                 InspectorTest.addResult("Finally removing breakpoint:");
109                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
110
111                 dumpBreakpointStorageAndLocations();
112                 next();
113             }
114         },
115
116         function testEditCommit(next)
117         {
118             var javaScriptSourceFrame, uiSourceCode, script, originalJavaScriptSourceFrame, originalUISourceCode;
119
120             InspectorTest.showScriptSource("edit-me-breakpoints.js", didShowScriptSource);
121
122             function didShowScriptSource(sourceFrame)
123             {
124                 javaScriptSourceFrame = sourceFrame;
125                 uiSourceCode = sourceFrame._uiSourceCode;
126                 
127                 InspectorTest.addResult("Setting breakpoint:");
128                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
129                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
130             }
131
132             function breakpointResolved(location)
133             {
134                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
135
136                 dumpBreakpointStorageAndLocations();
137                 InspectorTest.addResult("Editing source:");
138                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "}//");
139
140                 originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
141                 InspectorTest.showUISourceCode(originalUISourceCode, didShowOriginalUISourceCode);
142             }
143
144             function didShowOriginalUISourceCode(sourceFrame)
145             {
146                 originalJavaScriptSourceFrame = sourceFrame;
147                 InspectorTest.assertTrue(originalJavaScriptSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
148                 InspectorTest.assertTrue(originalUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
149
150                 dumpBreakpointStorageAndLocations();
151                 InspectorTest.addResult("Committing edited source:");
152                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolvedAgain);
153                 InspectorTest.commitSource(javaScriptSourceFrame);
154             }
155
156             function breakpointResolvedAgain()
157             {
158                 dumpBreakpointStorageAndLocations();
159                 InspectorTest.addResult("Finally removing breakpoint:");
160                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
161
162                 dumpBreakpointStorageAndLocations();
163                 next();
164             }
165         },
166
167         function testEditCommitFailEditCommit(next)
168         {
169             var javaScriptSourceFrame, uiSourceCode, script, originalJavaScriptSourceFrame, originalUISourceCode;
170
171             InspectorTest.showScriptSource("edit-me-breakpoints.js", didShowScriptSource);
172
173             function didShowScriptSource(sourceFrame)
174             {
175                 javaScriptSourceFrame = sourceFrame;
176                 uiSourceCode = sourceFrame._uiSourceCode;
177                 
178                 InspectorTest.addResult("Setting breakpoint:");
179                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
180                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
181             }
182
183             function breakpointResolved(location)
184             {
185                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
186
187                 dumpBreakpointStorageAndLocations();
188                 InspectorTest.addResult("Editing source:");
189                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "//}");
190
191                 originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
192                 InspectorTest.showUISourceCode(originalUISourceCode, didShowOriginalUISourceCode);
193             }
194
195             function didShowOriginalUISourceCode(sourceFrame)
196             {
197                 originalJavaScriptSourceFrame = sourceFrame;
198                 InspectorTest.assertTrue(originalJavaScriptSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
199                 InspectorTest.assertTrue(originalUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
200
201                 dumpBreakpointStorageAndLocations();
202                 InspectorTest.addResult("Committing edited source:");
203                 InspectorTest.addSniffer(WebInspector.debuggerModel, "_didEditScriptSource", commitFailed);
204                 InspectorTest.commitSource(javaScriptSourceFrame);
205             }
206
207             function commitFailed(error)
208             {
209                 InspectorTest.assertTrue(!!error, "Commit should have failed.");
210                 dumpBreakpointStorageAndLocations();
211                 InspectorTest.addResult("Editing source again so that live edit could succeed:");
212                 InspectorTest.replaceInSource(javaScriptSourceFrame, "//}", "}//");
213
214                 dumpBreakpointStorageAndLocations();
215                 InspectorTest.addResult("Committing edited source again:");
216                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolvedAgain);
217                 InspectorTest.commitSource(javaScriptSourceFrame);
218             }
219
220             function breakpointResolvedAgain()
221             {
222                 dumpBreakpointStorageAndLocations();
223                 InspectorTest.addResult("Finally removing breakpoint:");
224                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
225
226                 dumpBreakpointStorageAndLocations();
227                 next();
228             }
229         },
230
231         function testEditCommitFailUndoCommit(next)
232         {
233             var javaScriptSourceFrame, uiSourceCode, script, originalJavaScriptSourceFrame, originalUISourceCode;
234
235             InspectorTest.showScriptSource("edit-me-breakpoints.js", didShowScriptSource);
236
237             function didShowScriptSource(sourceFrame)
238             {
239                 javaScriptSourceFrame = sourceFrame;
240                 uiSourceCode = sourceFrame._uiSourceCode;
241                 
242                 InspectorTest.addResult("Setting breakpoint:");
243                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
244                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
245             }
246
247             function breakpointResolved(location)
248             {
249                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
250
251                 dumpBreakpointStorageAndLocations();
252                 InspectorTest.addResult("Editing source:");
253                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "//}");
254
255                 originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
256                 InspectorTest.showUISourceCode(originalUISourceCode, didShowOriginalUISourceCode);
257             }
258
259             function didShowOriginalUISourceCode(sourceFrame)
260             {
261                 originalJavaScriptSourceFrame = sourceFrame;
262                 InspectorTest.assertTrue(originalJavaScriptSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
263                 InspectorTest.assertTrue(originalUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
264
265                 dumpBreakpointStorageAndLocations();
266                 InspectorTest.addResult("Committing edited source:");
267                 InspectorTest.addSniffer(WebInspector.debuggerModel, "_didEditScriptSource", commitFailed);
268                 InspectorTest.commitSource(javaScriptSourceFrame);
269             }
270
271             function commitFailed(error)
272             {
273                 InspectorTest.assertTrue(!!error, "Commit should have failed.");
274                 dumpBreakpointStorageAndLocations();
275                 InspectorTest.addResult("Undoing source editing:");
276                 InspectorTest.undoSourceEditing(javaScriptSourceFrame);
277
278                 dumpBreakpointStorageAndLocations();
279                 InspectorTest.addResult("Committing edited source again:");
280                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolvedAgain);
281                 InspectorTest.commitSource(javaScriptSourceFrame);
282             }
283
284             function breakpointResolvedAgain()
285             {
286                 dumpBreakpointStorageAndLocations();
287                 InspectorTest.addResult("Finally removing breakpoint:");
288                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
289
290                 dumpBreakpointStorageAndLocations();
291                 next();
292             }
293         },
294
295         function testEditAnonymousUndo(next)
296         {
297             var javaScriptSourceFrame, uiSourceCode, script, liveEditSourceFrame, liveEditUISourceCode;
298             
299             function testFunction()
300             {
301                 debugger;
302             }
303             InspectorTest.evaluateInPage(String(testFunction), InspectorTest.runTestFunctionAndWaitUntilPaused.bind(InspectorTest, paused));
304             function paused()
305             {
306                 InspectorTest.resumeExecution(resumed);
307             }
308
309             function resumed()
310             {
311                 InspectorTest.addResult("Showing script source:");
312                 InspectorTest.showUISourceCode(panel.visibleView._uiSourceCode, didShowScriptSource);
313             }
314
315             function didShowScriptSource(sourceFrame)
316             {
317                 javaScriptSourceFrame = sourceFrame;
318                 uiSourceCode = sourceFrame._uiSourceCode;
319                 javaScriptSourceFrame._textEditor.main = true;
320                 
321                 InspectorTest.addResult("Setting breakpoint:");
322                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
323                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
324             }
325
326             function breakpointResolved(location)
327             {
328                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
329
330                 dumpBreakpointStorageAndLocations();
331                 liveEditUISourceCode = WebInspector.liveEditSupport.uiSourceCodeForLiveEdit(uiSourceCode);
332                 InspectorTest.showUISourceCode(liveEditUISourceCode, didShowLiveEditScriptSource);
333             }
334
335             function didShowLiveEditScriptSource(sourceFrame)
336             {
337                 liveEditSourceFrame = sourceFrame;
338                 InspectorTest.assertTrue(liveEditSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
339                 InspectorTest.assertTrue(liveEditUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
340                 InspectorTest.addResult("Editing source:");
341                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "}//");
342                 dumpBreakpointStorageAndLocations();
343
344                 var originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
345                 InspectorTest.assertTrue(originalUISourceCode === uiSourceCode, "Mapped and original uiSourceCodes should be the same.");
346                 InspectorTest.addResult("Undoing source editing:");
347                 InspectorTest.undoSourceEditing(liveEditSourceFrame);
348                 dumpBreakpointStorageAndLocations();
349                 InspectorTest.showUISourceCode(uiSourceCode, didShowOriginalUISourceCode);
350             }
351
352             function didShowOriginalUISourceCode(sourceFrame)
353             {
354                 InspectorTest.addResult("Finally removing breakpoint:");
355                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
356                 dumpBreakpointStorageAndLocations();
357                 next();
358             }
359         },
360
361         function testEditDynamicAnonymousUndo(next)
362         {
363             var javaScriptSourceFrame, uiSourceCode, script, liveEditSourceFrame, liveEditUISourceCode;
364             
365             InspectorTest.evaluateInPage("loadDynamicAnonymousScript()", InspectorTest.runTestFunctionAndWaitUntilPaused.bind(InspectorTest, paused));
366             function paused()
367             {
368                 InspectorTest.resumeExecution(resumed);
369             }
370
371             function resumed()
372             {
373                 InspectorTest.addResult("Showing script source:");
374                 InspectorTest.showUISourceCode(panel.visibleView._uiSourceCode, didShowScriptSource);
375             }
376
377             function didShowScriptSource(sourceFrame)
378             {
379                 javaScriptSourceFrame = sourceFrame;
380                 uiSourceCode = sourceFrame._uiSourceCode;
381                 javaScriptSourceFrame._textEditor.main = true;
382                 
383                 InspectorTest.addResult("Setting breakpoint:");
384                 InspectorTest.addSniffer(WebInspector.BreakpointManager.Breakpoint.prototype, "_addResolvedLocation", breakpointResolved);
385                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
386             }
387
388             function breakpointResolved(location)
389             {
390                 script = WebInspector.debuggerModel.scriptForId(location.scriptId);
391
392                 dumpBreakpointStorageAndLocations();
393                 liveEditUISourceCode = WebInspector.liveEditSupport.uiSourceCodeForLiveEdit(uiSourceCode);
394                 InspectorTest.showUISourceCode(liveEditUISourceCode, didShowLiveEditScriptSource);
395             }
396
397             function didShowLiveEditScriptSource(sourceFrame)
398             {
399                 liveEditSourceFrame = sourceFrame;
400                 InspectorTest.assertTrue(liveEditSourceFrame !== javaScriptSourceFrame, "Edited and original javaScriptSourceFrames should differ.");
401                 InspectorTest.assertTrue(liveEditUISourceCode !== uiSourceCode, "Edited and original uiSourceCodes should differ.");
402                 InspectorTest.addResult("Editing source:");
403                 InspectorTest.replaceInSource(javaScriptSourceFrame, "}", "}//");
404                 dumpBreakpointStorageAndLocations();
405
406                 var originalUISourceCode = script.rawLocationToUILocation(location.lineNumber).uiSourceCode;
407                 InspectorTest.assertTrue(originalUISourceCode === uiSourceCode, "Mapped and original uiSourceCodes should be the same.");
408                 InspectorTest.addResult("Undoing source editing:");
409                 InspectorTest.undoSourceEditing(liveEditSourceFrame);
410                 dumpBreakpointStorageAndLocations();
411                 InspectorTest.showUISourceCode(uiSourceCode, didShowOriginalUISourceCode);
412             }
413
414             function didShowOriginalUISourceCode(sourceFrame)
415             {
416                 InspectorTest.addResult("Finally removing breakpoint:");
417                 InspectorTest.removeBreakpoint(javaScriptSourceFrame, 2);
418                 dumpBreakpointStorageAndLocations();
419                 next();
420             }
421         },
422     ]);
423 };
424
425 </script>
426 </head>
427 <body onload="runTest()">
428 <p>Tests breakpoints are correctly dimmed and restored in JavaScriptSourceFrame during live edit.</p>
429 <a href="https://bugs.webkit.org/show_bug.cgi?id=99598">Bug 99598</a>
430 </body>
431 </html>