Skip to content

Commit

Permalink
DevTools: add input event instrumentation test
Browse files Browse the repository at this point in the history
BUG=554215

Review URL: https://codereview.chromium.org/1704723002

Cr-Commit-Position: refs/heads/master@{#375802}
  • Loading branch information
caseq authored and Commit bot committed Feb 17, 2016
1 parent b21deec commit 8d8b8d3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
16 changes: 15 additions & 1 deletion front_end/common/TestBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ WebInspector.TestBase.prototype.releaseControl = function()
clearTimeout(this.timerId_);
this.timerId_ = -1;
}
this.controlTaken_ = false;
this.reportOk_();
};

Expand Down Expand Up @@ -107,7 +108,7 @@ WebInspector.TestBase.prototype.reportFailure_ = function(error)

/**
* Run specified test on a fresh instance of the test suite.
* @param {string} name Name of a test method from implementation class.
* @param {Array<string>} args method name followed by its parameters.
*/
WebInspector.TestBase.prototype.dispatch = function(args)
{
Expand All @@ -122,6 +123,19 @@ WebInspector.TestBase.prototype.dispatch = function(args)
};


/**
* Wrap an async method with TestBase.{takeControl(), releaseControl()}
* and invoke TestBase.reportOk_ upon completion.
* @param {Array<string>} args method name followed by its parameters.
*/
WebInspector.TestBase.prototype.waitForAsync = function(var_args)
{
var args = Array.prototype.slice.call(arguments);
this.takeControl();
args.push(this.releaseControl.bind(this));
this.dispatch(args);
};

/**
* Overrides the method with specified name until it's called first time.
* @param {!Object} receiver An object whose method to override.
Expand Down
71 changes: 58 additions & 13 deletions front_end/main/Tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function createTestSuite(domAutomationController)
function TestSuite()
{
WebInspector.TestBase.call(this, domAutomationController);
this._asyncInvocationId = 0;
};

TestSuite.prototype = {
Expand Down Expand Up @@ -651,43 +652,87 @@ TestSuite.prototype.waitForTestResultsInConsole = function()
this.takeControl();
};

TestSuite.prototype.invokeAsyncWithTimeline_ = function(functionName, callback)
TestSuite.prototype.startTimeline = function(callback)
{
var test = this;
test.showPanel("timeline").then(function() {
this.showPanel("timeline").then(function() {
WebInspector.panels.timeline._model.addEventListener(WebInspector.TimelineModel.Events.RecordingStarted, onRecordingStarted);
WebInspector.panels.timeline._toggleRecording();
});

function onRecordingStarted()
{
WebInspector.panels.timeline._model.removeEventListener(WebInspector.TimelineModel.Events.RecordingStarted, onRecordingStarted);
test.evaluateInConsole_(functionName + "(function() { console.log('DONE'); });", function() {});
WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, onConsoleMessage);
callback();
}
}

TestSuite.prototype.stopTimeline = function(callback)
{
WebInspector.panels.timeline._model.addEventListener(WebInspector.TimelineModel.Events.RecordingStopped, onRecordingStopped);
WebInspector.panels.timeline._toggleRecording();
function onRecordingStopped()
{
WebInspector.panels.timeline._model.removeEventListener(WebInspector.TimelineModel.Events.RecordingStopped, onRecordingStopped);
callback();
}
}

TestSuite.prototype.invokePageFunctionAsync = function(functionName, opt_args, callback_is_always_last)
{
var callback = arguments[arguments.length - 1];
var doneMessage = `DONE: ${functionName}.${++this._asyncInvocationId}`;
var argsString = arguments.length < 3 ? "" : Array.prototype.slice.call(arguments, 1, -1).map(arg => JSON.stringify(arg)).join(",") + ",";
this.evaluateInConsole_(`${functionName}(${argsString} function() { console.log('${doneMessage}'); });`, function() {});
WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, onConsoleMessage);

function onConsoleMessage(event)
{
var text = event.data.messageText;
if (text === "DONE") {
if (text === doneMessage) {
WebInspector.multitargetConsoleModel.removeEventListener(WebInspector.ConsoleModel.Events.MessageAdded, onConsoleMessage);
pageActionsDone();
callback();
}
}
}

function pageActionsDone()
TestSuite.prototype.invokeAsyncWithTimeline_ = function(functionName, callback)
{
var test = this;

this.startTimeline(onRecordingStarted);

function onRecordingStarted()
{
WebInspector.panels.timeline._model.addEventListener(WebInspector.TimelineModel.Events.RecordingStopped, onRecordingStopped);
WebInspector.panels.timeline._toggleRecording();
test.invokePageFunctionAsync(functionName, pageActionsDone);
}

function onRecordingStopped()
function pageActionsDone()
{
WebInspector.panels.timeline._model.removeEventListener(WebInspector.TimelineModel.Events.RecordingStopped, onRecordingStopped);
callback();
test.stopTimeline(callback);
}
};

TestSuite.prototype.enableExperiment = function(name)
{
Runtime.experiments.enableForTest(name);
}

TestSuite.prototype.checkInputEventsPresent = function()
{
var expectedEvents = new Set(arguments);
var model = WebInspector.panels.timeline._model;
var asyncEvents = model.mainThreadAsyncEvents();
var input = asyncEvents.get(WebInspector.TimelineUIUtils.asyncEventGroups().input) || [];
var prefix = "InputLatency::";
for (var e of input) {
if (!e.name.startsWith(prefix))
continue;
expectedEvents.delete(e.name.substr(prefix.length));
}
if (expectedEvents.size)
throw "Some expected events are not found: " + Array.from(expectedEvents.keys()).join(",");
}

/**
* Serializes array of uiSourceCodes to string.
* @param {!Array.<!WebInspectorUISourceCode>} uiSourceCodes
Expand Down

0 comments on commit 8d8b8d3

Please sign in to comment.