Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / sources / debugger / breakpoint-manager.js
1 var initialize_BreakpointManagerTest = function() {
2
3 InspectorTest.uiSourceCodes = {};
4
5 InspectorTest.dumpTargetIds = false;
6
7 InspectorTest.initializeDefaultMappingOnTarget = function(target)
8 {
9     var defaultMapping = {
10         rawLocationToUILocation: function(rawLocation)
11         {
12             return InspectorTest.uiSourceCodes[rawLocation.scriptId].uiLocation(rawLocation.lineNumber, 0);
13         },
14
15         uiLocationToRawLocation: function(uiSourceCode, lineNumber)
16         {
17             if (!InspectorTest.uiSourceCodes[uiSourceCode.url])
18                 return null;
19             return new WebInspector.DebuggerModel.Location(target, uiSourceCode.url, lineNumber, 0);
20         },
21
22         isIdentity: function()
23         {
24             return true;
25         }
26     };
27     target.defaultMapping = defaultMapping;
28 }
29
30 InspectorTest.createMockTarget = function(id)
31 {
32     var target = {
33         id: function()
34         {
35             return id;
36         },
37         resourceTreeModel: new WebInspector.Object(),
38         addEventListener: function() { },
39         removeEventListener: function() { },
40         dispose: function() { },
41         isDetached: function() { return !!this.detached; }
42     };
43     target._modelByConstructor = new Map();
44     InspectorTest.initializeDefaultMappingOnTarget(target);
45     target.debuggerModel = new InspectorTest.DebuggerModelMock(target, target.defaultMapping, InspectorTest.testDebuggerWorkspaceBinding);
46     target.debuggerModel._target = target;
47     return target;
48 }
49
50 InspectorTest.dumpTarget = function(targetAware)
51 {
52     return InspectorTest.dumpTargetIds ?  "target " + targetAware.target().id() + " " : "";
53 }
54
55 InspectorTest.DebuggerModelMock = function(target, sourceMapping, debuggerWorkspaceBinding)
56 {
57     WebInspector.SDKModel.call(this, WebInspector.DebuggerModel, target);
58     this._breakpointResolvedEventTarget = new WebInspector.Object();
59     this._scripts = {};
60     this._sourceMapping = sourceMapping;
61     this._breakpoints = {};
62     this._debuggerWorkspaceBinding = InspectorTest.testDebuggerWorkspaceBinding;
63 }
64
65 InspectorTest.DebuggerModelMock.prototype = {
66     target: function()
67     {
68         return this._target;
69     },
70
71     debuggerEnabled: function()
72     {
73         return true;
74     },
75
76     scriptsForSourceURL: function(url)
77     {
78         var script = this._scriptForURL(url);
79         return script ? [script] : [];
80     },
81
82     _addScript: function(scriptId, url)
83     {
84         var script = new WebInspector.Script(this._target, scriptId, url);
85         this._scripts[scriptId] = script;
86         this._debuggerWorkspaceBinding._targetToData.get(this._target)._parsedScriptSource({data: script});
87     },
88
89     _registerScript: function(script)
90     {
91         this._scripts[script.scriptId] = script;
92         this._debuggerWorkspaceBinding._targetToData.get(this._target)._parsedScriptSource({data: script});
93     },
94
95     _scriptForURL: function(url)
96     {
97         for (var scriptId in this._scripts) {
98             var script = this._scripts[scriptId];
99             if (script.sourceURL === url)
100                 return script;
101         }
102     },
103
104     _scheduleSetBeakpointCallback: function(callback, breakpointId, locations)
105     {
106         setTimeout(innerCallback.bind(this), 0);
107
108         function innerCallback()
109         {
110             if (callback)
111                 callback(breakpointId, locations);
112             if (window.setBreakpointCallback) {
113                 var savedCallback = window.setBreakpointCallback;
114                 delete window.setBreakpointCallback;
115                 savedCallback();
116             }
117         }
118     },
119
120     createRawLocation: function(script, line, column)
121     {
122         return new WebInspector.DebuggerModel.Location(this.target(), script.scriptId, line, column);
123     },
124
125     setBreakpointByURL: function(url, lineNumber, columnNumber, condition, callback)
126     {
127         InspectorTest.addResult("    " + InspectorTest.dumpTarget(this) + "debuggerModel.setBreakpoint(" + [url, lineNumber, condition].join(":") + ")");
128
129         var breakpointId = url + ":" + lineNumber;
130         if (this._breakpoints[breakpointId]) {
131             this._scheduleSetBeakpointCallback(callback, null);
132             return;
133         }
134         this._breakpoints[breakpointId] = true;
135
136         if (lineNumber >= 2000) {
137             this._scheduleSetBeakpointCallback(callback, breakpointId, []);
138             return;
139         }
140         if (lineNumber >= 1000) {
141             var shiftedLocation = new WebInspector.DebuggerModel.Location(this._target, url, lineNumber + 10, columnNumber);
142             this._scheduleSetBeakpointCallback(callback, breakpointId, [shiftedLocation]);
143             return;
144         }
145
146         var locations = [];
147         var script = this._scriptForURL(url);
148         if (script) {
149             var location = new WebInspector.DebuggerModel.Location(this._target, script.scriptId, lineNumber, 0);
150             locations.push(location);
151         }
152
153         this._scheduleSetBeakpointCallback(callback, breakpointId, locations);
154     },
155
156     removeBreakpoint: function(breakpointId, callback)
157     {
158         InspectorTest.addResult("    " + InspectorTest.dumpTarget(this) + "debuggerModel.removeBreakpoint(" + breakpointId + ")");
159         delete this._breakpoints[breakpointId];
160         if (callback)
161             setTimeout(callback, 0);
162     },
163
164     setBreakpointsActive: function() { },
165
166     scriptForId: function(scriptId)
167     {
168         return this._scripts[scriptId];
169     },
170
171     reset: function()
172     {
173         InspectorTest.addResult("  Resetting debugger.");
174         this._scripts = {};
175         this._debuggerWorkspaceBinding._reset(this._target);
176     },
177
178     pushSourceMapping: function(sourceMapping)
179     {
180         for (var scriptId in this._scripts)
181             this._debuggerWorkspaceBinding.pushSourceMapping(this._scripts[scriptId], sourceMapping);
182     },
183
184     disableSourceMapping: function(sourceMapping)
185     {
186         sourceMapping._disabled = true;
187         for (var scriptId in this._scripts)
188             this._debuggerWorkspaceBinding.updateLocations(this._scripts[scriptId]);
189     },
190
191     addBreakpointListener: function(breakpointId, listener, thisObject)
192     {
193         this._breakpointResolvedEventTarget.addEventListener(breakpointId, listener, thisObject)
194     },
195
196     removeBreakpointListener: function(breakpointId, listener, thisObject)
197     {
198         this._breakpointResolvedEventTarget.removeEventListener(breakpointId, listener, thisObject);
199     },
200
201     _breakpointResolved: function(breakpointId, location)
202     {
203         this._breakpointResolvedEventTarget.dispatchEventToListeners(breakpointId, location);
204     },
205
206     __proto__: WebInspector.Object.prototype
207 }
208
209 InspectorTest.setupLiveLocationSniffers = function()
210 {
211     InspectorTest.addSniffer(WebInspector.DebuggerWorkspaceBinding.prototype, "createLiveLocation", function(rawLocation)
212     {
213         InspectorTest.addResult("    Location created: " + InspectorTest.dumpTarget(rawLocation) + rawLocation.scriptId + ":" + rawLocation.lineNumber);
214     }, true);
215     InspectorTest.addSniffer(WebInspector.DebuggerWorkspaceBinding.Location.prototype, "dispose", function()
216     {
217         InspectorTest.addResult("    Location disposed: " + InspectorTest.dumpTarget(this._rawLocation) + this._rawLocation.scriptId + ":" + this._rawLocation.lineNumber);
218     }, true);
219 }
220
221 InspectorTest.addScript = function(target, breakpointManager, url)
222 {
223     target.debuggerModel._addScript(url, url);
224     InspectorTest.addResult("  Adding script: " + url);
225     var uiSourceCodes = breakpointManager._workspace.uiSourceCodesForProjectType(WebInspector.projectTypes.Debugger);
226     for (var i = 0; i < uiSourceCodes.length; ++i) {
227         var uiSourceCode = uiSourceCodes[i];
228         if (uiSourceCode.url === url) {
229             breakpointManager._debuggerWorkspaceBinding.setSourceMapping(target, uiSourceCode, breakpointManager.defaultMapping);
230             InspectorTest.uiSourceCodes[url] = uiSourceCode;
231             return uiSourceCode;
232         }
233     }
234 }
235
236 InspectorTest.addUISourceCode = function(target, breakpointManager, url, doNotSetSourceMapping, doNotAddScript)
237 {
238     if (!doNotAddScript)
239         InspectorTest.addScript(target, breakpointManager, url);
240     InspectorTest.addResult("  Adding UISourceCode: " + url);
241     var contentProvider = new WebInspector.StaticContentProvider(WebInspector.resourceTypes.Script, "");
242     var binding = breakpointManager._debuggerWorkspaceBinding;
243     var uiSourceCode = binding._networkWorkspaceBinding.addFileForURL(url, contentProvider);
244     InspectorTest.uiSourceCodes[url] = uiSourceCode;
245     if (!doNotSetSourceMapping) {
246         breakpointManager._debuggerWorkspaceBinding.setSourceMapping(target, uiSourceCode, breakpointManager.defaultMapping);
247         breakpointManager._debuggerWorkspaceBinding.updateLocations(target.debuggerModel.scriptForId(url));
248     }
249     return uiSourceCode;
250 }
251
252 InspectorTest.createBreakpointManager = function(targetManager, debuggerWorkspaceBinding, persistentBreakpoints)
253 {
254     InspectorTest._pendingBreakpointUpdates = 0;
255     InspectorTest.addSniffer(WebInspector.BreakpointManager.TargetBreakpoint.prototype, "_updateInDebugger", updateInDebugger, true);
256     InspectorTest.addSniffer(WebInspector.BreakpointManager.TargetBreakpoint.prototype, "_didUpdateInDebugger", didUpdateInDebugger, true);
257
258     function updateInDebugger()
259     {
260         InspectorTest._pendingBreakpointUpdates++;
261     }
262
263     function didUpdateInDebugger()
264     {
265         InspectorTest._pendingBreakpointUpdates--;
266         InspectorTest._notifyAfterBreakpointUpdate();
267     }
268
269     persistentBreakpoints = persistentBreakpoints || [];
270     var setting = {
271         get: function() { return persistentBreakpoints; },
272         set: function(breakpoints) { persistentBreakpoints = breakpoints; }
273     };
274
275     function breakpointAdded(event)
276     {
277         var breakpoint = event.data.breakpoint;
278         var uiLocation = event.data.uiLocation;
279         InspectorTest.addResult("    breakpointAdded(" + [uiLocation.uiSourceCode.originURL(), uiLocation.lineNumber, uiLocation.columnNumber, breakpoint.condition(), breakpoint.enabled()].join(", ") + ")");
280     }
281
282     function breakpointRemoved(event)
283     {
284         var uiLocation = event.data.uiLocation;
285         InspectorTest.addResult("    breakpointRemoved(" + [uiLocation.uiSourceCode.originURL(), uiLocation.lineNumber, uiLocation.columnNumber].join(", ") + ")");
286     }
287     var targets = targetManager.targets();
288     var mappingForManager;
289     for (var i = 0; i < targets.length; ++i) {
290         InspectorTest.initializeDefaultMappingOnTarget(targets[i]);
291         if (!mappingForManager)
292             mappingForManager = targets[i].defaultMapping;
293         var model = new InspectorTest.DebuggerModelMock(targets[i], targets[i].defaultMapping, debuggerWorkspaceBinding);
294         targets[i].debuggerModel = model;
295     }
296
297     var breakpointManager = new WebInspector.BreakpointManager(setting, debuggerWorkspaceBinding._workspace, targetManager, debuggerWorkspaceBinding);
298     breakpointManager.defaultMapping = mappingForManager;
299     breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointAdded, breakpointAdded);
300     breakpointManager.addEventListener(WebInspector.BreakpointManager.Events.BreakpointRemoved, breakpointRemoved);
301     InspectorTest.addResult("  Created breakpoints manager");
302     InspectorTest.dumpBreakpointStorage(breakpointManager);
303     return breakpointManager;
304 }
305
306 InspectorTest.setBreakpoint = function(breakpointManager, uiSourceCode, lineNumber, columnNumber, condition, enabled, setBreakpointCallback)
307 {
308     InspectorTest.addResult("  Setting breakpoint at " + uiSourceCode.originURL() + ":" + lineNumber + ":" + columnNumber + " enabled:" + enabled + " condition:" + condition);
309     if (setBreakpointCallback)
310         window.setBreakpointCallback = setBreakpointCallback;
311     return breakpointManager.setBreakpoint(uiSourceCode, lineNumber, columnNumber, condition, enabled);
312 }
313
314 InspectorTest.removeBreakpoint = function(breakpointManager, uiSourceCode, lineNumber, columnNumber)
315 {
316     InspectorTest.addResult("  Removing breakpoint at " + uiSourceCode.originURL() + ":" + lineNumber + ":" + columnNumber);
317     breakpointManager.findBreakpoint(uiSourceCode, lineNumber, columnNumber).remove();
318 }
319
320 InspectorTest.dumpBreakpointStorage = function(breakpointManager)
321 {
322     var breakpoints = breakpointManager._storage._setting.get();
323     InspectorTest.addResult("  Dumping Storage");
324     for (var i = 0; i < breakpoints.length; ++i)
325         InspectorTest.addResult("    " + breakpoints[i].sourceFileId + ":" + breakpoints[i].lineNumber  + " enabled:" + breakpoints[i].enabled + " condition:" + breakpoints[i].condition);
326 }
327
328 InspectorTest.dumpBreakpointLocations = function(breakpointManager)
329 {
330     var allBreakpointLocations = breakpointManager.allBreakpointLocations();
331     InspectorTest.addResult("  Dumping Breakpoint Locations");
332     var lastUISourceCode = null;
333     var locations = [];
334
335     function dumpLocations(uiSourceCode, locations)
336     {
337         locations.sort(function(a, b) {
338             return a.lineNumber - b.lineNumber;
339         });
340         InspectorTest.addResult("    UISourceCode (url='" + uiSourceCode.url + "', uri='" + uiSourceCode.uri() + "')");
341         for (var i = 0; i < locations.length; ++i)
342             InspectorTest.addResult("      Location: (" + locations[i].lineNumber + ", " + locations[i].columnNumber + ")");
343     }
344
345     for (var i = 0; i < allBreakpointLocations.length; ++i) {
346         var uiLocation = allBreakpointLocations[i].uiLocation;
347         var uiSourceCode = uiLocation.uiSourceCode;
348         if (lastUISourceCode && lastUISourceCode != uiSourceCode) {
349             dumpLocations(uiSourceCode, locations);
350             locations = [];
351         }
352         lastUISourceCode = uiSourceCode;
353         locations.push(uiLocation);
354     }
355     if (lastUISourceCode)
356         dumpLocations(lastUISourceCode, locations);
357 }
358
359 InspectorTest.resetBreakpointManager = function(breakpointManager, next)
360 {
361     InspectorTest.addResult("  Resetting breakpoint manager");
362     breakpointManager.removeAllBreakpoints();
363     breakpointManager.removeProvisionalBreakpointsForTest();
364     InspectorTest.uiSourceCodes = {};
365     next();
366 }
367
368 InspectorTest.runAfterPendingBreakpointUpdates = function(breakpointManager, callback)
369 {
370     InspectorTest._pendingBreakpointUpdatesCallback = callback;
371     InspectorTest._notifyAfterBreakpointUpdate();
372 }
373
374 InspectorTest._notifyAfterBreakpointUpdate = function()
375 {
376     if (!InspectorTest._pendingBreakpointUpdates && InspectorTest._pendingBreakpointUpdatesCallback) {
377         var callback = InspectorTest._pendingBreakpointUpdatesCallback;
378         delete InspectorTest._pendingBreakpointUpdatesCallback;
379         callback();
380     }
381 }
382
383 InspectorTest.finishBreakpointTest = function(breakpointManager, next)
384 {
385     InspectorTest.runAfterPendingBreakpointUpdates(breakpointManager, dump);
386
387     function dump()
388     {
389         InspectorTest.dumpBreakpointLocations(breakpointManager);
390         InspectorTest.dumpBreakpointStorage(breakpointManager);
391         InspectorTest.runAfterPendingBreakpointUpdates(breakpointManager, reset);
392     }
393
394     function reset()
395     {
396         InspectorTest.resetBreakpointManager(breakpointManager, didReset);
397     }
398
399     function didReset()
400     {
401         InspectorTest.runAfterPendingBreakpointUpdates(breakpointManager, finish);
402     }
403
404     function finish()
405     {
406         InspectorTest.dumpBreakpointLocations(breakpointManager);
407         next();
408     }
409 }
410
411 }