1 /*
   2  * Copyright (c) 2011, 2015, 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.com.sun.javafx.util;
  27 
  28 import com.sun.javafx.util.Utils;
  29 import static org.junit.Assert.assertEquals;
  30 
  31 import java.util.Arrays;
  32 
  33 import javafx.geometry.HPos;
  34 import javafx.geometry.Point2D;
  35 import javafx.geometry.VPos;
  36 import javafx.scene.Group;
  37 import javafx.scene.Scene;
  38 import javafx.scene.SubScene;
  39 import javafx.scene.layout.VBox;
  40 import javafx.scene.shape.Rectangle;
  41 import javafx.stage.Stage;
  42 import org.junit.Test;
  43 
  44 public class UtilsTest {
  45     @Test
  46     public void testSplit() {
  47         // normal use case
  48         String s = "VK_ENTER";
  49         String[] split = Utils.split(s, "_");
  50         assertEquals("Array content: " + Arrays.toString(split),2, split.length);
  51         assertEquals("VK", split[0]);
  52         assertEquals("ENTER", split[1]);
  53 
  54         // normal use case
  55         s = "VK_LEFT_ARROW";
  56         split = Utils.split(s, "_");
  57         assertEquals("Array content: " + Arrays.toString(split),3, split.length);
  58         assertEquals("VK", split[0]);
  59         assertEquals("LEFT", split[1]);
  60         assertEquals("ARROW", split[2]);
  61 
  62         // split with same string as separator - expect empty array
  63         s = "VK_LEFT_ARROW";
  64         split = Utils.split(s, "VK_LEFT_ARROW");
  65         assertEquals("Array content: " + Arrays.toString(split),0, split.length);
  66 
  67         // split with longer string as separator - expect empty array
  68         s = "VK_LEFT_ARROW";
  69         split = Utils.split(s, "VK_LEFT_ARROW_EXT");
  70         assertEquals("Array content: " + Arrays.toString(split),0, split.length);
  71     }
  72     
  73     @Test
  74     public void testConvertUnicode() {
  75         String s = "";
  76         String r = Utils.convertUnicode(s);
  77         assertEquals("", r);
  78         
  79         /*String*/ s = "test";
  80         /*String*/ r = Utils.convertUnicode(s);
  81         assertEquals("test", r);
  82 
  83         /*String*/ s = "hi\\u1234";
  84         /*String*/ r = Utils.convertUnicode(s);
  85         assertEquals("hi\u1234", r);
  86 
  87         /*String*/ s = "\\u5678";
  88         /*String*/ r = Utils.convertUnicode(s);
  89         assertEquals("\u5678", r);
  90         
  91         /*String*/ s = "hi\\u1234there\\u432112";
  92         /*String*/ r = Utils.convertUnicode(s);
  93         assertEquals("hi\u1234there\u432112", r);
  94         
  95         /*String*/ s = "Hello\u5678There";
  96         /*String*/ r = Utils.convertUnicode(s);
  97         assertEquals("Hello\u5678There", r);
  98         
  99         /*String*/ s = "\\this\\is\\a\\windows\\path";
 100         /*String*/ r = Utils.convertUnicode(s);
 101         assertEquals("\\this\\is\\a\\windows\\path", r);
 102         
 103         /*String*/ s = "\\this\\is\\a\\12\\windows\\path";
 104         /*String*/ r = Utils.convertUnicode(s);
 105         assertEquals("\\this\\is\\a\\12\\windows\\path", r);
 106 
 107         /*String*/ s = "u12u12";
 108         /*String*/ r = Utils.convertUnicode(s);
 109         assertEquals("u12u12", r);
 110         
 111         /*String*/ s = "hello\nu1234\n";
 112         /*String*/ r = Utils.convertUnicode(s);
 113         assertEquals("hello\nu1234\n", r);
 114     }
 115 
 116     @Test
 117     public void testConvertUnicodeFail2_2() {
 118 
 119         //Error case - null
 120         //String s = null;
 121         //String r = Utils.convertUnicode(s);
 122         //assertEquals("", r);
 123         
 124         //String s = "\\";
 125         //String r = Utils.convertUnicode(s);
 126         //assertEquals("\\", r);
 127         
 128         //Error case - no length
 129         ///*String*/ s = "hi\\u";
 130         ///*String*/ r = Utils.convertUnicode(s);
 131         //assertEquals("hi\\u", r);
 132     }
 133 
 134     @Test
 135     public void testConvertUnicodeWrong2_2() {
 136         
 137         //Error case - short length
 138         String s = "hi\\u12";
 139         String r = Utils.convertUnicode(s);
 140         //assertEquals("hi\\u12", r);
 141         
 142         /*String*/ s = "\\this\\is\\a\\umm\\windows\\path";
 143         /*String*/ r = Utils.convertUnicode(s);
 144         //assertEquals("\\this\\is\\a\\umm\\windows\\path", r);
 145     }
 146 
 147     @Test
 148     public void testPointRelativeTo() {
 149         VBox root = new VBox();
 150         final Rectangle rectangle = new Rectangle(50, 50, 100, 100);
 151         root.getChildren().add(rectangle);
 152         Scene scene = new Scene(root,800,600);
 153         Stage stage = new Stage();
 154         stage.setX(0);
 155         stage.setY(0);
 156         stage.setScene(scene);
 157         final Point2D res = Utils.pointRelativeTo(rectangle, 0, 0, HPos.CENTER, VPos.CENTER, 0, 0, false);
 158         assertEquals(50, res.getX(), 1e-1);
 159         assertEquals(50, res.getY(), 1e-1);
 160 
 161     }
 162 
 163     @Test
 164     public void testPointRelativeTo_InSubScene() {
 165         Group root = new Group();
 166         Scene scene = new Scene(root,800,600);
 167         VBox subRoot = new VBox();
 168         SubScene subScene = new SubScene(subRoot, 100, 100);
 169         subScene.setLayoutX(20);
 170         subScene.setLayoutY(20);
 171         root.getChildren().addAll(subScene);
 172         final Rectangle rectangle = new Rectangle(50, 50, 100, 100);
 173         subRoot.getChildren().add(rectangle);
 174         Stage stage = new Stage();
 175         stage.setX(0);
 176         stage.setY(0);
 177         stage.setScene(scene);
 178         final Point2D res = Utils.pointRelativeTo(rectangle, 0, 0, HPos.CENTER, VPos.CENTER, 0, 0, false);
 179         assertEquals(70, res.getX(), 1e-1);
 180         assertEquals(70, res.getY(), 1e-1);
 181 
 182     }
 183 }