<!DOCTYPE html>
<html>
<title>HTMLSelectMenuElement Test: popup position with zoom</title>
<link rel="author" title="Ionel Popescu" href="mailto:iopopesc@microsoft.com">
<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>

<style>
  #selectMenu0 {
    position: absolute;
    top: 0px;
    left: 0px;
    zoom: 2;
  }

  #selectMenu1 {
    position: absolute;
    bottom: 0px;
    left: 0px;
    zoom: 1.5;
  }

  #selectMenu1-popup {
    zoom: 2;
  }

  #selectMenu2 {
    position: absolute;
    top: 0px;
    right: 0px;
    zoom: 3;
  }

  #selectMenu3 {
    position: absolute;
    bottom: 0px;
    right: 0px;
    zoom: 4;
  }

  #selectMenu3-popup {
    zoom: 1.5;
  }
</style>

<selectmenu id="selectMenu0">
  <div slot="button" behavior="button" id="selectMenu0-button">Custom bottom left</div>
  <div popup slot="listbox" behavior="listbox" id="selectMenu0-popup">
    <option>bottom left</option>
    <option>two</option>
    <option>three</option>
  </div>
</selectmenu>
<br>

<selectmenu id="selectMenu1">
  <div slot="button" behavior="button" id="selectMenu1-button">Custom top left</div>
  <div popup slot="listbox" behavior="listbox" id="selectMenu1-popup">
    <option>top left</option>
    <option>two</option>
    <option>three</option>
  </div>
</selectmenu>

<selectmenu id="selectMenu2">
  <div slot="button" behavior="button" id="selectMenu2-button">Custom bottom right</div>
  <div popup slot="listbox" behavior="listbox" id="selectMenu2-popup">
    <option>bottom right</option>
    <option>two</option>
    <option>three</option>
  </div>
</selectmenu>

<selectmenu id="selectMenu3">
  <div slot="button" behavior="button" id="selectMenu3-button">Custom top right</div>
  <div popup slot="listbox" behavior="listbox" id="selectMenu3-popup">
    <option>top right</option>
    <option>two</option>
    <option>three</option>
  </div>
</selectmenu>

<script>
  function clickOn(element) {
    const actions = new test_driver.Actions();
    return actions.pointerMove(0, 0, {origin: element})
      .pointerDown({button: actions.ButtonType.LEFT})
      .pointerUp({button: actions.ButtonType.LEFT})
      .send();
  }

  promise_test(async () => {
    const selectMenu0 = document.getElementById("selectMenu0");
    const selectMenu0Popup = document.getElementById("selectMenu0-popup");
    const selectMenu0Button = document.getElementById("selectMenu0-button");

    await clickOn(selectMenu0);
    assert_equals(Math.abs(Math.trunc(selectMenu0.getBoundingClientRect().bottom - selectMenu0Popup.getBoundingClientRect().top)), 0);
    assert_equals(Math.abs(Math.trunc(selectMenu0.getBoundingClientRect().left - selectMenu0Popup.getBoundingClientRect().left)), 0);
  }, "The popup should be bottom left positioned");

  promise_test(async () => {
    const selectMenu1 = document.getElementById("selectMenu1");
    const selectMenu1Popup = document.getElementById("selectMenu1-popup");
    const selectMenu1Button = document.getElementById("selectMenu1-button");

    selectMenu1Button.click();
    assert_equals(Math.abs(Math.trunc(selectMenu1.getBoundingClientRect().top - selectMenu1Popup.getBoundingClientRect().bottom * 2)), 0);
    assert_equals(Math.abs(Math.trunc(selectMenu1.getBoundingClientRect().left - selectMenu1Popup.getBoundingClientRect().left * 2)), 0);
  }, "The popup should be top left positioned");

  promise_test(async () => {
    const selectMenu2 = document.getElementById("selectMenu2");
    const selectMenu2Popup = document.getElementById("selectMenu2-popup");
    const selectMenu2Button = document.getElementById("selectMenu2-button");

    selectMenu2Button.click();
    assert_equals(Math.abs(Math.trunc(selectMenu2.getBoundingClientRect().bottom - selectMenu2Popup.getBoundingClientRect().top)), 0);
    assert_equals(Math.abs(Math.trunc(selectMenu2.getBoundingClientRect().right - selectMenu2Popup.getBoundingClientRect().right)), 0);
  }, "The popup should be bottom right positioned");

  promise_test(async () => {
    const selectMenu3 = document.getElementById("selectMenu3");
    const selectMenu3Popup = document.getElementById("selectMenu3-popup");
    const selectMenu3Button = document.getElementById("selectMenu3-button");

    selectMenu3Button.click();
    assert_equals(Math.abs(Math.trunc(selectMenu3.getBoundingClientRect().top - selectMenu3Popup.getBoundingClientRect().bottom * 1.5)), 0);
    assert_equals(Math.abs(Math.trunc(selectMenu3.getBoundingClientRect().right - selectMenu3Popup.getBoundingClientRect().right * 1.5)), 0);
  }, "The popup should be top right positioned");

</script>
