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 test.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.Node;
  37 import javafx.scene.input.ContextMenuEvent;
  38 import javafx.scene.input.DragEvent;
  39 import javafx.scene.input.GestureEvent;
  40 import javafx.scene.input.InputEvent;
  41 import javafx.scene.input.InputMethodEvent;
  42 import javafx.scene.input.InputMethodTextRun;
  43 import javafx.scene.input.KeyCode;
  44 import javafx.scene.input.KeyEvent;
  45 import javafx.scene.input.MouseButton;
  46 import javafx.scene.input.MouseDragEvent;
  47 import javafx.scene.input.MouseEvent;
  48 import javafx.scene.input.RotateEvent;
  49 import javafx.scene.input.ScrollEvent;
  50 import javafx.scene.input.SwipeEvent;
  51 import javafx.scene.input.TouchEvent;
  52 import javafx.scene.input.TouchPoint;
  53 import javafx.scene.input.TransferMode;
  54 import javafx.scene.input.ZoomEvent;
  55 import javafx.scene.shape.Rectangle;
  56 import javafx.scene.transform.TransformChangedEvent;
  57 import javafx.stage.Stage;
  58 import javafx.stage.WindowEvent;
  59 import org.junit.runner.RunWith;
  60 import org.junit.runners.Parameterized;
  61 import org.junit.runners.Parameterized.Parameters;
  62 import static org.junit.Assert.assertTrue;
  63 import org.junit.Test;
  64 
  65 @RunWith(Parameterized.class)
  66 public class EventAnyTest {
  67     @Parameters
  68     public static Collection getParams() {
  69         return Arrays.asList(new Object[][] {
  70             { ActionEvent.ANY,           actionEvent(),           true},
  71             { ActionEvent.ANY,           focusUngrabEvent(),      false},
  72             { FocusUngrabEvent.ANY,      focusUngrabEvent(),      true},
  73             { FocusUngrabEvent.ANY,      actionEvent(),           false},
  74             { ContextMenuEvent.ANY,      contextMenuEvent(),      true},
  75             { ContextMenuEvent.ANY,      actionEvent(),           false},
  76             { DragEvent.ANY,             dragEvent(),             true },
  77             { DragEvent.ANY,             keyEvent(),              false },
  78             { InputMethodEvent.ANY,      inputMethodEvent(),      true },
  79             { InputMethodEvent.ANY,      keyEvent(),              false },
  80             { KeyEvent.ANY,              keyEvent(),              true },
  81             { KeyEvent.ANY,              inputMethodEvent(),      false },
  82             { MouseDragEvent.ANY,        mouseDragEvent(),        true },
  83             { MouseDragEvent.ANY,        mouseEvent(),            false },
  84             { MouseEvent.ANY,            mouseEvent(),            true },
  85             { MouseEvent.ANY,            mouseDragEvent(),        true },
  86             { MouseEvent.ANY,            keyEvent(),              false },
  87             { RotateEvent.ANY,           rotateEvent(),           true },
  88             { RotateEvent.ANY,           zoomEvent(),             false },
  89             { ZoomEvent.ANY,             zoomEvent(),             true },
  90             { ZoomEvent.ANY,             rotateEvent(),           false },
  91             { ScrollEvent.ANY,           scrollEvent(),           true },
  92             { ScrollEvent.ANY,           swipeEvent(),            false },
  93             { SwipeEvent.ANY,            swipeEvent(),            true },
  94             { SwipeEvent.ANY,            scrollEvent(),           false },
  95             { TouchEvent.ANY,            touchEvent(),            true },
  96             { TouchEvent.ANY,            rotateEvent(),           false },
  97             { TransformChangedEvent.ANY, transformChangedEvent(), true },
  98             { TransformChangedEvent.ANY, mouseEvent(),            false },
  99             { WindowEvent.ANY,           windowEvent(),           true },
 100             { WindowEvent.ANY,           actionEvent(),           false },
 101             { GestureEvent.ANY,          rotateEvent(),           true },
 102             { GestureEvent.ANY,          mouseEvent(),            false },
 103             { InputEvent.ANY,            mouseEvent(),            true },
 104             { InputEvent.ANY,            actionEvent(),           false },
 105         });
 106     }
 107 
 108     private boolean delivered;
 109     private EventType type;
 110     private Event event;
 111     private boolean matches;
 112 
 113     public EventAnyTest(EventType type, Event event, boolean matches) {
 114         this.type = type;
 115         this.event = event;
 116         this.matches = matches;
 117     }
 118 
 119     @Test
 120     public void testEventDelivery() {
 121         Node n = new Rectangle();
 122         delivered = false;
 123 
 124         n.addEventHandler(type, event1 -> {
 125             delivered = true;
 126         });
 127 
 128         Event.fireEvent(n, event);
 129         assertTrue(matches == delivered);
 130     }
 131 
 132     private static Event actionEvent() {
 133         return new ActionEvent();
 134     }
 135 
 136     private static Event focusUngrabEvent() {
 137         return new FocusUngrabEvent();
 138     }
 139 
 140     private static Event contextMenuEvent() {
 141         return new ContextMenuEvent(
 142                 ContextMenuEvent.CONTEXT_MENU_REQUESTED, 10, 10, 10, 10, true,
 143                 null);
 144     }
 145 
 146     private static Event dragEvent() {
 147         return new DragEvent(DragEvent.DRAG_DROPPED, null,
 148                 1, 1, 1, 1, TransferMode.MOVE, null, null, null);
 149     }
 150 
 151     private static Event keyEvent() {
 152         return new KeyEvent(KeyEvent.KEY_PRESSED, null, null,
 153                 KeyCode.TAB, true, true, true, true);
 154     }
 155 
 156     private static Event inputMethodEvent() {
 157         return new InputMethodEvent(
 158                 InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
 159                 new ArrayList<InputMethodTextRun>(), null, 1);
 160     }
 161 
 162     private static Event mouseDragEvent() {
 163         return new MouseDragEvent(MouseDragEvent.MOUSE_DRAG_OVER,
 164                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 165                 true, true, true, true, null, null);
 166     }
 167 
 168     private static Event mouseEvent() {
 169         return new MouseEvent(MouseEvent.MOUSE_CLICKED,
 170                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 171                 true, true, true, true, true, null);
 172     }
 173 
 174     private static Event rotateEvent() {
 175         return new RotateEvent(RotateEvent.ROTATION_FINISHED,
 176                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 177     }
 178 
 179     private static Event zoomEvent() {
 180         return new ZoomEvent(ZoomEvent.ZOOM_STARTED,
 181                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 182     }
 183 
 184     private static Event scrollEvent() {
 185         return new ScrollEvent(ScrollEvent.SCROLL_STARTED,
 186                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, 1, 1,
 187                 ScrollEvent.HorizontalTextScrollUnits.NONE, 1,
 188                 ScrollEvent.VerticalTextScrollUnits.NONE, 1, 1, null);
 189     }
 190 
 191     private static Event swipeEvent() {
 192         return new SwipeEvent(SwipeEvent.SWIPE_DOWN, 1, 1, 1, 1,
 193                 true, true, true, true, true, 1, null);
 194     }
 195 
 196     private static Event transformChangedEvent() {
 197         return new TransformChangedEvent();
 198     }
 199 
 200     private static Event windowEvent() {
 201         return new WindowEvent(new Stage(), WindowEvent.WINDOW_SHOWN);
 202     }
 203 
 204     private static Event touchEvent() {
 205         TouchPoint tp = new TouchPoint(
 206                 1, TouchPoint.State.MOVED, 1, 1, 1, 1, null, null);
 207 
 208         return new TouchEvent(TouchEvent.TOUCH_MOVED, tp,
 209                 new ArrayList<>(Arrays.asList(new TouchPoint[] { tp })),
 210                 1, true, true, true, true);
 211     }
 212 }