3 <script src=
"../../../http/tests/inspector/inspector-test.js"></script>
4 <script src=
"../../../http/tests/inspector/debugger-test.js"></script>
8 var workerScript
= "postMessage('Done.');";
9 var blob
= new Blob([workerScript
], { type
: "text/javascript" });
10 var worker
= new Worker(URL
.createObjectURL(blob
));
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
);
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
)
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
)
76 InspectorTest
.addResult("Snippet renamed successfully.");
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));
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));
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));
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
);
148 function testEvaluate(next
)
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));
161 uiSourceCode1
= WebInspector
.scriptSnippetModel
.project().uiSourceCode(path
);
162 uiSourceCode1
.rename("Snippet1", function() { });
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));
172 uiSourceCode2
= WebInspector
.scriptSnippetModel
.project().uiSourceCode(path
);
173 uiSourceCode2
.rename("Snippet2", function() { });
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";
179 content
+= "doesNothing;\n";
180 uiSourceCode2
.setWorkingCopy(content
);
181 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode1
, context
, step4
);
186 evaluateSnippetAndDumpEvaluationDetails(uiSourceCode2
, context
, 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));
218 var uiSourceCode1
= WebInspector
.scriptSnippetModel
.project().uiSourceCode(path
);
219 uiSourceCode1
.rename("Snippet1", function() { });
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
)
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));
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
);
258 <body onload=
"runTest()">
259 <p>Tests script snippet model.
</p>