click_event_target_siblings.html (3985B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Click targets the nearest common ancestor</title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="/resources/testdriver.js"></script> 9 <script src="/resources/testdriver-actions.js"></script> 10 <script src="/resources/testdriver-vendor.js"></script> 11 <style> 12 div { 13 padding: 10px; 14 margin: 5px; 15 } 16 </style> 17 </head> 18 <body id='body'> 19 <h1>Click targeting when targets of down and up are sibling elements</h1> 20 This test verifies that click event always goes to the first common ancestor of down and up event targets. 21 22 <ul> 23 <li>Press down the primary button on red div and move to blue box and release.</li> 24 <li>Press down the primary button on button b1 and move to button b2 and release.</li> 25 <li>Press down the primary button on input i1 and move to input i2 and release.</li> 26 <li>Press down the primary button on link 1 and move to link 2 and release.</li> 27 <li>Click done.</li> 28 </ul> 29 30 <div id="div_container" style="background: green"> 31 <div id="red_div" style="background: red"></div> 32 <div id="blue_div" style="background: blue"></div> 33 </div> 34 35 <div id="button_container" style="background: green"> 36 <button id="button1">b1</button> 37 <button id="button2">b2</button> 38 </div> 39 40 <div id="input_container" style="background: green"> 41 <input id="input1" value="i1"> 42 <input id="input2" value="i2"> 43 </div> 44 45 <div id="link_container" style="background: green"> 46 <a id="link1" href="#">link1</a> 47 <br/> 48 <a id="link2" href="#">link2</a> 49 </div> 50 51 <button id="done">Done</button> 52 <script> 53 var test_click_target = async_test("Click targets the nearest common ancestor"); 54 var actions_promise; 55 56 // Prevent drag to avoid interfering with the click. 57 document.addEventListener('dragstart', (e) => e.preventDefault()); 58 59 var events = []; 60 var nodes = ['div_container', 'red_div', 'blue_div', 'button_container', 'button1', 'button2', 'input_container', 'input1', 'input2', 'link_container', 'link1', 'link2', 'body']; 61 62 for (var i = 0; i < nodes.length; i++) { 63 ['mousedown', 'mouseup', 'click'].forEach((eventName) => { 64 document.getElementById(nodes[i]).addEventListener(eventName, (e) => { 65 if (e.eventPhase == Event.AT_TARGET) 66 events.push(e.type + '@' + e.target.id); 67 }); 68 }); 69 } 70 document.getElementById('done').addEventListener('click', () => { 71 test_click_target.step(() => { 72 assert_equals (events.join(','), 73 "mousedown@red_div,mouseup@blue_div,click@div_container,mousedown@button1,mouseup@button2,click@button_container,mousedown@link1,mouseup@link2,click@link_container", 74 "Click should be sent to the nearest common ancestor"); 75 }); 76 // Make sure the test finishes after all the input actions are completed. 77 actions_promise.then( () => { 78 test_click_target.done(); 79 }); 80 }); 81 82 // Inject mouse events. 83 var actions = new test_driver.Actions(); 84 actions_promise = actions.pointerMove(0, 0, {origin: document.getElementById('red_div')}) 85 .pointerDown() 86 .pointerMove(0, 0, {origin: document.getElementById('blue_div')}) 87 .pointerUp() 88 .pointerMove(0, 0, {origin: document.getElementById('button1')}) 89 .pointerDown() 90 .pointerMove(0, 0, {origin: document.getElementById('button2')}) 91 .pointerUp() 92 .pointerMove(0, 0, {origin: document.getElementById('link1')}) 93 .pointerDown() 94 .pointerMove(0, 0, {origin: document.getElementById('link2')}) 95 .pointerUp() 96 .pointerMove(0, 0, {origin: document.getElementById('done')}) 97 .pointerDown() 98 .pointerUp() 99 .send(); 100 </script> 101 </body> 102 </html>