2011-04-11 Pavel Podivilov <podivilov@chromium.org>
[WebKit-https.git] / LayoutTests / inspector / debugger / live-edit.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="resources/edit-me.js"></script>
6 <script src="resources/edit-me-when-paused.js"></script>
7
8 <script>
9
10 function test()
11 {
12     var panel = WebInspector.panels.scripts;
13
14     InspectorTest.runDebuggerTestSuite([
15         function testLiveEdit(next)
16         {
17             InspectorTest.showScriptSource("edit-me.js", didShowScriptSource);
18
19             function didShowScriptSource(sourceFrame)
20             {
21                 replaceInSource(sourceFrame, "return 0;", "return \"live-edited string\";", didEditScriptSource);
22             }
23
24             function didEditScriptSource()
25             {
26                 InspectorTest.evaluateInPage("f()", didEvaluateInPage)
27             }
28
29             function didEvaluateInPage(result)
30             {
31                 InspectorTest.assertEquals("live-edited string", result.description, "edited function returns wrong result");
32                 InspectorTest.dumpSourceFrameContents(panel.visibleView);
33                 next();
34             }
35         },
36
37         function testLiveEditWhenPaused(next)
38         {
39             InspectorTest.showScriptSource("edit-me-when-paused.js", didShowScriptSource);
40
41             function didShowScriptSource(sourceFrame)
42             {
43                 InspectorTest.waitUntilPaused(paused);
44                 InspectorTest.evaluateInPage("f1()", didEvaluateInPage);
45             }
46
47             function paused(callFrames)
48             {
49                 replaceInSource(panel.visibleView, "return 1;", "return 2;\n\n\n\n", didEditScriptSource);
50             }
51
52             function didEditScriptSource()
53             {
54                 InspectorTest.captureStackTrace(WebInspector.debuggerModel.callFrames);
55                 InspectorTest.resumeExecution();
56             }
57
58             function didEvaluateInPage(result)
59             {
60                 InspectorTest.assertEquals("3", result.description, "edited function returns wrong result");
61                 next();
62             }
63         },
64
65         function testBreakpointsUpdated(next)
66         {
67             InspectorTest.showScriptSource("edit-me.js", didShowScriptSource);
68
69             function didShowScriptSource(sourceFrame)
70             {
71                 InspectorTest.addSniffer(sourceFrame, "addBreakpoint", breakpointAdded);
72                 InspectorTest.setBreakpoint(sourceFrame, 2, "", true);
73             }
74
75             function breakpointAdded()
76             {
77                 replaceInSource(panel.visibleView, "function f()", "function newFunctionCreatedWithLiveEdit()\n{\n}\nfunction f()", didEditScriptSource);
78             }
79
80             function didEditScriptSource()
81             {
82                 var breakpoints = panel.visibleView._breakpoints;
83                 for (var lineNumber in breakpoints)
84                     InspectorTest.assertEquals("5", lineNumber);
85                 next();
86             }
87         }
88     ]);
89
90     function replaceInSource(sourceFrame, string, replacement, callback)
91     {
92         InspectorTest.addSniffer(WebInspector.debuggerModel, "_didEditScriptSource", callback);
93         sourceFrame._textViewer._mainPanel.readOnly = false;
94         sourceFrame.startEditing();
95         var oldRange, newRange;
96         var lines = sourceFrame._textModel._lines;
97         for (var i = 0; i < lines.length; ++i) {
98             var column = lines[i].indexOf(string);
99             if (column === -1)
100                 continue;
101             lines[i] = lines[i].replace(string, replacement);
102             var lineEndings = replacement.lineEndings();
103             var lastLineLength = lineEndings[lineEndings.length - 1] - (lineEndings[lineEndings.length - 2] || 0);
104             oldRange = new WebInspector.TextRange(i, column, i, column + string.length);
105             newRange = new WebInspector.TextRange(i, column, i + lineEndings.length - 1, lastLineLength);
106             break;
107         }
108         sourceFrame.endEditing(oldRange, newRange);
109         sourceFrame._textViewer._commitEditing();
110     }
111 };
112
113 </script>
114
115 </head>
116
117 <body onload="runTest()">
118 <p>Tests live edit feature.</p>
119
120 </body>
121 </html>