| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var evalCallbackCallId = 3; | 5 (class TracingHelper { |
| 6 constructor(testRunner, session) { |
| 7 this._testRunner = testRunner; |
| 8 this._session = session; |
| 9 } |
| 6 | 10 |
| 7 initialize_tracingHarness = function() | 11 startTracing() { |
| 8 { | 12 return this.startTracingWithArguments({ "categories": "-*,disabled-by-defaul
t-devtools.timeline,devtools.timeline", "type": "", "options": "" }); |
| 13 } |
| 9 | 14 |
| 10 InspectorTest.startTracing = function(callback) | 15 startTracingAndSaveAsStream() { |
| 11 { | 16 var args = { |
| 12 InspectorTest.startTracingWithArguments({ "categories": "-*,disabled-by-defa
ult-devtools.timeline,devtools.timeline", "type": "", "options": "" }, callback)
; | 17 "categories": "-*,disabled-by-default-devtools.timeline,devtools.timeline"
, |
| 13 } | 18 "type": "", |
| 19 "options": "", |
| 20 "transferMode": "ReturnAsStream" |
| 21 }; |
| 22 return this.startTracingWithArguments(args); |
| 23 } |
| 14 | 24 |
| 15 InspectorTest.startTracingAndSaveAsStream = function(callback) | 25 async startTracingWithArguments(args) { |
| 16 { | 26 await this._session.protocol.Tracing.start(args); |
| 17 var args = { | 27 this._testRunner.log("Recording started"); |
| 18 "categories": "-*,disabled-by-default-devtools.timeline,devtools.timelin
e", | 28 } |
| 19 "type": "", | 29 |
| 20 "options": "", | 30 async stopTracing() { |
| 21 "transferMode": "ReturnAsStream" | 31 var devtoolsEvents = []; |
| 32 |
| 33 function dataCollected(reply) { |
| 34 var allEvents = reply.params.value; |
| 35 var filteredEvents = allEvents.filter(e => /devtools.timeline/.test(e.cat)
); |
| 36 devtoolsEvents = devtoolsEvents.concat(filteredEvents); |
| 22 }; | 37 }; |
| 23 InspectorTest.startTracingWithArguments(args, callback); | |
| 24 } | |
| 25 | 38 |
| 26 InspectorTest.startTracingWithArguments = function(args, callback) | 39 this._session.protocol.Tracing.onDataCollected(dataCollected); |
| 27 { | 40 this._session.protocol.Tracing.end(); |
| 28 InspectorTest.sendCommand("Tracing.start", args, onStart); | 41 await this._session.protocol.Tracing.onceTracingComplete(); |
| 42 this._testRunner.log("Tracing complete"); |
| 43 this._session.protocol.Tracing.offDataCollected(dataCollected); |
| 44 this._devtoolsEvents = devtoolsEvents; |
| 45 return devtoolsEvents; |
| 46 } |
| 29 | 47 |
| 30 function onStart(response) | 48 async stopTracingAndReturnStream() { |
| 31 { | 49 function dataCollected() { |
| 32 InspectorTest.log("Recording started"); | 50 this._testRunner.log("FAIL: dataCollected event should not be fired when r
eturning trace as stream."); |
| 33 callback(); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 InspectorTest.stopTracing = function(callback) | |
| 38 { | |
| 39 InspectorTest.eventHandler["Tracing.tracingComplete"] = tracingComplete; | |
| 40 InspectorTest.eventHandler["Tracing.dataCollected"] = dataCollected; | |
| 41 InspectorTest.sendCommand("Tracing.end", { }); | |
| 42 | |
| 43 InspectorTest.devtoolsEvents = []; | |
| 44 function dataCollected(reply) | |
| 45 { | |
| 46 var allEvents = reply.params.value; | |
| 47 InspectorTest.devtoolsEvents = InspectorTest.devtoolsEvents.concat(allEv
ents.filter(function(e) | |
| 48 { | |
| 49 return /devtools.timeline/.test(e.cat); | |
| 50 })); | |
| 51 } | 51 } |
| 52 | 52 |
| 53 function tracingComplete(event) | 53 this._session.protocol.Tracing.onDataCollected(dataCollected); |
| 54 { | 54 this._session.protocol.Tracing.end(); |
| 55 InspectorTest.log("Tracing complete"); | 55 var event = await this._session.protocol.Tracing.onceTracingComplete(); |
| 56 InspectorTest.eventHandler["Tracing.tracingComplete"] = null; | 56 this._testRunner.log("Tracing complete"); |
| 57 InspectorTest.eventHandler["Tracing.dataCollected"] = null; | 57 this._session.protocol.Tracing.offDataCollected(dataCollected); |
| 58 callback(InspectorTest.devtoolsEvents); | 58 return event.params.stream; |
| 59 } | 59 } |
| 60 } | |
| 61 | 60 |
| 62 InspectorTest.stopTracingAndReturnStream = function(callback) | 61 retrieveStream(streamHandle, offset, chunkSize) { |
| 63 { | 62 var callback; |
| 64 InspectorTest.eventHandler["Tracing.tracingComplete"] = tracingComplete; | 63 var promise = new Promise(f => callback = f); |
| 65 InspectorTest.eventHandler["Tracing.dataCollected"] = dataCollected; | |
| 66 InspectorTest.sendCommand("Tracing.end"); | |
| 67 | |
| 68 function dataCollected(reply) | |
| 69 { | |
| 70 InspectorTest.log("FAIL: dataCollected event should not be fired when re
turning trace as stream."); | |
| 71 | |
| 72 } | |
| 73 | |
| 74 function tracingComplete(event) | |
| 75 { | |
| 76 InspectorTest.log("Tracing complete"); | |
| 77 InspectorTest.eventHandler["Tracing.tracingComplete"] = null; | |
| 78 InspectorTest.eventHandler["Tracing.dataCollected"] = null; | |
| 79 callback(event.params.stream); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 InspectorTest.retrieveStream = function(streamHandle, offset, chunkSize, callbac
k) | |
| 84 { | |
| 85 var result = ""; | 64 var result = ""; |
| 86 var had_eof = false; | 65 var had_eof = false; |
| 87 | 66 |
| 88 var readArguments = { handle: streamHandle }; | 67 var readArguments = { handle: streamHandle }; |
| 89 if (typeof chunkSize === "number") | 68 if (typeof chunkSize === "number") |
| 90 readArguments.size = chunkSize; | 69 readArguments.size = chunkSize; |
| 91 var firstReadArguments = JSON.parse(JSON.stringify(readArguments)); | 70 var firstReadArguments = JSON.parse(JSON.stringify(readArguments)); |
| 92 if (typeof offset === "number") | 71 if (typeof offset === "number") |
| 93 firstReadArguments.offset = 0; | 72 firstReadArguments.offset = 0; |
| 94 InspectorTest.sendCommandOrDie("IO.read", firstReadArguments, onChunkRead); | 73 this._session.protocol.IO.read(firstReadArguments).then(message => onChunkRe
ad.call(this, message.result)); |
| 95 // Assure multiple in-lfight reads are fine (also, save on latencies). | 74 // Assure multiple in-flight reads are fine (also, save on latencies). |
| 96 InspectorTest.sendCommandOrDie("IO.read", readArguments, onChunkRead); | 75 this._session.protocol.IO.read(readArguments).then(message => onChunkRead.ca
ll(this, message.result)); |
| 76 return promise; |
| 97 | 77 |
| 98 function onChunkRead(response) | 78 function onChunkRead(response) { |
| 99 { | 79 if (had_eof) |
| 100 if (had_eof) | 80 return; |
| 101 return; | 81 result += response.data; |
| 102 result += response.data; | 82 if (response.eof) { |
| 103 if (response.eof) { | 83 // Ignore stray callbacks from proactive read requests. |
| 104 // Ignore stray callbacks from proactive read requests. | 84 had_eof = true; |
| 105 had_eof = true; | 85 callback(result); |
| 106 callback(result); | 86 return; |
| 107 return; | 87 } |
| 108 } | 88 this._session.protocol.IO.read(readArguments).then(message => onChunkRead.
call(this, message.result)); |
| 109 InspectorTest.sendCommandOrDie("IO.read", readArguments, onChunkRead); | |
| 110 } | 89 } |
| 111 } | 90 } |
| 112 | 91 |
| 113 InspectorTest.findEvents = function(name, ph, condition) | 92 findEvents(name, ph, condition) { |
| 114 { | 93 return this._devtoolsEvents.filter(e => e.name === name && e.ph === ph && (!
condition || condition(e))); |
| 115 return InspectorTest.devtoolsEvents.filter(e => e.name === name && e.ph ===
ph && (!condition || condition(e))); | 94 } |
| 116 } | |
| 117 | 95 |
| 118 InspectorTest.findEvent = function(name, ph, condition) | 96 findEvent(name, ph, condition) { |
| 119 { | 97 var events = this.findEvents(name, ph, condition); |
| 120 var events = InspectorTest.findEvents(name, ph, condition); | |
| 121 if (events.length) | 98 if (events.length) |
| 122 return events[0]; | 99 return events[0]; |
| 123 throw new Error("Couldn't find event " + name + " / " + ph + "\n\n in " + JS
ON.stringify(InspectorTest.devtoolsEvents, null, 2)); | 100 throw new Error("Couldn't find event " + name + " / " + ph + "\n\n in " + JS
ON.stringify(this.devtoolsEvents, null, 2)); |
| 124 } | 101 } |
| 125 | 102 |
| 126 InspectorTest.invokeAsyncWithTracing = function(functionName, callback) | 103 filterEvents(callback) { |
| 127 { | 104 return this._devtoolsEvents.filter(callback); |
| 128 InspectorTest.startTracing(onStart); | 105 } |
| 129 | 106 |
| 130 function onStart() | 107 async invokeAsyncWithTracing(performActions) { |
| 131 { | 108 await this.startTracing(); |
| 132 InspectorTest.evaluateInPageAsync(functionName + "()").then((data) => In
spectorTest.stopTracing((devtoolsEvents) => callback(devtoolsEvents, data))); | 109 var data = await this._session.evaluateAsync(`(${performActions.toString()})
()`); |
| 133 } | 110 await this.stopTracing(); |
| 134 } | 111 return data; |
| 112 } |
| 135 | 113 |
| 136 } | 114 formattedEvents() { |
| 115 var formattedEvents = this._devtoolsEvents.map(e => e.name + (e.args.data ?
'(' + e.args.data.type + ')' : '')); |
| 116 return JSON.stringify(formattedEvents, null, 2); |
| 117 } |
| 118 }) |
| OLD | NEW |