<!DOCTYPE html>

<html>
<head>
    <script id="ui-script" type="text/plain">
        (function() {
            var completions = 'Running the UI-side script\n';
            var outstandingTaskCount = 3;
            
            uiController.doAsyncTask(function() {
                completions += 'Completed async task 1.1.\n';

                uiController.doAsyncTask(function() {
                    completions += 'Completed async task 1.2.\n';

                    uiController.doAsyncTask(function() {
                        completions += 'Completed async task 1.3.\n';

                        --outstandingTaskCount;
                        checkIfTasksFinished();
                    });
                });
            });

            uiController.doAsyncTask(function() {
                completions += 'Completed async task 2.1.\n';

                uiController.doAsyncTask(function() {
                    completions += 'Completed async task 2.1.\n';

                    uiController.doAsyncTask(function() {
                        completions += 'Completed async task 2.3.\n';

                        --outstandingTaskCount;
                        checkIfTasksFinished();
                    });
                });

                uiController.doAsyncTask(function() {
                    completions += 'Completed async task 2.2.1.\n';

                    uiController.doAsyncTask(function() {
                        completions += 'Completed async task 2.2.2.\n';

                        --outstandingTaskCount;
                        checkIfTasksFinished();
                    });
                });
            });
            
            function checkIfTasksFinished()
            {
                if (!outstandingTaskCount)
                    uiController.uiScriptComplete(completions);
            }
        })();
    </script>

    <script>
        if (window.testRunner) {
            testRunner.dumpAsText();
            testRunner.waitUntilDone();
        }

        function runTest()
        {
            if (testRunner.runUIScript) {
                var uiScript = document.getElementById('ui-script').text;
                testRunner.runUIScript(uiScript, function(result) {
                    document.getElementById('result').textContent = result;
                    testRunner.notifyDone();
                });
            }
        }

        window.addEventListener('load', runTest, false);
    </script>
</head>
<body>

<pre id="result">This test has to run in iOS WebKitTestRunner.</pre>

</body>
</html>
.