rev 9504 : need to fix test log and others

   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.robot.com.sun.glass.ui.monocle;
  27 
  28 import com.sun.glass.ui.monocle.TestLog;
  29 import test.robot.com.sun.glass.ui.monocle.TestApplication;
  30 import test.robot.com.sun.glass.ui.monocle.input.devices.TestTouchDevice;
  31 import test.robot.com.sun.glass.ui.monocle.input.devices.TestTouchDevices;
  32 import javafx.geometry.BoundingBox;
  33 import javafx.geometry.Bounds;
  34 import javafx.geometry.Point2D;
  35 import javafx.scene.Node;
  36 import javafx.scene.input.MouseEvent;
  37 import javafx.scene.shape.Rectangle;
  38 import org.junit.Before;
  39 import org.junit.Ignore;
  40 import org.junit.Test;
  41 import org.junit.runners.Parameterized;
  42 
  43 import java.util.Collection;
  44 import java.util.concurrent.atomic.AtomicReference;
  45 import test.com.sun.glass.ui.monocle.TestRunnable;
  46 
  47 public class TouchButtonTest extends ParameterizedTestBase {
  48 
  49     private Node button1;
  50     private Node button2;
  51     private Node button3;
  52 
  53     public TouchButtonTest(TestTouchDevice device) {
  54         super(device);
  55     }
  56 
  57     @Parameterized.Parameters
  58     public static Collection<Object[]> data() {
  59         return TestTouchDevices.getTouchDeviceParameters(1);
  60     }
  61 
  62     public Node createButton(String text, int x, int y, boolean setListeners) {
  63         final Node button = new Rectangle(100.0, 20.0);
  64         button.setId(text);
  65         button.setLayoutX(x);
  66         button.setLayoutY(y);
  67         button.setOnMousePressed((e) -> button.requestFocus());
  68         if (setListeners) {
  69             button.addEventHandler(MouseEvent.ANY, e ->
  70                 TestLog.log(e.getEventType().getName() +": " 
  71                                                      + (int) e.getScreenX()
  72                                                      + ", " + (int) e.getScreenY()));
  73             button.focusedProperty().addListener((observable, oldValue, newValue) ->
  74                     TestLog.log(button.getId() + " isFocused=" + newValue));
  75         }
  76 
  77         return button;
  78     }
  79 
  80     @Before
  81     public void createButtons() throws Exception {
  82         TestRunnable.invokeAndWait(() -> {
  83             int X = (int) width / 2;
  84             int Y = (int) height / 2;
  85 
  86             button1 = createButton("button1", X, Y - 100, true);
  87             button2 = createButton("button2", X, Y + 100, true);
  88             button3 = createButton("button3", 0, 0, false);
  89 
  90             TestApplication.getRootGroup().getChildren().clear();
  91             TestApplication.getRootGroup().getChildren().addAll(
  92                     button1, button2, button3);
  93             button3.requestFocus();
  94         });
  95 
  96         TestApplication.waitForLayout();
  97     }
  98 
  99     /**
 100      * Tests
 101      */
 102 
 103     @Test
 104     public void tapOnButton() throws Exception {
 105         Point2D clickAt = tapInsideButton(button1);
 106         waitForFocusGainOn("button1");
 107         waitForMouseEnteredAt(clickAt);
 108         waitForMouseClickAt(clickAt);
 109     }
 110 
 111     @Test
 112     public void tapOn2Buttons() throws Exception {
 113         Point2D clickAt = tapInsideButton(button1);
 114         waitForFocusGainOn("button1");
 115         waitForMouseEnteredAt(clickAt);
 116         waitForMouseClickAt(clickAt);
 117 
 118         clickAt = tapInsideButton(button2);
 119         waitForFocusLostOn("button1");
 120         waitForFocusGainOn("button2");
 121         waitForMouseEnteredAt(clickAt);
 122         waitForMouseClickAt(clickAt);
 123     }
 124 
 125     @Test
 126     public void tapOutAndInButton() throws Exception {
 127         tapOutSideButton();
 128         TestLog.reset();
 129         Point2D clickAt = tapInsideButton(button1);
 130         waitForMouseClickAt(clickAt);
 131         waitForFocusGainOn("button1");
 132     }
 133 
 134     @Test
 135     public void tapOutInAndOutButton() throws Exception {
 136         tapOutSideButton();
 137         TestLog.reset();
 138         Point2D clickAt = tapInsideButton(button1);
 139         waitForMouseClickAt(clickAt);
 140         waitForFocusGainOn("button1");
 141 
 142         tapOutSideButton();
 143         tapInsideButton(button3);
 144         waitForFocusLostOn("button1");
 145     }
 146 
 147     @Test
 148     public void tapInAndOutLoop() throws Exception {
 149         tapOutSideButton();
 150         TestLog.reset();
 151         for (int i = 0 ; i < 2 ; i++) {
 152             tapOutSideButton();
 153             tapInsideButton(button3);
 154             TestLog.reset();
 155             Point2D clickAt = tapInsideButton(button1);
 156             waitForFocusGainOn("button1");
 157             waitForMouseEnteredAt(clickAt);
 158             waitForMouseClickAt(clickAt);
 159 
 160             tapOutSideButton();
 161             tapInsideButton(button3);
 162             waitForFocusLostOn("button1");
 163             TestLog.reset();
 164 
 165             clickAt = tapInsideButton(button2);
 166             waitForFocusGainOn("button2");
 167             waitForMouseEnteredAt(clickAt);
 168 
 169             waitForMouseClickAt(clickAt);
 170             TestLog.reset();
 171             tapOutSideButton();
 172             tapInsideButton(button3);
 173             waitForFocusLostOn("button2");
 174         }
 175     }
 176 
 177     /**
 178      * RT-34625 - we should get a click when tapping on a control, dragging the 
 179      * finger and release the finger inside the control 
 180      */
 181     @Test
 182     public void tapAndDrag() throws Exception {
 183         Bounds buttonBounds = getButtonBounds(button2);
 184 
 185         //start at right most x and center y
 186         int x = (int) buttonBounds.getMaxX() - 1;
 187         int y = (int) (buttonBounds.getMinY() + buttonBounds.getMaxY()) / 2;
 188 
 189         ///tap
 190         int p = device.addPoint(x, y);
 191         device.sync();
 192 
 193         waitForFocusGainOn("button2");
 194 
 195         //drag inside button         
 196         for (; x > buttonBounds.getMinX(); x-- ) {
 197             device.setPoint(p, x, y);
 198             device.sync();
 199         }
 200 
 201         //release inside the button
 202         device.removePoint(p);
 203         device.sync();
 204         TestLog.waitForLogContaining("MOUSE_CLICKED:", 3000l);
 205         TestLog.waitForLogContaining("MOUSE_RELEASED:", 3000l);
 206     }
 207 
 208     /**
 209      * RT-34625 - Currently a control will not generate a click when tapping on 
 210      * it, drag the finger outside the control and release the finger. 
 211      * This might be a desired behavior, but sometime there are small 
 212      * unintentional drags that resulting in a finger release outside the
 213      * control.
 214      *  
 215      * This test should fail and throw RuntimeException
 216      * 
 217      */
 218     @Ignore("RT-34625")
 219     @Test
 220     public void tapAndDrag_fail() throws Exception {
 221         Bounds buttonBounds = getButtonBounds(button2);
 222 
 223         //start at right most x and center y
 224         int x = (int) buttonBounds.getMaxX() - 1;
 225         int y = (int) (buttonBounds.getMinY() + buttonBounds.getMaxY()) / 2;
 226 
 227         ///tap
 228         int p = device.addPoint(x, y);
 229         device.sync();
 230 
 231         waitForFocusGainOn("button2");
 232 
 233         //drag outside button         
 234         for (; x > buttonBounds.getMinX() - device.getTapRadius() - 10; x-- ) {
 235             device.setPoint(p, x, y);
 236             device.sync();
 237         }
 238 
 239         //release outside the button
 240         device.removePoint(p);
 241         device.sync();
 242         TestLog.waitForLogContaining("MOUSE_CLICKED:", 3000l);
 243     }
 244 
 245     @Test
 246     public void tapping_oneButtonOnScreen () throws Exception {
 247         AtomicReference<Node> buttonRef = new AtomicReference<>();
 248         TestRunnable.invokeAndWait(() -> {
 249             Node button4 = createButton("button4", 0, 0, true);
 250             buttonRef.set(button4);
 251             TestApplication.getRootGroup().getChildren().clear();
 252             TestApplication.getRootGroup().getChildren().addAll(button4);
 253         });
 254         TestApplication.waitForLayout();
 255 
 256         for (int i = 0; i < 5; i++) {
 257             Point2D clickAt = tapInsideButton(buttonRef.get());
 258             waitForMouseClickAt(clickAt);
 259             TestLog.reset();
 260         }
 261     }
 262     
 263     /** utilities */
 264     public Bounds getButtonBounds(Node button) throws Exception {
 265         AtomicReference<Bounds> ref = new AtomicReference<>();
 266         TestRunnable.invokeAndWait(() -> {
 267             ref.set(button.localToScreen(
 268                     new BoundingBox(0, 0,
 269                                     button.getBoundsInParent().getWidth(),
 270                                     button.getBoundsInParent().getHeight())));
 271             TestLog.log("Bounds for " + button.getId() + " are " + ref.get());
 272         });
 273         return ref.get();
 274     }
 275 
 276     public Point2D getCenterOfButton(Node button) throws Exception {
 277         Bounds buttonBounds = getButtonBounds(button);
 278         Point2D clickAt = new Point2D(
 279                 buttonBounds.getMinX()+ buttonBounds.getWidth() / 2,
 280                 buttonBounds.getMinY()+ buttonBounds.getHeight() / 2);
 281         return clickAt;
 282     }
 283 
 284     public Point2D tapInsideButton(Node button) throws Exception {
 285         Point2D clickAt = getCenterOfButton(button);
 286         //tap
 287         int p = device.addPoint(clickAt.getX(), clickAt.getY());
 288         device.sync();
 289         //release
 290         device.removePoint(p);
 291         device.sync();
 292         TestLog.waitForLog("Mouse clicked: %.0f, %.0f", clickAt.getX(), clickAt.getY());
 293         return clickAt;
 294     }
 295 
 296     public void tapOutSideButton() throws Exception {
 297         Bounds buttonBounds = getButtonBounds(button3);
 298         ///tap
 299         double x = buttonBounds.getMaxX() + device.getTapRadius() + 10;
 300         double y = buttonBounds.getMaxY() + device.getTapRadius() + 10;
 301         int p = device.addPoint(x, y);
 302         device.sync();
 303         //release
 304         device.removePoint(p);
 305         device.sync();
 306         TestLog.waitForLog("Mouse clicked: %.0f, %.0f", x, y);
 307     }
 308 
 309     public void waitForMouseClickAt(Point2D clickAt) throws Exception{
 310         TestLog.waitForLog("MOUSE_CLICKED: %d, %d",
 311                            Math.round(clickAt.getX()),
 312                            Math.round(clickAt.getY()));
 313     }
 314 
 315     public void waitForMouseEnteredAt(Point2D clickAt) throws Exception{
 316         TestLog.waitForLog("MOUSE_ENTERED: %d, %d",
 317                            Math.round(clickAt.getX()),
 318                            Math.round(clickAt.getY()));
 319     }
 320 
 321     public void waitForFocus(String id, boolean focusState) throws Exception {
 322         TestLog.waitForLog("%s isFocused=%b", id, focusState);
 323     }
 324 
 325     public void waitForFocusGainOn(String id) throws Exception{
 326         waitForFocus(id, true);
 327     }
 328 
 329     public void waitForFocusLostOn(String id) throws Exception{
 330         waitForFocus(id, false);
 331     }
 332 
 333 }
--- EOF ---