1 /*
   2  * Copyright (c) 2013, 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, new EventHandler() {
 124             @Override public void handle(Event event) {
 125                 delivered = true;
 126             }
 127         });
 128 
 129         Event.fireEvent(n, event);
 130         assertTrue(matches == delivered);
 131     }
 132 
 133     private static Event actionEvent() {
 134         return new ActionEvent();
 135     }
 136 
 137     private static Event focusUngrabEvent() {
 138         return new FocusUngrabEvent();
 139     }
 140 
 141     private static Event contextMenuEvent() {
 142         return new ContextMenuEvent(
 143                 ContextMenuEvent.CONTEXT_MENU_REQUESTED, 10, 10, 10, 10, true,
 144                 null);
 145     }
 146 
 147     private static Event dragEvent() {
 148         return new DragEvent(DragEvent.DRAG_DROPPED, null,
 149                 1, 1, 1, 1, TransferMode.MOVE, null, null, null);
 150     }
 151 
 152     private static Event keyEvent() {
 153         return new KeyEvent(KeyEvent.KEY_PRESSED, null, null,
 154                 KeyCode.TAB, true, true, true, true);
 155     }
 156 
 157     private static Event inputMethodEvent() {
 158         return new InputMethodEvent(
 159                 InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
 160                 new ArrayList<InputMethodTextRun>(), null, 1);
 161     }
 162 
 163     private static Event mouseDragEvent() {
 164         return new MouseDragEvent(MouseDragEvent.MOUSE_DRAG_OVER,
 165                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 166                 true, true, true, true, null, null);
 167     }
 168 
 169     private static Event mouseEvent() {
 170         return new MouseEvent(MouseEvent.MOUSE_CLICKED,
 171                 1, 1, 1, 1, MouseButton.NONE, 1, true, true, true, true, true,
 172                 true, true, true, true, true, null);
 173     }
 174 
 175     private static Event rotateEvent() {
 176         return new RotateEvent(RotateEvent.ROTATION_FINISHED,
 177                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 178     }
 179 
 180     private static Event zoomEvent() {
 181         return new ZoomEvent(ZoomEvent.ZOOM_STARTED,
 182                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, null);
 183     }
 184 
 185     private static Event scrollEvent() {
 186         return new ScrollEvent(ScrollEvent.SCROLL_STARTED,
 187                 1, 1, 1, 1, true, true, true, true, true, true, 1, 1, 1, 1,
 188                 ScrollEvent.HorizontalTextScrollUnits.NONE, 1,
 189                 ScrollEvent.VerticalTextScrollUnits.NONE, 1, 1, null);
 190     }
 191 
 192     private static Event swipeEvent() {
 193         return new SwipeEvent(SwipeEvent.SWIPE_DOWN, 1, 1, 1, 1,
 194                 true, true, true, true, true, 1, null);
 195     }
 196 
 197     private static Event transformChangedEvent() {
 198         return new TransformChangedEvent();
 199     }
 200 
 201     private static Event windowEvent() {
 202         return new WindowEvent(new Stage(), WindowEvent.WINDOW_SHOWN);
 203     }
 204 
 205     private static Event touchEvent() {
 206         TouchPoint tp = new TouchPoint(
 207                 1, TouchPoint.State.MOVED, 1, 1, 1, 1, null, null);
 208 
 209         return new TouchEvent(TouchEvent.TOUCH_MOVED, tp,
 210                 new ArrayList<>(Arrays.asList(new TouchPoint[] { tp })),
 211                 1, true, true, true, true);
 212     }
 213 }