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