<!doctype html>
<script>
  var searchParams = new URL(location).searchParams;
  var test = searchParams.get("test");

  window.onportalactivate = function(e) {
    if (test == "adopt-once") {
      var portal = e.adoptPredecessor();
      document.body.appendChild(portal);

      if (portal instanceof HTMLPortalElement) {
        portal.postMessage("adopted");
      }
    }
    if (test == "adopt-twice") {
      var portal = e.adoptPredecessor();
      document.body.appendChild(portal);

      try {
        e.adoptPredecessor();
      } catch(e) {
        if (e.name == "InvalidStateError") {
          portal.postMessage("passed");
        }
      }
    }
    if (test == "adopt-after-event") {
      setTimeout(function() {
        try {
          e.adoptPredecessor();
        } catch(e) {
          if (e.name == "InvalidStateError") {
            var bc_test = new BroadcastChannel(`test-${test}`);
            bc_test.postMessage("passed");
            bc_test.close();
          }
        }
      });
    }
    if (test == "adopt-and-activate") {
      var portal = e.adoptPredecessor();
      portal.activate();
    }
    if (test == "adopt-attach-remove") {
      var portal = e.adoptPredecessor();
      document.body.appendChild(portal);
      setTimeout(() => {
        document.body.removeChild(portal);
        var bc_test = new BroadcastChannel(`test-${test}`);
        bc_test.postMessage("passed");
        bc_test.close();
      });
    }
    if (test == "adopt-and-discard") {
      var portal = e.adoptPredecessor();
      setTimeout(() => {
        // portal should be inactive and activate should fail.
        portal.activate().catch(e => {
          if (e.name == "InvalidStateError") {
            var bc_test = new BroadcastChannel(`test-${test}`);
            bc_test.postMessage("passed");
            bc_test.close();
          }
        });
      });
    }
    if (test == "adopt-to-disconnected-node") {
      var portal = e.adoptPredecessor();
      document.body.appendChild(portal);
      var node = document.createElement("div");
      node.appendChild(portal);
      var bc_test = new BroadcastChannel(`test-${test}`);
      bc_test.postMessage("passed");
      bc_test.close();
    }
  }
</script>
