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