Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / sources / debugger / file-system-project-mapping.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="../../../http/tests/inspector/workspace-test.js"></script>
6 <script src="../../../http/tests/inspector/isolated-filesystem-test.js"></script>
7 <script src="../../../http/tests/inspector/live-edit-test.js"></script>
8 <script src="resources/edit-me.js"></script>
9 <script>
10 function test()
11 {
12     var mockTargetId = 1;
13     var MockTarget = function(name, connection, callback)
14     {
15         WebInspector.Target.call(this, name, connection, callback);
16     }
17     MockTarget.prototype = {
18         _loadedWithCapabilities: function(callback)
19         {
20             this.resourceTreeModel = new WebInspector.Object();
21             this.debuggerModel = new WebInspector.DebuggerModel(this);
22             this.runtimeModel = WebInspector.targetManager.mainTarget().runtimeModel;
23             this.consoleModel = WebInspector.targetManager.mainTarget().consoleModel;
24
25             if (callback)
26                 callback();
27         },
28
29         __proto__: WebInspector.Target.prototype
30     }
31
32     function createMockTarget(userCallback, useRealTarget)
33     {
34         var target = new MockTarget("mock-target-" + (mockTargetId++), new InspectorBackendClass.StubConnection(), callback);
35         function callback()
36         {
37             InspectorTest.testTargetManager.addTarget(target);
38             setTimeout(function() { userCallback(target); }, 0);
39         }
40     }
41
42     function createWorkspaceWithTarget(userCallback, useRealTarget)
43     {
44         InspectorTest.createWorkspace();
45         createMockTarget(callback, useRealTarget);
46         function callback(target)
47         {
48             target.resourceTreeModel = WebInspector.targetManager.mainTarget().resourceTreeModel;
49             userCallback(target);
50         }
51     }
52
53     function dumpUISourceCodes(uiSourceCodes, next)
54     {
55         innerDumpUISourceCodes(uiSourceCodes, 0, next);
56
57         function innerDumpUISourceCodes(uiSourceCodes, startIndex, next)
58         {
59             InspectorTest.addResult("");
60             if (startIndex === uiSourceCodes.length) {
61                 next();
62                 return;
63             }
64             InspectorTest.dumpUISourceCode(uiSourceCodes[startIndex], innerDumpUISourceCodes.bind(this, uiSourceCodes, startIndex + 1, next));
65         }
66     }
67
68     var manager;
69     var resourceScriptMapping;
70     var defaultScriptMapping;
71
72     function createObjects(userCallback, useRealTarget)
73     {
74         createWorkspaceWithTarget(callback, useRealTarget);
75         function callback(target)
76         {
77             manager = InspectorTest.createIsolatedFileSystemManager(InspectorTest.testWorkspace, InspectorTest.testFileSystemMapping);
78             var entry = InspectorTest.testDebuggerWorkspaceBinding._targetToData.get(target);
79             resourceScriptMapping = entry._resourceMapping;
80             defaultScriptMapping = entry._defaultMapping;
81             userCallback();
82         }
83     }
84
85     InspectorTest.runTestSuite([
86         function testAutomaticMapping(next)
87         {
88             function uiSourceCodeAdded(uiSourceCode) { }
89
90             function dumpFileSystemUISourceCodesMappings()
91             {
92                 var uiSourceCodes = InspectorTest.testWorkspace.project(fileSystemProjectId).uiSourceCodes();
93                 InspectorTest.addResult("UISourceCode uri to url mappings:");
94                 for (var i = 0; i < uiSourceCodes.length; ++i)
95                     InspectorTest.addResult("    " + uiSourceCodes[i].uri() + " -> " + uiSourceCodes[i].url);
96             }
97
98             var fileSystemPath = "/var/www";
99             var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
100             var files = {"/html/foo.js": "<foo content>", "/bar.js": "<bar content>"};
101             var uiSourceCode;
102             var networkUISourceCode;
103             var setting;
104
105             createObjects(step1);
106             function step1()
107             {
108                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 4);
109                 InspectorTest.addResult("Adding file system.");
110                 manager.addMockFileSystem(fileSystemPath);
111                 manager.addFiles(fileSystemPath, files);
112                 InspectorTest.addResult("Adding network resource.");
113                 InspectorTest.addMockUISourceCodeToWorkspace("http://localhost/html/foo.js", WebInspector.resourceTypes.Script, "<foo content>");
114                 InspectorTest.addMockUISourceCodeToWorkspace("http://localhost/bar.js", WebInspector.resourceTypes.Script, "<foo content>");
115                 dumpFileSystemUISourceCodesMappings();
116                 uiSourceCode = InspectorTest.testWorkspace.uiSourceCode(fileSystemProjectId, "html/foo.js");
117                 networkUISourceCode = InspectorTest.testWorkspace.uiSourceCode("http://localhost", "html/foo.js");
118                 InspectorTest.override(WebInspector.SourcesPanel.prototype, "_suggestReload", function() { });
119                 InspectorTest.addResult("Adding mapping between network and file system resources.");
120                 InspectorTest.testWorkspace.addMapping(networkUISourceCode, uiSourceCode, manager.fileSystemWorkspaceBinding);
121                 setting = InspectorTest.testFileSystemMapping._fileSystemMappingSetting;
122                 InspectorTest.addResult("Emulate reloading inspector.");
123                 createObjects(step2);
124             }
125
126             function step2()
127             {
128                 InspectorTest.testFileSystemMapping._fileSystemMappingSetting = setting;
129                 InspectorTest.testFileSystemMapping._loadFromSettings();
130                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 2);
131                 manager.addMockFileSystem(fileSystemPath, true);
132                 manager.addFiles(fileSystemPath, files);
133                 dumpFileSystemUISourceCodesMappings();
134                 InspectorTest.addResult("Removing mapping between network and file system resources.");
135                 uiSourceCode = InspectorTest.testWorkspace.uiSourceCode(fileSystemProjectId, "html/foo.js");
136                 InspectorTest.testWorkspace.removeMapping(uiSourceCode);
137
138                 InspectorTest.addResult("Emulate reloading inspector.");
139                 createObjects(step3);
140             }
141
142             function step3()
143             {
144                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 2);
145                 manager.addMockFileSystem(fileSystemPath);
146                 manager.addFiles(fileSystemPath, files);
147                 dumpFileSystemUISourceCodesMappings();
148
149                 InspectorTest.testFileSystemMapping.removeMappingForURL(networkUISourceCode.url);
150                 next();
151             }
152         },
153
154         function testScriptFileOnReloadWithDirtyFile(next)
155         {
156             var originalFileContent = "<foo content>";
157             var scriptURL = "http://localhost/html/foo.js";
158             var scriptContent = originalFileContent + "\n  //# sourceURL=" + scriptURL + "  \n ";
159             function uiSourceCodeAdded(uiSourceCode) { }
160
161             function unloadScript()
162             {
163                 resourceScriptMapping._debuggerReset();
164                 defaultScriptMapping._debuggerReset();
165             }
166
167             function loadScript()
168             {
169                 script = InspectorTest.createScriptMock(scriptURL, 0, 0, false, scriptContent, InspectorTest.testTargetManager.targets()[0]);
170                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 1);
171                 defaultScriptMapping.addScript(script);
172                 resourceScriptMapping.addScript(script);
173             }
174
175             var fileSystemPath = "/var/www";
176             var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
177             var files = {"/html/foo.js": originalFileContent, "/bar.js": "<bar content>"};
178             var uiSourceCode;
179             var target;
180             createObjects(step1, true);
181
182             function step1()
183             {
184                 InspectorTest._originalDebuggerBinding = WebInspector.debuggerWorkspaceBinding;
185                 WebInspector.debuggerWorkspaceBinding = InspectorTest.testDebuggerWorkspaceBinding;
186                 target = InspectorTest.testTargetManager.targets()[0];
187                 WebInspector._originalTargetManager = WebInspector.targetManager;
188                 WebInspector.targetManager = InspectorTest.testTargetManager;
189                 InspectorTest.addResult("Adding file system.");
190                 manager.addMockFileSystem(fileSystemPath);
191                 InspectorTest.addResult("Adding file system mapping.");
192                 InspectorTest.testFileSystemMapping.addFileMapping(fileSystemPath, "http://localhost/", "/");
193                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 2);
194                 manager.addFiles(fileSystemPath, files);
195
196                 loadScript();
197
198                 uiSourceCode = InspectorTest.testWorkspace.uiSourceCode(fileSystemProjectId, "html/foo.js");
199                 InspectorTest.showUISourceCode(uiSourceCode, didShowScriptSource);
200             }
201
202             function dumpUISourceCodeAndScriptContents()
203             {
204                 InspectorTest.addResult("Dumping uiSourceCode and script content:");
205                 InspectorTest.addResult("    uiSourceCode: " + uiSourceCode.workingCopy());
206                 InspectorTest.addResult("    script: " + scriptContent);
207             }
208
209             function didShowScriptSource(sourceFrame)
210             {
211                 dumpUISourceCodeAndScriptContents();
212                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
213                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
214
215                 InspectorTest.addResult("Editing uiSourceCode:");
216                 uiSourceCode.setWorkingCopy("<foo content edited>");
217                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
218                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
219
220                 function setScriptSourceOverrideFailure(scriptId, newContent, callback)
221                 {
222                     callback("error");
223                 }
224                 InspectorTest.override(WebInspector.DebuggerModel.prototype, "setScriptSource", setScriptSourceOverrideFailure);
225
226                 InspectorTest.addResult("Committing uiSourceCode with live edit failure:");
227                 uiSourceCode.commitWorkingCopy(function() { });
228                 dumpUISourceCodeAndScriptContents();
229                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
230                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
231
232                 InspectorTest.addResult("Reloading with saved but diverged uiSourceCode:");
233                 unloadScript();
234                 InspectorTest.assertTrue(!resourceScriptMapping.scriptFile(uiSourceCode), "UISourceCode should not have script file after unloading scripts.");
235                 InspectorTest.assertTrue(!sourceFrame._muted, "Saved but diverged source frame should not be muted");
236
237                 InspectorTest.addResult("Loading script.");
238                 loadScript();
239                 dumpUISourceCodeAndScriptContents();
240                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
241                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
242
243                 InspectorTest.addResult("Editing uiSourceCode again:");
244                 uiSourceCode.setWorkingCopy("<foo content edited again>");
245                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
246                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
247
248                 function setScriptSourceOverrideSuccess(scriptId, newContent, callback)
249                 {
250                     scriptContent = newContent;
251                     callback();
252                 }
253                 InspectorTest.override(WebInspector.DebuggerModel.prototype, "setScriptSource", setScriptSourceOverrideSuccess);
254
255                 InspectorTest.addResult("Committing uiSourceCode again (with live edit success now):");
256                 uiSourceCode.commitWorkingCopy(function() { });
257                 dumpUISourceCodeAndScriptContents();
258                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
259                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
260
261                 InspectorTest.addResult("Reloading page:");
262                 unloadScript();
263                 InspectorTest.assertTrue(!resourceScriptMapping.scriptFile(uiSourceCode), "UISourceCode should not have script file after unloading scripts.");
264                 InspectorTest.assertTrue(!sourceFrame._muted, "Saved and merged source frame should not be muted");
265                 InspectorTest.addResult("Loading script.");
266                 loadScript();
267                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
268                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
269
270                 InspectorTest.addResult("Editing uiSourceCode again and reloading while it is dirty:");
271                 uiSourceCode.setWorkingCopy("<foo content edited and dirty>");
272                 unloadScript();
273                 InspectorTest.assertTrue(!resourceScriptMapping.scriptFile(uiSourceCode), "UISourceCode should not have script file after unloading scripts.");
274                 InspectorTest.assertTrue(sourceFrame._muted, "Dirty source frame should be muted");
275                 InspectorTest.addResult("Loading script.");
276                 loadScript();
277                 InspectorTest.addResult(" - hasDivergedFromVM: " + !!resourceScriptMapping.scriptFile(uiSourceCode).hasDivergedFromVM());
278                 InspectorTest.addResult(" - sourceFrame._muted: " + !!sourceFrame._muted);
279
280                 unloadScript();
281                 loadScript;
282
283                 InspectorTest.testFileSystemMapping.removeFileMapping(fileSystemPath, "http://localhost/", "/");
284                 WebInspector.debuggerWorkspaceBinding = InspectorTest._originalDebuggerBinding;
285                 next();
286             }
287         },
288
289         function testLiveEditReload(next)
290         {
291             InspectorTest.showScriptSource("edit-me.js", didShowScriptSource);
292             
293             function didShowScriptSource(sourceFrame)
294             {
295                 var scriptURL = sourceFrame._uiSourceCode.url;
296
297                 var fileContent = "function f()\n{\n    return 0;\n}\n";
298                 var fileSystemPath = "/var/www";
299                 var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
300                 var filePath = "/html/edit-me.js"; 
301                 var files = {};
302                 files[filePath] = fileContent;
303                 InspectorTest.addResult("Adding file system.");
304                 InspectorTest.addMockFileSystem(fileSystemPath)
305                 InspectorTest.addResult("Adding file system mapping.");
306                 WebInspector.isolatedFileSystemManager.mapping().addMappingForResource(scriptURL, fileSystemPath, filePath.substr(1));
307                 InspectorTest.addFilesToMockFileSystem(fileSystemPath, files);
308                 // Now that we've set everything up, we need to reload a page to remove all network resources.
309                 InspectorTest.reloadPage(pageReloaded);
310             }
311
312             function pageReloaded()
313             {
314                 // Now reload again as part of test.
315                 InspectorTest.reloadPage(pageReloadedAgain);
316             }
317
318             function pageReloadedAgain()
319             {
320                 WebInspector.targetManager = WebInspector._originalTargetManager;
321                 InspectorTest.showScriptSource("edit-me.js", didShowFile);
322             }
323
324             function didShowFile(sourceFrame)
325             {
326                 replaceInSource(sourceFrame, "return 0;", "return \"live-edited string\";", didEditScriptSource);
327             }
328
329             function didEditScriptSource()
330             {
331                 InspectorTest.evaluateInPage("f()", didEvaluateInPage)
332             }
333
334             function didEvaluateInPage(result)
335             {
336                 InspectorTest.assertEquals("live-edited string", result.description, "edited function returns wrong result");
337                 var panel = WebInspector.panels.sources;
338                 InspectorTest.dumpSourceFrameContents(panel.visibleView);
339                 next();
340             }
341
342             function replaceInSource(sourceFrame, string, replacement, callback)
343             {
344                 InspectorTest.addSniffer(WebInspector.DebuggerModel.prototype, "_didEditScriptSource", callback);
345                 InspectorTest.replaceInSource(sourceFrame, string, replacement);
346                 InspectorTest.commitSource(sourceFrame);
347             }
348         },
349
350         function testExcludingFolders(next)
351         {
352             function uiSourceCodeAdded(uiSourceCode) {}
353             function uiSourceCodeRemoved(uiSourceCode) {}
354
355             function dumpWorkspaceUISourceCodes()
356             {
357                 InspectorTest.addResult("Dumping uiSourceCodes origin URLs:");
358                 var uiSourceCodes = InspectorTest.testWorkspace.uiSourceCodes();
359                 for (var i = 0; i < uiSourceCodes.length; ++i)
360                     InspectorTest.addResult("  - " + uiSourceCodes[i].originURL());
361             }
362
363             var fileSystemPath = "/var/www";
364             var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
365             var files = {"/html/foo.js": "", "/.git/foogit.js": "", "/bar.js": "", "/html2/foo.js": ""};
366             createObjects(step1);
367
368             function step1()
369             {
370                 InspectorTest.addResult("Adding file system.");
371                 manager.addMockFileSystem(fileSystemPath);
372                 InspectorTest.addResult("Adding exclusion pattern and excluded folder.");
373                 InspectorTest.testFileSystemMapping.addExcludedFolder(fileSystemPath, "/html/");
374                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 2);
375                 manager.addFiles(fileSystemPath, files);
376                 dumpWorkspaceUISourceCodes();
377                 InspectorTest.addResult("Excluding html2 folder:");
378                 InspectorTest.waitForWorkspaceUISourceCodeRemovedEvent(uiSourceCodeRemoved);
379                 InspectorTest.testWorkspace.uiSourceCodes()[0].project().excludeFolder("/html2/");
380                 dumpWorkspaceUISourceCodes();
381                 next();
382             }
383         },
384
385         function testRemoveProject(next)
386         {
387             function uiSourceCodeAdded(uiSourceCode) {}
388             function uiSourceCodeRemoved(uiSourceCode) {}
389
390             function dumpWorkspaceUISourceCodes()
391             {
392                 InspectorTest.addResult("Dumping uiSourceCodes origin URLs:");
393                 var uiSourceCodes = InspectorTest.testWorkspace.uiSourceCodes();
394                 for (var i = 0; i < uiSourceCodes.length; ++i)
395                     InspectorTest.addResult("  - " + uiSourceCodes[i].originURL());
396             }
397
398             var fileSystemPath = "/var/www";
399             var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
400             var files = {"/foo.js": ""};
401             createObjects(step1);
402
403             function step1()
404             {
405                 InspectorTest.addResult("Adding file system.");
406                 manager.addMockFileSystem(fileSystemPath);
407                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 2);
408                 manager.addFiles(fileSystemPath, files);
409                 dumpWorkspaceUISourceCodes();
410                 InspectorTest.addResult("Removing project:");
411                 var project = InspectorTest.testWorkspace.uiSourceCodes()[0].project();
412                 InspectorTest.testWorkspace.addEventListener(WebInspector.Workspace.Events.ProjectRemoved, projectRemoved);
413                 project.remove();
414             }
415
416             function projectRemoved()
417             {
418                 InspectorTest.addResult("Received project removed event.");
419                 next();
420             }
421         },
422
423         function testURLAfterRenaming(next)
424         {
425             function uiSourceCodeAdded(uiSourceCode) { }
426
427             var fileSystemPath = "/var/www";
428             var fileSystemProjectId = WebInspector.FileSystemWorkspaceBinding.projectId(fileSystemPath);
429             var files = {"/html/foo.js": "<foo content>", "/bar.js": "<bar content>", "/foo/bar/baz.js": "<baz content>"};
430             var uiSourceCodes;
431             var index = 0;
432             var oldURL;
433
434             createObjects(objectsCreated);
435
436             function objectsCreated()
437             {
438                 InspectorTest.addResult("Adding file system.");
439                 manager.addMockFileSystem(fileSystemPath);
440                 InspectorTest.addResult("Adding file system mapping.");
441                 InspectorTest.testFileSystemMapping.addFileMapping(fileSystemPath, "http://localhost/", "/");
442                 InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded, 3);
443                 manager.addFiles(fileSystemPath, files);
444
445                 InspectorTest.addResult("Renaming files:");
446                 uiSourceCodes = InspectorTest.testWorkspace.uiSourceCodes();
447                 renameNextFile();
448             }
449
450             function renameNextFile()
451             {
452                 var uiSourceCode = uiSourceCodes[index];
453                 oldURL = uiSourceCode.url;
454                 uiSourceCodes[index].rename("newName.js", fileRenamed);
455             }
456
457             function fileRenamed()
458             {
459                 var uiSourceCode = uiSourceCodes[index];
460                 InspectorTest.addResult("    uiSourceCode URL change after renaming: " + oldURL + " -> " + uiSourceCode.url);
461                 ++index;
462                 if (index < uiSourceCodes.length)
463                     renameNextFile();
464                 else
465                     next();
466             }
467         },
468     ]);
469 };
470 </script>
471 </head>
472 <body onload="runTest()">
473 <p>Tests file system project mappings.</p>
474 </body>
475 </html>