1 /*
   2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene;
  27 
  28 import com.sun.javafx.stage.FocusUngrabEvent;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import java.util.Collection;
  32 import javafx.event.ActionEvent;
  33 import javafx.event.Event;
  34 import javafx.event.EventHandler;
  35 import javafx.event.EventType;
  36 import javafx.scene.input.ContextMenuEvent;
  37 import javafx.scene.input.DragEvent;
  38 import javafx.scene.input.GestureEvent;
  39 import javafx.scene.input.InputEvent;
  40 import javafx.scene.input.InputMethodEvent;
  41 import javafx.scene.input.InputMethodTextRun;
  42 import javafx.scene.input.KeyCode;
  43 import javafx.scene.input.KeyEvent;
  44 import javafx.scene.input.MouseButton;
  45 import javafx.scene.input.MouseDragEvent;
  46 import javafx.scene.input.MouseEvent;
  47 import javafx.scene.input.RotateEvent;
  48 import javafx.scene.input.ScrollEvent;
  49 import javafx.scene.input.SwipeEvent;
  50 import javafx.scene.input.TouchEvent;
  51 import javafx.scene.input.TouchPoint;
  52 import javafx.scene.input.TransferMode;
  53 import javafx.scene.input.ZoomEvent;
  54 import javafx.scene.shape.Rectangle;
  55 import javafx.scene.transform.TransformChangedEvent;
  56 import javafx.stage.Stage;
  57 import javafx.stage.WindowEvent;
  58 import org.junit.runner.RunWith;
  59 import org.junit.runners.Parameterized;
  60 import org.junit.runners.Parameterized.Parameters;
  61 import static org.junit.Assert.assertTrue;
  62 import org.junit.Test;
  63 
  64 @RunWith(Parameterized.class)
  65 public class EventAnyTest {
  66     @Parameters
  67     public static Collection getParams() {
  68         return Arrays.asList(new Object[][] {
  69             { ActionEvent.ANY,           actionEvent(),           true},
  70             { ActionEvent.ANY,           focusUngrabEvent(),      false},
  71             { FocusUngrabEvent.ANY,      focusUngrabEvent(),      true},
  72             { FocusUngrabEvent.ANY,      actionEvent(),           false},
  73             { ContextMenuEvent.ANY,      contextMenuEvent(),      true},
  74             { ContextMenuEvent.ANY,      actionEvent(),           false},
  75             { DragEvent.ANY,             dragEvent(),             true },
  76             { DragEvent.ANY,             keyEvent(),              false },
  77             { InputMethodEvent.ANY,      inputMethodEvent(),      true },
  78             { InputMethodEvent.ANY,      keyEvent(),              false },
  79             { KeyEvent.ANY,              keyEvent(),              true },
  80             { KeyEvent.ANY,              inputMethodEvent(),      false },
  81             { MouseDragEvent.ANY,        mouseDragEvent(),        true },
  82             { MouseDragEvent.ANY,        mouseEvent(),            false },
  83             { MouseEvent.ANY,            mouseEvent(),            true },
  84             { MouseEvent.ANY,            mouseDragEvent(),        true },
  85             { MouseEvent.ANY,            keyEvent(),              false },
  86             { RotateEvent.ANY,           rotateEvent(),           true },
  87             { RotateEvent.ANY,           zoomEvent(),             false },
  88             { ZoomEvent.ANY,             zoomEvent(),             true },
  89             { ZoomEvent.ANY,             rotateEvent(),           false },
  90             { ScrollEvent.ANY,           scrollEvent(),           true },
  91             { ScrollEvent.ANY,           swipeEvent(),            false },
  92             { SwipeEvent.ANY,            swipeEvent(),            true },
  93             { SwipeEvent.ANY,            scrollEvent(),           false },
  94             { TouchEvent.ANY,            touchEvent(),            true },
  95             { TouchEvent.ANY,            rotateEvent(),           false },
  96             { TransformChangedEvent.ANY, transformChangedEvent(), true },
  97             { TransformChangedEvent.ANY, mouseEvent(),            false },
  98             { WindowEvent.ANY,           windowEvent(),           true },
  99             { WindowEvent.ANY,           actionEvent(),           false },
 100             { GestureEvent.ANY,          rotateEvent(),           true },
 101             { GestureEvent.ANY,          mouseEvent(),            false },
 102             { InputEvent.ANY,            mouseEvent(),            true },
 103             { InputEvent.ANY,            actionEvent(),           false },
 104         });
 105     }
 106 
 107     private boolean delivered;
 108     private EventType type;
 109     private Event event;
 110     private boolean matches;
 111 
 112     public EventAnyTest(EventType type, Event event, boolean matches) {
 113         this.type = type;
 114         this.event = event;
 115         this.matches = matches;
 116     }
 117 
 118     @Test
 119     public void testEventDelivery() {
 120         Node n = new Rectangle();
 121         delivered = false;
 122 
 123         n.addEventHandler(type, event1 -> {
 124             delivered = true;
 125         });
 126 
 127         Event.fireEvent(n, event);
 128         assertTrue(matches == delivered);
 129     }
 130 
 131     private static Event actionEvent() {
 132         return new ActionEvent();
 133     }
 134 
 135     private static Event focusUngrabEvent() {
 136         return new FocusUngrabEvent();
 137     }
 138 
 139     private static Event contextMenuEvent() {
 140         return new ContextMenuEvent(
 141                 ContextMenuEvent.CONTEXT_MENU_REQUESTED, 10, 10, 10, 10, true,
 142                 null);
 143     }
 144 
 145     private static Event dragEvent() {
 146         return new DragEvent(DragEvent.DRAG_DROPPED, null,
 147                 1, 1, 1, 1, TransferMode.MOVE, null, null, null);
 148     }
 149 
 150     private static Event keyEvent() {
 151         return new KeyEvent(KeyEvent.KEY_PRESSED, null, null,
 152                 KeyCode.TAB, true, true, true, true);
 153     }
 154 
 155     private static Event inputMethodEvent() {
 156         return new InputMethodEvent(
 157                 InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
 158                 new ArrayList<InputMethodTextRun>(), null, 1);
 159     }
 160 
 161     private static Event mouseDragEvent() {
 162         return new MouseDragEvent(MouseDragEvent.MOUSE_DRAG_OVER,
 163                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 164                 true, true, true, true, null, null);
 165     }
 166 
 167     private static Event mouseEvent() {
 168         return new MouseEvent(MouseEvent.MOUSE_CLICKED,
 169                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 170                 true, true, true, true, true, null);
 171     }
 172 
 173     private static Event rotateEvent() {
 174         return new RotateEvent(RotateEvent.ROTATION_FINISHED,
 175                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 176     }
 177 
 178     private static Event zoomEvent() {
 179         return new ZoomEvent(ZoomEvent.ZOOM_STARTED,
 180                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 181     }
 182 
 183     private static Event scrollEvent() {
 184         return new ScrollEvent(ScrollEvent.SCROLL_STARTED,
 185                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, 1, 1,
 186                 ScrollEvent.HorizontalTextScrollUnits.NONE, 1,
 187                 ScrollEvent.VerticalTextScrollUnits.NONE, 1, 1, null);
 188     }
 189 
 190     private static Event swipeEvent() {
 191         return new SwipeEvent(SwipeEvent.SWIPE_DOWN, 1, 1, 1, 1,
 192                 true, true, true, true, true, 1, null);
 193     }
 194 
 195     private static Event transformChangedEvent() {
 196         return new TransformChangedEvent();
 197     }
 198 
 199     private static Event windowEvent() {
 200         return new WindowEvent(new Stage(), WindowEvent.WINDOW_SHOWN);
 201     }
 202 
 203     private static Event touchEvent() {
 204         TouchPoint tp = new TouchPoint(
 205                 1, TouchPoint.State.MOVED, 1, 1, 1, 1, null, null);
 206 
 207         return new TouchEvent(TouchEvent.TOUCH_MOVED, tp,
 208                 new ArrayList<>(Arrays.asList(new TouchPoint[] { tp })),
 209                 1, true, true, true, true);
 210     }
 211 }