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