Web Inspector: Create JavaScriptSources based on network resources.
[WebKit-https.git] / LayoutTests / inspector / debugger / network-uisourcecode-provider.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>
7 function test()
8 {
9     var mockContentsMap = {};
10     var lastResouceId = 1;
11
12     NetworkAgent.getResponseBody = function(requestId, callback)
13     {
14         callback(undefined, mockContentsMap[requestId], false);
15     }
16
17     PageAgent.getResourceContent = function(frameId, url, callback)
18     {
19         callback(undefined, mockContentsMap[url], false);
20     }
21
22     function createResourceMock(type, withPendingRequest, content)
23     {
24         var documentURL = "http://fake.url";
25         var resourceId = ++lastResouceId + "";
26         var url = documentURL + "/" + resourceId;
27         var frameId = "frame-id";
28         var loaderId = "loader-id";
29         var mimeType;
30         switch (type) {
31         case WebInspector.resourceTypes.Document:
32             mimeType = "text/html";
33             break;
34         case WebInspector.resourceTypes.Script:
35             mimeType = "text/javascript";
36             break;
37         case WebInspector.resourceTypes.Stylesheet:
38             mimeType = "text/css";
39             break;
40         }
41
42         if (withPendingRequest) {
43             var request = new WebInspector.NetworkRequest(resourceId, url, "http://fake.url", frameId, loaderId);
44             request.type = type;
45             request.mimeType = mimeType;
46             mockContentsMap[resourceId] = content;
47         }
48         var resource = new WebInspector.Resource(request, url, documentURL, frameId, loaderId, type, mimeType);
49         mockContentsMap[url] = content;
50         WebInspector.resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, resource);
51
52         return resource;
53     }
54
55     function finishResource(resource)
56     {
57         resource.request.finished = true;
58         resource.request.dispatchEventToListeners(WebInspector.NetworkRequest.Events.FinishedLoading, resource.request);
59     }
60
61     function createNetworkUISourceCodeProvider()
62     {
63         InspectorTest.createWorkspace();
64         WebInspector.resourceTreeModel = new WebInspector.Object();
65         var networkUISourceCodeProvider = new WebInspector.NetworkUISourceCodeProvider(InspectorTest.testWorkspace);
66         return networkUISourceCodeProvider;
67     }
68
69     InspectorTest.runTestSuite([
70         function testDocumentResourceWithPendingRequest(next)
71         {
72             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
73
74             InspectorTest.addResult("Creating request.");
75             var resource = createResourceMock(WebInspector.resourceTypes.Document, true, "<document request content>");
76
77             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
78             InspectorTest.addResult("Finishing request.");
79             finishResource(resource);
80
81             function uiSourceCodeAdded(uiSourceCode)
82             {
83                 InspectorTest.dumpUISourceCode(uiSourceCode, next);
84             }
85         },
86
87         function testDocumentResource(next)
88         {
89             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
90             InspectorTest.addResult("Creating resource.");
91             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
92             var resource = createResourceMock(WebInspector.resourceTypes.Document, false, "<document resource content>");
93
94             function uiSourceCodeAdded(uiSourceCode)
95             {
96                 InspectorTest.dumpUISourceCode(uiSourceCode, next);
97             }
98         },
99
100         function testScriptResourceWithPendingRequest(next)
101         {
102             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
103
104             InspectorTest.addResult("Creating request.");
105             var resource = createResourceMock(WebInspector.resourceTypes.Script, true, "<script request content>");
106
107             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
108             InspectorTest.addResult("Finishing request.");
109             finishResource(resource);
110
111             function uiSourceCodeAdded(uiSourceCode)
112             {
113                 InspectorTest.dumpUISourceCode(uiSourceCode, next);
114             }
115         },
116
117         function testScriptResource(next)
118         {
119             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
120             InspectorTest.addResult("Creating resource.");
121             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
122             var resource = createResourceMock(WebInspector.resourceTypes.Script, false, "<script resource content>");
123
124             function uiSourceCodeAdded(uiSourceCode)
125             {
126                 InspectorTest.dumpUISourceCode(uiSourceCode, next);
127             }
128         },
129
130         function testStylesheetResourceWithPendingRequest(next)
131         {
132             var endTestCallsLeft = 2;
133             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
134             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
135             InspectorTest.addResult("Creating request.");
136             var resource = createResourceMock(WebInspector.resourceTypes.Stylesheet, true, "<stylesheet request content>");
137             InspectorTest.addResult("Finishing request.");
138             finishResource(resource);
139             endTest();
140
141             function uiSourceCodeAdded(uiSourceCode)
142             {
143                 InspectorTest.dumpUISourceCode(uiSourceCode, endTest);
144             }
145
146             function endTest()
147             {
148                 if (--endTestCallsLeft)
149                     return;
150                 next();
151             }
152         },
153
154         function testStylesheetResource(next)
155         {
156             var networkUISourceCodeProvider = createNetworkUISourceCodeProvider();
157             InspectorTest.addResult("Creating resource.");
158             InspectorTest.waitForWorkspaceUISourceCodeAddedEvent(uiSourceCodeAdded);
159             var resource = createResourceMock(WebInspector.resourceTypes.Stylesheet, false, "<stylesheet resource content>");
160
161             function uiSourceCodeAdded(uiSourceCode)
162             {
163                 InspectorTest.dumpUISourceCode(uiSourceCode, next);
164             }
165         },
166     ]);
167 };
168 </script>
169 </head>
170 <body onload="runTest()">
171 <p>Tests NetworkUISourceCodeProvider class.</p>
172 </body>
173 </html>