Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / sources / debugger / script-formatter.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/obfuscated.js"></script>
6
7 <script>
8
9 var test = function()
10 {
11     var panel = WebInspector.panels.sources;
12     var worker = Runtime.startWorker("script_formatter_worker");
13
14     InspectorTest.runDebuggerTestSuite([
15         function testScriptFormatterWorker(next)
16         {
17             worker.onmessage = InspectorTest.safeWrap(function(event)
18             {
19                 InspectorTest.assertEquals("var x = 0\n", event.data.content);
20                 next();
21             });
22
23             worker.onerror = function(event)
24             {
25                 InspectorTest.addResult("Error in worker: " + event.data);
26                 next();
27             };
28
29             worker.postMessage({ method: "format", params: { mimeType: "text/javascript", content: "var x=0" } });
30         },
31
32         function testSourceMapping(next)
33         {
34             var formatter = new WebInspector.ScriptFormatter();
35
36             InspectorTest.showScriptSource("obfuscated.js", didShowScriptSource);
37             function didShowScriptSource(sourceFrame)
38             {
39                 formatter.formatContent("text/javascript", sourceFrame._textEditor.text(), didFormatContent);
40             }
41
42             function didFormatContent(content, mapping)
43             {
44                 var source = WebInspector.panels.sources.visibleView._textEditor.text();
45                 var formattedSource = content;
46
47                 function testMapping(string)
48                 {
49                     var originalPosition = source.indexOf(string);
50                     InspectorTest.assertTrue(originalPosition !== -1);
51                     var originalLocation = WebInspector.Formatter.positionToLocation(source.lineEndings(), originalPosition);
52                     var formattedLocation = mapping.originalToFormatted(originalLocation[0], originalLocation[1]);
53                     var formattedPosition = WebInspector.Formatter.locationToPosition(formattedSource.lineEndings(), formattedLocation[0], formattedLocation[1]);
54                     var expectedFormattedPosition = formattedSource.indexOf(string);
55                     InspectorTest.assertEquals(expectedFormattedPosition, formattedPosition, "wrong mapping for <" + string + ">");
56                 }
57
58                 testMapping("function");
59                 testMapping("formatted1");
60                 testMapping("variable1");
61
62                 testMapping("    return \"functionWithComments\"");
63
64                 testMapping("onmessage");
65                 testMapping("indent_start");
66                 testMapping("function require");
67                 testMapping("var regexp");
68                 testMapping("importScripts");
69
70                 testMapping("formatted2");
71                 testMapping("variable2");
72
73                 next();
74             }
75         },
76
77         function testFormatInlinedScripts(next)
78         {
79             worker.onmessage = InspectorTest.safeWrap(function(event)
80             {
81                 InspectorTest.addResult(event.data.content);
82                 next();
83             });
84
85             worker.onerror = function(event)
86             {
87                 InspectorTest.addResult("Error in worker: " + event.data);
88                 next();
89             };
90
91             var content = "<html><body><script>function f(){}<" + "/script><script>function g(){var a;window.return = 10;if (a) return;}<" + "/script></body></html>";
92             worker.postMessage({ method: "format", params: { mimeType: "text/html", content: content, indentString: "**" } });
93         }
94     ]);
95 }
96
97 </script>
98
99 </head>
100
101 <body onload="runTest()">
102 <p>Tests the script formatting functionality.
103 </p>
104
105 </body>
106 </html>