Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(891)

Unified Diff: test/inspector/protocol-test.js

Issue 2390733002: [inspector] Make InspectorTest.sendCommand* private (Closed)
Patch Set: addressed comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/inspector/inspector-test.cc ('k') | test/inspector/runtime/await-promise.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/inspector/protocol-test.js
diff --git a/test/inspector/protocol-test.js b/test/inspector/protocol-test.js
index 07a71635a9cc8a6f170f1e19bb68528357ea453c..3589d37e1eb2bc5e8f6675f24a5713f676e0f1f0 100644
--- a/test/inspector/protocol-test.js
+++ b/test/inspector/protocol-test.js
@@ -6,84 +6,51 @@ InspectorTest = {};
InspectorTest._dispatchTable = new Map();
InspectorTest._requestId = 0;
InspectorTest._dumpInspectorProtocolMessages = false;
-InspectorTest.eventHandler = {};
-
-InspectorTest.startDumpingProtocolMessages = function()
-{
- InspectorTest._dumpInspectorProtocolMessages = true;
-}
-
-InspectorTest.sendCommand = function(method, params, handler)
-{
- var requestId = ++InspectorTest._requestId;
- var messageObject = { "id": requestId, "method": method, "params": params };
- InspectorTest.sendRawCommand(requestId, JSON.stringify(messageObject), handler);
-}
-
-InspectorTest.sendRawCommand = function(requestId, command, handler)
-{
- if (InspectorTest._dumpInspectorProtocolMessages)
- print("frontend: " + command);
- InspectorTest._dispatchTable.set(requestId, handler);
- sendMessageToBackend(command);
-}
-
-InspectorTest.sendCommandOrDie = function(command, properties, callback)
-{
- InspectorTest.sendCommand(command, properties, commandCallback);
- function commandCallback(msg)
- {
- if (msg.error) {
- InspectorTest.log("ERROR: " + msg.error.message);
- InspectorTest.completeTest();
- return;
- }
- if (callback)
- callback(msg.result);
+InspectorTest._eventHandler = {};
+
+Protocol = new Proxy({}, {
+ get: function(target, agentName, receiver) {
+ return new Proxy({}, {
+ get: function(target, methodName, receiver) {
+ const eventPattern = /^on(ce)?([A-Z][A-Za-z0-9]+)/;
+ var match = eventPattern.exec(methodName);
+ if (!match) {
+ return (args) => InspectorTest._sendCommandPromise(`${agentName}.${methodName}`, args || {});
+ } else {
+ var eventName = match[2];
+ eventName = eventName.charAt(0).toLowerCase() + eventName.slice(1);
+ if (match[1])
+ return (args) => InspectorTest._waitForEventPromise(`${agentName}.${eventName}`, args || {});
+ else
+ return (listener) => { InspectorTest._eventHandler[`${agentName}.${eventName}`] = listener };
+ }
+ }
+ });
}
-}
+});
-InspectorTest.sendCommandPromise = function(method, params)
-{
- return new Promise(fulfill => InspectorTest.sendCommand(method, params, fulfill));
-}
+InspectorTest.log = print.bind(null);
-InspectorTest.waitForEventPromise = function(eventName)
+InspectorTest.logMessage = function(message)
{
- return new Promise(fulfill => InspectorTest.eventHandler[eventName] = fullfillAndClearListener.bind(null, fulfill));
-
- function fullfillAndClearListener(fulfill, result)
- {
- delete InspectorTest.eventHandler[eventName];
- fulfill(result);
- }
-}
+ if (message.id)
+ message.id = "<messageId>";
-InspectorTest.dispatchMessage = function(messageObject)
-{
- if (InspectorTest._dumpInspectorProtocolMessages)
- print("backend: " + JSON.stringify(messageObject));
- try {
- var messageId = messageObject["id"];
- if (typeof messageId === "number") {
- var handler = InspectorTest._dispatchTable.get(messageId);
- if (handler) {
- handler(messageObject);
- InspectorTest._dispatchTable.delete(messageId);
- }
- } else {
- var eventName = messageObject["method"];
- var eventHandler = InspectorTest.eventHandler[eventName];
- if (eventHandler)
- eventHandler(messageObject);
+ const nonStableFields = new Set(["objectId", "scriptId", "exceptionId", "timestamp"]);
+ var objects = [ message ];
+ while (objects.length) {
+ var object = objects.shift();
+ for (var key in object) {
+ if (nonStableFields.has(key))
+ object[key] = `<${key}>`;
+ else if (typeof object[key] === "object")
+ objects.push(object[key]);
}
- } catch (e) {
- InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.stack + "\n message = " + JSON.stringify(messageObject, null, 2));
- InspectorTest.completeTest();
}
-}
-InspectorTest.log = print.bind(null);
+ InspectorTest.logObject(message);
+ return message;
+}
InspectorTest.logObject = function(object, title)
{
@@ -133,27 +100,6 @@ InspectorTest.logObject = function(object, title)
InspectorTest.log(lines.join("\n"));
}
-InspectorTest.logMessage = function(message)
-{
- if (message.id)
- message.id = 0;
-
- const nonStableFields = new Set(["objectId", "scriptId", "exceptionId"]);
- var objects = [ message ];
- while (objects.length) {
- var object = objects.shift();
- for (var key in object) {
- if (nonStableFields.has(key))
- object[key] = `<${key}>`;
- else if (typeof object[key] === "object")
- objects.push(object[key]);
- }
- }
-
- InspectorTest.logObject(message);
- return message;
-}
-
InspectorTest.completeTest = quit.bind(null);
InspectorTest.completeTestAfterPendingTimeouts = function()
@@ -163,18 +109,32 @@ InspectorTest.completeTestAfterPendingTimeouts = function()
awaitPromise: true }, InspectorTest.completeTest);
}
-InspectorTest.evaluateInPage = function(string, callback)
+InspectorTest.addScript = function(string)
{
- InspectorTest.sendCommand("Runtime.evaluate", { "expression": string }, function(message) {
+ return InspectorTest._sendCommandPromise("Runtime.evaluate", { "expression": string }).then(dumpErrorIfNeeded);
+
+ function dumpErrorIfNeeded(message)
+ {
if (message.error) {
InspectorTest.log("Error while executing '" + string + "': " + message.error.message);
InspectorTest.completeTest();
}
- else if (callback)
- callback(message.result.result.value);
- });
+ }
};
+InspectorTest.startDumpingProtocolMessages = function()
+{
+ InspectorTest._dumpInspectorProtocolMessages = true;
+}
+
+InspectorTest.sendRawCommand = function(requestId, command, handler)
+{
+ if (InspectorTest._dumpInspectorProtocolMessages)
+ print("frontend: " + command);
+ InspectorTest._dispatchTable.set(requestId, handler);
+ sendMessageToBackend(command);
+}
+
InspectorTest.checkExpectation = function(fail, name, messageObject)
{
if (fail === !!messageObject.error) {
@@ -203,3 +163,48 @@ InspectorTest.runTestSuite = function(testSuite)
}
nextTest();
}
+
+InspectorTest._sendCommandPromise = function(method, params)
+{
+ var requestId = ++InspectorTest._requestId;
+ var messageObject = { "id": requestId, "method": method, "params": params };
+ var fulfillCallback;
+ var promise = new Promise(fulfill => fulfillCallback = fulfill);
+ InspectorTest.sendRawCommand(requestId, JSON.stringify(messageObject), fulfillCallback);
+ return promise;
+}
+
+InspectorTest._waitForEventPromise = function(eventName)
+{
+ return new Promise(fulfill => InspectorTest._eventHandler[eventName] = fullfillAndClearListener.bind(null, fulfill));
+
+ function fullfillAndClearListener(fulfill, result)
+ {
+ delete InspectorTest._eventHandler[eventName];
+ fulfill(result);
+ }
+}
+
+InspectorTest._dispatchMessage = function(messageObject)
+{
+ if (InspectorTest._dumpInspectorProtocolMessages)
+ print("backend: " + JSON.stringify(messageObject));
+ try {
+ var messageId = messageObject["id"];
+ if (typeof messageId === "number") {
+ var handler = InspectorTest._dispatchTable.get(messageId);
+ if (handler) {
+ handler(messageObject);
+ InspectorTest._dispatchTable.delete(messageId);
+ }
+ } else {
+ var eventName = messageObject["method"];
+ var eventHandler = InspectorTest._eventHandler[eventName];
+ if (eventHandler)
+ eventHandler(messageObject);
+ }
+ } catch (e) {
+ InspectorTest.log("Exception when dispatching message: " + e + "\n" + e.stack + "\n message = " + JSON.stringify(messageObject, null, 2));
+ InspectorTest.completeTest();
+ }
+}
« no previous file with comments | « test/inspector/inspector-test.cc ('k') | test/inspector/runtime/await-promise.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698