chromium/third_party/blink/web_tests/external/wpt/html/semantics/invokers/invoketarget-on-popover-behavior.tentative.html

<!doctype html>
<meta charset="utf-8" />
<meta name="author" title="Keith Cirkel" href="mailto:[email protected]" />
<meta name="timeout" content="long" />
<link rel="help" href="https://open-ui.org/components/invokers.explainer/" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/invoker-utils.js"></script>

<div id="invokee" popover>
  <button id="containedinvoker" commandfor="invokee" command="hidepopover"></button>
</div>
<button id="invokerbutton" commandfor="invokee" command="togglepopover"></button>

<script>
  function resetState() {
    invokerbutton.setAttribute("commandfor", "invokee");
    invokerbutton.setAttribute("command", "togglepopover");
    containedinvoker.setAttribute("commandfor", "invokee");
    containedinvoker.setAttribute("command", "closepopover");
    try {
      invokee.hidePopover();
    } catch {}
    invokee.setAttribute("popover", "");
  }

  promise_test(async function (t) {
    assert_false(invokee.matches(":popover-open"));
    invokee.addEventListener("command", (e) => { invokerbutton.setAttribute('command', 'hidepopover'); }, {
      once: true,
    });
    await clickOn(invokerbutton);
    t.add_cleanup(resetState);
    assert_true(invokee.matches(":popover-open"));
  }, "changing command attribute inside invokeevent doesn't impact the invocation");

  // Open actions
  [
    "togglepopover",
    "showpopover",
    /* test case sensitivity */
    "tOgGlEpOpOvEr",
    "sHoWpOpOvEr",
  ].forEach((command) => {
    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        invokerbutton.command = command;
        assert_false(invokee.matches(":popover-open"));
        await clickOn(invokerbutton);
        assert_true(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) closed popover opens`,
    );

    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        invokerbutton.command = command;
        assert_false(invokee.matches(":popover-open"));
        invokee.addEventListener("command", (e) => e.preventDefault(), {
          once: true,
        });
        await clickOn(invokerbutton);
        assert_false(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) closed popover with preventDefault does not open`,
    );
  });

  // Close actions
  [
    "togglepopover",
    "hidepopover",
    /* test case sensitivity */
    "tOgGlEpOpOvEr",
    "hIdEpOpOvEr",
  ].forEach((command) => {
    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        invokerbutton.command = command;
        invokee.showPopover();
        assert_true(invokee.matches(":popover-open"));
        await clickOn(invokerbutton);
        assert_false(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) open popover closes`,
    );

    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        invokerbutton.command = command;
        invokee.showPopover();
        assert_true(invokee.matches(":popover-open"));
        invokee.addEventListener("command", (e) => e.preventDefault(), {
          once: true,
        });
        await clickOn(invokerbutton);
        assert_true(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) open popover with preventDefault does not close`,
    );

    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        containedinvoker.command = command;
        invokee.showPopover();
        assert_true(invokee.matches(":popover-open"));
        await clickOn(containedinvoker);
        assert_false(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) from within open popover closes`,
    );

    promise_test(
      async function (t) {
        t.add_cleanup(resetState);
        containedinvoker.command = command;
        invokee.showPopover();
        invokee.addEventListener("command", (e) => e.preventDefault(), {
          once: true,
        });
        assert_true(invokee.matches(":popover-open"));
        await clickOn(containedinvoker);
        assert_true(invokee.matches(":popover-open"));
      },
      `invoking (as ${command}) from within open popover with preventDefault does not close`,
    );
  });

  // showpopover specific
  promise_test(async function (t) {
    t.add_cleanup(resetState);
    invokerbutton.setAttribute("command", "showpopover");
    invokee.showPopover();
    assert_true(invokee.matches(":popover-open"));
    await clickOn(invokerbutton);
    assert_true(invokee.matches(":popover-open"));
  }, "invoking (as showpopover) open popover is noop");

  // hidepopover specific
  promise_test(async function (t) {
    t.add_cleanup(resetState);
    invokerbutton.setAttribute("command", "hidepopover");
    assert_false(invokee.matches(":popover-open"));
    await clickOn(invokerbutton);
    assert_false(invokee.matches(":popover-open"));
  }, "invoking (as hidepopover) closed popover is noop");
</script>