summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/inspector/debugger/script-snippet-model.html
blob: 18611401013f591174e5b16f22e6e781d986fd64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<html>
<head>
<script src="../../http/tests/inspector/inspector-test.js"></script>
<script src="../../http/tests/inspector/debugger-test.js"></script>
<script>
function test()
{
    function resetSnippetsSettings()
    {
        WebInspector.scriptSnippetModel._snippetStorage.reset();
        WebInspector.scriptSnippetModel._lastSnippetEvaluationIndexSetting.set(0);
        WebInspector.scriptSnippetModel.reset();
    }

    var workspace = WebInspector.workspace;
    var snippetsProject = WebInspector.scriptSnippetModel.project();
    InspectorTest.runDebuggerTestSuite([
        function testCreateEditRenameRemove(next)
        {
            var uiSourceCode1;

            function filterSnippet(uiSourceCode)
            {
                return uiSourceCode.project().type() === WebInspector.projectTypes.Snippets;
            }

            function uiSourceCodeAdded(event)
            {
                var uiSourceCode = event.data;
                InspectorTest.addResult("UISourceCodeAdded: " + uiSourceCode.name());
            }

            function uiSourceCodeRemoved(event)
            {
                var uiSourceCode = event.data;
                InspectorTest.addResult("UISourceCodeRemoved: " + uiSourceCode.name());
            }

            workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, uiSourceCodeAdded);
            workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, uiSourceCodeRemoved);

            function renameSnippetAndCheckWorkspace(uiSourceCode, snippetName)
            {
                InspectorTest.addResult("Renaming snippet to '" + snippetName + "' ...");
                uiSourceCode.rename(snippetName, renameCallback);

                function renameCallback(success)
                {
                    if (success)
                        InspectorTest.addResult("Snippet renamed successfully.");
                    else
                        InspectorTest.addResult("Snippet was not renamed.");
                }
                InspectorTest.addResult("UISourceCode name is '" + uiSourceCode.name() + "' now.");
                InspectorTest.addResult("Number of uiSourceCodes in workspace: " + workspace.uiSourceCodes().filter(filterSnippet).length);
                var storageSnippetsCount = WebInspector.scriptSnippetModel._snippetStorage.snippets().length;
                InspectorTest.addResult("Number of snippets in the storage: " + storageSnippetsCount);
            }

            function contentCallback(content)
            {
                InspectorTest.addResult("Snippet content: " + content);
            }

            resetSnippetsSettings();
            
            snippetsProject.createFile("", null, "", step2.bind(this));
            
            function step2(path)
            {
                uiSourceCode1 = snippetsProject.uiSourceCode(path);
                
                uiSourceCode1.requestContent(contentCallback);
                uiSourceCode1.addRevision("<snippet content>");
                InspectorTest.addResult("Snippet content set.");
                delete uiSourceCode1._content;
                delete uiSourceCode1._contentLoaded;
                uiSourceCode1.requestContent(contentCallback);
                InspectorTest.addResult("Snippet1 created.");

                snippetsProject.createFile("", null, "", step3.bind(this));
            }

            function step3(path)
            {
                var uiSourceCode2 = snippetsProject.uiSourceCode(path);
                InspectorTest.addResult("Snippet2 created.");
                renameSnippetAndCheckWorkspace(uiSourceCode1, "foo");
                renameSnippetAndCheckWorkspace(uiSourceCode1, "   ");
                renameSnippetAndCheckWorkspace(uiSourceCode1, " bar ");
                renameSnippetAndCheckWorkspace(uiSourceCode1, "foo");
                renameSnippetAndCheckWorkspace(uiSourceCode2, "bar");
                renameSnippetAndCheckWorkspace(uiSourceCode2, "foo");
                delete uiSourceCode1._content;
                delete uiSourceCode1._contentLoaded;
                uiSourceCode1.requestContent(contentCallback);
    
                snippetsProject.deleteFile(uiSourceCode1.path());
                snippetsProject.deleteFile(uiSourceCode2.path());

                snippetsProject.createFile("", null, "", step4.bind(this));
            }

            function step4(path)
            {
                var uiSourceCode3 = snippetsProject.uiSourceCode(path);
                InspectorTest.addResult("Snippet3 created.");
                snippetsProject.deleteFile(uiSourceCode3.path());
                InspectorTest.addResult("Snippet3 deleted.");
    
                InspectorTest.addResult("Number of uiSourceCodes in workspace: " + workspace.uiSourceCodes().filter(filterSnippet).length);
                var storageSnippetsCount = WebInspector.scriptSnippetModel._snippetStorage.snippets().length;
                InspectorTest.addResult("Number of snippets in the storage: " + storageSnippetsCount);
    
                workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, uiSourceCodeAdded);
                workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, uiSourceCodeRemoved);
    
                next();
            }
        },

        function testEvaluate(next)
        {
            var uiSourceCode1;
            var uiSourceCode2;

            function evaluateSnippetAndDumpEvaluationDetails(uiSourceCode, callback)
            {
                InspectorTest.addSniffer(WebInspector.SnippetScriptMapping.prototype, "addScript", dumpScript);
                // JSC does not support compile-and-run for snippet evaluation, so different platforms have slightly different codeflow here, hence two sniffers.
                InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "_printResult", dumpResult);
                InspectorTest.addSniffer(WebInspector.ScriptSnippetModel.prototype, "_printRunScriptResult", dumpResult);
                WebInspector.scriptSnippetModel.evaluateScriptSnippet(uiSourceCode);
                var evaluationSourceURL = WebInspector.scriptSnippetModel._evaluationSourceURL(uiSourceCode);
                var snippetId = WebInspector.scriptSnippetModel._snippetIdForUISourceCode.get(uiSourceCode);
                InspectorTest.addResult("Last evaluation source url for snippet: " + evaluationSourceURL);
                InspectorTest.assertEquals(snippetId, WebInspector.scriptSnippetModel._snippetIdForSourceURL(evaluationSourceURL), "Snippet can not be identified by its evaluation sourceURL.");

                function dumpScript(script)
                {
                    InspectorTest.addResult("Snippet script added, sourceURL = " + script.sourceURL);
                }

                function dumpResult(result, wasThrown)
                {
                    InspectorTest.addResult("Snippet execution result: " + result.description);
                    callback();
                }
            }

            resetSnippetsSettings();

            snippetsProject.createFile("", null, "", step2.bind(this));
            
            function step2(path)
            {
                uiSourceCode1 = snippetsProject.uiSourceCode(path);
                uiSourceCode1.rename("Snippet1", function() { });
                var content = "";
                content += "// This snippet does nothing.\n";
                content += "var i = 2+2;\n";
                uiSourceCode1.setWorkingCopy(content);
                snippetsProject.createFile("", null, "", step3.bind(this));
            }
            
            function step3(path)
            {
                uiSourceCode2 = snippetsProject.uiSourceCode(path);
                uiSourceCode2.rename("Snippet2", function() { });
                content = "";
                content += "// This snippet creates a function that does nothing and returns it.\n";
                content += "function doesNothing() {\n";
                content += "    var  i = 2+2;\n";
                content += "};\n";
                content += "doesNothing;\n";
                uiSourceCode2.setWorkingCopy(content);
                evaluateSnippetAndDumpEvaluationDetails(uiSourceCode1, step4);
            }

            function step4()
            {
                evaluateSnippetAndDumpEvaluationDetails(uiSourceCode2, step5);
            }

            function step5()
            {
                evaluateSnippetAndDumpEvaluationDetails(uiSourceCode1, next);
            }
        },

        function testEvaluateEditReload(next)
        {
            function evaluateSnippetAndReloadPage(uiSourceCode, callback)
            {
                // JSC does not support compile-and-run for snippet evaluation, so different platforms have slightly different codeflow here, hence two sniffers.
                InspectorTest.addSniffer(WebInspector.ConsoleView.prototype, "_printResult", snippetFinished);
                InspectorTest.addSniffer(WebInspector.ScriptSnippetModel.prototype, "_printRunScriptResult", snippetFinished);
                WebInspector.scriptSnippetModel.evaluateScriptSnippet(uiSourceCode);

                function snippetFinished(result, wasThrown)
                {
                    var script = WebInspector.scriptSnippetModel._scriptForUISourceCode.get(uiSourceCode);
                    InspectorTest.addResult("Snippet execution result: " + result.description);

                    InspectorTest.reloadPage(callback)
                }
            }

            resetSnippetsSettings();

            snippetsProject.createFile("", null, "", step3.bind(this));
            
            function step3(path)
            {
                var uiSourceCode1 = snippetsProject.uiSourceCode(path);
                uiSourceCode1.rename("Snippet1", function() { });
                var content = "";
                content += "// This snippet does nothing.\n";
                content += "var i = 2+2;\n";
                uiSourceCode1.setWorkingCopy(content);

                evaluateSnippetAndReloadPage(uiSourceCode1, next);
            }
        }
    ]);
};
</script>
</head>
<body onload="runTest()">
<p>Tests script snippet model.</p>
</body>
</html>