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