Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / 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             if (!uiSourceCodes[uiSourceCode.url])
20                 return null;
21             return new WebInspector.DebuggerModel.Location(uiSourceCode.url, lineNumber, 0);
22         }
23     };
24
25     var shiftingMapping = {
26         rawLocationToUILocation: function(rawLocation)
27         {
28             if (this._disabled)
29                 return null;
30             return new WebInspector.UILocation(uiSourceCodes[rawLocation.scriptId], rawLocation.lineNumber + 10, 0);
31         },
32
33         uiLocationToRawLocation: function(uiSourceCode, lineNumber)
34         {
35             return new WebInspector.DebuggerModel.Location(uiSourceCode.url, lineNumber - 10, 0);
36         }
37     };
38
39     function createSourceMapping(uiSourceCodeA, uiSourceCodeB)
40     {
41         var mapping = {
42             rawLocationToUILocation: function(rawLocation)
43             {
44                 if (this._disabled)
45                     return null;
46                 return new WebInspector.UILocation(uiSourceCodeB, rawLocation.lineNumber + 10, 0);
47             },
48
49             uiLocationToRawLocation: function(uiSourceCode, lineNumber)
50             {
51                 return new WebInspector.DebuggerModel.Location(uiSourceCodeA.url, lineNumber - 10, 0);
52             }
53         };
54         
55         return mapping;
56     }
57
58     function DebuggerModelMock(sourceMapping)
59     {
60         this._scripts = {};
61         this._sourceMapping = sourceMapping;
62         this._breakpoints = {};
63     }
64
65     DebuggerModelMock.prototype = {
66         _addScript: function(scriptId, url)
67         {
68             this._scripts[scriptId] = new WebInspector.Script(scriptId, url);
69             this._scripts[scriptId].pushSourceMapping(this._sourceMapping);
70         },
71
72         _scriptForURL: function(url)
73         {
74             for (var scriptId in this._scripts) {
75                 var script = this._scripts[scriptId];
76                 if (script.sourceURL === url)
77                     return script;
78             }
79        },
80
81         _scheduleSetBeakpointCallback: function(callback, breakpointId, locations)
82         {
83             setTimeout(innerCallback.bind(this), 0);
84
85             function innerCallback()
86             {
87                 if (callback)
88                     callback(breakpointId, locations);
89                 if (window.setBreakpointCallback) {
90                     var savedCallback = window.setBreakpointCallback;
91                     delete window.setBreakpointCallback;
92                     savedCallback();
93                 }
94             }
95         },
96
97         setBreakpointByURL: function(url, lineNumber, columnNumber, condition, callback)
98         {
99             InspectorTest.addResult("    debuggerModel.setBreakpoint(" + [url, lineNumber, condition].join(":") + ")");
100
101             var breakpointId = url + ":" + lineNumber;
102             if (this._breakpoints[breakpointId]) {
103                 this._scheduleSetBeakpointCallback(callback, null);
104                 return;
105             }
106             this._breakpoints[breakpointId] = true;
107
108             var locations = [];
109             var script = this._scriptForURL(url);
110             if (script) {
111                 var location = new WebInspector.DebuggerModel.Location(script.scriptId, lineNumber, 0);
112                 locations.push(location);
113             }
114
115             this._scheduleSetBeakpointCallback(callback, breakpointId, locations);
116         },
117
118         setBreakpointByScriptLocation: function(location, condition, callback)
119         {
120             InspectorTest.addResult("    debuggerModel.setBreakpoint(" + [location.scriptId, location.lineNumber, condition].join(":") + ")");
121
122             var breakpointId = location.scriptId + ":" + location.lineNumber;
123             if (this._breakpoints[breakpointId]) {
124                 this._scheduleSetBeakpointCallback(callback, null);
125                 return;
126             }
127             this._breakpoints[breakpointId] = true;
128
129             if (location.lineNumber >= 2000) {
130                 this._scheduleSetBeakpointCallback(callback, breakpointId, []);
131                 return;
132             }
133             if (location.lineNumber >= 1000) {
134                 var shiftedLocation = {scriptId: location.scriptId, lineNumber: location.lineNumber + 10, columnNumber: location.columnNumber };
135                 this._scheduleSetBeakpointCallback(callback, breakpointId, [shiftedLocation]);
136                 return;
137             }
138
139             this._scheduleSetBeakpointCallback(callback, breakpointId, [location]);
140         },
141
142         removeBreakpoint: function(breakpointId, callback)
143         {
144             InspectorTest.addResult("    debuggerModel.removeBreakpoint(" + breakpointId + ")");
145             delete this._breakpoints[breakpointId];
146             if (callback)
147                 callback();
148         },
149
150         setBreakpointsActive: function() { },
151
152         createLiveLocation: function(rawLocation, updateDelegate)
153         {
154              return this._scripts[rawLocation.scriptId].createLiveLocation(rawLocation, updateDelegate);
155         },
156
157         scriptForId: function(scriptId)
158         {
159             return this._scripts[scriptId];
160         },
161
162         reset: function()
163         {
164             InspectorTest.addResult("  Resetting debugger.");
165             this._scripts = {};
166         },
167
168         pushSourceMapping: function(sourceMapping)
169         {
170             for (var scriptId in this._scripts)
171                 this._scripts[scriptId].pushSourceMapping(sourceMapping);
172         },
173
174         disableSourceMapping: function(sourceMapping)
175         {
176             sourceMapping._disabled = true;
177             for (var scriptId in this._scripts)
178                 this._scripts[scriptId].updateLocations();
179         }
180     }
181     DebuggerModelMock.prototype.__proto__ = WebInspector.Object.prototype;
182
183     var dummySetting = {
184         get: function() { return []; },
185         set: function(breakpoints) { }
186     };
187
188     function resetWorkspace(breakpointManager)
189     {
190         InspectorTest.addResult("  Resetting workspace.");
191         breakpointManager._networkWorkspaceProvider.reset();
192         breakpointManager._debuggerWorkspaceProvider.reset();
193     }
194
195     function breakpointAdded(event)
196     {
197         var breakpoint = event.data.breakpoint;
198         var uiLocation = event.data.uiLocation;
199         InspectorTest.addResult("    breakpointAdded(" + [uiLocation.uiSourceCode.originURL(), uiLocation.lineNumber, breakpoint.condition(), breakpoint.enabled()].join(", ") + ")");
200     }
201
202     function breakpointRemoved(event)
203     {
204         var uiLocation = event.data.uiLocation;
205         InspectorTest.addResult("    breakpointRemoved(" + [uiLocation.uiSourceCode.originURL(), uiLocation.lineNumber].join(", ") + ")");
206     }
207
208     InspectorTest.addSniffer(WebInspector.Script.prototype, "createLiveLocation", function(rawLocation)
209     {
210         InspectorTest.addResult("    Location created: " + rawLocation.scriptId + ":" + rawLocation.lineNumber);
211     }, true);
212     InspectorTest.addSniffer(WebInspector.Script.Location.prototype, "dispose", function()
213     {
214         InspectorTest.addResult("    Location disposed: " + this._rawLocation.scriptId + ":" + this._rawLocation.lineNumber);
215     }, true);
216
217     function addUISourceCode(breakpointManager, url, doNotSetSourceMapping)
218     {
219         breakpointManager._debuggerModel._addScript(url, url);
220         InspectorTest.addResult("  Adding UISourceCode: " + url);
221         var contentProvider = new WebInspector.StaticContentProvider(WebInspector.resourceTypes.Script, "");
222         var uiSourceCode = breakpointManager._networkWorkspaceProvider.addFileForURL(url, contentProvider, true);
223         uiSourceCodes[url] = uiSourceCode;
224         if (!doNotSetSourceMapping)
225             uiSourceCode.setSourceMapping(defaultMapping);
226         return uiSourceCode;
227     }
228
229     function addTemporaryUISourceCode(breakpointManager, url)
230     {
231
232         breakpointManager._debuggerModel._addScript(url, url);
233         InspectorTest.addResult("  Adding temporary UISourceCode: " + url);
234         var contentProvider = new WebInspector.StaticContentProvider(WebInspector.resourceTypes.Script, "");
235         var uiSourceCode = breakpointManager._debuggerWorkspaceProvider.addUniqueFileForURL(url, contentProvider, true);
236         uiSourceCode.setSourceMapping(defaultMapping);
237         uiSourceCodes[url] = uiSourceCode;
238         return uiSourceCode;
239     }
240
241     function createBreakpoint(uiSourceCodeId, lineNumber, condition, enabled)
242     {
243         return { sourceFileId: uiSourceCodeId, lineNumber: lineNumber, condition: condition, enabled: enabled };
244     }
245
246     var serializedBreakpoints = [];
247     serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
248     serializedBreakpoints.push(createBreakpoint("a.js", 20, "", false));
249     serializedBreakpoints.push(createBreakpoint("b.js", 3, "", true));
250
251     function createBreakpointManager(persistentBreakpoints, sourceMapping)
252     {
253         persistentBreakpoints = persistentBreakpoints || [];
254         var setting = {
255             get: function() { return persistentBreakpoints; },
256             set: function(breakpoints) { persistentBreakpoints = breakpoints; }
257         };
258
259         var sourceMapping = sourceMapping || defaultMapping;
260         var debuggerModel = new DebuggerModelMock(sourceMapping);
261         var workspace = new WebInspector.Workspace();
262         var breakpointManager = new WebInspector.BreakpointManager(setting, debuggerModel, workspace);
263         breakpointManager._networkWorkspaceProvider = new WebInspector.SimpleWorkspaceProvider(workspace, WebInspector.projectTypes.Network);
264         breakpointManager._debuggerWorkspaceProvider = new WebInspector.SimpleWorkspaceProvider(workspace, WebInspector.projectTypes.Debugger);
265         breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointAdded, breakpointAdded);
266         breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointRemoved, breakpointRemoved);
267         InspectorTest.addResult("  Created breakpoints manager");
268         dumpBreakpointStorage(breakpointManager);
269         return breakpointManager;
270     }
271
272     function setBreakpoint(breakpointManager, uiSourceCode, lineNumber, condition, enabled)
273     {
274         InspectorTest.addResult("  Setting breakpoint at " + uiSourceCode.originURL() + ":" + lineNumber + " enabled:" + enabled + " condition:" + condition);
275         return breakpointManager.setBreakpoint(uiSourceCode, lineNumber, condition, enabled);
276     }
277
278     function removeBreakpoint(breakpointManager, uiSourceCode, lineNumber)
279     {
280         InspectorTest.addResult("  Removing breakpoint at " + uiSourceCode.originURL() + ":" + lineNumber);
281         breakpointManager.findBreakpoint(uiSourceCode, lineNumber).remove();
282     }
283
284     function dumpBreakpointStorage(breakpointManager)
285     {
286         var breakpoints = breakpointManager._storage._setting.get();
287         InspectorTest.addResult("  Dumping Storage");
288         for (var i = 0; i < breakpoints.length; ++i)
289             InspectorTest.addResult("    " + breakpoints[i].sourceFileId + ":" + breakpoints[i].lineNumber  + " enabled:" + breakpoints[i].enabled + " condition:" + breakpoints[i].condition);
290     }
291
292     function resetBreakpointManager(breakpointManager, next)
293     {
294         dumpBreakpointStorage(breakpointManager);
295         InspectorTest.addResult("  Resetting breakpoint manager");
296         breakpointManager.removeAllBreakpoints();
297         breakpointManager.removeProvisionalBreakpoints();
298         uiSourceCodes = {};
299         next();
300     }
301
302     InspectorTest.runTestSuite([
303         function testSetBreakpoint(next)
304         {
305             var breakpointManager = createBreakpointManager();
306             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
307             setBreakpoint(breakpointManager, uiSourceCode, 30, "", true);
308             window.setBreakpointCallback = step2.bind(this);
309
310             function step2()
311             {
312                 resetBreakpointManager(breakpointManager, next);
313             }
314         },
315
316         function testSetDisabledBreakpoint(next)
317         {
318             var breakpointManager = createBreakpointManager();
319             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
320             var breakpoint = setBreakpoint(breakpointManager, uiSourceCode, 30, "", false);
321             dumpBreakpointStorage(breakpointManager);
322             InspectorTest.addResult("  Enabling breakpoint");
323             breakpoint.setEnabled(true);
324             window.setBreakpointCallback = step2.bind(this);
325
326             function step2()
327             {
328                 resetBreakpointManager(breakpointManager, next);
329             }
330         },
331
332         function testSetConditionalBreakpoint(next)
333         {
334             var breakpointManager = createBreakpointManager();
335             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
336             var breakpoint = setBreakpoint(breakpointManager, uiSourceCode, 30, "condition", true);
337             window.setBreakpointCallback = step2.bind(this);
338
339             function step2()
340             {
341                 dumpBreakpointStorage(breakpointManager);
342                 InspectorTest.addResult("  Updating condition");
343                 breakpoint.setCondition("");
344                 window.setBreakpointCallback = step3.bind(this);
345             }
346
347             function step3()
348             {
349                 resetBreakpointManager(breakpointManager, next);
350             }
351         },
352
353         function testRestoreBreakpoints(next)
354         {
355             var breakpointManager = createBreakpointManager(serializedBreakpoints);
356             addUISourceCode(breakpointManager, "a.js");
357             window.setBreakpointCallback = step2.bind(this);
358
359             function step2()
360             {
361                 resetBreakpointManager(breakpointManager, next);
362             }
363         },
364
365         function testRestoreBreakpointsTwice(next)
366         {
367             var breakpointManager = createBreakpointManager(serializedBreakpoints);
368             addUISourceCode(breakpointManager, "a.js");
369             addUISourceCode(breakpointManager, "a.js");
370             window.setBreakpointCallback = step2.bind(this);
371
372             function step2()
373             {
374                 resetBreakpointManager(breakpointManager, next);
375             }
376         },
377
378         function testRemoveBreakpoints(next)
379         {
380             var breakpointManager = createBreakpointManager(serializedBreakpoints);
381             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
382             window.setBreakpointCallback = step2.bind(this);
383
384             function step2()
385             {
386                 setBreakpoint(breakpointManager, uiSourceCode, 30, "", true);
387                 window.setBreakpointCallback = step3.bind(this);
388             }
389
390             function step3()
391             {
392                 removeBreakpoint(breakpointManager, uiSourceCode, 30);
393                 removeBreakpoint(breakpointManager, uiSourceCode, 10);
394                 removeBreakpoint(breakpointManager, uiSourceCode, 20);
395                 resetBreakpointManager(breakpointManager, next);
396             }
397         },
398
399         function testSetBreakpointThatShifts(next)
400         {
401             var breakpointManager = createBreakpointManager();
402             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
403             setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
404             window.setBreakpointCallback = step2.bind(this);
405
406             function step2()
407             {
408                 resetBreakpointManager(breakpointManager, next);
409             }
410         },
411
412         function testSetBreakpointThatShiftsTwice(next)
413         {
414             var breakpointManager = createBreakpointManager();
415             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
416             setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
417             window.setBreakpointCallback = step2.bind(this);
418
419             function step2()
420             {
421                 setBreakpoint(breakpointManager, uiSourceCode, 1015, "", true);
422                 window.setBreakpointCallback = step3.bind(this);
423             }
424
425             function step3()
426             {
427                 resetBreakpointManager(breakpointManager, next);
428             }
429         },
430
431         function testSetBreakpointOutsideScript(next)
432         {
433             var breakpointManager = createBreakpointManager([]);
434             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
435             breakpointManager.setBreakpoint(uiSourceCode, 2500, "", true);
436             window.setBreakpointCallback = step2.bind(this);
437
438             function step2()
439             {
440                 resetBreakpointManager(breakpointManager, next);
441             }
442        },
443
444         function testNavigation(next)
445         {
446             var breakpointManager = createBreakpointManager(serializedBreakpoints);
447             var uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
448             window.setBreakpointCallback = step2.bind(this);
449
450             function step2()
451             {
452                 InspectorTest.addResult("\n  Navigating to B.");
453                 breakpointManager._debuggerModel.reset();
454                 resetWorkspace(breakpointManager);
455                 var uiSourceCodeB = addUISourceCode(breakpointManager, "b.js");
456                 window.setBreakpointCallback = step3.bind(this);
457             }
458
459             function step3()
460             {
461                 InspectorTest.addResult("\n  Navigating back to A.");
462                 breakpointManager._debuggerModel.reset();
463                 resetWorkspace(breakpointManager);
464                 InspectorTest.addResult("  Resolving provisional breakpoint.");
465                 addTemporaryUISourceCode(breakpointManager, "a.js");
466                 var eventData = { breakpointId: "a.js:10", location: { scriptId: "a.js", lineNumber: 11, columnNumber: 5 }};
467                 breakpointManager._debuggerModel.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, eventData);
468                 addUISourceCode(breakpointManager, "a.js");
469                 window.setBreakpointCallback = step4.bind(this);
470             }
471
472             function step4()
473             {
474                 resetBreakpointManager(breakpointManager, next);
475             }
476         },
477
478         function testSourceMapping(next)
479         {
480             // Source mapping will shift everthing 10 lines ahead so that breakpoint 1 clashed with breakpoint 2.
481             var serializedBreakpoints = [];
482             serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
483             serializedBreakpoints.push(createBreakpoint("a.js", 20, "", true));
484
485             var breakpointManager = createBreakpointManager(serializedBreakpoints);
486             var uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
487             window.setBreakpointCallback = step2.bind(this);
488
489             function step2()
490             {
491                 window.setBreakpointCallback = step3.bind(this);
492             }
493
494             function step3()
495             {
496                 InspectorTest.addResult("\n  Toggling source mapping.");
497                 breakpointManager._debuggerModel.pushSourceMapping(shiftingMapping);
498                 InspectorTest.addResult("\n  Toggling source mapping back.");
499                 breakpointManager._debuggerModel.disableSourceMapping(shiftingMapping);
500                 resetBreakpointManager(breakpointManager, next);
501             }
502         },
503
504         function testProvisionalBreakpointsResolve(next)
505         {
506             var serializedBreakpoints = [];
507             serializedBreakpoints.push(createBreakpoint("a.js", 10, "foo == bar", true));
508
509             var breakpointManager = createBreakpointManager(serializedBreakpoints);
510             var uiSourceCode = addUISourceCode(breakpointManager, "a.js");
511             window.setBreakpointCallback = step2.bind(this);
512
513             function step2()
514             {
515                 breakpointManager._debuggerModel.reset();
516                 resetWorkspace(breakpointManager);
517                 InspectorTest.addResult("  Resolving provisional breakpoint.");
518                 addTemporaryUISourceCode(breakpointManager, "a.js")            
519                 var eventData = { breakpointId: "a.js:10", location: { scriptId: "a.js", lineNumber: 11, columnNumber: 5 }};
520                 breakpointManager._debuggerModel.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, eventData);
521                 var breakpoints = breakpointManager.allBreakpoints();
522                 InspectorTest.assertEquals(breakpoints.length, 1, "Exactly one provisional breakpoint should be registered in breakpoint manager.");
523                 resetBreakpointManager(breakpointManager, next);
524             }
525         },
526
527         function testSourceMappingReload(next)
528         {
529             // Source mapping will shift everthing 10 lines ahead.
530             var serializedBreakpoints = [createBreakpoint("b.js", 20, "foo == bar", true)];
531             var breakpointManager = createBreakpointManager(serializedBreakpoints);
532             InspectorTest.addResult("\n  Adding files:");
533             var uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
534             var uiSourceCodeB = addUISourceCode(breakpointManager, "b.js", true);
535             InspectorTest.addResult("\n  Toggling source mapping.");
536             var sourceMapping = createSourceMapping(uiSourceCodeA, uiSourceCodeB);
537             breakpointManager._debuggerModel.pushSourceMapping(sourceMapping);
538             window.setBreakpointCallback = step2.bind(this);
539             uiSourceCodeB.setSourceMapping(sourceMapping);
540
541             function step2()
542             {
543                 InspectorTest.addResult("\n  Reloading:");
544                 breakpointManager._debuggerModel.reset();
545                 resetWorkspace(breakpointManager);
546
547                 InspectorTest.addResult("\n  Adding files:");
548                 addTemporaryUISourceCode(breakpointManager, "a.js");
549                 var eventData = { breakpointId: "a.js:10", location: { scriptId: "a.js", lineNumber: 10, columnNumber: 5 }};
550                 breakpointManager._debuggerModel.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, eventData);
551                 uiSourceCodeA = addUISourceCode(breakpointManager, "a.js");
552                 uiSourceCodeB = addUISourceCode(breakpointManager, "b.js", true);
553                 InspectorTest.addResult("\n  Toggling source mapping.");
554                 var sourceMapping = createSourceMapping(uiSourceCodeA, uiSourceCodeB);
555                 breakpointManager._debuggerModel.pushSourceMapping(sourceMapping);
556                 window.setBreakpointCallback = step3.bind(this);
557                 uiSourceCodeB.setSourceMapping(sourceMapping);
558             }
559
560             function step3()
561             {
562                 resetBreakpointManager(breakpointManager, next);
563             }
564         },
565     ]);
566 };
567
568 </script>
569
570 </head>
571
572 <body onload="runTest()">
573 <p>Tests BreakpointManager class.</p>
574
575 </body>
576 </html>