Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / inspector-protocol / tracing-test.js
blob230a0128be2e65d525be84614b04a88c0e671465
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var evalCallbackCallId = 3;
7 initialize_tracingHarness = function()
10 InspectorTest.startTracing = function(callback)
12 InspectorTest.startTracingWithArguments({ "categories": "-*,disabled-by-default-devtools.timeline,devtools.timeline", "type": "", "options": "" }, callback);
15 InspectorTest.startTracingAndSaveAsStream = function(callback)
17 var args = {
18 "categories": "-*,disabled-by-default-devtools.timeline,devtools.timeline",
19 "type": "",
20 "options": "",
21 "transferMode": "ReturnAsStream"
23 InspectorTest.startTracingWithArguments(args, callback);
26 InspectorTest.startTracingWithArguments = function(args, callback)
28 InspectorTest.sendCommand("Tracing.start", args, onStart);
30 function onStart(response)
32 InspectorTest.log("Recording started");
33 callback();
37 InspectorTest.stopTracing = function(callback)
39 InspectorTest.eventHandler["Tracing.tracingComplete"] = tracingComplete;
40 InspectorTest.eventHandler["Tracing.dataCollected"] = dataCollected;
41 InspectorTest.sendCommand("Tracing.end", { });
43 InspectorTest.devtoolsEvents = [];
44 function dataCollected(reply)
46 var allEvents = reply.params.value;
47 InspectorTest.devtoolsEvents = InspectorTest.devtoolsEvents.concat(allEvents.filter(function(e)
49 return /devtools.timeline/.test(e.cat);
50 }));
53 function tracingComplete(event)
55 InspectorTest.log("Tracing complete");
56 InspectorTest.eventHandler["Tracing.tracingComplete"] = null;
57 InspectorTest.eventHandler["Tracing.dataCollected"] = null;
58 callback(InspectorTest.devtoolsEvents);
62 InspectorTest.stopTracingAndReturnStream = function(callback)
64 InspectorTest.eventHandler["Tracing.tracingComplete"] = tracingComplete;
65 InspectorTest.eventHandler["Tracing.dataCollected"] = dataCollected;
66 InspectorTest.sendCommand("Tracing.end");
68 function dataCollected(reply)
70 InspectorTest.log("FAIL: dataCollected event should not be fired when returning trace as stream.");
74 function tracingComplete(event)
76 InspectorTest.log("Tracing complete");
77 InspectorTest.eventHandler["Tracing.tracingComplete"] = null;
78 InspectorTest.eventHandler["Tracing.dataCollected"] = null;
79 callback(event.params.stream);
83 InspectorTest.retrieveStream = function(streamHandle, offset, chunkSize, callback)
85 var result = "";
86 var had_eof = false;
88 var readArguments = { handle: streamHandle };
89 if (typeof chunkSize === "number")
90 readArguments.size = chunkSize;
91 var firstReadArguments = JSON.parse(JSON.stringify(readArguments));
92 if (typeof offset === "number")
93 firstReadArguments.offset = 0;
94 InspectorTest.sendCommandOrDie("IO.read", firstReadArguments, onChunkRead);
95 // Assure multiple in-lfight reads are fine (also, save on latencies).
96 InspectorTest.sendCommandOrDie("IO.read", readArguments, onChunkRead);
98 function onChunkRead(response)
100 if (had_eof)
101 return;
102 result += response.data;
103 if (response.eof) {
104 // Ignore stray callbacks from proactive read requests.
105 had_eof = true;
106 callback(result);
107 return;
109 InspectorTest.sendCommandOrDie("IO.read", readArguments, onChunkRead);
113 InspectorTest.findEvent = function(name, ph, condition)
115 for (var i = 0; i < InspectorTest.devtoolsEvents.length; i++) {
116 var e = InspectorTest.devtoolsEvents[i];
117 if (e.name === name && e.ph === ph && (!condition || condition(e)))
118 return e;
120 throw new Error("Couldn't find event " + name + " / " + ph + "\n\n in " + JSON.stringify(InspectorTest.devtoolsEvents, null, 2));
123 InspectorTest.invokeAsyncWithTracing = function(functionName, callback)
125 InspectorTest.startTracing(onStart);
127 function onStart()
129 InspectorTest.invokePageFunctionAsync(functionName, done);
132 function done()
134 InspectorTest.stopTracing(callback);
138 InspectorTest._lastEvalId = 0;
139 InspectorTest._pendingEvalRequests = {};
141 InspectorTest.invokePageFunctionAsync = function(functionName, callback)
143 var id = ++InspectorTest._lastEvalId;
144 InspectorTest._pendingEvalRequests[id] = callback;
145 var asyncEvalWrapper = function(callId, functionName)
147 function evalCallback(result)
149 evaluateInFrontend("InspectorTest.didInvokePageFunctionAsync(" + callId + ", " + JSON.stringify(result) + ");");
151 eval(functionName + "(" + evalCallback + ")");
153 InspectorTest.evaluateInPage("(" + asyncEvalWrapper.toString() + ")(" + id + ", unescape('" + escape(functionName) + "'))", function() { });
156 InspectorTest.didInvokePageFunctionAsync = function(callId, value)
158 var callback = InspectorTest._pendingEvalRequests[callId];
160 if (!callback) {
161 InspectorTest.addResult("Missing callback for async eval " + callId + ", perhaps callback invoked twice?");
162 return;
164 delete InspectorTest._pendingEvalRequests[callId];
165 callback(value);