Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / inspector / timeline-test.js
blob588e57d31d472a8a35e4922d2bdb88522187c6c3
1 var initialize_Timeline = function() {
3 InspectorTest.preloadPanel("timeline");
4 WebInspector.TempFile = InspectorTest.TempFileMock;
6 // Scrub values when printing out these properties in the record or data field.
7 InspectorTest.timelinePropertyFormatters = {
8 children: "formatAsTypeName",
9 endTime: "formatAsTypeName",
10 requestId: "formatAsTypeName",
11 startTime: "formatAsTypeName",
12 stackTrace: "formatAsTypeName",
13 url: "formatAsURL",
14 scriptName: "formatAsTypeName",
15 scriptId: "formatAsTypeName",
16 usedHeapSizeDelta: "skip",
17 mimeType: "formatAsTypeName",
18 id: "formatAsTypeName",
19 timerId: "formatAsTypeName",
20 scriptLine: "formatAsTypeName",
21 layerId: "formatAsTypeName",
22 lineNumber: "formatAsTypeName",
23 columnNumber: "formatAsTypeName",
24 frameId: "formatAsTypeName",
25 frame: "formatAsTypeName",
26 page: "formatAsTypeName",
27 encodedDataLength: "formatAsTypeName",
28 identifier: "formatAsTypeName",
29 clip: "formatAsTypeName",
30 root: "formatAsTypeName",
31 backendNodeId: "formatAsTypeName",
32 nodeId: "formatAsTypeName",
33 rootNode: "formatAsTypeName",
34 networkTime: "formatAsTypeName",
35 thread: "formatAsTypeName"
38 InspectorTest.InvalidationFormatters = {
39 _tracingEvent: "skip",
40 cause: "formatAsInvalidationCause",
41 frame: "skip",
42 invalidatedSelectorId: "skip",
43 invalidationList: "skip",
44 invalidationSet: "skip",
45 linkedRecalcStyleEvent: "skip",
46 linkedLayoutEvent: "skip",
47 nodeId: "skip",
48 paintId: "skip",
49 startTime: "skip",
52 InspectorTest.formatters.formatAsInvalidationCause = function(cause)
54 if (!cause)
55 return "<undefined>";
56 var stackTrace;
57 if (cause.stackTrace && cause.stackTrace.length)
58 stackTrace = InspectorTest.formatters.formatAsURL(cause.stackTrace[0].url) + ":" + cause.stackTrace[0].lineNumber;
59 return "{reason: " + cause.reason + ", stackTrace: " + stackTrace + "}";
62 InspectorTest.switchTimelineToWaterfallMode = function()
64 if (WebInspector.panels.timeline._flameChartToggleButton.toggled())
65 WebInspector.panels.timeline._flameChartToggleButton.element.click();
68 InspectorTest.timelinePresentationModel = function()
70 InspectorTest.switchTimelineToWaterfallMode();
71 return WebInspector.panels.timeline._currentViews[0]._presentationModel;
74 InspectorTest.timelineModel = function()
76 return WebInspector.panels.timeline._model;
79 InspectorTest.timelineFrameModel = function()
81 return WebInspector.panels.timeline._frameModel();
84 InspectorTest.startTimeline = function(callback)
86 var panel = WebInspector.panels.timeline;
87 function onRecordingStarted()
89 panel._model.removeEventListener(WebInspector.TimelineModel.Events.RecordingStarted, onRecordingStarted, this)
90 callback();
92 panel._model.addEventListener(WebInspector.TimelineModel.Events.RecordingStarted, onRecordingStarted, this)
93 panel._enableJSSamplingSettingSetting.set(false);
94 panel.toggleTimelineButton.element.click();
97 InspectorTest.stopTimeline = function(callback)
99 var panel = WebInspector.panels.timeline;
100 function didStop()
102 panel._model.removeEventListener(WebInspector.TimelineModel.Events.RecordingStopped, didStop, this)
103 InspectorTest.runAfterPendingDispatches(callback);
105 panel._model.addEventListener(WebInspector.TimelineModel.Events.RecordingStopped, didStop, this)
106 panel.toggleTimelineButton.element.click();
109 InspectorTest.evaluateWithTimeline = function(actions, doneCallback)
111 InspectorTest.startTimeline(step1);
112 function step1()
114 InspectorTest.evaluateInPage(actions, step2);
117 function step2()
119 InspectorTest.stopTimeline(doneCallback);
123 InspectorTest.invokeAsyncWithTimeline = function(functionName, doneCallback)
125 InspectorTest.startTimeline(step1);
126 function step1()
128 InspectorTest.invokePageFunctionAsync(functionName, step2);
131 function step2()
133 InspectorTest.stopTimeline(InspectorTest.safeWrap(doneCallback));
137 InspectorTest.loadTimelineRecords = function(records)
139 var model = WebInspector.panels.timeline._model;
140 model.reset();
141 records.forEach(model._addRecord, model);
144 InspectorTest.performActionsAndPrint = function(actions, typeName, includeTimeStamps)
146 function callback()
148 InspectorTest.printTimelineRecords(typeName);
149 if (includeTimeStamps) {
150 InspectorTest.addResult("Timestamp records: ");
151 InspectorTest.printTimestampRecords(typeName);
153 InspectorTest.completeTest();
155 InspectorTest.evaluateWithTimeline(actions, callback);
158 InspectorTest.printTimelineRecords = function(typeName, formatter)
160 InspectorTest.timelineModel().forAllRecords(InspectorTest._printTimlineRecord.bind(InspectorTest, typeName, formatter));
163 InspectorTest.printTimelinePresentationRecords = function(typeName, formatter)
165 InspectorTest.innerPrintTimelinePresentationRecords(WebInspector.panels.timeline._model.records(), typeName, formatter);
168 InspectorTest.printTimestampRecords = function(typeName, formatter)
170 InspectorTest.innerPrintTimelineRecords(InspectorTest.timelineModel().eventDividerRecords(), typeName, formatter);
173 InspectorTest.innerPrintTimelineRecords = function(records, typeName, formatter)
175 for (var i = 0; i < records.length; ++i)
176 InspectorTest._printTimlineRecord(typeName, formatter, records[i]);
179 InspectorTest._printTimlineRecord = function(typeName, formatter, record)
181 if (typeName && record.type() === typeName)
182 InspectorTest.printTimelineRecordProperties(record);
183 if (formatter)
184 formatter(record);
188 InspectorTest.innerPrintTimelinePresentationRecords = function(records, typeName, formatter)
190 for (var i = 0; i < records.length; ++i) {
191 if (typeName && records[i].type() === typeName)
192 InspectorTest.printTimelineRecordProperties(records[i]);
193 if (formatter)
194 formatter(records[i]);
195 InspectorTest.innerPrintTimelinePresentationRecords(records[i].children(), typeName, formatter);
199 // Dump just the record name, indenting output on separate lines for subrecords
200 InspectorTest.dumpTimelineRecord = function(record, detailsCallback, level, filterTypes)
202 if (typeof level !== "number")
203 level = 0;
204 var message = "";
205 for (var i = 0; i < level ; ++i)
206 message = "----" + message;
207 if (level > 0)
208 message = message + "> ";
209 if (record.type() === WebInspector.TimelineModel.RecordType.TimeStamp
210 || record.type() === WebInspector.TimelineModel.RecordType.ConsoleTime) {
211 message += WebInspector.TimelineUIUtils.eventTitle(record.traceEvent());
212 } else {
213 message += record.type();
215 if (detailsCallback)
216 message += " " + detailsCallback(record);
217 InspectorTest.addResult(message);
219 var children = record.children();
220 var numChildren = children.length;
221 for (var i = 0; i < numChildren; ++i) {
222 if (filterTypes && filterTypes.indexOf(children[i].type()) == -1)
223 continue;
224 InspectorTest.dumpTimelineRecord(children[i], detailsCallback, level + 1, filterTypes);
228 InspectorTest.dumpTimelineModelRecord = function(record, level)
230 if (typeof level !== "number")
231 level = 0;
232 var prefix = "";
233 for (var i = 0; i < level ; ++i)
234 prefix = "----" + prefix;
235 if (level > 0)
236 prefix = prefix + "> ";
237 InspectorTest.addResult(prefix + record.type() + ": " + (WebInspector.TimelineUIUtils.buildDetailsTextForTraceEvent(record.traceEvent(), null) || ""));
239 var numChildren = record.children() ? record.children().length : 0;
240 for (var i = 0; i < numChildren; ++i)
241 InspectorTest.dumpTimelineModelRecord(record.children()[i], level + 1);
244 // Dump just the record name, indenting output on separate lines for subrecords
245 InspectorTest.dumpPresentationRecord = function(presentationRecord, detailsCallback, level, filterTypes)
247 var record = !presentationRecord.presentationParent() ? null : presentationRecord.record();
248 if (typeof level !== "number")
249 level = 0;
250 var message = "";
251 for (var i = 0; i < level ; ++i)
252 message = "----" + message;
253 if (level > 0)
254 message = message + "> ";
255 if (!record) {
256 message += "Root";
257 } else if (presentationRecord.coalesced()) {
258 message += record.type() + " x " + presentationRecord.presentationChildren().length;
259 } else if (record.type() === WebInspector.TimelineModel.RecordType.TimeStamp
260 || record.type() === WebInspector.TimelineModel.RecordType.ConsoleTime) {
261 message += WebInspector.TimelineUIUtils.eventTitle(record.traceEvent());
262 } else {
263 message += record.type();
265 if (detailsCallback)
266 message += " " + detailsCallback(presentationRecord);
267 InspectorTest.addResult(message);
269 var numChildren = presentationRecord.presentationChildren() ? presentationRecord.presentationChildren().length : 0;
270 for (var i = 0; i < numChildren; ++i) {
271 if (filterTypes && filterTypes.indexOf(presentationRecord.presentationChildren()[i].record().type()) == -1)
272 continue;
273 InspectorTest.dumpPresentationRecord(presentationRecord.presentationChildren()[i], detailsCallback, level + 1, filterTypes);
277 InspectorTest.dumpTimelineRecords = function(timelineRecords)
279 for (var i = 0; i < timelineRecords.length; ++i)
280 InspectorTest.dumpTimelineRecord(timelineRecords[i], 0);
283 InspectorTest.printTimelineRecordProperties = function(record)
285 InspectorTest.addResult(record.type() + " Properties:");
286 var traceEvent = record.traceEvent();
287 var data = traceEvent.args["beginData"] || traceEvent.args["data"];
288 var frameId = data && data["frame"];
289 var object = {
290 data: traceEvent.args["data"] || traceEvent.args,
291 endTime: record.endTime(),
292 frameId: frameId,
293 stackTrace: traceEvent.stackTrace,
294 startTime: record.startTime(),
295 thread: record.thread(),
296 type: record.type()
298 for (var field in object) {
299 if (object[field] === null || object[field] === undefined)
300 delete object[field];
302 if (record.children().length)
303 object["children"] = [];
304 InspectorTest.addObject(object, InspectorTest.timelinePropertyFormatters);
307 InspectorTest.findFirstTimelineRecord = function(type)
309 return InspectorTest.findTimelineRecord(type, 0);
312 // Find the (n+1)th timeline record of a specific type.
313 InspectorTest.findTimelineRecord = function(type, n)
315 var result;
316 function findByType(record)
318 if (record.type() !== type)
319 return false;
320 if (n === 0) {
321 result = record;
322 return true;
324 n--;
325 return false;
327 InspectorTest.timelineModel().forAllRecords(findByType);
328 return result;
331 InspectorTest.FakeFileReader = function(input, delegate, callback)
333 this._delegate = delegate;
334 this._callback = callback;
335 this._input = input;
336 this._loadedSize = 0;
337 this._fileSize = input.length;
340 InspectorTest.dumpFrame = function(frame)
342 var fieldsToDump = ["cpuTime", "duration", "startTime", "endTime", "id", "mainThreadFrameId", "timeByCategory", "other", "scripting", "painting", "rendering", "committedFrom", "idle"];
343 function formatFields(object)
345 var result = {};
346 for (var key in object) {
347 if (fieldsToDump.indexOf(key) < 0)
348 continue;
349 var value = object[key];
350 if (typeof value === "number")
351 value = Number(value.toFixed(7));
352 else if (typeof value === "object" && value)
353 value = formatFields(value);
354 result[key] = value;
356 return result;
358 InspectorTest.addObject(formatFields(frame));
361 InspectorTest.FakeFileReader.prototype = {
362 start: function(output)
364 this._delegate.onTransferStarted(this);
366 var length = this._input.length;
367 var half = (length + 1) >> 1;
369 var chunk = this._input.substring(0, half);
370 this._loadedSize += chunk.length;
371 output.write(chunk);
372 this._delegate.onChunkTransferred(this);
374 chunk = this._input.substring(half);
375 this._loadedSize += chunk.length;
376 output.write(chunk);
377 this._delegate.onChunkTransferred(this);
379 output.close();
380 this._delegate.onTransferFinished(this);
382 this._callback();
385 cancel: function() { },
387 loadedSize: function()
389 return this._loadedSize;
392 fileSize: function()
394 return this._fileSize;
397 fileName: function()
399 return "fakeFile";
405 function generateFrames(count, callback)
407 makeFrame();
408 function makeFrame()
410 document.body.style.backgroundColor = count & 1 ? "rgb(200, 200, 200)" : "rgb(240, 240, 240)";
411 if (!--count) {
412 callback();
413 return;
415 if (window.testRunner)
416 testRunner.capturePixelsAsyncThen(requestAnimationFrame.bind(window, makeFrame));
417 else
418 window.requestAnimationFrame(makeFrame);