1 /*
   2  * Copyright (c) 2012, 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 javafx.beans.property.ReadOnlyBooleanProperty;
  29 import javafx.beans.property.ReadOnlyIntegerProperty;
  30 import javafx.beans.property.ReadOnlyListPropertyBase;
  31 import javafx.beans.property.ReadOnlyListPropertyBaseShim;
  32 import test.javafx.beans.InvalidationListenerMock;
  33 import test.javafx.beans.value.ChangeListenerMock;
  34 import javafx.collections.FXCollections;
  35 import javafx.collections.ObservableList;
  36 import org.junit.Before;
  37 import org.junit.Test;
  38 
  39 import static org.junit.Assert.fail;
  40 
  41 public class ReadOnlyListPropertyBaseTest {
  42     
  43     private static final Object UNDEFINED = null;
  44     private static final Object DEFAULT = null;
  45     private static final ObservableList<Object> VALUE_1 = FXCollections.observableArrayList();
  46     private static final ObservableList<Object> VALUE_2 = FXCollections.observableArrayList(new Object());
  47     
  48     private ReadOnlyPropertyMock property;
  49     private InvalidationListenerMock invalidationListener;
  50     private ChangeListenerMock<Object> changeListener;
  51 
  52     @Before
  53     public void setUp() throws Exception {
  54         property = new ReadOnlyPropertyMock();
  55         invalidationListener = new InvalidationListenerMock();
  56         changeListener = new ChangeListenerMock<Object>(UNDEFINED);
  57     }
  58     
  59     @Test
  60     public void testInvalidationListener() {
  61         property.addListener(invalidationListener);
  62         property.get();
  63         invalidationListener.reset();
  64         property.set(VALUE_1);
  65         invalidationListener.check(property, 1);
  66         property.removeListener(invalidationListener);
  67         invalidationListener.reset();
  68         property.set(VALUE_2);
  69         invalidationListener.check(null, 0);
  70     }
  71 
  72     @Test
  73     public void testChangeListener() {
  74         property.addListener(changeListener);
  75         property.get();
  76         changeListener.reset();
  77         property.set(VALUE_1);
  78         changeListener.check(property, DEFAULT, VALUE_1, 1);
  79         property.removeListener(changeListener);
  80         changeListener.reset();
  81         property.set(VALUE_2);
  82         changeListener.check(null, UNDEFINED, UNDEFINED, 0);
  83     }
  84     
  85     private static class ReadOnlyPropertyMock extends ReadOnlyListPropertyBase<Object> {
  86 
  87         private ObservableList<Object> value;
  88         
  89         @Override
  90         public Object getBean() {
  91             // not used
  92             return null;
  93         }
  94 
  95         @Override
  96         public String getName() {
  97             // not used
  98             return null;
  99         }
 100         
 101         private void set(ObservableList<Object> value) {
 102             this.value = value;
 103             ReadOnlyListPropertyBaseShim.fireValueChangedEvent(this);
 104         }
 105 
 106         @Override
 107         public ObservableList<Object> get() {
 108             return value;
 109         }
 110 
 111         @Override
 112         public ReadOnlyIntegerProperty sizeProperty() {
 113             fail("Not in use");
 114             return null;
 115         }
 116 
 117         @Override
 118         public ReadOnlyBooleanProperty emptyProperty() {
 119             fail("Not in use");
 120             return null;
 121         }
 122     }
 123 
 124 }