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