fb3d0d7740957974a993ebc78df429d442221015
[WebKit-https.git] / LayoutTests / inspector / debugger / breakpoint-manager.html
1 <html>
2 <head>
3 <script src="../../http/tests/inspector/inspector-test.js"></script>
4
5 <script>
6
7 function test()
8 {
9     var uiSourceCodes = {};
10
11     var defaultMapping = {
12         rawLocationToUILocation: function(rawLocation)
13         {
14             return new WebInspector.UILocation(uiSourceCodes[rawLocation.scriptId], rawLocation.lineNumber, 0);
15         },
16
17         uiLocationToRawLocation: function(uiSourceCode, lineNumber)
18         {
19             return new WebInspector.DebuggerModel.Location(uiSourceCode.url, lineNumber, 0);
20         }
21     };
22
23     var shiftingMapping = {
24         rawLocationToUILocation: function(rawLocation)
25         {
26             return new WebInspector.UILocation(uiSourceCodes[rawLocation.scriptId], rawLocation.lineNumber + 10, 0);
27         },
28
29         uiLocationToRawLocation: function(uiSourceCode, lineNumber)
30         {
31             return new WebInspector.DebuggerModel.Location(uiSourceCode.url, lineNumber - 10, 0);
32         }
33     };
34
35     function DebuggerModelMock(sourceMapping)
36     {
37         this._scripts = {};
38         this._sourceMapping = sourceMapping;
39         this._breakpoints = {};
40     }
41
42     DebuggerModelMock.prototype = {
43         _addScript: function(scriptId, url)
44         {
45             this._scripts[scriptId] = new WebInspector.Script(scriptId, url);
46             this._scripts[scriptId].setSourceMapping(this._sourceMapping);
47         },
48
49         setBreakpointByScriptLocation: function(location, condition, callback)
50         {
51             var breakpointId = location.scriptId + ":" + location.lineNumber;
52             if (this._breakpoints[breakpointId]) {
53                 callback(null);
54                 return;
55             }
56
57             this._breakpoints[breakpointId] = true;
58             InspectorTest.addResult("    debuggerModel.setBreakpoint(" + [location.scriptId, location.lineNumber, condition].join(":") + ")");
59             if (!callback)
60                 return;
61             if (location.lineNumber >= 2000) {
62                 callback(breakpointId, []);
63                 return;
64             }
65             if (location.lineNumber >= 1000) {
66                 var shiftedLocation = {scriptId: location.scriptId, lineNumber: location.lineNumber + 10, columnNumber: location.columnNumber };
67                 callback(breakpointId, [shiftedLocation]);
68                 return;
69             }
70             callback(breakpointId, [location]);
71         },
72
73         removeBreakpoint: function(breakpointId, callback)
74         {
75             InspectorTest.addResult("    debuggerModel.removeBreakpoint(" + breakpointId + ")");
76             delete this._breakpoints[breakpointId];
77             if (callback)
78                 callback();
79         },
80
81         setBreakpointsActive: function() { },
82
83         createLiveLocation: function(rawLocation, updateDelegate)
84         {
85              return this._scripts[rawLocation.scriptId].createLiveLocation(rawLocation, updateDelegate);
86         },
87
88         scriptForId: function(scriptId)
89         {
90             return this._scripts[scriptId];
91         },
92
93         reset: function()
94         {
95             InspectorTest.addResult("  Resetting debugger.");
96             this._scripts = {};
97         },
98
99         setSourceMapping: function(sourceMapping)
100         {
101             for (var scriptId in this._scripts)
102                 this._scripts[scriptId].setSourceMapping(sourceMapping);
103         }
104     }
105     DebuggerModelMock.prototype.__proto__ = WebInspector.Object.prototype;
106
107     function WorkspaceMock()
108     {
109     }
110
111     WorkspaceMock.prototype = {
112         _addUISourceCode: function(url)
113         {
114             var uiSourceCode = new WebInspector.JavaScriptSource(url, null, null, defaultMapping, false);
115             uiSourceCodes[url] = uiSourceCode;
116             this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeAdded, uiSourceCode);
117             return uiSourceCode;
118         },
119
120         reset: function()
121         {
122             InspectorTest.addResult("  Resetting workspace.");
123             this.dispatchEventToListeners(WebInspector.Workspace.Events.ProjectWillReset);
124         }
125     }
126     WorkspaceMock.prototype.__proto__ = WebInspector.Object.prototype;
127
128     var dummySetting = {
129         get: function() { return []; },
130         set: function(breakpoints) { }
131     };
132
133     function breakpointAdded(event)
134     {
135         var breakpoint = event.data.breakpoint;
136         var uiLocation = event.data.uiLocation;
137         InspectorTest.addResult("    breakpointAdded(" + [uiLocation.uiSourceCode.url, uiLocation.lineNumber, breakpoint.condition(), breakpoint.enabled()].join(", ") + ")");
138     }
139
140     function breakpointRemoved(event)
141     {
142         var uiLocation = event.data.uiLocation;
143         InspectorTest.addResult("    breakpointRemoved(" + [uiLocation.uiSourceCode.url, uiLocation.lineNumber].join(", ") + ")");
144     }
145
146     InspectorTest.addSniffer(WebInspector.Script.prototype, "createLiveLocation", function(rawLocation)
147     {
148         InspectorTest.addResult("    Location created: " + rawLocation.scriptId + ":" + rawLocation.lineNumber);
149     }, true);
150     InspectorTest.addSniffer(WebInspector.Script.Location.prototype, "dispose", function()
151     {
152         InspectorTest.addResult("    Location disposed: " + this._rawLocation.scriptId + ":" + this._rawLocation.lineNumber);
153     }, true);
154
155     function addUISourceCode(breakpointManager, url)
156     {
157         breakpointManager._debuggerModel._addScript(url, url);
158         InspectorTest.addResult("  Adding UISourceCode: " + url + ")");
159         return breakpointManager._workspace._addUISourceCode(url);
160     }
161
162     function createBreakpoint(uiSourceCodeId, lineNumber, condition, enabled)
163     {
164         return { sourceFileId: uiSourceCodeId, lineNumber: lineNumber, condition: condition, enabled: enabled };
165     }
166
167     var serializedBreakpoints = [];
168     serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
169     serializedBreakpoints.push(createBreakpoint("a.js", 20, "", false));
170     serializedBreakpoints.push(createBreakpoint("b.js", 3, "", true));
171
172     function createBreakpointManager(persistentBreakpoints, sourceMapping)
173     {
174         persistentBreakpoints = persistentBreakpoints || [];
175         var setting = {
176             get: function() { return persistentBreakpoints; },
177             set: function(breakpoints) { persistentBreakpoints = breakpoints; }
178         };
179
180         var sourceMapping = sourceMapping || defaultMapping;
181         var debuggerModel = new DebuggerModelMock(sourceMapping);
182         var workspaceMock = new WorkspaceMock();
183         var breakpointManager = new WebInspector.BreakpointManager(setting, debuggerModel, workspaceMock);
184         breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointAdded, breakpointAdded);
185         breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointRemoved, breakpointRemoved);
186         InspectorTest.addResult("  Created breakpoints manager");
187         dumpBreakpointStorage(breakpointManager);
188         return breakpointManager;
189     }
190
191     function setBreakpoint(breakpointManager, uiSourceCode, lineNumber, condition, enabled)
192     {
193         InspectorTest.addResult("  Setting breakpoint at " + uiSourceCode.url + ":" + lineNumber + " enabled:" + enabled + " condition:" + condition);
194         return breakpointManager.setBreakpoint(uiSourceCode, lineNumber, condition, enabled);
195     }
196
197     function removeBreakpoint(breakpointManager, uiSourceCode, lineNumber)
198     {
199         InspectorTest.addResult("  Removing breakpoint at " + uiSourceCode.url + ":" + lineNumber);
200         breakpointManager.findBreakpoint(uiSourceCode, lineNumber).remove();
201     }
202
203     function dumpBreakpointStorage(breakpointManager)
204     {
205         var breakpoints = breakpointManager._storage._setting.get();
206         InspectorTest.addResult("  Dumping Storage");
207         for (var i = 0; i < breakpoints.length; ++i)
208             InspectorTest.addResult("    " + breakpoints[i].sourceFileId + ":" + breakpoints[i].lineNumber  + " enabled:" + breakpoints[i].enabled + " condition:" + breakpoints[i].condition);
209     }
210
211     function resetBreakpointManager(breakpointManager, next)
212     {
213         dumpBreakpointStorage(breakpointManager);
214         InspectorTest.addResult("  Resetting breakpoint manager");
215         breakpointManager.reset();
216         uiSourceCodes = {};
217         next();
218     }
219
220     InspectorTest.runTestSuite([
221         function testSetBreakpoint(next)
222         {
223             var breakpointManager = createBreakpointManager();
224             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
225             setBreakpoint(breakpointManager, uiSourceCode, 30, "", true);
226             resetBreakpointManager(breakpointManager, next);
227         },
228
229         function testSetDisabledBreakpoint(next)
230         {
231             var breakpointManager = createBreakpointManager();
232             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
233             var breakpoint = setBreakpoint(breakpointManager, uiSourceCode, 30, "", false);
234             dumpBreakpointStorage(breakpointManager);
235             InspectorTest.addResult("  Enabling breakpoint");
236             breakpoint.setEnabled(true);
237             resetBreakpointManager(breakpointManager, next);
238         },
239
240         function testSetConditionalBreakpoint(next)
241         {
242             var breakpointManager = createBreakpointManager();
243             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
244             var breakpoint = setBreakpoint(breakpointManager, uiSourceCode, 30, "condition", true);
245             dumpBreakpointStorage(breakpointManager);
246             InspectorTest.addResult("  Updating condition");
247             breakpoint.setCondition("");
248             resetBreakpointManager(breakpointManager, next);
249         },
250
251         function testRestoreBreakpoints(next)
252         {
253             var breakpointManager = createBreakpointManager(serializedBreakpoints);
254             addUISourceCode(breakpointManager, "a.js");
255             resetBreakpointManager(breakpointManager, next);
256         },
257
258         function testRestoreBreakpointsTwice(next)
259         {
260             var breakpointManager = createBreakpointManager(serializedBreakpoints);
261             addUISourceCode(breakpointManager, "a.js");
262             addUISourceCode(breakpointManager, "a.js");
263             resetBreakpointManager(breakpointManager, next);
264         },
265
266         function testRemoveBreakpoints(next)
267         {
268             var breakpointManager = createBreakpointManager(serializedBreakpoints);
269             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
270             setBreakpoint(breakpointManager, uiSourceCode, 30, "", true);
271
272             removeBreakpoint(breakpointManager, uiSourceCode, 30);
273             removeBreakpoint(breakpointManager, uiSourceCode, 10);
274             removeBreakpoint(breakpointManager, uiSourceCode, 20);
275             resetBreakpointManager(breakpointManager, next);
276         },
277
278         function testSetBreakpointThatShifts(next)
279         {
280             var breakpointManager = createBreakpointManager();
281             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
282             setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
283             resetBreakpointManager(breakpointManager, next);
284         },
285
286         function testSetBreakpointThatShiftsTwice(next)
287         {
288             var breakpointManager = createBreakpointManager();
289             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
290             setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
291             setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
292             resetBreakpointManager(breakpointManager, next);
293         },
294
295         function testSetBreakpointOutsideScript(next)
296         {
297             var breakpointManager = createBreakpointManager([]);
298             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
299             breakpointManager.setBreakpoint(uiSourceCode, 2500, "", true);
300             resetBreakpointManager(breakpointManager, next);
301         },
302
303         function testNavigation(next)
304         {
305             var breakpointManager = createBreakpointManager(serializedBreakpoints);
306             var uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
307
308             InspectorTest.addResult("\n  Navigating to B.");
309             breakpointManager._debuggerModel.reset();
310             breakpointManager._workspace.reset();
311             var uiSourceCodeB = addUISourceCode(breakpointManager, "b.js");
312
313             InspectorTest.addResult("\n  Navigating back to A.");
314             breakpointManager._debuggerModel.reset();
315             breakpointManager._workspace.reset();
316             InspectorTest.addResult("  Resolving provisional breakpoint.");
317             var eventData = { breakpointId: "a.js:10", location: { scriptId: "a.js", lineNumber: 11, columnNumber: 5 }};
318             breakpointManager._debuggerModel.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, eventData);
319             addUISourceCode(breakpointManager, "a.js");
320             resetBreakpointManager(breakpointManager, next);
321         },
322
323         function testSourceMapping(next)
324         {
325             // Source mapping will shift everthing 10 lines ahead so that breakpoint 1 clashed with breakpoint 2.
326             var serializedBreakpoints = [];
327             serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
328             serializedBreakpoints.push(createBreakpoint("a.js", 20, "", true));
329
330             var breakpointManager = createBreakpointManager(serializedBreakpoints);
331             var uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
332             InspectorTest.addResult("\n  Toggling source mapping.");
333             breakpointManager._sourceMapping = shiftingMapping;
334             breakpointManager._debuggerModel.setSourceMapping(shiftingMapping);
335             InspectorTest.addResult("\n  Toggling source mapping back.");
336             breakpointManager._sourceMapping = defaultMapping;
337             breakpointManager._debuggerModel.setSourceMapping(defaultMapping);
338             resetBreakpointManager(breakpointManager, next);
339         },
340
341         function testProvisionalBreakpointsResolve(next)
342         {
343             var serializedBreakpoints = [];
344             serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
345
346             var breakpointManager = createBreakpointManager(serializedBreakpoints);
347             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
348             breakpointManager._debuggerModel.reset();
349             breakpointManager._workspace.reset();
350             InspectorTest.addResult("  Resolving provisional breakpoint.");
351             var eventData = { breakpointId: "a.js:10", location: { scriptId: "a.js", lineNumber: 11, columnNumber: 5 }};
352             breakpointManager._debuggerModel.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, eventData);
353             resetBreakpointManager(breakpointManager, next);
354         }
355     ]);
356 };
357
358 </script>
359
360 </head>
361
362 <body onload="runTest()">
363 <p>Tests BreakpointManager class.</p>
364
365 </body>
366 </html>