<!DOCTYPE html>
<title>Test Worker constructor functionality.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

test(() => {
  assert_throws_js(Error,
                   function() {
                     new Worker({toString:function(){throw new Error()}})},
                   'toString exception should be propagated');
}, 'Test toString propagation exception.');

test(() => {
  try {
    var foo = {toString:function(){new Worker(foo)}};
    new Worker(foo);
  } catch(ex) {
    assert_true(true, 'trying to create workers recursively should result in an: ' +
                ex + 'exception.')
  }
}, 'Test recursive Worker creation.');

test(() => {
  assert_throws_js(TypeError,
                   function() { new Worker(); },
                   'invoking Worker constructor without arguments should result ' +
                   'in an exception.')
}, 'Test worker creation with no arguments');

async_test(t => {
  var worker = new Worker('');
  worker.onerror = function(e) {
    e.preventDefault();
    t.done();
  }
}, 'Test Worker creation with empty script URL.');

test(() => {
  assert_throws_dom("SyntaxError",
                    function() { var worker = new Worker('http://invalid:123$'); },
                    'Invoking Worker constructor with invalid script URL should ' +
                    'result in an exception.');
}, 'Test invalid script URL.');

async_test(t => {
  var worker = new Worker('does-not-exist.js');
  worker.onerror = function(e) {
    t.done();
  }
}, 'Test not existent script URL.');

test(() => {
  var worker = new Worker('../../support/Worker-common.js');
  assert_true('postMessage' in worker,
              'worker.postMessage did not exist.');
  assert_true('addEventListener' in worker,
              'worker.addEventListener did not exist.');
}, 'Test the Worker object defines postMessage() and addEventListener().');
</script>
