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
)
18 "categories": "-*,disabled-by-default-devtools.timeline,devtools.timeline",
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");
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
);
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
)
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
)
102 result
+= response
.data
;
104 // Ignore stray callbacks from proactive read requests.
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
)))
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
);
129 InspectorTest
.invokePageFunctionAsync(functionName
, 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
];
161 InspectorTest
.addResult("Missing callback for async eval " + callId
+ ", perhaps callback invoked twice?");
164 delete InspectorTest
._pendingEvalRequests
[callId
];