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.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.BooleanBinding;
  33 import javafx.beans.binding.BooleanExpression;
  34 import javafx.beans.binding.ObjectExpression;
  35 import javafx.beans.property.ReadOnlyBooleanProperty;
  36 import javafx.beans.property.ReadOnlyBooleanWrapper;
  37 import javafx.beans.property.ReadOnlyObjectProperty;
  38 import javafx.beans.property.ReadOnlyObjectWrapper;
  39 import javafx.beans.value.ChangeListener;
  40 import javafx.beans.value.ObservableValue;
  41 import javafx.beans.value.ObservableValueStub;
  42 import javafx.collections.FXCollections;
  43 
  44 import org.junit.Before;
  45 import org.junit.Test;
  46 
  47 public class ReadOnlyBooleanPropertyTest {
  48     
  49     private static final boolean DEFAULT = false;
  50 
  51         @Before
  52         public void setUp() throws Exception {
  53         }
  54 
  55         @Test
  56         public void testToString() {
  57         final ReadOnlyBooleanProperty v1 = new ReadOnlyBooleanPropertyStub(null, "");
  58         assertEquals("ReadOnlyBooleanProperty [value: " + DEFAULT + "]", v1.toString());
  59         
  60         final ReadOnlyBooleanProperty v2 = new ReadOnlyBooleanPropertyStub(null, null);
  61         assertEquals("ReadOnlyBooleanProperty [value: " + DEFAULT + "]", v2.toString());
  62         
  63                 final Object bean = new Object();
  64                 final String name = "My name";
  65         final ReadOnlyBooleanProperty v3 = new ReadOnlyBooleanPropertyStub(bean, name);
  66         assertEquals("ReadOnlyBooleanProperty [bean: " + bean.toString() + ", name: My name, value: " + DEFAULT + "]", v3.toString());
  67         
  68         final ReadOnlyBooleanProperty v4 = new ReadOnlyBooleanPropertyStub(bean, "");
  69         assertEquals("ReadOnlyBooleanProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v4.toString());
  70         
  71         final ReadOnlyBooleanProperty v5 = new ReadOnlyBooleanPropertyStub(bean, null);
  72         assertEquals("ReadOnlyBooleanProperty [bean: " + bean.toString() + ", value: " + DEFAULT + "]", v5.toString());
  73         
  74         final ReadOnlyBooleanProperty v6 = new ReadOnlyBooleanPropertyStub(null, name);
  75         assertEquals("ReadOnlyBooleanProperty [name: My name, value: " + DEFAULT + "]", v6.toString());
  76         
  77         }
  78 
  79     @Test
  80     public void testAsObject() {
  81         final ReadOnlyBooleanWrapper valueModel = new ReadOnlyBooleanWrapper();
  82         final ReadOnlyObjectProperty<Boolean> exp = valueModel.getReadOnlyProperty().asObject();
  83 
  84         assertEquals(Boolean.FALSE, exp.get());
  85         valueModel.set(true);
  86         assertEquals(Boolean.TRUE, exp.get());
  87         valueModel.set(false);
  88         assertEquals(Boolean.FALSE, exp.get());
  89     }
  90     
  91     @Test
  92     public void testObjectToBoolean() {
  93         final ReadOnlyObjectWrapper<Boolean> valueModel = new ReadOnlyObjectWrapper<Boolean>();
  94         final ReadOnlyBooleanProperty exp = ReadOnlyBooleanProperty.readOnlyBooleanProperty(valueModel.getReadOnlyProperty());
  95         
  96 
  97         assertEquals(false, exp.get());
  98         valueModel.set(true);
  99         assertEquals(true, exp.get());
 100         valueModel.set(false);
 101         assertEquals(false, exp.get());
 102     }
 103 
 104 
 105         private static class ReadOnlyBooleanPropertyStub extends ReadOnlyBooleanProperty {
 106                 
 107                 private final Object bean;
 108                 private final String name;
 109                 
 110                 private ReadOnlyBooleanPropertyStub(Object bean, String name) {
 111                         this.bean = bean;
 112                         this.name = name;
 113                 }
 114 
 115                 @Override public Object getBean() { return bean; }
 116                 @Override public String getName() { return name; }
 117                 @Override public boolean get() { return false; }
 118 
 119         @Override
 120         public void addListener(ChangeListener<? super Boolean> listener) {
 121         }
 122 
 123         @Override
 124         public void removeListener(ChangeListener<? super Boolean> listener) {
 125         }
 126 
 127         @Override
 128         public void addListener(InvalidationListener listener) {
 129         }
 130 
 131         @Override
 132         public void removeListener(InvalidationListener listener) {
 133         }
 134                 
 135         }
 136 
 137 }