Chromium Code Reviews| Index: LayoutTests/http/tests/inspector/inspector-test.js |
| diff --git a/LayoutTests/http/tests/inspector/inspector-test.js b/LayoutTests/http/tests/inspector/inspector-test.js |
| index a72e627f59b134b30101080bc2828765a25858ca..1f618b9cbda6e23cd2979ce22fe76969dc71f367 100644 |
| --- a/LayoutTests/http/tests/inspector/inspector-test.js |
| +++ b/LayoutTests/http/tests/inspector/inspector-test.js |
| @@ -58,6 +58,60 @@ InspectorTest.evaluateInPageWithTimeout = function(code) |
| var lastEvalId = 0; |
| var pendingEvalRequests = {}; |
| +var lastPromiseEvalId = 0; |
| +var pendingPromiseEvalRequests = {}; |
| + |
| +/** |
| + * The given function should take two callback paraters before the arguments: |
| + * * resolve - called when successful (with optional result) |
| + * * reject - called when there was a failure (with optional error) |
| + */ |
| +InspectorTest.invokePageFunctionPromise = function(functionName, parameters) |
| +{ |
| + return new Promise(function(resolve, reject) { |
| + var id = ++lastPromiseEvalId; |
| + pendingPromiseEvalRequests[id] = { resolve: InspectorTest.safeWrap(resolve), reject: InspectorTest.safeWrap(reject) }; |
| + |
| + var jsonParameters = []; |
| + for (var i = 0; i < parameters.length; ++i) |
| + jsonParameters.push(JSON.stringify(parameters[i])); |
| + var asyncEvalWrapper = function(callId, functionName, argumentsArray) |
| + { |
| + function evalCallbackResolve(result) |
| + { |
| + testRunner.evaluateInWebInspector(evalCallbackCallId, "InspectorTest.didInvokePageFunctionPromise(" + callId + ", " + JSON.stringify(result) + ", true);"); |
| + } |
| + function evalCallbackReject(result) |
|
pfeldman
2015/04/15 17:50:08
Blank lines around functions.
dmurph
2015/04/15 21:29:32
Done.
|
| + { |
| + testRunner.evaluateInWebInspector(evalCallbackCallId, "InspectorTest.didInvokePageFunctionPromise(" + callId + ", " + JSON.stringify(result) + ", false);"); |
| + } |
| + var args = [evalCallbackResolve, evalCallbackReject].concat(argumentsArray.map(JSON.stringify)); |
| + var functionCall = functionName + ".call(null, " + args.join(", ") + ")"; |
| + try { |
| + eval(functionCall); |
| + } catch(e) { |
| + console.error(e); |
|
pfeldman
2015/04/15 17:50:08
Inspector.addResult, no console.error please, they
dmurph
2015/04/15 21:29:32
Done. (This was copied from below, so I changed in
|
| + evalCallbackReject(e); |
| + } |
| + } |
| + var pageRequest = "(" + asyncEvalWrapper.toString() + ")(" + id + ", unescape('" + escape(functionName) + "'), [" + jsonParameters.join(", ") + "])"; |
| + InspectorTest.evaluateInPage(pageRequest); |
| + }); |
| +} |
| + |
| + |
| +InspectorTest.didInvokePageFunctionPromise = function(callId, value, didResolve) |
| +{ |
| + var callbacks = pendingPromiseEvalRequests[callId]; |
| + if (!callbacks) { |
| + InspectorTest.addResult("Missing callback for async eval " + callId + ", perhaps callback invoked twice?"); |
| + return; |
| + } |
| + var callback = didResolve ? callbacks.resolve : callbacks.reject; |
| + delete pendingPromiseEvalRequests[callId]; |
| + callback(value); |
| +} |
| + |
| InspectorTest.invokePageFunctionAsync = function(functionName, callback) |
| { |
| var id = ++lastEvalId; |
| @@ -429,6 +483,33 @@ InspectorTest.addSniffer = function(receiver, methodName, override, opt_sticky) |
| }; |
| } |
| +InspectorTest.addSnifferPromise = function(receiver, methodName) |
| +{ |
| + return new Promise(function (resolve, reject) { |
| + var original = receiver[methodName]; |
|
pfeldman
2015/04/15 17:50:08
Poor indent.
dmurph
2015/04/15 21:29:32
Done.
|
| + if (typeof original !== "function") { |
| + reject("Cannot find method to override: " + methodName); |
| + return; |
| + } |
| + |
| + receiver[methodName] = function(var_args) { |
| + try { |
| + var result = original.apply(this, arguments); |
| + } finally { |
| + receiver[methodName] = original; |
| + } |
| + // In case of exception the override won't be called. |
| + try { |
| + Array.prototype.push.call(arguments, result); |
| + resolve.apply(this, arguments); |
| + } catch (e) { |
| + reject("Exception in overriden method '" + methodName + "': " + e); |
| + } |
| + return result; |
| + }; |
| + }); |
| +} |
| + |
| InspectorTest.addConsoleSniffer = function(override, opt_sticky) |
| { |
| InspectorTest.addSniffer(WebInspector.ConsoleModel.prototype, "addMessage", override, opt_sticky); |