<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1081037
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1081037</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1081037 **/

function shouldThrow(fun, msg, ex, todo) {
  try {
    fun();
    ok(todo, msg);
  } catch (e) {
    ok(new RegExp(ex).test(e), msg + " (thrown:" + e + ")")
  }
}

var Foo = document.registerElement('x-foo', {
  prototype: {bar: 5}
});

Foo.prototype.bar = 6;
var foo = new Foo();
is(foo.bar, 6, "prototype of the ctor returned from registerElement works");

var protoDesc = Object.getOwnPropertyDescriptor(Foo, "prototype");
is(protoDesc.configurable, false, "proto should be non-configurable");
is(protoDesc.enumerable, false, "proto should be non-enumerable");
is(protoDesc.writable, false, "proto should be non-writable");

// TODO: FIXME!
shouldThrow(function() {
              document.registerElement('x-foo2', {
                prototype: Foo.prototype
              });
            },
            "if proto is an interface prototype object, registerElement should throw",
            "not supported",
            /* todo = */ true);

var nonConfigReadonlyProto = Object.create(HTMLElement.prototype,
  { constructor: { configurable: false, writable: false, value: 42 } });

shouldThrow(function() {
              document.registerElement('x-nonconfig-readonly', {
                prototype: nonConfigReadonlyProto
              });
            },
            "non-configurable and not-writable constructor property",
            "not supported");


// this is not defined in current spec:
var readonlyProto = Object.create(HTMLElement.prototype,
  { constructor: { configurable: true, writable: false, value: 42 } });

var Readonly = document.registerElement('x-nonconfig-readonly', {
  prototype: readonlyProto
});

is(Readonly.prototype, readonlyProto, "configurable readonly constructor property");

var handler = {
  getOwnPropertyDescriptor: function(target, name) {
    return name == "constructor" ? undefined : Object.getOwnPropertyDescriptor(target,name);
  },
  defineProperty: function(target, name, propertyDescriptor) {
     if (name == "constructor") {
      throw "spec this";
    }

    return Object.defineProperty(target, name, propertyDescriptor);
  },
  has: function(target, name) {
    if (name == "constructor") {
      return false;
    }
    return name in target;
  }
};
var proxy = new Proxy({}, handler);

shouldThrow(function() {
              document.registerElement('x-proxymagic', {
                prototype: proxy
              });
            },
            "proxy magic",
            "spec this");

var getOwn = 0;
var defineProp = 0;
var handler2 = {
  getOwnPropertyDescriptor: function(target, name) {
    if (name == "constructor") {
      getOwn++;
    }
    return Object.getOwnPropertyDescriptor(target,name);
  },
  defineProperty: function(target, name, propertyDescriptor) {
    if (name == "constructor") {
      defineProp++;
    }
    return Object.defineProperty(target, name, propertyDescriptor);
  }
};
var proxy2 = new Proxy({}, handler2);

document.registerElement('x-proxymagic2', {
  prototype: proxy2
});

is(getOwn, 1, "number of getOwnPropertyDescriptor calls from registerElement: " + getOwn);
is(defineProp, 1, "number of defineProperty calls from registerElement: " + defineProp);

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1081037">Mozilla Bug 1081037</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>