1 /* 2 * Copyright (c) 2011, 2018, 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.beans.property; 27 28 import javafx.beans.InvalidationListener; 29 import javafx.beans.property.FloatProperty; 30 import javafx.beans.property.ObjectProperty; 31 import javafx.beans.property.SimpleFloatProperty; 32 import javafx.beans.property.SimpleObjectProperty; 33 import javafx.beans.value.ChangeListener; 34 import javafx.beans.value.ObservableValue; 35 import test.com.sun.javafx.binding.ErrorLoggingUtiltity; 36 import org.junit.BeforeClass; 37 import org.junit.Test; 38 39 import static org.junit.Assert.*; 40 41 public class FloatPropertyTest { 42 43 private static final Object NO_BEAN = null; 44 private static final String NO_NAME_1 = null; 45 private static final String NO_NAME_2 = ""; 46 private static final float VALUE_1 = (float)Math.PI; 47 private static final float VALUE_2 = (float)-Math.E; 48 private static final float DEFAULT = 0.0f; 49 private static final float EPSILON = 1e-6f; 50 51 @BeforeClass 52 public static void setUpClass() { 53 ErrorLoggingUtiltity.reset(); 54 } 55 56 @Test 57 public void testSetValue_Null() { 58 final FloatProperty p = new SimpleFloatProperty(VALUE_1); 59 p.setValue(null); 60 assertEquals(DEFAULT, p.get(), EPSILON); 61 ErrorLoggingUtiltity.checkFine(NullPointerException.class); 62 } 63 64 @Test 65 public void testBindBidirectional() { 66 final FloatProperty p1 = new SimpleFloatProperty(VALUE_2); 67 final FloatProperty p2 = new SimpleFloatProperty(VALUE_1); 68 69 p1.bindBidirectional(p2); 70 assertEquals(VALUE_1, p1.get(), EPSILON); 71 assertEquals(VALUE_1, p2.get(), EPSILON); 72 73 p1.set(VALUE_2); 74 assertEquals(VALUE_2, p1.get(), EPSILON); 75 assertEquals(VALUE_2, p2.get(), EPSILON); 76 77 p2.set(VALUE_1); 78 assertEquals(VALUE_1, p1.get(), EPSILON); 79 assertEquals(VALUE_1, p2.get(), EPSILON); 80 81 p1.unbindBidirectional(p2); 82 p1.set(VALUE_2); 83 assertEquals(VALUE_2, p1.get(), EPSILON); 84 assertEquals(VALUE_1, p2.get(), EPSILON); 85 86 p1.set(VALUE_1); 87 p2.set(VALUE_2); 88 assertEquals(VALUE_1, p1.get(), EPSILON); 89 assertEquals(VALUE_2, p2.get(), EPSILON); 90 } 91 92 @Test 93 public void testToString() { 94 final FloatProperty v0 = new FloatPropertyStub(NO_BEAN, NO_NAME_1); 95 assertEquals("FloatProperty [value: " + DEFAULT + "]", v0.toString()); 96 97 final FloatProperty v1 = new FloatPropertyStub(NO_BEAN, NO_NAME_2); 98 assertEquals("FloatProperty [value: " + DEFAULT + "]", v1.toString()); 99 100 final Object bean = new Object(); 101 final String name = "My name"; 102 final FloatProperty v2 = new FloatPropertyStub(bean, name); 103 assertEquals("FloatProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v2.toString()); 104 v2.set(VALUE_1); 105 assertEquals("FloatProperty [bean: " + bean.toString() + ", name: My name, value: " + VALUE_1 + "]", v2.toString()); 106 107 final FloatProperty v3 = new FloatPropertyStub(bean, NO_NAME_1); 108 assertEquals("FloatProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v3.toString()); 109 v3.set(VALUE_1); 110 assertEquals("FloatProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v3.toString()); 111 112 final FloatProperty v4 = new FloatPropertyStub(bean, NO_NAME_2); 113 assertEquals("FloatProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString()); 114 v4.set(VALUE_1); 115 assertEquals("FloatProperty [bean: " + bean.toString() + ", value: " + VALUE_1 + "]", v4.toString()); 116 117 final FloatProperty v5 = new FloatPropertyStub(NO_BEAN, name); 118 assertEquals("FloatProperty [name: My name, value: " + DEFAULT + "]", v5.toString()); 119 v5.set(VALUE_1); 120 assertEquals("FloatProperty [name: My name, value: " + VALUE_1 + "]", v5.toString()); 121 } 122 123 @Test 124 public void testAsObject() { 125 final FloatProperty valueModel = new SimpleFloatProperty(); 126 final ObjectProperty<Float> exp = valueModel.asObject(); 127 128 assertEquals(0.0f, exp.getValue(), EPSILON); 129 valueModel.set(-4354.3f); 130 assertEquals(-4354.3f, exp.getValue(), EPSILON); 131 valueModel.set(5e11f); 132 assertEquals(5e11f, exp.getValue(), EPSILON); 133 134 exp.set(1234.0f); 135 assertEquals(1234.0f, valueModel.floatValue(), EPSILON); 136 137 } 138 139 @Test 140 public void testObjectToFloat() { 141 final ObjectProperty<Float> valueModel = new SimpleObjectProperty<Float>(2f); 142 final FloatProperty exp = FloatProperty.floatProperty(valueModel); 143 144 assertEquals(2f, exp.floatValue(), EPSILON); 145 valueModel.set(-4354.3f); 146 assertEquals(-4354.3f, exp.floatValue(), EPSILON); 147 valueModel.set(5e11f); 148 assertEquals(5e11f, exp.floatValue(), EPSILON); 149 150 exp.set(1234.0f); 151 assertEquals(1234.0f, valueModel.getValue(), EPSILON); 152 } 153 154 private class FloatPropertyStub extends FloatProperty { 155 156 private final Object bean; 157 private final String name; 158 private float value; 159 160 private FloatPropertyStub(Object bean, String name) { 161 this.bean = bean; 162 this.name = name; 163 } 164 165 @Override 166 public Object getBean() { 167 return bean; 168 } 169 170 @Override 171 public String getName() { 172 return name; 173 } 174 175 @Override 176 public float get() { 177 return value; 178 } 179 180 @Override 181 public void set(float value) { 182 this.value = value; 183 } 184 185 @Override 186 public void bind(ObservableValue<? extends Number> observable) { 187 fail("Not in use"); 188 } 189 190 @Override 191 public void unbind() { 192 fail("Not in use"); 193 } 194 195 @Override 196 public boolean isBound() { 197 fail("Not in use"); 198 return false; 199 } 200 201 @Override 202 public void addListener(ChangeListener<? super Number> listener) { 203 fail("Not in use"); 204 } 205 206 @Override 207 public void removeListener(ChangeListener<? super Number> listener) { 208 fail("Not in use"); 209 } 210 211 @Override 212 public void addListener(InvalidationListener listener) { 213 fail("Not in use"); 214 } 215 216 @Override 217 public void removeListener(InvalidationListener listener) { 218 fail("Not in use"); 219 } 220 221 } 222 }