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