1 /*
   2  * Copyright (c) 2014, 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.collections;
  27 
  28 import com.sun.javafx.collections.VetoableListDecorator;
  29 import java.util.Arrays;
  30 import java.util.Collection;
  31 import java.util.List;
  32 import javafx.beans.Observable;
  33 import javafx.collections.transformation.SortedList;
  34 import org.junit.Before;
  35 import org.junit.Test;
  36 import org.junit.runner.RunWith;
  37 import org.junit.runners.Parameterized;
  38 
  39 @RunWith(Parameterized.class)
  40 public class SourceAdapterChangeTest {
  41 
  42     @FunctionalInterface
  43     public interface ListFactory<E> {
  44         public ObservableList<E> createList(ObservableList<E> items);
  45     }
  46 
  47     private static final ListFactory unmodifiableObservableList =
  48             items -> FXCollections.unmodifiableObservableList(items);
  49     private static final ListFactory checkedObservableList =
  50             items -> FXCollections.checkedObservableList(items, Person.class);
  51     private static final ListFactory synchronizedObservableList =
  52             items -> FXCollections.synchronizedObservableList(items);
  53     private static final ListFactory vetoableListDecorator =
  54             items -> new VetoableListDecorator<Person>(items) {
  55 
  56                 @Override
  57                 protected void onProposedChange(List<Person> added, int[] removed) {
  58                 }
  59             };
  60 
  61     @Parameterized.Parameters
  62     public static Collection createParameters() {
  63         Object[][] data = new Object[][] {
  64             { unmodifiableObservableList },
  65             { checkedObservableList },
  66             { synchronizedObservableList },
  67             { vetoableListDecorator },
  68          };
  69         return Arrays.asList(data);
  70     }
  71 
  72     final ListFactory listFactory;
  73     ObservableList<Person> items;
  74     ObservableList<Person> list;
  75     MockListObserver<Person> mlo;
  76 
  77     public SourceAdapterChangeTest(ListFactory listFactory) {
  78         this.listFactory = listFactory;
  79     }
  80 
  81     @Before
  82     public void setUp() throws Exception {
  83         items = FXCollections.observableArrayList(
  84                 (Person p) -> new Observable[]{p.name});
  85         items.addAll(
  86                 new Person("one"), new Person("two"), new Person("three"),
  87                 new Person("four"), new Person("five"));
  88         list = listFactory.createList(items);
  89         mlo = new MockListObserver<>();
  90         list.addListener(mlo);
  91     }
  92 
  93     @Test
  94     public void testUpdate() {
  95         items.get(3).name.set("zero"); // four -> zero
  96         ObservableList<Person> expected = FXCollections.observableArrayList(
  97                 new Person("one"), new Person("two"), new Person("three"),
  98                 new Person("zero"), new Person("five"));
  99         mlo.check1Update(expected, 3, 4);
 100     }
 101 
 102     @Test
 103     public void testPermutation() {
 104         FXCollections.sort(items);
 105         ObservableList<Person> expected = FXCollections.observableArrayList(
 106                 new Person("five"), new Person("four"), new Person("one"),
 107                 new Person("three"), new Person("two"));
 108         mlo.check1Permutation(expected, new int[]{2, 4, 3, 1, 0});
 109     }
 110 
 111     @Test
 112     public void testPermutationUpdate() {
 113         SortedList<Person> sorted = items.sorted((o1, o2) -> o1.compareTo(o2));
 114         list.removeListener(mlo);
 115         list = listFactory.createList(sorted);
 116         list.addListener(mlo);
 117         items.get(3).name.set("zero"); // four -> zero
 118         ObservableList<Person> expected = FXCollections.observableArrayList(
 119                 new Person("five"), new Person("one"), new Person("three"),
 120                 new Person("two"), new Person("zero"));
 121         mlo.checkPermutation(0, expected, 0, expected.size(), new int[] {0, 4, 1, 2, 3});
 122         mlo.checkUpdate(1, expected, 4, 5);
 123     }
 124 }