1 /* 2 * Copyright (c) 2011, 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 test.javafx.scene.shape; 27 28 import com.sun.javafx.sg.prism.NGNode; 29 import com.sun.prism.paint.Color; 30 import javafx.scene.Node; 31 32 import java.lang.reflect.Method; 33 34 import static org.junit.Assert.*; 35 36 public abstract class TestUtils { 37 38 private TestUtils() { 39 } 40 41 public static void testBooleanPropertyGetterSetter(final Object bean, final String propertyName) throws Exception { 42 final StringBuilder propertyNameBuilder = new StringBuilder(propertyName); 43 propertyNameBuilder.setCharAt(0, Character.toUpperCase(propertyName.charAt(0))); 44 final String setterName = new StringBuilder("set").append(propertyNameBuilder).toString(); 45 final String getterName = new StringBuilder("is").append(propertyNameBuilder).toString(); 46 final Class<? extends Object> beanClass = bean.getClass(); 47 final Method setter = beanClass.getMethod(setterName, boolean.class); 48 final Method getter = beanClass.getMethod(getterName); 49 setter.invoke(bean, true); 50 assertTrue((Boolean) getter.invoke(bean)); 51 setter.invoke(bean, false); 52 assertFalse((Boolean) getter.invoke(bean)); 53 setter.invoke(bean, true); 54 assertTrue((Boolean) getter.invoke(bean)); 55 } 56 57 public static void testFloatPropertyGetterSetter(final Object bean, final String propertyName, final float initialValue, final float newValue) throws Exception { 58 final StringBuilder propertyNameBuilder = new StringBuilder(propertyName); 59 propertyNameBuilder.setCharAt(0, Character.toUpperCase(propertyName.charAt(0))); 60 final String setterName = new StringBuilder("set").append(propertyNameBuilder).toString(); 61 final String getterName = new StringBuilder("get").append(propertyNameBuilder).toString(); 62 final Class<? extends Object> beanClass = bean.getClass(); 63 final Method setter = beanClass.getMethod(setterName, float.class); 64 final Method getter = beanClass.getMethod(getterName); 65 setter.invoke(bean, initialValue); 66 assertEquals(initialValue, (Float) getter.invoke(bean), 1.0E-100); 67 setter.invoke(bean, newValue); 68 assertEquals(newValue, (Float) getter.invoke(bean), 1.0E-100); 69 } 70 71 public static void testDoublePropertyGetterSetter(final Object bean, final String propertyName, final double initialValue, final double newValue) throws Exception { 72 final StringBuilder propertyNameBuilder = new StringBuilder(propertyName); 73 propertyNameBuilder.setCharAt(0, Character.toUpperCase(propertyName.charAt(0))); 74 final String setterName = new StringBuilder("set").append(propertyNameBuilder).toString(); 75 final String getterName = new StringBuilder("get").append(propertyNameBuilder).toString(); 76 final Class<? extends Object> beanClass = bean.getClass(); 77 final Method setter = beanClass.getMethod(setterName, double.class); 78 final Method getter = beanClass.getMethod(getterName); 79 setter.invoke(bean, initialValue); 80 assertEquals(initialValue, (Double) getter.invoke(bean), 1.0E-100); 81 setter.invoke(bean, newValue); 82 assertEquals(newValue, (Double) getter.invoke(bean), 1.0E-100); 83 } 84 85 public static float getFloatValue(Node node, String pgPropertyName) 86 throws Exception { 87 return ((Float)getObjectValue(node, pgPropertyName, false)).floatValue(); 88 } 89 90 public static float getIntValue(Node node, String pgPropertyName) 91 throws Exception { 92 return ((Integer)getObjectValue(node, pgPropertyName, false)).intValue(); 93 } 94 95 public static boolean getBooleanValue(Node node, String pgPropertyName) 96 throws Exception { 97 return ((Boolean)getObjectValue(node, pgPropertyName, true)).booleanValue(); 98 } 99 100 public static String getStringValue(Node node, String pgPropertyName) 101 throws Exception { 102 return ((String)getObjectValue(node, pgPropertyName)); 103 } 104 105 public static Object getObjectValue(Node node, String pgPropertyName, boolean isBool) 106 throws Exception { 107 final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName); 108 pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0))); 109 final String pgGetterName = new StringBuilder(isBool ? "is" : "get").append(pgPropertyNameBuilder).toString(); 110 111 final NGNode peer = node.impl_getPeer(); 112 final Class<? extends NGNode> impl_class = peer.getClass(); 113 final Method impl_getter = impl_class.getMethod(pgGetterName); 114 115 Object result = impl_getter.invoke(peer); 116 // This is a hack workaround because we supply a javafx Color to a call, and pull a prism Color 117 // from the NG node, and need to compare the two. So have to convert back to javafx Color. 118 if (result instanceof Color) { 119 Color prismColor = (Color)result; 120 result = new javafx.scene.paint.Color(prismColor.getRed(), prismColor.getGreen(), prismColor.getBlue(), prismColor.getAlpha()); 121 } 122 return result; 123 } 124 125 public static Object getObjectValue(Node node, String pgPropertyName) throws Exception { 126 return getObjectValue(node, pgPropertyName, false); 127 } 128 }