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 javafx.beans.property;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 import javafx.beans.InvalidationListener;
  32 import javafx.beans.binding.ObjectExpression;
  33 import javafx.beans.value.ChangeListener;
  34 import static org.junit.Assert.assertEquals;
  35 
  36 import org.junit.Before;
  37 import org.junit.Test;
  38 
  39 public class ReadOnlyFloatPropertyTest {
  40 
  41     private static final float DEFAULT = 0.0f;
  42     private static final float EPSILON = 1e-6f;
  43 
  44     @Before
  45     public void setUp() throws Exception {
  46     }
  47 
  48     @Test
  49     public void testToString() {
  50         final ReadOnlyFloatProperty v1 = new ReadOnlyFloatPropertyStub(null, "");
  51         assertEquals("ReadOnlyFloatProperty [value: " + DEFAULT + "]", v1.toString());
  52         
  53         final ReadOnlyFloatProperty v2 = new ReadOnlyFloatPropertyStub(null, null);
  54         assertEquals("ReadOnlyFloatProperty [value: " + DEFAULT + "]", v2.toString());
  55         
  56         final Object bean = new Object();
  57         final String name = "My name";
  58         final ReadOnlyFloatProperty v3 = new ReadOnlyFloatPropertyStub(bean, name);
  59         assertEquals("ReadOnlyFloatProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v3.toString());
  60         
  61         final ReadOnlyFloatProperty v4 = new ReadOnlyFloatPropertyStub(bean, "");
  62         assertEquals("ReadOnlyFloatProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
  63         
  64         final ReadOnlyFloatProperty v5 = new ReadOnlyFloatPropertyStub(bean, null);
  65         assertEquals("ReadOnlyFloatProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v5.toString());
  66         
  67         final ReadOnlyFloatProperty v6 = new ReadOnlyFloatPropertyStub(null, name);
  68         assertEquals("ReadOnlyFloatProperty [name: My name, value: " + DEFAULT + "]", v6.toString());
  69         
  70     }
  71     
  72     @Test
  73     public void testAsObject() {
  74         final ReadOnlyFloatWrapper valueModel = new ReadOnlyFloatWrapper();
  75         final ReadOnlyObjectProperty<Float> exp = valueModel.getReadOnlyProperty().asObject();
  76 
  77         assertEquals(0.0, exp.getValue(), EPSILON);
  78         valueModel.set(-4354.3f);
  79         assertEquals(-4354.3f, exp.getValue(), EPSILON);
  80         valueModel.set(5e11f);
  81         assertEquals(5e11f, exp.getValue(), EPSILON);
  82     }
  83     
  84     @Test
  85     public void testObjectToFloat() {
  86         final ReadOnlyObjectWrapper<Float> valueModel = new ReadOnlyObjectWrapper<Float>();
  87         final ReadOnlyFloatProperty exp = ReadOnlyFloatProperty.readOnlyFloatProperty(valueModel.getReadOnlyProperty());
  88         
  89 
  90         assertEquals(0.0, exp.floatValue(), EPSILON);
  91         valueModel.set(-4354.3f);
  92         assertEquals(-4354.3f, exp.floatValue(), EPSILON);
  93         valueModel.set(5e11f);
  94         assertEquals(5e11f, exp.floatValue(), EPSILON);
  95     }
  96     
  97     private static class ReadOnlyFloatPropertyStub extends ReadOnlyFloatProperty {
  98         
  99         private final Object bean;
 100         private final String name;
 101         
 102         private ReadOnlyFloatPropertyStub(Object bean, String name) {
 103             this.bean = bean;
 104             this.name = name;
 105         }
 106 
 107         @Override public Object getBean() { return bean; }
 108         @Override public String getName() { return name; }
 109         @Override public float get() { return 0.0f; }
 110 
 111         @Override
 112         public void addListener(ChangeListener<? super Number> listener) {
 113         }
 114 
 115         @Override
 116         public void removeListener(ChangeListener<? super Number> listener) {
 117         }
 118 
 119         @Override
 120         public void addListener(InvalidationListener listener) {
 121         }
 122 
 123         @Override
 124         public void removeListener(InvalidationListener listener) {
 125         }
 126         
 127     }
 128 
 129 }