<!DOCTYPE html>
<html lang="en">
<title>HTMLSelectMenuElement Test: validity</title>
<link rel="author" title="Ionel Popescu" href="mailto:iopopesc@microsoft.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<selectmenu id="selectmenu1" required>
  <option>one</option>
  <option>two</option>
  <option>three</option>
  <option>four</option>
</selectmenu>

<form>
  <selectmenu id="selectmenu2" required>
  </selectmenu>
</form>

<script>

test(() => {
  let selectMenu = document.createElement('selectmenu');
  assert_true(selectMenu.willValidate, "A selectmenu element is a submittable element that is a candidate for constraint validation.");
  let option = document.createElement('option');
  selectMenu.appendChild(option);
  assert_true(selectMenu.checkValidity(), "Always valid when the selectmenu isn't a required value.");

  selectMenu.required = true;
  assert_equals(selectMenu.value, "");
  assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu.");

  let emptyOption = document.createElement('option');
  selectMenu.appendChild(emptyOption);
  assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu even if there are multiple options.");
  emptyOption.selected = true;
  assert_true(selectMenu.checkValidity(), "An empty non-placeholder option should be a valid choice.");

  let filledOption = document.createElement('option');
  filledOption.value = "test";
  selectMenu.appendChild(filledOption);
  filledOption.selected = true;
  assert_equals(selectMenu.value, "test", "The non-empty value should be set.");
  assert_true(selectMenu.checkValidity(), "A non-empty non-placeholder option should be a valid choice.");

  selectMenu.removeChild(option);
  selectMenu.appendChild(emptyOption);
  emptyOption.selected = true;
  assert_equals(selectMenu.value, "", "The empty value should be set.");
  assert_true(selectMenu.checkValidity(), "Only the first option can be seen as a placeholder.");

  selectMenu.removeChild(filledOption);
  assert_false(selectMenu.checkValidity(), "A selected placeholder option should invalidate the selectmenu.");

  emptyOption.value = "test2";
  assert_equals(selectMenu.value, "test2");
  assert_true(selectMenu.checkValidity(), "A non-empty option value should be a valid choice.");

  emptyOption.removeAttribute("value");
  assert_equals(selectMenu.value, "");
  assert_false(selectMenu.checkValidity());
  emptyOption.innerText = "test";
  assert_equals(selectMenu.value, "test");
  assert_true(selectMenu.checkValidity(), "A non-empty option should be a valid choice.");

  const selectMenu1 = document.getElementById('selectmenu1');
  assert_equals(selectMenu1.value, "one");
  assert_true(selectMenu1.checkValidity(), "A selectmenu with non-empty placeholder option should be valid.");
}, "Validation for placeholder option");

test(() => {
  const selectMenu2 = document.getElementById('selectmenu2');
  assert_equals(selectMenu2.value, "");
  assert_false(selectMenu2.checkValidity());
  let form = document.querySelector('form');
  let invalidControl = form.querySelector('selectmenu:invalid');
  assert_equals(selectMenu2, invalidControl);
  let didDispatchInvalid = false;
  invalidControl.addEventListener('invalid', e => { didDispatchInvalid = true; });
  let didDispatchSubmit = false;
  form.addEventListener('submit', event => { event.preventDefault(); didDispatchSubmit = true; });

  form.requestSubmit();
  assert_true(didDispatchInvalid);
  assert_false(didDispatchSubmit);
}, "Check form not submitted for invalid selectmenu");

</script>
