Created
July 13, 2015 21:03
-
-
Save burg/87e3200782ef57647a5e to your computer and use it in GitHub Desktop.
New promise-based protocol test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <script type="text/javascript" src="../../http/tests/inspector-protocol/resources/protocol-test.js"></script> | |
| <script> | |
| function test() | |
| { | |
| InspectorTest.TestRunner = class TestRunner { | |
| constructor() { | |
| this.testcases = []; | |
| } | |
| addTestCase(testcase) | |
| { | |
| if (!testcase || !(testcase instanceof Object)) | |
| throw new Error("Tried to add non-object test case."); | |
| if (typeof testcase.name !== "string") | |
| throw new Error("Tried to add test case without a name."); | |
| if (typeof testcase.test !== "function") | |
| throw new Error("Tried to add test case without `test` function."); | |
| this.testcases.push(testcase); | |
| } | |
| runTestCases() | |
| { | |
| var result = this.testcases.reduce(function(chain, testcase) { | |
| return chain.then(function() { | |
| return new Promise(testcase.test); | |
| }); | |
| }, Promise.resolve()); | |
| result.catch(function(e) { | |
| InspectorTest.log(JSON.stringify(e)); | |
| InspectorTest.completeTest(); | |
| }); | |
| } | |
| } | |
| var runner = new InspectorTest.TestRunner; | |
| runner.addTestCase({ | |
| name: "Runtime.CheckPropertiesOfWrapperObject", | |
| description: "Check properties of `Object(5)`", | |
| test: function(resolve, reject) { | |
| var expression = "(function(){var r = Object(5); r.foo = 'cat';return r;})()"; | |
| InspectorTest.awaitCommand({ | |
| command: "Runtime.evaluate", | |
| params: {expression} | |
| }) | |
| .then(function(reply) { | |
| var objectId = reply.result.objectId; | |
| if (objectId === undefined) | |
| throw new Error("objectId is expected"); | |
| return InspectorTest.awaitCommand({ | |
| command: "Runtime.getProperties", | |
| params: {objectId, ownProperties: true} | |
| }); | |
| }) | |
| .then(function(reply) { | |
| logGetPropertiesResult("Object(5)", reply); | |
| resolve(); | |
| }); | |
| } | |
| }); | |
| runner.addTestCase({ | |
| name: "Runtime.CheckPropertiesOfArray", | |
| description: "check properties of the array `['red', 'green', 'blue']`.", | |
| test: function(resolve, reject) { | |
| // Create an wrapper object with additional property. | |
| var expression = "['red', 'green', 'blue']"; | |
| InspectorTest.awaitCommand({ | |
| command: "Runtime.evaluate", | |
| params: {expression} | |
| }) | |
| .then(function(reply) { | |
| var objectId = reply.result.objectId; | |
| if (objectId === undefined) | |
| throw new Error("objectId is expected"); | |
| return InspectorTest.awaitCommand({ | |
| command: "Runtime.getProperties", | |
| params: {objectId, ownProperties: true} | |
| }); | |
| }) | |
| .then(function(reply) { | |
| logGetPropertiesResult("array", reply); | |
| resolve(); | |
| }); | |
| } | |
| }); | |
| runner.addTestCase({ | |
| name: "Runtime.CheckPropertiesOfBoundConstructor", | |
| description: "Check properties of a bound function (has a bunch of internal properties).", | |
| test: function(resolve, reject) { | |
| var expression = "Number.bind({}, 5)"; | |
| InspectorTest.awaitCommand({ | |
| command: "Runtime.evaluate", | |
| params: {expression} | |
| }) | |
| .then(function(reply) { | |
| var objectId = reply.result.objectId; | |
| if (objectId === undefined) | |
| throw new Error("objectId is expected"); | |
| return InspectorTest.awaitCommand({ | |
| command: "Runtime.getProperties", | |
| params: {objectId, ownProperties: true} | |
| }); | |
| }) | |
| .then(function(reply) { | |
| logGetPropertiesResult("Bound function", reply); | |
| resolve(); | |
| }); | |
| } | |
| }); | |
| runner.runTestCases(); | |
| // A helper function that dumps object properties and internal properties in sorted order. | |
| function logGetPropertiesResult(title, protocolResult) { | |
| InspectorTest.log("Properties of " + title); | |
| var propertyArray = protocolResult.result; | |
| propertyArray.sort(NamedThingComparator); | |
| for (var i = 0; i < propertyArray.length; i++) { | |
| var p = propertyArray[i]; | |
| var v = p.value; | |
| if (v) | |
| InspectorTest.log(" " + p.name + " " + v.type + " " + (v.value || v.description)); | |
| else | |
| InspectorTest.log(" " + p.name); | |
| } | |
| var internalPropertyArray = protocolResult.internalProperties; | |
| if (internalPropertyArray) { | |
| InspectorTest.log("Internal properties"); | |
| internalPropertyArray.sort(NamedThingComparator); | |
| for (var i = 0; i < internalPropertyArray.length; i++) { | |
| var p = internalPropertyArray[i]; | |
| var v = p.value; | |
| InspectorTest.log(" " + p.name + " " + v.type + " " + (v.value || v.description)); | |
| } | |
| } | |
| function NamedThingComparator(o1, o2) { | |
| return o1.name.localeCompare(o2.name); | |
| } | |
| } | |
| } | |
| </script> | |
| </head> | |
| <body onLoad="runTest()"> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment