Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / inspector / timeline-test.js
1 var initialize_Timeline = function() {
2
3 // Scrub values when printing out these properties in the record or data field.
4 InspectorTest.timelinePropertyFormatters = {
5     children: "formatAsTypeName",
6     endTime: "formatAsTypeName",
7     requestId: "formatAsTypeName",
8     startTime: "formatAsTypeName",
9     stackTrace: "formatAsTypeName",
10     url: "formatAsURL",
11     scriptName: "formatAsTypeName",
12     scriptId: "formatAsTypeName",
13     usedHeapSizeDelta: "skip",
14     mimeType: "formatAsTypeName",
15     id: "formatAsTypeName",
16     timerId: "formatAsTypeName",
17     scriptLine: "formatAsTypeName",
18     layerId: "formatAsTypeName",
19     lineNumber: "formatAsTypeName",
20     columnNumber: "formatAsTypeName",
21     frameId: "formatAsTypeName",
22     encodedDataLength: "formatAsTypeName",
23     identifier: "formatAsTypeName",
24     clip: "formatAsTypeName",
25     root: "formatAsTypeName",
26     backendNodeId: "formatAsTypeName",
27     networkTime: "formatAsTypeName",
28     thread: "formatAsTypeName"
29 };
30
31 InspectorTest.timelinePresentationModel = function()
32 {
33     return WebInspector.panels.timeline._currentViews[0]._presentationModel;
34 }
35
36 InspectorTest.timelineModel = function()
37 {
38     return WebInspector.panels.timeline._model;
39 }
40
41 InspectorTest.startTimeline = function(callback)
42 {
43     var panel = WebInspector.inspectorView.panel("timeline");
44     panel.toggleTimelineButton.toggled = true;
45     panel._model._collectionEnabled = true;
46     panel._userInitiatedRecording = true;
47     panel._model._currentTarget = WebInspector.targetManager.mainTarget();
48     TimelineAgent.start(5, true, undefined, true, false, callback);
49 };
50
51 InspectorTest.stopTimeline = function(callback)
52 {
53     function didStop(error)
54     {
55         if (error)
56             testRunner.logToStderr("error: " + error);
57         var panel = WebInspector.inspectorView.panel("timeline");
58         panel.toggleTimelineButton.toggled = false;
59         panel._userInitiatedRecording = false;
60         callback();
61     }
62     TimelineAgent.stop(didStop);
63 };
64
65 InspectorTest.evaluateWithTimeline = function(actions, doneCallback)
66 {
67     InspectorTest.startTimeline(step1);
68     function step1()
69     {
70         InspectorTest.evaluateInPage(actions, step2);
71     }
72
73     function step2()
74     {
75         InspectorTest.stopTimeline(doneCallback);
76     }
77 }
78
79 InspectorTest.invokeAsyncWithTimeline = function(functionName, doneCallback)
80 {
81     InspectorTest.startTimeline(step1);
82     function step1()
83     {
84         InspectorTest.invokePageFunctionAsync(functionName, step2);
85     }
86
87     function step2()
88     {
89        InspectorTest.stopTimeline(doneCallback);
90     }
91 }
92
93 InspectorTest.loadTimelineRecords = function(records)
94 {
95     var model = WebInspector.inspectorView.showPanel("timeline")._model;
96     model.reset();
97     records.forEach(model._addRecord, model);
98 }
99
100 InspectorTest.performActionsAndPrint = function(actions, typeName, includeTimeStamps)
101 {
102     function callback()
103     {
104         InspectorTest.printTimelineRecords(typeName);
105         if (includeTimeStamps) {
106             InspectorTest.addResult("Timestamp records: ");
107             InspectorTest.printTimestampRecords(typeName);
108         }
109         InspectorTest.completeTest();
110     }
111     InspectorTest.evaluateWithTimeline(actions, callback);
112 };
113
114 InspectorTest.printTimelineRecords = function(typeName, formatter)
115 {
116     InspectorTest.timelineModel().forAllRecords(InspectorTest._printTimlineRecord.bind(InspectorTest, typeName, formatter));
117 };
118
119 InspectorTest.printTimelinePresentationRecords = function(typeName, formatter)
120 {
121     InspectorTest.innerPrintTimelinePresentationRecords(WebInspector.panels.timeline._model.records(), typeName, formatter);
122 };
123
124 InspectorTest.printTimestampRecords = function(typeName, formatter)
125 {
126     InspectorTest.innerPrintTimelineRecords(InspectorTest.timelineModel().eventDividerRecords(), typeName, formatter);
127 };
128
129 InspectorTest.innerPrintTimelineRecords = function(records, typeName, formatter)
130 {
131     for (var i = 0; i < records.length; ++i)
132         InspectorTest._printTimlineRecord(typeName, formatter, records[i]);
133 };
134
135 InspectorTest._printTimlineRecord = function(typeName, formatter, record)
136 {
137     if (typeName && record.type() === WebInspector.TimelineModel.RecordType[typeName])
138         InspectorTest.printTimelineRecordProperties(record);
139     if (formatter)
140         formatter(record);
141 };
142
143
144 InspectorTest.innerPrintTimelinePresentationRecords = function(records, typeName, formatter)
145 {
146     for (var i = 0; i < records.length; ++i) {
147         if (typeName && records[i].type() === WebInspector.TimelineModel.RecordType[typeName])
148             InspectorTest.printTimelineRecordProperties(records[i]);
149         if (formatter)
150             formatter(records[i]);
151         InspectorTest.innerPrintTimelinePresentationRecords(records[i].children(), typeName, formatter);
152     }
153 };
154
155 // Dump just the record name, indenting output on separate lines for subrecords
156 InspectorTest.dumpTimelineRecord = function(record, detailsCallback, level, filterTypes)
157 {
158     if (typeof level !== "number")
159         level = 0;
160     var prefix = "";
161     var suffix = "";
162     for (var i = 0; i < level ; ++i)
163         prefix = "----" + prefix;
164     if (level > 0)
165         prefix = prefix + "> ";
166     if (record.type() === WebInspector.TimelineModel.RecordType.TimeStamp
167         || record.type() === WebInspector.TimelineModel.RecordType.ConsoleTime) {
168         suffix = " : " + record.data().message;
169     }
170     if (detailsCallback)
171         suffix += " " + detailsCallback(record);
172     InspectorTest.addResult(prefix + InspectorTest._timelineAgentTypeToString(record.type()) + suffix);
173
174     var children = record.children();
175     var numChildren = children.length;
176     for (var i = 0; i < numChildren; ++i) {
177         if (filterTypes && filterTypes.indexOf(children[i].type()) == -1)
178             continue;
179         InspectorTest.dumpTimelineRecord(children[i], detailsCallback, level + 1, filterTypes);
180     }
181 }
182
183 InspectorTest.dumpTimelineModelRecord = function(record, level)
184 {
185     if (typeof level !== "number")
186         level = 0;
187     var prefix = "";
188     for (var i = 0; i < level ; ++i)
189         prefix = "----" + prefix;
190     if (level > 0)
191         prefix = prefix + "> ";
192     InspectorTest.addResult(prefix + record.type());
193
194     var numChildren = record.children() ? record.children().length : 0;
195     for (var i = 0; i < numChildren; ++i)
196         InspectorTest.dumpTimelineModelRecord(record.children()[i], level + 1);
197 }
198
199 // Dump just the record name, indenting output on separate lines for subrecords
200 InspectorTest.dumpPresentationRecord = function(presentationRecord, detailsCallback, level, filterTypes)
201 {
202     var record = !presentationRecord.presentationParent() ? null : presentationRecord.record();
203     if (typeof level !== "number")
204         level = 0;
205     var prefix = "";
206     var suffix = "";
207     for (var i = 0; i < level ; ++i)
208         prefix = "----" + prefix;
209     if (level > 0)
210         prefix = prefix + "> ";
211     if (presentationRecord.coalesced()) {
212         suffix = " x " + presentationRecord.presentationChildren().length;
213     } else if (record && (record.type() === WebInspector.TimelineModel.RecordType.TimeStamp
214         || record.type() === WebInspector.TimelineModel.RecordType.ConsoleTime)) {
215         suffix = " : " + record.data().message;
216     }
217     if (detailsCallback)
218         suffix += " " + detailsCallback(presentationRecord);
219     var typeString = record ? InspectorTest._timelineAgentTypeToString(record.type()) : "Root";
220     InspectorTest.addResult(prefix + typeString + suffix);
221
222     var numChildren = presentationRecord.presentationChildren() ? presentationRecord.presentationChildren().length : 0;
223     for (var i = 0; i < numChildren; ++i) {
224         if (filterTypes && filterTypes.indexOf(presentationRecord.presentationChildren()[i].record().type()) == -1)
225             continue;
226         InspectorTest.dumpPresentationRecord(presentationRecord.presentationChildren()[i], detailsCallback, level + 1, filterTypes);
227     }
228 }
229
230 InspectorTest.dumpTimelineRecords = function(timelineRecords)
231 {
232     for (var i = 0; i < timelineRecords.length; ++i)
233         InspectorTest.dumpTimelineRecord(timelineRecords[i], 0);
234 };
235
236 InspectorTest.printTimelineRecordProperties = function(record)
237 {
238     var recordType = (typeof record.type === "string") ? record.type : record.type();
239     InspectorTest.addResult(InspectorTest._timelineAgentTypeToString(recordType) + " Properties:");
240     // Use this recursive routine to print the properties
241     if (record instanceof WebInspector.TimelineModel.RecordImpl)
242         record = record._record;
243     InspectorTest.addObject(record, InspectorTest.timelinePropertyFormatters);
244 };
245
246 InspectorTest._timelineAgentTypeToString = function(numericType)
247 {
248     for (var prop in WebInspector.TimelineModel.RecordType) {
249         if (WebInspector.TimelineModel.RecordType[prop] === numericType)
250             return prop;
251     }
252     return undefined;
253 };
254
255 InspectorTest.findFirstTimelineRecord = function(type)
256 {
257     var result;
258     function findByType(record)
259     {
260         if (record.type() !== type)
261             return false;
262         result = record;
263         return true;
264     }
265     InspectorTest.timelineModel().forAllRecords(findByType);
266     return result;
267 }
268
269 InspectorTest.FakeFileReader = function(input, delegate, callback)
270 {
271     this._delegate = delegate;
272     this._callback = callback;
273     this._input = input;
274     this._loadedSize = 0;
275     this._fileSize = input.length;
276 };
277
278 InspectorTest.dumpFrame = function(frame)
279 {
280     var fieldsToDump = ["cpuTime", "duration", "startTime", "endTime", "id", "mainThreadFrameId", "timeByCategory", "other", "scripting", "painting", "rendering", "committedFrom"];
281     function formatFields(object)
282     {
283         var result = {};
284         for (var key in object) {
285             if (fieldsToDump.indexOf(key) < 0)
286                 continue;
287             var value = object[key];
288             if (typeof value === "number")
289                 value = Number(value.toFixed(7));
290             else if (typeof value === "object" && value)
291                 value = formatFields(value);
292             result[key] = value;
293         }
294         return result;
295     }
296     InspectorTest.addObject(formatFields(frame));
297 }
298
299 InspectorTest.FakeFileReader.prototype = {
300     start: function(output)
301     {
302         this._delegate.onTransferStarted(this);
303
304         var length = this._input.length;
305         var half = (length + 1) >> 1;
306
307         var chunk = this._input.substring(0, half);
308         this._loadedSize += chunk.length;
309         output.write(chunk);
310         this._delegate.onChunkTransferred(this);
311
312         chunk = this._input.substring(half);
313         this._loadedSize += chunk.length;
314         output.write(chunk);
315         this._delegate.onChunkTransferred(this);
316
317         output.close();
318         this._delegate.onTransferFinished(this);
319
320         this._callback();
321     },
322
323     cancel: function() { },
324
325     loadedSize: function()
326     {
327         return this._loadedSize;
328     },
329
330     fileSize: function()
331     {
332         return this._fileSize;
333     },
334
335     fileName: function()
336     {
337         return "fakeFile";
338     }
339 };
340
341 };