1 /*
   2  * Copyright (c) 2010, 2015, 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.collections;
  27 
  28 import com.sun.javafx.collections.ObservableListWrapper;
  29 import java.util.Arrays;
  30 import java.util.Collections;
  31 import java.util.List;
  32 import java.util.function.Predicate;
  33 import javafx.beans.property.ObjectProperty;
  34 import javafx.beans.property.SimpleObjectProperty;
  35 import javafx.collections.FXCollections;
  36 import javafx.collections.ObservableList;
  37 import javafx.collections.ObservableListWrapperShim;
  38 import javafx.collections.transformation.FilteredList;
  39 import static org.junit.Assert.*;
  40 import org.junit.Before;
  41 import org.junit.Test;
  42 
  43 public class FilteredListTest {
  44 
  45     private ObservableList<String> list;
  46     private MockListObserver<String> mlo;
  47     private FilteredList<String> filteredList;
  48 
  49     @Before
  50     public void setUp() {
  51         list = FXCollections.observableArrayList();
  52         list.addAll("a", "c", "d", "c");
  53         Predicate<String> predicate = (String e) -> !e.equals("c");
  54         mlo = new MockListObserver<String>();
  55         filteredList = new FilteredList<>(list, predicate);
  56         filteredList.addListener(mlo);
  57     }
  58 
  59     private <E> void compareIndices(FilteredList<E> filtered) {
  60         ObservableList<? extends E> source = filtered.getSource();
  61         for (int i = 0; i < filtered.size(); i++) {
  62             // i as a view index
  63             int sourceIndex = filtered.getSourceIndex(i);
  64             assertEquals(i, filtered.getViewIndex(sourceIndex));
  65             assertSame(filtered.get(i), source.get(sourceIndex));
  66         }
  67         for (int i = 0; i < source.size(); i++) {
  68             // i as a source index
  69             int viewIndex = filtered.getViewIndex(i);
  70             if (viewIndex >= 0) {
  71                 assertEquals(i, filtered.getSourceIndex(viewIndex));
  72                 assertSame(source.get(i), filtered.get(viewIndex));
  73             }
  74         }
  75     }
  76 
  77     private void compareIndices() {
  78         compareIndices(filteredList);
  79     }
  80 
  81     @Test
  82     public void testLiveMode() {
  83         assertEquals(Arrays.asList("a", "d"), filteredList);
  84         mlo.check0();
  85     }
  86 
  87     @Test
  88     public void testLiveMode_Add() {
  89         list.clear();
  90         mlo.clear();
  91         assertEquals(Collections.emptyList(), filteredList);
  92         list.addAll("a", "c", "d", "c");
  93         assertEquals(Arrays.asList("a", "d"), filteredList);
  94         mlo.check1AddRemove(filteredList, Collections.<String>emptyList(), 0, 2);
  95         compareIndices();
  96 
  97         mlo.clear();
  98         list.add("c");
  99         mlo.check0();
 100         list.add(1, "b");
 101         assertEquals(Arrays.asList("a", "b", "d"), filteredList);
 102         mlo.check1AddRemove(filteredList, Collections.<String>emptyList(), 1, 2);
 103         compareIndices();
 104     }
 105 
 106     @Test
 107     public void testLiveMode_Remove() {
 108         list.removeAll(Arrays.asList("c"));
 109         assertEquals(Arrays.asList("a", "d"), filteredList);
 110         mlo.check0();
 111         compareIndices();
 112 
 113         mlo.clear();
 114         list.remove("a");
 115         assertEquals(Arrays.asList("d"), filteredList);
 116         mlo.check1AddRemove(filteredList, Arrays.asList("a"), 0, 0);
 117         compareIndices();
 118     }
 119 
 120     @Test
 121     public void testLiveMode_Permutation() {
 122         FXCollections.sort(list, (o1, o2) -> -o1.compareTo(o2));
 123         mlo.check1Permutation(filteredList, new int[] {1, 0});
 124         assertEquals(Arrays.asList("d", "a"), filteredList);
 125         compareIndices();
 126     }
 127 
 128     @Test
 129     public void testLiveMode_changeMatcher() {
 130         ObjectProperty<Predicate<String>> pProperty = new SimpleObjectProperty<>();
 131         pProperty.set((String e) -> !e.equals("c"));
 132         filteredList = new FilteredList<>(list);
 133         filteredList.predicateProperty().bind(pProperty);
 134         filteredList.addListener(mlo);
 135         assertEquals(Arrays.asList("a", "d"), filteredList);
 136         mlo.check0();
 137         pProperty.set((String s) -> !s.equals("d"));
 138         mlo.check1AddRemove(filteredList, Arrays.asList("a", "d"), 0, 3);
 139         compareIndices();
 140     }
 141 
 142     @Test
 143     public void testLiveMode_mutableElement() {
 144         ObservableList<Person> list = Person.createPersonsList("A", "BB", "C");
 145 
 146         FilteredList<Person> filtered = new FilteredList<>(list,
 147                 (Person p) -> p.name.get().length() > 1);
 148         MockListObserver<Person> lo = new MockListObserver<>();
 149         filtered.addListener(lo);
 150 
 151         assertEquals(Arrays.asList(new Person("BB")), filtered);
 152         compareIndices(filtered);
 153 
 154         list.get(0).name.set("AA");
 155         lo.check1AddRemove(filtered, Collections.EMPTY_LIST, 0, 1);
 156         assertEquals(Person.createPersonsList("AA", "BB"), filtered);
 157         compareIndices(filtered);
 158 
 159         lo.clear();
 160         list.get(1).name.set("BBB");
 161         lo.check1Update(filtered, 1, 2);
 162         assertEquals(Person.createPersonsList("AA", "BBB"), filtered);
 163         compareIndices(filtered);
 164 
 165         lo.clear();
 166         list.get(1).name.set("B");
 167         lo.check1AddRemove(filtered, Person.createPersonsList("B"), 1, 1);
 168         assertEquals(Person.createPersonsList("AA"), filtered);
 169         compareIndices(filtered);
 170     }
 171 
 172     @Test
 173     public void testLiveMode_mutableElementEmptyList() {
 174         ObservableList<Person> list = Person.createPersonsList("A", "B", "C");
 175 
 176         FilteredList<Person> filtered = new FilteredList<>(list,
 177                 (Person p) -> p.name.get().length() > 1);
 178         MockListObserver<Person> lo = new MockListObserver<>();
 179         filtered.addListener(lo);
 180 
 181         assertEquals(Collections.EMPTY_LIST, filtered);
 182         compareIndices(filtered);
 183 
 184         list.get(0).name.set("AA");
 185         lo.check1AddRemove(filtered, Collections.EMPTY_LIST, 0, 1);
 186         assertEquals(Person.createPersonsList("AA"), filtered);
 187         compareIndices(filtered);
 188     }
 189 
 190     @Test
 191     public void testLiveMode_mutableElements() {
 192         Person p1 = new Person("A");
 193         ObservableList<Person> list = Person.createPersonsList(
 194                 p1, p1, new Person("BB"), new Person("B"), p1, p1, new Person("BC"), p1, new Person("C"));
 195 
 196         FilteredList<Person> filtered = new FilteredList<>(list,
 197                 (Person p) -> p.name.get().length() > 1);
 198         MockListObserver<Person> lo = new MockListObserver<>();
 199         filtered.addListener(lo);
 200 
 201         assertEquals(Person.createPersonsList("BB", "BC"), filtered);
 202 
 203         p1.name.set("AA");
 204         lo.checkAddRemove(0, filtered, Collections.EMPTY_LIST, 0, 2);
 205         lo.checkAddRemove(1, filtered, Collections.EMPTY_LIST, 3, 5);
 206         lo.checkAddRemove(2, filtered, Collections.EMPTY_LIST, 6, 7);
 207         assertEquals(Person.createPersonsList("AA", "AA", "BB", "AA", "AA", "BC", "AA"), filtered);
 208         compareIndices(filtered);
 209 
 210         lo.clear();
 211         p1.name.set("AAA");
 212         lo.checkUpdate(0, filtered, 0, 2);
 213         lo.checkUpdate(1, filtered, 3, 5);
 214         lo.checkUpdate(2, filtered, 6, 7);
 215         assertEquals(Person.createPersonsList("AAA", "AAA", "BB", "AAA", "AAA", "BC", "AAA"), filtered);
 216         compareIndices(filtered);
 217 
 218         lo.clear();
 219         p1.name.set("A");
 220         lo.checkAddRemove(0, filtered, Person.createPersonsList("A", "A"), 0, 0);
 221         lo.checkAddRemove(1, filtered, Person.createPersonsList("A", "A"), 1, 1);
 222         lo.checkAddRemove(2, filtered, Person.createPersonsList("A"), 2, 2);
 223         assertEquals(Person.createPersonsList( "BB", "BC"), filtered);
 224         compareIndices(filtered);
 225     }
 226 
 227     private static class Updater<E> extends ObservableListWrapper<E> {
 228         public Updater(List<E> list) {
 229             super(list);
 230         }
 231 
 232         public void update(int from, int to) {
 233             ObservableListWrapperShim.beginChange(this);
 234             for (int i = from; i < to; ++i) {
 235                ObservableListWrapperShim.nextUpdate(this, i);
 236             }
 237             ObservableListWrapperShim.endChange(this);
 238         }
 239 
 240         public void updateAll() {
 241             update(0, size());
 242         }
 243     }
 244 
 245     @Test
 246     public void testCustomMutableElements() {
 247         Updater<Person> list = new Updater<>(Person.createPersonsFromNames(
 248                 "A0", "A1", "BB2", "B3", "A4", "A5", "BC6", "A7", "C8"));
 249 
 250         FilteredList<Person> filtered = new FilteredList<>(list,
 251                 (Person p) -> p.name.get().length() > 2);
 252         MockListObserver<Person> lo = new MockListObserver<>();
 253         filtered.addListener(lo);
 254 
 255         assertEquals(Person.createPersonsList("BB2", "BC6"), filtered);
 256 
 257         list.updateAll();
 258         lo.checkUpdate(0, filtered, 0, filtered.size());
 259         compareIndices(filtered);
 260 
 261         lo.clear();
 262         list.get(0).name.set("AA0");
 263         list.get(3).name.set("BB3");
 264         list.get(5).name.set("AA5");
 265         list.get(6).name.set("B6");
 266         list.get(7).name.set("AA7");
 267         list.updateAll();
 268         assertEquals(Person.createPersonsList("AA0", "BB2", "BB3", "AA5", "AA7"), filtered);
 269         lo.checkAddRemove(0, filtered, Collections.EMPTY_LIST, 0, 1);
 270         lo.checkAddRemove(1, filtered, Person.createPersonsList("B6"), 2, 5);
 271         lo.checkUpdate(2, filtered, 1, 2);
 272         compareIndices(filtered);
 273     }
 274 
 275     @Test
 276     public void testNullPredicate() {
 277         filteredList.setPredicate(null);
 278         assertEquals(list.size(), filteredList.size());
 279         assertEquals(list, filteredList);
 280         mlo.check1AddRemove(filteredList, Arrays.asList("a", "d"), 0, 4);
 281         compareIndices();
 282     }
 283 
 284     @Test
 285     public void testSingleArgConstructor() {
 286         filteredList = new FilteredList<>(list);
 287         assertEquals(list.size(), filteredList.size());
 288         assertEquals(list, filteredList);
 289         compareIndices();
 290     }
 291 }