https://github.com/web-platform-tests/wpt
Raw File
Tip revision: eb313d646aff9c95247c506a19f1922f44b267ea authored by Sandra Sun on 19 December 2018, 15:38:18 UTC
Snap after layout change
Tip revision: eb313d6
Event-dispatch-click.html
<!doctype html>
<title>Synthetic click event "magic"</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<div id=dump style=display:none></div>
<script>
var dump = document.getElementById("dump")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "basic with click()")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.dispatchEvent(new MouseEvent("click", {bubbles:true})) // equivalent to the above
}, "basic with dispatchEvent()")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_false(input.checked)
  })
  input.dispatchEvent(new Event("click", {bubbles:true})) // no MouseEvent
}, "basic with wrong event class")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var child = input.appendChild(new Text("does not matter"))
  child.dispatchEvent(new MouseEvent("click")) // does not bubble
  assert_false(input.checked)
  t.done()
}, "look at parents only when event bubbles")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  var child = input.appendChild(new Text("does not matter"))
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
}, "look at parents when event bubbles")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func(function() {
    assert_false(input.checked, "input pre-click must not be triggered")
  })
  var child = input.appendChild(document.createElement("input"))
  child.type = "checkbox"
  child.onclick = t.step_func(function() {
    assert_true(child.checked, "child pre-click must be triggered")
  })
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
  t.done()
}, "pick the first with activation behavior <input type=checkbox>")

var globalCounter = 0 // sorry
async_test(function(t) { // as above with <a>
  var i = 0
  var link = document.createElement("a")
  link.href = "javascript:(function(){globalCounter--})()" // must not be triggered
  dump.appendChild(link)
  var child = link.appendChild(document.createElement("a"))
  child.href = "javascript:(function(){globalCounter++})()"
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
  assert_equals(globalCounter, 1)
  t.done()
}, "pick the first with activation behavior <a href>")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var clickEvent = new MouseEvent("click")
  input.onchange = t.step_func_done(function() {
    assert_false(clickEvent.defaultPrevented)
    assert_true(clickEvent.returnValue)
    assert_equals(clickEvent.eventPhase, 0)
    assert_equals(clickEvent.currentTarget, null)
    assert_equals(clickEvent.target, input)
    assert_equals(clickEvent.srcElement, input)
    assert_equals(clickEvent.composedPath().length, 0)
  })
  input.dispatchEvent(clickEvent)
}, "event state during post-click handling")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var clickEvent = new MouseEvent("click")
  var finalTarget = document.createElement("doesnotmatter")
  finalTarget.onclick = t.step_func_done(function() {
    assert_equals(clickEvent.target, finalTarget)
    assert_equals(clickEvent.srcElement, finalTarget)
  })
  input.onchange = t.step_func(function() {
    finalTarget.dispatchEvent(clickEvent)
  })
  input.dispatchEvent(clickEvent)
}, "redispatch during post-click handling")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var child = input.appendChild(document.createElement("input"))
  child.type = "checkbox"
  child.disabled = true
  child.click()
  assert_false(input.checked)
  assert_false(child.checked)
  t.done()
}, "disabled checkbox still has activation behavior")

async_test(function(t) {
  var state = "start"

  var form = document.createElement("form")
  form.onsubmit = t.step_func(() => {
    if(state == "start" || state == "checkbox") {
      state = "failure"
    } else if(state == "form") {
      state = "done"
    }
    return false
  })
  dump.appendChild(form)
  var button = form.appendChild(document.createElement("button"))
  button.type = "submit"
  var checkbox = button.appendChild(document.createElement("input"))
  checkbox.type = "checkbox"
  checkbox.onclick = t.step_func(() => {
    if(state == "start") {
      assert_unreached()
    } else if(state == "checkbox") {
      assert_true(checkbox.checked)
    }
  })
  checkbox.disabled = true
  checkbox.click()
  assert_equals(state, "start")

  state = "checkbox"
  checkbox.disabled = false
  checkbox.click()
  assert_equals(state, "checkbox")

  state = "form"
  button.click()
  assert_equals(state, "done")

  t.done()
}, "disabled checkbox still has activation behavior, part 2")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "disconnected checkbox should be checked")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "radio"
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "disconnected radio should be checked")

async_test(function(t) {
  var form = document.createElement("form")
  var didSubmit = false
  form.onsubmit = t.step_func(() => {
    didSubmit = true
    return false
  })
  var input = form.appendChild(document.createElement("input"))
  input.type = "submit"
  input.click()
  assert_false(didSubmit)
  t.done()
}, "disconnected form should not submit")
</script>
back to top