<!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>[189213] 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/189213">189213</a></dd>
<dt>Author</dt> <dd>bburg@apple.com</dd>
<dt>Date</dt> <dd>2015-09-01 10:37:16 -0700 (Tue, 01 Sep 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Web Inspector: add TestSuite hooks for per-testcase setup and teardown actions
https://bugs.webkit.org/show_bug.cgi?id=148641

Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

It should be possible to run setup and teardown actions for each test case.
The use case here is to add listeners, set up testcase-specific state, etc.
To match how test functions work in each suite, the sync and async implementations
take functions with no arguments and(resolve, reject), respectively.

* UserInterface/Test/TestSuite.js:
(TestSuite.prototype.addTestCase):
(TestSuite.messageFromThrownObject):
(TestSuite):
(AsyncTestSuite.prototype.runTestCases): Stick the extra functions in the chain.
(AsyncTestSuite):
(SyncTestSuite.prototype.runTestCases):
(SyncTestSuite):

LayoutTests:

Extend existing unit tests to also exercise setup and teardown actions.

* inspector/unit-tests/async-test-suite.html:
* inspector/unit-tests/async-test-suite-expected.txt:
* inspector/unit-tests/sync-test-suite.html:
* inspector/unit-tests/sync-test-suite-expected.txt:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsinspectorunittestsasynctestsuiteexpectedtxt">trunk/LayoutTests/inspector/unit-tests/async-test-suite-expected.txt</a></li>
<li><a href="#trunkLayoutTestsinspectorunittestsasynctestsuitehtml">trunk/LayoutTests/inspector/unit-tests/async-test-suite.html</a></li>
<li><a href="#trunkLayoutTestsinspectorunittestssynctestsuiteexpectedtxt">trunk/LayoutTests/inspector/unit-tests/sync-test-suite-expected.txt</a></li>
<li><a href="#trunkLayoutTestsinspectorunittestssynctestsuitehtml">trunk/LayoutTests/inspector/unit-tests/sync-test-suite.html</a></li>
<li><a href="#trunkSourceWebInspectorUIChangeLog">trunk/Source/WebInspectorUI/ChangeLog</a></li>
<li><a href="#trunkSourceWebInspectorUIUserInterfaceTestTestSuitejs">trunk/Source/WebInspectorUI/UserInterface/Test/TestSuite.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/LayoutTests/ChangeLog        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2015-09-01  Brian Burg  &lt;bburg@apple.com&gt;
+
+        Web Inspector: add TestSuite hooks for per-testcase setup and teardown actions
+        https://bugs.webkit.org/show_bug.cgi?id=148641
+
+        Reviewed by Timothy Hatcher.
+
+        Extend existing unit tests to also exercise setup and teardown actions.
+
+        * inspector/unit-tests/async-test-suite.html:
+        * inspector/unit-tests/async-test-suite-expected.txt:
+        * inspector/unit-tests/sync-test-suite.html:
+        * inspector/unit-tests/sync-test-suite-expected.txt:
+
</ins><span class="cx"> 2015-09-01  Zalan Bujtas  &lt;zalan@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Repaint cleanup:
</span></span></pre></div>
<a id="trunkLayoutTestsinspectorunittestsasynctestsuiteexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/unit-tests/async-test-suite-expected.txt (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/unit-tests/async-test-suite-expected.txt        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/LayoutTests/inspector/unit-tests/async-test-suite-expected.txt        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -7,6 +7,12 @@
</span><span class="cx"> PASS: test case should require string name.
</span><span class="cx"> PASS: test case should require non-whitespace name.
</span><span class="cx"> PASS: test case should require test function.
</span><ins>+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
</ins><span class="cx"> PASS: should not be able to run empty test suite.
</span><span class="cx"> 
</span><span class="cx"> == Running test suite: AsyncTestSuite.RunTwiceSuite
</span><span class="lines">@@ -38,3 +44,36 @@
</span><span class="cx"> PASS: abortOnFailureSuite should have failed one test.
</span><span class="cx"> PASS: abortOnFailureSuite should have skipped one test.
</span><span class="cx"> 
</span><ins>+== Running test suite: AsyncTestSuite.SetupAndTeardown
+-- Running test setup.
+-- Running test case: TestWithSetupAndTeardown
+PASS: Test should see side effects of running setup() action.
+-- Running test teardown.
+PASS: Teardown should see side effects of running setup() action.
+
+-- Running test case: TestRunningAfterTeardown
+PASS: Test should see side effects of previous test's teardown() action.
+PASS: Promise from setupAndTeardownTestSuite.runTestCases() should resolve.
+
+== Running test suite: AsyncTestSuite.SetupException
+-- Running test setup.
+!! EXCEPTION: 
+PASS: Promise from setupExceptionTestSuite.runTestCases() should reject.
+
+== Running test suite: AsyncTestSuite.SetupFailure
+-- Running test setup.
+!! EXCEPTION: undefined
+PASS: Promise from setupFailureTestSuite.runTestCases() should reject.
+
+== Running test suite: SyncTestSuite.TeardownException
+-- Running test case: TestWithExceptionDuringTeardown
+-- Running test teardown.
+!! EXCEPTION: 
+PASS: Promise from teardownExceptionTestSuite.runTestCases() should reject.
+
+== Running test suite: SyncTestSuite.TeardownFailure
+-- Running test case: TestWithExceptionDuringTeardown
+-- Running test teardown.
+!! EXCEPTION: undefined
+PASS: Promise from teardownFailureTestSuite.runTestCases() should reject.
+
</ins></span></pre></div>
<a id="trunkLayoutTestsinspectorunittestsasynctestsuitehtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/unit-tests/async-test-suite.html (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/unit-tests/async-test-suite.html        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/LayoutTests/inspector/unit-tests/async-test-suite.html        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -73,6 +73,66 @@
</span><span class="cx">     } catch (e) {
</span><span class="cx">         ProtocolTest.log(&quot;PASS: test case should require test function.&quot;);
</span><span class="cx">     }
</span><ins>+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: &quot;astd&quot;
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: 123
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: {}
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: &quot;astd&quot;
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: 123
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: {}
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
</ins><span class="cx"> 
</span><span class="cx">     let runEmptySuite = ProtocolTest.createAsyncSuite(&quot;AsyncTestSuite.RunEmptySuite&quot;);
</span><span class="cx">     try {
</span><span class="lines">@@ -176,6 +236,157 @@
</span><span class="cx">         return Promise.resolve(); // Continue this test.
</span><span class="cx">     });
</span><span class="cx"> 
</span><ins>+    var setupAndTeardownSymbol = Symbol(&quot;async-suite-setup-and-teardown-token&quot;);
+    window[setupAndTeardownSymbol] = 0;
+
+    let setupAndTeardownTestSuite = ProtocolTest.createAsyncSuite(&quot;AsyncTestSuite.SetupAndTeardown&quot;);
+    setupAndTeardownTestSuite.addTestCase({
+        name: &quot;TestWithSetupAndTeardown&quot;,
+        description: &quot;Check execution order for setup and teardown actions.&quot;,
+        setup: (resolve, reject) =&gt; {
+            window[setupAndTeardownSymbol] = 1;
+            resolve();
+        },
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 1, &quot;Test should see side effects of running setup() action.&quot;);
+            window[setupAndTeardownSymbol] = 2;
+            resolve();
+        },
+        teardown: (resolve, reject) =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 2, &quot;Teardown should see side effects of running setup() action.&quot;);
+            window[setupAndTeardownSymbol] = 3;
+            resolve();
+        }
+    });
+    setupAndTeardownTestSuite.addTestCase({
+        name: &quot;TestRunningAfterTeardown&quot;,
+        description: &quot;Check execution order for test after a teardown action.&quot;,
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 3, &quot;Test should see side effects of previous test's teardown() action.&quot;);
+            resolve();
+        },
+    });
+
+    result = result.then(() =&gt; {
+        return setupAndTeardownTestSuite.runTestCases();
+    }).then(function resolved() {
+        ProtocolTest.log(&quot;PASS: Promise from setupAndTeardownTestSuite.runTestCases() should resolve.&quot;);
+        return Promise.resolve(); // Continue this test.
+    }, function rejected(e) {
+        ProtocolTest.log(&quot;FAIL: Promise from setupAndTeardownTestSuite.runTestCases() should resolve.&quot;);
+        return Promise.resolve(); // Continue this test.
+    });
+
+    let setupExceptionTestSuite = ProtocolTest.createAsyncSuite(&quot;AsyncTestSuite.SetupException&quot;);
+    setupExceptionTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringSetup&quot;,
+        description: &quot;Check execution order for setup action that throws an exception.&quot;,
+        setup: (resolve, reject) =&gt; { throw new Error() },
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if its setup action threw an exception.&quot;);
+            reject();
+        },
+        teardown: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Teardown action should not execute if its setup action threw an exception.&quot;);
+            reject();           
+        }
+    });
+
+    result = result.then(() =&gt; {
+        return setupExceptionTestSuite.runTestCases();
+    }).then(function resolved() {
+        ProtocolTest.log(&quot;FAIL: Promise from setupExceptionTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    }, function rejected(e) {
+        ProtocolTest.log(&quot;PASS: Promise from setupExceptionTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    });
+
+    let setupFailureTestSuite = ProtocolTest.createAsyncSuite(&quot;AsyncTestSuite.SetupFailure&quot;);
+    setupFailureTestSuite.addTestCase({
+        name: &quot;TestWithFailureDuringSetup&quot;,
+        description: &quot;Check execution order for setup action that has failed.&quot;,
+        setup: (resolve, reject) =&gt; { reject(); },
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if its setup action failed.&quot;)
+            reject();
+        },
+        teardown: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Teardown action should not execute if its setup action failed.&quot;)
+            reject();
+        }
+    });
+
+    result = result.then(() =&gt; {
+        return setupFailureTestSuite.runTestCases();
+    }).then(function resolved() {
+        ProtocolTest.log(&quot;FAIL: Promise from setupFailureTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    }, function rejected(e) {
+        ProtocolTest.log(&quot;PASS: Promise from setupFailureTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    });
+
+    let teardownExceptionTestSuite = ProtocolTest.createAsyncSuite(&quot;SyncTestSuite.TeardownException&quot;);
+    teardownExceptionTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringTeardown&quot;,
+        description: &quot;Check execution order for teardown action that throws an exception.&quot;,
+        test: (resolve, reject) =&gt; { resolve(); },
+        teardown: (resolve, reject) =&gt; { throw new Error() }
+    });
+    teardownExceptionTestSuite.addTestCase({
+        name: &quot;TestAfterTeardownException&quot;,
+        descrption: &quot;Check execution order for test after previous test's teardown throws an exception.&quot;,
+        setup: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Setup action should not execute if previous test's teardown action threw an exception.&quot;);
+            reject();
+        },
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if previous test's teardown action threw an exception.&quot;);
+            reject();
+        }
+    });
+
+    result = result.then(() =&gt; {
+        return teardownExceptionTestSuite.runTestCases();
+    }).then(function resolved() {
+        ProtocolTest.log(&quot;FAIL: Promise from teardownExceptionTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    }, function rejected(e) {
+        ProtocolTest.log(&quot;PASS: Promise from teardownExceptionTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    });
+
+    let teardownFailureTestSuite = ProtocolTest.createAsyncSuite(&quot;SyncTestSuite.TeardownFailure&quot;);
+    teardownFailureTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringTeardown&quot;,
+        description: &quot;Check execution order for teardown action that has failed.&quot;,
+        test: (resolve, reject) =&gt; { resolve(); },
+        teardown: (resolve, reject) =&gt; { reject(); },
+    });
+    teardownFailureTestSuite.addTestCase({
+        name: &quot;TestAfterTeardownException&quot;,
+        description: &quot;Check execution order for test after previous test's teardown throws an exception&quot;,
+        setup: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Setup action should not execute if previous test's teardown action failed.&quot;);
+            reject();
+        },
+        test: (resolve, reject) =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if previous test's teardown action failed.&quot;);
+            reject();
+        }
+    });
+
+    result = result.then(() =&gt; {
+        return teardownFailureTestSuite.runTestCases();
+    }).then(function resolved() {
+        ProtocolTest.log(&quot;FAIL: Promise from teardownFailureTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    }, function rejected(e) {
+        ProtocolTest.log(&quot;PASS: Promise from teardownFailureTestSuite.runTestCases() should reject.&quot;);
+        return Promise.resolve(); // Continue this test.
+    });
+
</ins><span class="cx">     // This will finish the test whether the chain was resolved or rejected.
</span><span class="cx">     result = result.then(() =&gt; { ProtocolTest.completeTest(); });
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkLayoutTestsinspectorunittestssynctestsuiteexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/unit-tests/sync-test-suite-expected.txt (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/unit-tests/sync-test-suite-expected.txt        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/LayoutTests/inspector/unit-tests/sync-test-suite-expected.txt        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -7,6 +7,12 @@
</span><span class="cx"> PASS: test case should require string name.
</span><span class="cx"> PASS: test case should require non-whitespace name.
</span><span class="cx"> PASS: test case should require test function.
</span><ins>+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `setup` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
+PASS: should not be able to specify non-Function `teardown` parameter.
</ins><span class="cx"> PASS: should not be able to run empty test suite.
</span><span class="cx"> 
</span><span class="cx"> == Running test suite: SyncTestSuite.RunTwiceSuite
</span><span class="lines">@@ -35,3 +41,31 @@
</span><span class="cx"> PASS: abortOnFailureSuite should have failed one test.
</span><span class="cx"> PASS: abortOnFailureSuite should have skipped one test.
</span><span class="cx"> 
</span><ins>+== Running test suite: SyncTestSuite.SetupAndTeardown
+-- Running test setup.
+-- Running test case: TestWithSetupAndTeardown
+PASS: Test should see side effects of running setup() action.
+-- Running test teardown.
+PASS: Teardown should see side effects of running setup() action.
+
+-- Running test case: TestRunningAfterTeardown
+PASS: Test should see side effects of previous test's teardown() action.
+
+== Running test suite: SyncTestSuite.SetupException
+-- Running test setup.
+!! EXCEPTION: 
+
+== Running test suite: SyncTestSuite.SetupFailure
+-- Running test setup.
+!! EXCEPTION
+
+== Running test suite: SyncTestSuite.TeardownException
+-- Running test case: TestWithExceptionDuringTeardown
+-- Running test teardown.
+!! EXCEPTION: 
+
+== Running test suite: SyncTestSuite.TeardownFailure
+-- Running test case: TestWithExceptionDuringTeardown
+-- Running test teardown.
+!! EXCEPTION:
+
</ins></span></pre></div>
<a id="trunkLayoutTestsinspectorunittestssynctestsuitehtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/inspector/unit-tests/sync-test-suite.html (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/inspector/unit-tests/sync-test-suite.html        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/LayoutTests/inspector/unit-tests/sync-test-suite.html        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -73,7 +73,66 @@
</span><span class="cx">     } catch (e) {
</span><span class="cx">         ProtocolTest.log(&quot;PASS: test case should require test function.&quot;);
</span><span class="cx">     }
</span><del>-
</del><ins>+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: &quot;astd&quot;
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: 123
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            setup: Promise.resolve()
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `setup` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `setup` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: &quot;astd&quot;
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: 123
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
+    try {
+        badArgsSuite.addTestCase({
+            name: &quot;foo&quot;,
+            test: () =&gt; {},
+            teardown: Promise.resolve()
+        });
+        ProtocolTest.log(&quot;FAIL: should not be able to specify non-Function `teardown` parameter.&quot;);
+    } catch (e) {
+        ProtocolTest.log(&quot;PASS: should not be able to specify non-Function `teardown` parameter.&quot;);
+    }
</ins><span class="cx">     let runEmptySuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.RunEmptySuite&quot;);
</span><span class="cx">     try {
</span><span class="cx">         runEmptySuite.runTestCases();
</span><span class="lines">@@ -154,6 +213,113 @@
</span><span class="cx">     ProtocolTest.expectThat(abortOnFailureSuite.failCount === 1, &quot;abortOnFailureSuite should have failed one test.&quot;);
</span><span class="cx">     ProtocolTest.expectThat(abortOnFailureSuite.skipCount === 1, &quot;abortOnFailureSuite should have skipped one test.&quot;);
</span><span class="cx"> 
</span><ins>+    let setupAndTeardownSymbol = Symbol(&quot;sync-suite-setup-and-teardown-token&quot;);
+    window[setupAndTeardownSymbol] = 0;
+
+    let setupAndTeardownTestSuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.SetupAndTeardown&quot;);
+    setupAndTeardownTestSuite.addTestCase({
+        name: &quot;TestWithSetupAndTeardown&quot;,
+        description: &quot;Check execution order for setup and teardown actions.&quot;,
+        setup: () =&gt; {
+            window[setupAndTeardownSymbol] = 1;
+            return true;
+        },
+        test: () =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 1, &quot;Test should see side effects of running setup() action.&quot;);
+            window[setupAndTeardownSymbol] = 2;
+            return true;
+        },
+        teardown: () =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 2, &quot;Teardown should see side effects of running setup() action.&quot;);
+            window[setupAndTeardownSymbol] = 3;
+            return true;
+        }
+    });
+    setupAndTeardownTestSuite.addTestCase({
+        name: &quot;TestRunningAfterTeardown&quot;,
+        description: &quot;Check execution order for test after a teardown action.&quot;,
+        test: () =&gt; {
+            ProtocolTest.expectThat(window[setupAndTeardownSymbol] === 3, &quot;Test should see side effects of previous test's teardown() action.&quot;);
+            return true;
+        },
+    });
+
+    setupAndTeardownTestSuite.runTestCases();
+
+    let setupExceptionTestSuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.SetupException&quot;);
+    setupExceptionTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringSetup&quot;,
+        description: &quot;Check execution order for setup action that throws an exception.&quot;,
+        setup: () =&gt; { throw new Error() },
+        test: () =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if its setup action threw an exception.&quot;);
+            return false;
+        },
+        teardown: () =&gt; {
+            ProtocolTest.assert(false, &quot;Teardown action should not execute if its setup action threw an exception.&quot;);
+            return false;           
+        }
+    });
+    setupExceptionTestSuite.runTestCases();
+
+    let setupFailureTestSuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.SetupFailure&quot;);
+    setupFailureTestSuite.addTestCase({
+        name: &quot;TestWithFailureDuringSetup&quot;,
+        description: &quot;Check execution order for setup action that has failed.&quot;,
+        setup: () =&gt; false,
+        test: () =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if its setup action returned false.&quot;)
+            return false;
+        },
+        teardown: () =&gt; {
+            ProtocolTest.assert(false, &quot;Teardown action should not execute if its setup action returned false.&quot;)
+            return false;           
+        }
+    });
+    setupFailureTestSuite.runTestCases();
+
+    let teardownExceptionTestSuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.TeardownException&quot;);
+    teardownExceptionTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringTeardown&quot;,
+        description: &quot;Check execution order for teardown action that throws an exception.&quot;,
+        test: () =&gt; true,
+        teardown: () =&gt; { throw new Error() }
+    });
+    teardownExceptionTestSuite.addTestCase({
+        name: &quot;TestAfterTeardownException&quot;,
+        descrption: &quot;Check execution order for test after previous test's teardown throws an exception&quot;,
+        setup: () =&gt; {
+            ProtocolTest.assert(false, &quot;Setup action should not execute if previous test's teardown action threw an exception.&quot;);
+            return false;
+        },
+        test: () =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if previous test's teardown action threw an exception.&quot;);
+            return false;
+        }
+    });
+    teardownExceptionTestSuite.runTestCases();
+
+    let teardownFailureTestSuite = ProtocolTest.createSyncSuite(&quot;SyncTestSuite.TeardownFailure&quot;);
+    teardownFailureTestSuite.addTestCase({
+        name: &quot;TestWithExceptionDuringTeardown&quot;,
+        description: &quot;Check execution order for teardown action that has failed.&quot;,
+        test: () =&gt; true,
+        teardown: () =&gt; false,
+    });
+    teardownFailureTestSuite.addTestCase({
+        name: &quot;TestAfterTeardownException&quot;,
+        descrption: &quot;Check execution order for test after previous test's teardown throws an exception&quot;,
+        setup: () =&gt; {
+            ProtocolTest.assert(false, &quot;Setup action should not execute if previous test's teardown action failed.&quot;);
+            return false;
+        },
+        test: () =&gt; {
+            ProtocolTest.assert(false, &quot;Test should not execute if previous test's teardown action failed.&quot;);
+            return false;
+        }
+    });
+    teardownFailureTestSuite.runTestCases();
+
</ins><span class="cx">     ProtocolTest.completeTest();
</span><span class="cx"> }
</span><span class="cx"> &lt;/script&gt;
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/ChangeLog (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/ChangeLog        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/Source/WebInspectorUI/ChangeLog        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2015-09-01  Brian Burg  &lt;bburg@apple.com&gt;
+
+        Web Inspector: add TestSuite hooks for per-testcase setup and teardown actions
+        https://bugs.webkit.org/show_bug.cgi?id=148641
+
+        Reviewed by Timothy Hatcher.
+
+        It should be possible to run setup and teardown actions for each test case.
+        The use case here is to add listeners, set up testcase-specific state, etc.
+        To match how test functions work in each suite, the sync and async implementations
+        take functions with no arguments and(resolve, reject), respectively.
+
+        * UserInterface/Test/TestSuite.js:
+        (TestSuite.prototype.addTestCase):
+        (TestSuite.messageFromThrownObject):
+        (TestSuite):
+        (AsyncTestSuite.prototype.runTestCases): Stick the extra functions in the chain.
+        (AsyncTestSuite):
+        (SyncTestSuite.prototype.runTestCases):
+        (SyncTestSuite):
+
</ins><span class="cx"> 2015-09-01  Matt Baker  &lt;mattbaker@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Web Inspector: Visual style editor shouldn't allow alpha characters in numeric input fields
</span></span></pre></div>
<a id="trunkSourceWebInspectorUIUserInterfaceTestTestSuitejs"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebInspectorUI/UserInterface/Test/TestSuite.js (189212 => 189213)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebInspectorUI/UserInterface/Test/TestSuite.js        2015-09-01 17:21:21 UTC (rev 189212)
+++ trunk/Source/WebInspectorUI/UserInterface/Test/TestSuite.js        2015-09-01 17:37:16 UTC (rev 189213)
</span><span class="lines">@@ -78,8 +78,26 @@
</span><span class="cx">         if (typeof testcase.test !== &quot;function&quot;)
</span><span class="cx">             throw new Error(&quot;Tried to add test case without `test` function.&quot;);
</span><span class="cx"> 
</span><ins>+        if (testcase.setup &amp;&amp; typeof testcase.setup !== &quot;function&quot;)
+            throw new Error(&quot;Tried to add test case with invalid `setup` parameter (must be a function).&quot;)
+
+        if (testcase.teardown &amp;&amp; typeof testcase.teardown !== &quot;function&quot;)
+            throw new Error(&quot;Tried to add test case with invalid `teardown` parameter (must be a function).&quot;)
+
</ins><span class="cx">         this.testcases.push(testcase);
</span><span class="cx">     }
</span><ins>+
+    static messageFromThrownObject(e)
+    {
+        let message = e;
+        if (e instanceof Error)
+            message = e.message;
+
+        if (typeof message !== &quot;string&quot;)
+            message = JSON.stringify(message);
+
+        return message;
+    }
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> AsyncTestSuite = class AsyncTestSuite extends TestSuite
</span><span class="lines">@@ -103,32 +121,42 @@
</span><span class="cx">         this._startedRunning = true;
</span><span class="cx"> 
</span><span class="cx">         this._harness.log(&quot;&quot;);
</span><del>-        this._harness.log(&quot;== Running test suite: &quot; + this.name);
</del><ins>+        this._harness.log(`== Running test suite: ${this.name}`);
</ins><span class="cx"> 
</span><span class="cx">         // Avoid adding newlines if nothing was logged.
</span><span class="cx">         let priorLogCount = this._harness.logCount;
</span><span class="cx">         let result = this.testcases.reduce((chain, testcase, i) =&gt; {
</span><del>-            return chain.then(() =&gt; {
</del><ins>+            if (testcase.setup) {
+                chain = chain.then(() =&gt; {
+                    this._harness.log(&quot;-- Running test setup.&quot;);
+                    return new Promise(testcase.setup);
+                });
+            }
+
+            chain = chain.then(() =&gt; {
</ins><span class="cx">                 if (i &gt; 0 &amp;&amp; priorLogCount + 1 &lt; this._harness.logCount)
</span><span class="cx">                     this._harness.log(&quot;&quot;);
</span><span class="cx"> 
</span><span class="cx">                 priorLogCount = this._harness.logCount;
</span><del>-                this._harness.log(&quot;-- Running test case: &quot; + testcase.name);
</del><ins>+                this._harness.log(`-- Running test case: ${testcase.name}`);
</ins><span class="cx">                 this.runCount++;
</span><span class="cx">                 return new Promise(testcase.test);
</span><span class="cx">             });
</span><ins>+
+            if (testcase.teardown) {
+                chain = chain.then(() =&gt; {
+                    this._harness.log(&quot;-- Running test teardown.&quot;);
+                    return new Promise(testcase.teardown);
+                });
+            }
+            return chain;
</ins><span class="cx">         }, Promise.resolve());
</span><span class="cx"> 
</span><span class="cx">         return result.catch((e) =&gt; {
</span><span class="cx">             this.failCount++;
</span><del>-            let message = e;
-            if (e instanceof Error)
-                message = e.message;
</del><ins>+            let message = TestSuite.messageFromThrownObject(e);
+            this._harness.log(`!! EXCEPTION: ${message}`);
</ins><span class="cx"> 
</span><del>-            if (typeof message !== &quot;string&quot;)
-                message = JSON.stringify(message);
-
-            this._harness.log(&quot;!! EXCEPTION: &quot; + message);
</del><span class="cx">             throw e; // Reject this promise by re-throwing the error.
</span><span class="cx">         });
</span><span class="cx">     }
</span><span class="lines">@@ -152,7 +180,7 @@
</span><span class="cx">         this._startedRunning = true;
</span><span class="cx"> 
</span><span class="cx">         this._harness.log(&quot;&quot;);
</span><del>-        this._harness.log(&quot;== Running test suite: &quot; + this.name);
</del><ins>+        this._harness.log(`== Running test suite: ${this.name}`);
</ins><span class="cx"> 
</span><span class="cx">         let priorLogCount = this._harness.logCount;
</span><span class="cx">         for (let i = 0; i &lt; this.testcases.length; i++) {
</span><span class="lines">@@ -162,6 +190,22 @@
</span><span class="cx"> 
</span><span class="cx">             priorLogCount = this._harness.logCount;
</span><span class="cx"> 
</span><ins>+            // Run the setup action, if one was provided.
+            if (testcase.setup) {
+                this._harness.log(&quot;-- Running test setup.&quot;);
+                try {
+                    let result = testcase.setup.call(null);
+                    if (result === false) {
+                        this._harness.log(&quot;!! EXCEPTION&quot;);
+                        return false;
+                    }
+                } catch (e) {
+                    let message = TestSuite.messageFromThrownObject(e);
+                    this._harness.log(`!! EXCEPTION: ${message}`);
+                    return false;
+                }
+            }
+
</ins><span class="cx">             this._harness.log(&quot;-- Running test case: &quot; + testcase.name);
</span><span class="cx">             this.runCount++;
</span><span class="cx">             try {
</span><span class="lines">@@ -172,18 +216,26 @@
</span><span class="cx">                 }
</span><span class="cx">             } catch (e) {
</span><span class="cx">                 this.failCount++;
</span><del>-                let message = e;
-                if (e instanceof Error)
-                    message = e.message;
-                else
-                    e = new Error(e);
-
-                if (typeof message !== &quot;string&quot;)
-                    message = JSON.stringify(message);
-
-                this._harness.log(&quot;!! EXCEPTION: &quot; + message);
</del><ins>+                let message = TestSuite.messageFromThrownObject(e);
+                this._harness.log(`!! EXCEPTION: ${message}`);
</ins><span class="cx">                 return false;
</span><span class="cx">             }
</span><ins>+
+            // Run the teardown action, if one was provided.
+            if (testcase.teardown) {
+                this._harness.log(&quot;-- Running test teardown.&quot;);
+                try {
+                    let result = testcase.teardown.call(null);
+                    if (result === false) {
+                        this._harness.log(&quot;!! EXCEPTION:&quot;);
+                        return false;
+                    }
+                } catch (e) {
+                    let message = TestSuite.messageFromThrownObject(e);
+                    this._harness.log(`!! EXCEPTION: ${message}`);
+                    return false;
+                }
+            }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return true;
</span></span></pre>
</div>
</div>

</body>
</html>