<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[189761] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/189761">189761</a></dd>
<dt>Author</dt> <dd>bburg@apple.com</dd>
<dt>Date</dt> <dd>2015-09-14 17:10:01 -0700 (Mon, 14 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: backend command promises are not rejected when a protocol error occurs
https://bugs.webkit.org/show_bug.cgi?id=141403

Reviewed by Joseph Pecoraro.

Source/WebInspectorUI:

Fix a few corner cases for how InspectorBackend delivers command failures.

* UserInterface/Protocol/InspectorBackend.js:
(InspectorBackend.Command.prototype.deliverFailure): Added.
(InspectorBackend.Command.prototype._invokeWithArguments):

    If argument-checking fails, return a rejected promise or invoke the supplied callback
    on a zero-delay setTimeout to ensure that the reply is asynchronous.

LayoutTests:

Expand coverage of an existing protocol layer test to cover success and failure modes.

* inspector/protocol/inspector-backend-invocation-return-value-expected.txt:
* inspector/protocol/inspector-backend-invocation-return-value.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsinspectorprotocolinspectorbackendinvocationreturnvalueexpectedtxt">trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value-expected.txt</a></li>
<li><a href="#trunkLayoutTestsinspectorprotocolinspectorbackendinvocationreturnvaluehtml">trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value.html</a></li>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceProtocolInspectorBackendjs">trunk/Source/WebInspectorUI/UserInterface/Protocol/InspectorBackend.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (189760 => 189761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-09-14 23:44:33 UTC (rev 189760)
+++ trunk/LayoutTests/ChangeLog        2015-09-15 00:10:01 UTC (rev 189761)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2015-09-14  Brian Burg  &lt;bburg@apple.com&gt;
+
+        Web Inspector: backend command promises are not rejected when a protocol error occurs
+        https://bugs.webkit.org/show_bug.cgi?id=141403
+
+        Reviewed by Joseph Pecoraro.
+
+        Expand coverage of an existing protocol layer test to cover success and failure modes.
+
+        * inspector/protocol/inspector-backend-invocation-return-value-expected.txt:
+        * inspector/protocol/inspector-backend-invocation-return-value.html:
+
</ins><span class="cx"> 2015-09-14  Myles C. Maxfield  &lt;mmaxfield@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Test gardening after r189670
</span></span></pre></div>
<a id="trunkLayoutTestsinspectorprotocolinspectorbackendinvocationreturnvalueexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value-expected.txt (189760 => 189761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value-expected.txt        2015-09-14 23:44:33 UTC (rev 189760)
+++ trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value-expected.txt        2015-09-15 00:10:01 UTC (rev 189761)
</span><span class="lines">@@ -1,5 +1,41 @@
</span><span class="cx"> Testing the inspector backend's return values when invoking a protocol command in various ways.
</span><span class="cx"> 
</span><del>-PASS: RuntimeAgent.evaluate should not return a promise when invoked with a function as the last parameter.
-PASS: RuntimeAgent.evaluate should return a Promise when invoked without a callback.
</del><span class="cx"> 
</span><ins>+== Running test suite: Protocol.BackendInvocationReturnValues
+-- Running test case: ResolveCommandPromiseOnSuccess
+PASS: A backend command should return a Promise when invoked without a callback.
+PASS: A successful command invocation's promise should be resolved.
+
+-- Running test case: RejectCommandPromiseWithInvalidArguments
+ERROR: Protocol Error: Invalid type of argument 'expression' for command 'Runtime.evaluate' call. It must be 'string' but it is 'number'.
+PASS: A backend command should return a Promise when invoked without a callback.
+PASS: An invalid command invocation's promise should be rejected.
+
+-- Running test case: RejectCommandPromiseWithMissingArguments
+ERROR: Protocol Error: Invalid number of arguments for command 'Runtime.evaluate'.
+PASS: A backend command should return a Promise when invoked without a callback.
+PASS: An invalid command invocation's promise should be rejected.
+
+-- Running test case: ReturnNothingIfCallback
+PASS: A backend command should not have a return value when invoked with a callback.
+
+-- Running test case: InvokeCallbackWithResultOnSuccess
+PASS: A backend command should not return anything when invoked with a callback.
+PASS: A backend command should always invoke its callback asynchronously.
+PASS: A successful command should invoke the callback with a 'null' first parameter.
+PASS: A successful command should invoke the callback with one or more result parameters.
+
+-- Running test case: InvokeCallbackWithErrorForInvalidArguments
+ERROR: Protocol Error: Invalid type of argument 'expression' for command 'Runtime.evaluate' call. It must be 'string' but it is 'number'.
+PASS: A backend command should not return anything when invoked with a callback.
+PASS: A backend command should always invoke its callback asynchronously.
+PASS: A failed command should invoke the callback with a string error message as its first parameter.
+PASS: A failed command should invoke the callback with only an error parameter.
+
+-- Running test case: InvokeCallbackWithErrorForMissingArguments
+ERROR: Protocol Error: Invalid number of arguments for command 'Runtime.evaluate'.
+PASS: A backend command should not return anything when invoked with a callback.
+PASS: A backend command should always invoke its callback asynchronously.
+PASS: A failed command should invoke the callback with a string error message as its first parameter.
+PASS: A failed command should invoke the callback with only an error parameter.
+
</ins></span></pre></div>
<a id="trunkLayoutTestsinspectorprotocolinspectorbackendinvocationreturnvaluehtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value.html (189760 => 189761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value.html        2015-09-14 23:44:33 UTC (rev 189760)
+++ trunk/LayoutTests/inspector/protocol/inspector-backend-invocation-return-value.html        2015-09-15 00:10:01 UTC (rev 189761)
</span><span class="lines">@@ -5,14 +5,140 @@
</span><span class="cx"> &lt;script&gt;
</span><span class="cx"> function test()
</span><span class="cx"> {
</span><del>-    let dummyCallback = function(){};
-    let invokeCallbackResult = RuntimeAgent.evaluate(&quot;41&quot;, dummyCallback);
-    InspectorTest.expectThat(!(invokeCallbackResult instanceof Promise), &quot;RuntimeAgent.evaluate should not return a promise when invoked with a function as the last parameter.&quot;);
</del><span class="cx"> 
</span><del>-    let invokePromiseResult = RuntimeAgent.evaluate(&quot;42&quot;);
-    InspectorTest.expectThat(invokePromiseResult instanceof Promise, &quot;RuntimeAgent.evaluate should return a Promise when invoked without a callback.&quot;);
</del><ins>+    let suite = InspectorTest.createAsyncSuite(&quot;Protocol.BackendInvocationReturnValues&quot;);
+    let dummyCallback = () =&gt; {};
</ins><span class="cx"> 
</span><del>-    InspectorTest.completeTest();
</del><ins>+    // Test behavior for promise-based callbacks.
+
+    suite.addTestCase({
+        name: &quot;ResolveCommandPromiseOnSuccess&quot;,
+        description: &quot;Backend command's returned promise should be resolved if the command is successful.&quot;,
+        test: (resolve, reject) =&gt; {
+            let returnValue = RuntimeAgent.evaluate(&quot;42&quot;);
+
+            InspectorTest.expectThat(returnValue instanceof Promise, &quot;A backend command should return a Promise when invoked without a callback.&quot;);
+            // If a promise wasn't returned, we can't test the rest so just die.
+            if (!(returnValue instanceof Promise))
+                reject();
+
+            returnValue.then(function resolved(result) {
+                InspectorTest.log(&quot;PASS: A successful command invocation's promise should be resolved.&quot;);
+                resolve();
+            }, function rejected(result) {
+                InspectorTest.log(&quot;FAIL: A successful command invocation's promise should be resolved.&quot;);
+                reject();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;RejectCommandPromiseWithInvalidArguments&quot;,
+        description: &quot;Backend command's returned promise should be rejected if the command has invalid arguments.&quot;,
+        test: (resolve, reject) =&gt; {
+            let result = RuntimeAgent.evaluate(42);
+
+            InspectorTest.expectThat(result instanceof Promise, &quot;A backend command should return a Promise when invoked without a callback.&quot;);
+            // If a promise wasn't returned, we can't test the rest so just die.
+            if (!(result instanceof Promise))
+                reject();
+
+            result.then(function resolved(result) {
+                InspectorTest.log(&quot;FAIL: An invalid command invocation's promise should be rejected.&quot;);
+                reject();
+            }, function rejected(result) {
+                InspectorTest.log(&quot;PASS: An invalid command invocation's promise should be rejected.&quot;);
+                resolve();
+            });
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;RejectCommandPromiseWithMissingArguments&quot;,
+        description: &quot;Backend command's returned promise should be rejected if the command lacks required arguments.&quot;,
+        test: (resolve, reject) =&gt; {
+            let result = RuntimeAgent.evaluate();
+
+            InspectorTest.expectThat(result instanceof Promise, &quot;A backend command should return a Promise when invoked without a callback.&quot;);
+            // If a promise wasn't returned, we can't test the rest so just die.
+            if (!(result instanceof Promise))
+                reject();
+
+            result.then(function resolved(result) {
+                InspectorTest.log(&quot;FAIL: An invalid command invocation's promise should be rejected.&quot;);
+                reject();
+            }, function rejected(result) {
+                InspectorTest.log(&quot;PASS: An invalid command invocation's promise should be rejected.&quot;);
+                resolve();
+            });
+        }
+    });
+
+    // Test behavior for function-based callbacks.
+
+    suite.addTestCase({
+        name: &quot;ReturnNothingIfCallback&quot;,
+        description: &quot;Backend commands should not return anything if a callback is supplied.&quot;,
+        test: (resolve, reject) =&gt; {
+            let returnValue = RuntimeAgent.evaluate(&quot;41&quot;, dummyCallback);
+            InspectorTest.expectThat(returnValue === undefined, &quot;A backend command should not have a return value when invoked with a callback.&quot;);
+            resolve();
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;InvokeCallbackWithResultOnSuccess&quot;,
+        description: &quot;Backend command callback should be invoked with a result if the command is successful.&quot;,
+        test: (resolve, reject) =&gt; {
+            let initialState = 1;
+            let returnValue = RuntimeAgent.evaluate(&quot;42&quot;, function(error, result) {
+                InspectorTest.expectThat(error === null, &quot;A successful command should invoke the callback with a 'null' first parameter.&quot;);
+                InspectorTest.expectThat(arguments.length &gt; 1, &quot;A successful command should invoke the callback with one or more result parameters.&quot;);
+                initialState++;
+                resolve();
+            });
+
+            InspectorTest.expectThat(returnValue === undefined, &quot;A backend command should not return anything when invoked with a callback.&quot;);
+            InspectorTest.expectThat(initialState === 1, &quot;A backend command should always invoke its callback asynchronously.&quot;);
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;InvokeCallbackWithErrorForInvalidArguments&quot;,
+        description: &quot;Backend command callback should be invoked with an error if the command has invalid arguments.&quot;,
+        test: (resolve, reject) =&gt; {
+            let initialState = 1;
+            let returnValue = RuntimeAgent.evaluate(42, function callback(error) {
+                InspectorTest.expectThat(typeof error === &quot;string&quot;, &quot;A failed command should invoke the callback with a string error message as its first parameter.&quot;);
+                InspectorTest.expectThat(arguments.length === 1, &quot;A failed command should invoke the callback with only an error parameter.&quot;);
+                initialState++;
+                resolve();
+            });
+
+            InspectorTest.expectThat(returnValue === undefined, &quot;A backend command should not return anything when invoked with a callback.&quot;);
+            InspectorTest.expectThat(initialState === 1, &quot;A backend command should always invoke its callback asynchronously.&quot;);
+        }
+    });
+
+    suite.addTestCase({
+        name: &quot;InvokeCallbackWithErrorForMissingArguments&quot;,
+        description: &quot;Backend command callback should be invoked with an error if the command lacks required arguments.&quot;,
+        test: (resolve, reject) =&gt; {
+            let initialState = 1;
+            let returnValue = RuntimeAgent.evaluate(function callback(error) {
+                InspectorTest.expectThat(typeof error === &quot;string&quot;, &quot;A failed command should invoke the callback with a string error message as its first parameter.&quot;);
+                InspectorTest.expectThat(arguments.length === 1, &quot;A failed command should invoke the callback with only an error parameter.&quot;);
+
+                initialState++;
+                resolve();
+            });
+
+            InspectorTest.expectThat(returnValue === undefined, &quot;A backend command should not return anything when invoked with a callback.&quot;);
+            InspectorTest.expectThat(initialState === 1, &quot;A backend command should always invoke its callback asynchronously.&quot;);
+        }
+    });
+
+    suite.runTestCasesAndFinish();
</ins><span class="cx"> }
</span><span class="cx"> &lt;/script&gt;
</span><span class="cx"> &lt;/head&gt;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (189760 => 189761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog        2015-09-14 23:44:33 UTC (rev 189760)
+++ trunk/Source/WebInspectorUI/ChangeLog        2015-09-15 00:10:01 UTC (rev 189761)
</span><span class="lines">@@ -1,5 +1,21 @@
</span><span class="cx"> 2015-09-14  Brian Burg  &lt;bburg@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Web Inspector: backend command promises are not rejected when a protocol error occurs
+        https://bugs.webkit.org/show_bug.cgi?id=141403
+
+        Reviewed by Joseph Pecoraro.
+
+        Fix a few corner cases for how InspectorBackend delivers command failures.
+
+        * UserInterface/Protocol/InspectorBackend.js:
+        (InspectorBackend.Command.prototype.deliverFailure): Added.
+        (InspectorBackend.Command.prototype._invokeWithArguments):
+
+            If argument-checking fails, return a rejected promise or invoke the supplied callback
+            on a zero-delay setTimeout to ensure that the reply is asynchronous.
+
+2015-09-14  Brian Burg  &lt;bburg@apple.com&gt;
+
</ins><span class="cx">         Web Inspector: middle-clicking a tab in the tab bar should close it
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=149135
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceProtocolInspectorBackendjs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Protocol/InspectorBackend.js (189760 => 189761)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Protocol/InspectorBackend.js        2015-09-14 23:44:33 UTC (rev 189760)
+++ trunk/Source/WebInspectorUI/UserInterface/Protocol/InspectorBackend.js        2015-09-15 00:10:01 UTC (rev 189761)
</span><span class="lines">@@ -475,35 +475,35 @@
</span><span class="cx">         let commandArguments = Array.from(arguments);
</span><span class="cx">         let callback = typeof commandArguments.lastValue === &quot;function&quot; ? commandArguments.pop() : null;
</span><span class="cx"> 
</span><ins>+        function deliverFailure(message) {
+            console.error(`Protocol Error: ${message}`);
+            if (callback)
+                setTimeout(callback.bind(null, message), 0);
+            else
+                return Promise.reject(new Error(message));
+        }
+
</ins><span class="cx">         let parameters = {};
</span><span class="cx">         for (let parameter of instance.callSignature) {
</span><span class="cx">             let parameterName = parameter[&quot;name&quot;];
</span><span class="cx">             let typeName = parameter[&quot;type&quot;];
</span><span class="cx">             let optionalFlag = parameter[&quot;optional&quot;];
</span><span class="cx"> 
</span><del>-            if (!commandArguments.length &amp;&amp; !optionalFlag) {
-                console.error(&quot;Protocol Error: Invalid number of arguments for method '&quot; + instance.qualifiedName + &quot;' call. It must have the following arguments '&quot; + JSON.stringify(instance.callSignature) + &quot;'.&quot;);
-                return;
-            }
</del><ins>+            if (!commandArguments.length &amp;&amp; !optionalFlag)
+                return deliverFailure(`Invalid number of arguments for command '${instance.qualifiedName}'.`);
</ins><span class="cx"> 
</span><span class="cx">             let value = commandArguments.shift();
</span><span class="cx">             if (optionalFlag &amp;&amp; value === undefined)
</span><span class="cx">                 continue;
</span><span class="cx"> 
</span><del>-            if (typeof value !== typeName) {
-                console.error(&quot;Protocol Error: Invalid type of argument '&quot; + parameterName + &quot;' for method '&quot; + instance.qualifiedName + &quot;' call. It must be '&quot; + typeName + &quot;' but it is '&quot; + typeof value + &quot;'.&quot;);
-                return;
-            }
</del><ins>+            if (typeof value !== typeName)
+                return deliverFailure(`Invalid type of argument '${parameterName}' for command '${instance.qualifiedName}' call. It must be '${typeName}' but it is '${typeof value}'.`);
</ins><span class="cx"> 
</span><span class="cx">             parameters[parameterName] = value;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (commandArguments.length === 1 &amp;&amp; !callback) {
-            if (commandArguments[0] !== undefined) {
-                console.error(&quot;Protocol Error: Optional callback argument for method '&quot; + instance.qualifiedName + &quot;' call must be a function but its type is '&quot; + typeof args[0] + &quot;'.&quot;);
-                return;
-            }
-        }
</del><ins>+        if (!callback &amp;&amp; commandArguments.length === 1 &amp;&amp; commandArguments[0] !== undefined)
+            return deliverFailure(`Protocol Error: Optional callback argument for command '${instance.qualifiedName}' call must be a function but its type is '${typeof args[0]}'.`);
</ins><span class="cx"> 
</span><span class="cx">         if (callback)
</span><span class="cx">             instance._backend._sendCommandToBackendWithCallback(instance, parameters, callback);
</span></span></pre>
</div>
</div>

</body>
</html>