<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<window title="Test spawnTawk function"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"/>
  <script type="application/javascript">
  <![CDATA[
    SimpleTest.waitForExplicitFinish();

    var externalGeneratorFunction = function* () {
      return 8;
    };

    var nestedFunction = function* () {
      return yield function* () {
        return yield function* () {
          return yield function* () {
            return yield Promise.resolve(9);
          }();
        }();
      }();
    }

    var variousTests = function* () {
      var val1 = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
      is(val1.join(""), "123", "Array of promises -> Promise.all");
      var val2 = yield Promise.resolve(2);
      is(val2, 2, "Resolved promise yields value.");
      var val3 = yield function* () { return 3; };
      is(val3, 3, "Generator functions are spawned.");
      //var val4 = yield function () { return 4; };
      //is(val4, 4, "Plain functions run and return.");
      var val5 = yield (function* () { return 5; }());
      is(val5, 5, "Generators are spawned.");
      try {
        var val6 = yield Promise.reject(Error("error6"));
        ok(false, "Shouldn't reach this line.");
      } catch (error) {
        is(error.message, "error6", "Rejected promise throws error.");
      }
      try {
        var val7 = yield function* () { throw Error("error7"); };
        ok(false, "Shouldn't reach this line.");
      } catch (error) {
        is(error.message, "error7", "Thrown error propagates.");
      }
      var val8 = yield externalGeneratorFunction();
      is(val8, 8, "External generator also spawned.");
      var val9 = yield nestedFunction();
      is(val9, 9, "Nested generator functions work.");
      return 10;
    };

    spawn_task(variousTests).then(function(result) {
      is(result, 10, "spawn_task(...) returns promise");
      SimpleTest.finish();
    });
    ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml" >
  </body>
</window>
