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.binding;
  27 
  28 import static javafx.binding.DependencyUtils.checkDependencies;
  29 import javafx.beans.binding.Binding;
  30 import javafx.beans.binding.Bindings;
  31 import javafx.beans.property.BooleanProperty;
  32 import javafx.beans.property.SimpleBooleanProperty;
  33 import javafx.beans.value.ObservableValue;
  34 import javafx.beans.value.WritableValue;
  35 
  36 import org.junit.Before;
  37 import org.junit.Test;
  38 
  39 public abstract class WhenTestBase<T, P extends WritableValue<T> & ObservableValue<T>> {
  40 
  41     static final float EPSILON_FLOAT = 1e-6f;
  42     static final double EPSILON_DOUBLE = 1e-10;
  43 
  44     public abstract Binding<T>[] generatePropertyPropertyList(P p0, P[] properties);
  45 
  46     public abstract Binding<T> generatePropertyProperty(P p0, P p1);
  47 
  48     public abstract Binding<T>[] generatePropertyPrimitive(P op0, T op1);
  49 
  50     public abstract Binding<T>[] generatePrimitiveProperty(T op0, P op1);
  51 
  52     public abstract Binding<T>[] generatePrimitivePrimitive(T op0, T op1);
  53 
  54     public abstract void check(T expected, Binding<T> binding);
  55 
  56     final BooleanProperty cond = new SimpleBooleanProperty();
  57     final P p0;
  58     final P[] properties;
  59     private final T v0;
  60     private final T v1;
  61     private final T v2;
  62     private final T v3;
  63 
  64     public WhenTestBase(T v0, T v1, T v2, T v3, P p0, P... properties) {
  65         this.v0 = v0;
  66         this.v1 = v1;
  67         this.v2 = v2;
  68         this.v3 = v3;
  69         this.p0 = p0;
  70         this.properties = properties;
  71     }
  72 
  73     @Before
  74     public void setUp() {
  75         cond.set(false);
  76         p0.setValue(v0);
  77         for (final P p : properties) {
  78                 p.setValue(v1);
  79         }
  80     }
  81 
  82     @Test
  83     public void test_expression_expression() {
  84         final Binding<T>[] bindings = generatePropertyPropertyList(p0, properties);
  85         final int n = bindings.length;
  86         for (int i=0; i<n; i++) {
  87                 final Binding<T> binding = bindings[i];
  88                 final P p1 = properties[i];
  89             // check initial state
  90                 checkDependencies(binding.getDependencies(), cond, p0, p1);
  91                 check(p1.getValue(), binding);
  92         
  93                 // set first value
  94                 p0.setValue(v2);
  95                 check(p1.getValue(), binding);
  96         
  97                 // set second value
  98                 p1.setValue(v3);
  99                 check(p1.getValue(), binding);
 100         
 101                 // change condition
 102                 cond.set(true);
 103                 check(v2, binding);
 104         
 105                 // set first value
 106                 p0.setValue(v0);
 107                 check(v0, binding);
 108         
 109                 // set second value
 110                 p1.setValue(v1);
 111                 check(v0, binding);
 112         
 113                 // change condition
 114                 cond.set(false);
 115                 check(p1.getValue(), binding);
 116         }
 117     }
 118 
 119     @Test
 120     public void test_expression_primitive() {
 121         final Binding<T>[] bindings = generatePropertyPrimitive(p0, v1);
 122         for (final Binding<T> binding : bindings) {
 123                 // check initial state
 124                 checkDependencies(binding.getDependencies(), cond, p0);
 125                 check(v1, binding);
 126         
 127                 // set first value
 128                 p0.setValue(v2);
 129                 check(v1, binding);
 130         
 131                 // change condition
 132                 cond.set(true);
 133                 check(v2, binding);
 134         
 135                 // set first value
 136                 p0.setValue(v0);
 137                 check(v0, binding);
 138         
 139                 // change condition
 140                 cond.set(false);
 141                 check(v1, binding);
 142         }
 143     }
 144 
 145     @Test
 146     public void test_primitive_expression() {
 147         final Binding<T>[] bindings = generatePrimitiveProperty(v1, p0);
 148         for (final Binding<T> binding : bindings) {
 149                 // check initial state
 150                 checkDependencies(binding.getDependencies(), cond, p0);
 151                 check(v0, binding);
 152         
 153                 // set second value
 154                 p0.setValue(v3);
 155                 check(v3, binding);
 156         
 157                 // change condition
 158                 cond.set(true);
 159                 check(v1, binding);
 160         
 161                 // set second value
 162                 p0.setValue(v0);
 163                 check(v1, binding);
 164         
 165                 // change condition
 166                 cond.set(false);
 167                 check(v0, binding);
 168         }
 169     }
 170 
 171     @Test
 172     public void test_primitive_primitive() {
 173         final Binding<T>[] bindings = generatePrimitivePrimitive(v0, v1);
 174         for (final Binding<T> binding : bindings) {
 175                 // check initial state
 176                 checkDependencies(binding.getDependencies(), cond);
 177                 check(v1, binding);
 178         
 179                 // change condition
 180                 cond.set(true);
 181                 check(v0, binding);
 182         
 183                 // change condition
 184                 cond.set(false);
 185                 check(v1, binding);
 186         }
 187     }
 188 
 189     @Test(expected=NullPointerException.class)
 190     public void test_Null() {
 191         Bindings.when(null);
 192     }
 193 
 194     @Test(expected=NullPointerException.class)
 195     public void test_Null_expression() {
 196         generatePropertyProperty(null, p0);
 197     }
 198 
 199     @Test(expected=NullPointerException.class)
 200     public void test_expression_Null() {
 201         generatePropertyProperty(p0, null);
 202     }
 203 
 204     @Test(expected=NullPointerException.class)
 205     public void test_Null_primitive() {
 206         generatePropertyPrimitive(null, v1);
 207     }
 208 
 209     @Test(expected=NullPointerException.class)
 210     public void test_primitive_Null() {
 211         generatePrimitiveProperty(v0, null);
 212     }
 213 }