Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / inspector / sources / debugger-ui / script-snippet-model.html
blob59983292c82bc0fed93c67dbb982e4c7c88dc9aa
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>
6 function startWorker()
8 var workerScript = "postMessage('Done.');";
9 var blob = new Blob([workerScript], { type: "text/javascript" });
10 var worker = new Worker(URL.createObjectURL(blob));
13 function test()
15 function evaluateSnippetAndDumpEvaluationDetails(uiSourceCode, context, callback)
17 InspectorTest.addSniffer(WebInspector.ScriptSnippetModel.prototype, "_printRunScriptResult", dumpResult);
18 WebInspector.scriptSnippetModel.evaluateScriptSnippet(context, uiSourceCode);
19 var target = context.target();
20 var mapping = WebInspector.scriptSnippetModel._mappingForTarget.get(target);
21 var evaluationSourceURL = mapping._evaluationSourceURL(uiSourceCode);
22 var snippetId = WebInspector.scriptSnippetModel._snippetIdForUISourceCode.get(uiSourceCode);
23 InspectorTest.addResult("Last evaluation source url for snippet: " + evaluationSourceURL);
24 InspectorTest.assertEquals(snippetId, WebInspector.scriptSnippetModel._snippetIdForSourceURL(evaluationSourceURL), "Snippet can not be identified by its evaluation sourceURL.");
27 function dumpResult(target, result, wasThrown)
29 InspectorTest.addResult("Snippet execution result: " + result.description);
30 callback();
34 function resetSnippetsSettings()
36 WebInspector.scriptSnippetModel._snippetStorage._lastSnippetIdentifierSetting.set(0);
37 WebInspector.scriptSnippetModel._snippetStorage._snippetsSetting.set([]);
38 WebInspector.scriptSnippetModel._lastSnippetEvaluationIndexSetting.set(0);
39 WebInspector.scriptSnippetModel = new WebInspector.ScriptSnippetModel(WebInspector.workspace);
42 var workspace = WebInspector.workspace;
43 InspectorTest.runDebuggerTestSuite([
44 function testCreateEditRenameRemove(next)
46 var uiSourceCode1;
48 function filterSnippet(uiSourceCode)
50 return uiSourceCode.project().type() === WebInspector.projectTypes.Snippets;
53 function uiSourceCodeAdded(event)
55 var uiSourceCode = event.data;
56 InspectorTest.addResult("UISourceCodeAdded: " + uiSourceCode.name());
59 function uiSourceCodeRemoved(event)
61 var uiSourceCode = event.data;
62 InspectorTest.addResult("UISourceCodeRemoved: " + uiSourceCode.name());
65 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, uiSourceCodeAdded);
66 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, uiSourceCodeRemoved);
68 function renameSnippetAndCheckWorkspace(uiSourceCode, snippetName)
70 InspectorTest.addResult("Renaming snippet to '" + snippetName + "' ...");
71 uiSourceCode.rename(snippetName, renameCallback);
73 function renameCallback(success)
75 if (success)
76 InspectorTest.addResult("Snippet renamed successfully.");
77 else
78 InspectorTest.addResult("Snippet was not renamed.");
80 InspectorTest.addResult("UISourceCode name is '" + uiSourceCode.name() + "' now.");
81 InspectorTest.addResult("Number of uiSourceCodes in workspace: " + workspace.uiSourceCodes().filter(filterSnippet).length);
82 var storageSnippetsCount = WebInspector.scriptSnippetModel._snippetStorage.snippets().length;
83 InspectorTest.addResult("Number of snippets in the storage: " + storageSnippetsCount);
86 function contentCallback(content)
88 InspectorTest.addResult("Snippet content: " + content);
91 resetSnippetsSettings();
93 WebInspector.scriptSnippetModel.project().createFile("", null, "", step2.bind(this));
95 function step2(path)
97 uiSourceCode1 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
99 uiSourceCode1.requestContent(contentCallback);
100 uiSourceCode1.addRevision("<snippet content>");
101 InspectorTest.addResult("Snippet content set.");
102 delete uiSourceCode1._content;
103 delete uiSourceCode1._contentLoaded;
104 uiSourceCode1.requestContent(contentCallback);
105 InspectorTest.addResult("Snippet1 created.");
107 WebInspector.scriptSnippetModel.project().createFile("", null, "", step3.bind(this));
110 function step3(path)
112 var uiSourceCode2 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
113 InspectorTest.addResult("Snippet2 created.");
114 renameSnippetAndCheckWorkspace(uiSourceCode1, "foo");
115 renameSnippetAndCheckWorkspace(uiSourceCode1, " ");
116 renameSnippetAndCheckWorkspace(uiSourceCode1, " bar ");
117 renameSnippetAndCheckWorkspace(uiSourceCode1, "foo");
118 renameSnippetAndCheckWorkspace(uiSourceCode2, "bar");
119 renameSnippetAndCheckWorkspace(uiSourceCode2, "foo");
120 delete uiSourceCode1._content;
121 delete uiSourceCode1._contentLoaded;
122 uiSourceCode1.requestContent(contentCallback);
124 WebInspector.scriptSnippetModel.project().deleteFile(uiSourceCode1.path());
125 WebInspector.scriptSnippetModel.project().deleteFile(uiSourceCode2.path());
127 WebInspector.scriptSnippetModel.project().createFile("", null, "", step4.bind(this));
130 function step4(path)
132 var uiSourceCode3 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
133 InspectorTest.addResult("Snippet3 created.");
134 WebInspector.scriptSnippetModel.project().deleteFile(uiSourceCode3.path());
135 InspectorTest.addResult("Snippet3 deleted.");
137 InspectorTest.addResult("Number of uiSourceCodes in workspace: " + workspace.uiSourceCodes().filter(filterSnippet).length);
138 var storageSnippetsCount = WebInspector.scriptSnippetModel._snippetStorage.snippets().length;
139 InspectorTest.addResult("Number of snippets in the storage: " + storageSnippetsCount);
141 workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, uiSourceCodeAdded);
142 workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, uiSourceCodeRemoved);
144 next();
148 function testEvaluate(next)
150 var uiSourceCode1;
151 var uiSourceCode2;
152 var context = WebInspector.context.flavor(WebInspector.ExecutionContext);
154 resetSnippetsSettings();
155 var snippetScriptMapping = WebInspector.scriptSnippetModel.snippetScriptMapping(WebInspector.targetManager.targets()[0]);
157 WebInspector.scriptSnippetModel.project().createFile("", null, "", step2.bind(this));
159 function step2(path)
161 uiSourceCode1 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
162 uiSourceCode1.rename("Snippet1", function() { });
163 var content = "";
164 content += "// This snippet does nothing.\n";
165 content += "var i = 2+2;\n";
166 uiSourceCode1.setWorkingCopy(content);
167 WebInspector.scriptSnippetModel.project().createFile("", null, "", step3.bind(this));
170 function step3(path)
172 uiSourceCode2 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
173 uiSourceCode2.rename("Snippet2", function() { });
174 content = "";
175 content += "// This snippet creates a function that does nothing and returns it.\n";
176 content += "function doesNothing() {\n";
177 content += " var i = 2+2;\n";
178 content += "};\n";
179 content += "doesNothing;\n";
180 uiSourceCode2.setWorkingCopy(content);
181 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode1, context, step4);
184 function step4()
186 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode2, context, step5);
189 function step5()
191 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode1, context, next);
195 function testEvaluateEditReload(next)
197 function evaluateSnippetAndReloadPage(uiSourceCode, callback)
199 InspectorTest.addSniffer(WebInspector.ScriptSnippetModel.prototype, "_printRunScriptResult", snippetFinished);
200 WebInspector.scriptSnippetModel.evaluateScriptSnippet(WebInspector.context.flavor(WebInspector.ExecutionContext), uiSourceCode);
202 function snippetFinished(result, wasThrown)
204 var script = snippetScriptMapping._scriptForUISourceCode.get(uiSourceCode);
205 InspectorTest.addResult("Snippet execution result: " + result.description);
207 InspectorTest.reloadPage(callback)
211 resetSnippetsSettings();
212 var snippetScriptMapping = WebInspector.scriptSnippetModel.snippetScriptMapping(WebInspector.targetManager.targets()[0]);
214 WebInspector.scriptSnippetModel.project().createFile("", null, "", step3.bind(this));
216 function step3(path)
218 var uiSourceCode1 = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
219 uiSourceCode1.rename("Snippet1", function() { });
220 var content = "";
221 content += "// This snippet does nothing.\n";
222 content += "var i = 2+2;\n";
223 uiSourceCode1.setWorkingCopy(content);
225 evaluateSnippetAndReloadPage(uiSourceCode1, next);
229 function testEvaluateInWorker(next)
231 var context;
233 InspectorTest.addSniffer(WebInspector.RuntimeModel.prototype, "_executionContextCreated", contextCreated);
234 InspectorTest.evaluateInPage("startWorker()");
236 function contextCreated()
238 // Take the only execution context from the worker's RuntimeModel.
239 context = this.executionContexts()[0];
241 resetSnippetsSettings();
242 WebInspector.scriptSnippetModel.project().createFile("", null, "", step2.bind(this));
245 function step2(path)
247 var uiSourceCode = WebInspector.scriptSnippetModel.project().uiSourceCode(path);
248 uiSourceCode.rename("Snippet1", function() { });
249 var content = "2+2;\n";
250 uiSourceCode.setWorkingCopy(content);
251 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode, context, next);
256 </script>
257 </head>
258 <body onload="runTest()">
259 <p>Tests script snippet model.</p>
260 </body>
261 </html>