ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith <title>YUI Context Menu Synth Event Tests</title>
f856a43636b28d436393ba16bcbe9016ebf7a59aTodd Kloots filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
f856a43636b28d436393ba16bcbe9016ebf7a59aTodd Kloots allowRollup: false,
f856a43636b28d436393ba16bcbe9016ebf7a59aTodd Kloots lazyEventFacade: true,
f856a43636b28d436393ba16bcbe9016ebf7a59aTodd Kloots logExclude: { Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true }
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith }).use('console', 'test', 'event-contextmenu', 'node-event-simulate', 'window-focus', 'node-screen', function (Y) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var called = 0;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var onContextMenu = function () {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith called = called + 1;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var isWinWebkit = (Y.UA.webkit && Y.UA.os === "windows");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var keyDownConfig = Y.UA.os == "macintosh" ? {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith shiftKey: true,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith ctrlKey: true,
e456dceed80992a528fd8dfed0ebedf3598a4adbTodd Kloots altKey: (Y.UA.opera ? false : true),
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith shiftKey: true,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith keyCode: 121
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var doc = Y.one("doc");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var btn = Y.one("#btn-1");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var simulateKeyDown = function () {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith // For Chrome on Windows a real Shift + F10 keydown
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith // event generated from the user WILL trigger the
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith // contextmenu event. A simulated Shift + F10 does
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith // not, so need to simulate what the browser does for
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith // the test.
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (isWinWebkit) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.once("keydown", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("keydown", keyDownConfig);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith (new Y.Console()).render();
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var suite = new Y.Test.Suite("ContextMenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith name: "ContextMenu Synth Tests",
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_contextmenu_event: function () {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.isTrue((called === 1), "contextmenu event listener not called");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_mac_kb_shortct: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith keyCode: 77,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith shiftKey: true,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith ctrlKey: true,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith altKey: true
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "macintosh") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(1, called, "contextmenu event listener not called");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(0, called, "ctrl + shift + alt + M should only fire the contextmenu event for mac");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_shiftF10_key: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (isWinWebkit) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.once("keydown", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("keydown", { shiftKey: true, keyCode: 121 });
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "windows") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(1, called, "contextmenu event listener not called");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(0, called, "the shift + f10 key should only fire the contextmenu event on windows");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_menu_key: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "windows") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.once("keydown", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (e.keyCode === 93) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("keydown", { keyCode: 93 });
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "windows") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(1, called, "contextmenu event listener not called");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(0, called, "the Menu key should only fire the contextmenu event on windows");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_xy_coords_for_menu: function () {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var received = false;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith received = true;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith clientX = (xy[0] + (btn.get("offsetWidth")/2)) - scrollX,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith clientY = (xy[1] + (btn.get("offsetHeight")/2)) - scrollY,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith pageX = clientX + scrollX,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith pageY = clientY + scrollY;
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.clientX, 10), parseInt(clientX, 10), "clientX mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.clientY, 10), parseInt(clientY, 10), "clientY mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.pageX, 10), parseInt(pageX, 10), "pageX mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.pageY, 10), parseInt(pageY, 10), "pageY mismatch");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "windows") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.once("keydown", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (e.keyCode === 93) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.simulate("keydown", { keyCode: 93 });
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith if (Y.UA.os == "windows") {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.isTrue(received, "contextmenu event xy coords don't reference the center of the event target");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.isFalse(received, "the Menu key should only fire the contextmenu event on windows");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_xy_coords_for_shift10_or_ctrl_shift_option_m: function () {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith var received = false;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", function (e) {
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith received = true;
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith clientX = Math.floor(xy[0] + (btn.get("offsetWidth")/2)) - scrollX,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith clientY = Math.floor(xy[1] + (btn.get("offsetHeight")/2)) - scrollY,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith pageX = clientX + scrollX,
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith pageY = clientY + scrollY;
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.clientX, 10), parseInt(clientX, 10), "clientX mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.clientY, 10), parseInt(clientY, 10), "clientY mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.pageX, 10), parseInt(pageX, 10), "pageX mismatch");
1ca6700c9f28a72e6f384f241739c23a0f6121c1Todd Kloots Y.Assert.areSame(parseInt(e.pageY, 10), parseInt(pageY, 10), "pageY mismatch");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith simulateKeyDown();
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.isTrue(received, "contextmenu event xy coords don't reference the center of the event target");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_multiple_on_listeners: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith simulateKeyDown();
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(2, called, "contextmenu event listener not called correct number of times");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_multiple_delegate_listeners: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.delegate("contextmenu", onContextMenu, "button");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.delegate("contextmenu", onContextMenu, "button");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith simulateKeyDown();
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(2, called, "contextmenu event listener not called correct number of times");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith test_multiple_on_and_delegate_listeners: function(){
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.on("contextmenu", onContextMenu);
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.delegate("contextmenu", onContextMenu, "button");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.delegate("contextmenu", onContextMenu, "button");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith simulateKeyDown();
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith Y.Assert.areSame(4, called, "contextmenu event listener not called correct number of times");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith btn.detach("contextmenu");
ce349fa7d1c0940483411c296f409c046cf7b711Luke Smith doc.detach("contextmenu");