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 }
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();
|
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 test.javafx.collections.Person;
29 import test.javafx.collections.MockListObserver;
30 import com.sun.javafx.collections.ObservableListWrapper;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.function.Predicate;
35 import javafx.beans.property.ObjectProperty;
36 import javafx.beans.property.SimpleObjectProperty;
37 import javafx.collections.FXCollections;
38 import javafx.collections.ObservableList;
39 import javafx.collections.ObservableListWrapperShim;
40 import javafx.collections.transformation.FilteredList;
41 import static org.junit.Assert.*;
42 import org.junit.Before;
43 import org.junit.Test;
44
45 public class FilteredListTest {
46
47 private ObservableList<String> list;
48 private MockListObserver<String> mlo;
49 private FilteredList<String> filteredList;
50
51 @Before
52 public void setUp() {
53 list = FXCollections.observableArrayList();
54 list.addAll("a", "c", "d", "c");
55 Predicate<String> predicate = (String e) -> !e.equals("c");
56 mlo = new MockListObserver<String>();
57 filteredList = new FilteredList<>(list, predicate);
58 filteredList.addListener(mlo);
59 }
177 p1.name.set("AAA");
178 lo.checkUpdate(0, filtered, 0, 2);
179 lo.checkUpdate(1, filtered, 3, 5);
180 lo.checkUpdate(2, filtered, 6, 7);
181 assertEquals(Person.createPersonsList("AAA", "AAA", "BB", "AAA", "AAA", "BC", "AAA"), filtered);
182
183 lo.clear();
184 p1.name.set("A");
185 lo.checkAddRemove(0, filtered, Person.createPersonsList("A", "A"), 0, 0);
186 lo.checkAddRemove(1, filtered, Person.createPersonsList("A", "A"), 1, 1);
187 lo.checkAddRemove(2, filtered, Person.createPersonsList("A"), 2, 2);
188 assertEquals(Person.createPersonsList( "BB", "BC"), filtered);
189 }
190
191 private static class Updater<E> extends ObservableListWrapper<E> {
192 public Updater(List<E> list) {
193 super(list);
194 }
195
196 public void update(int from, int to) {
197 ObservableListWrapperShim.beginChange(this);
198 for (int i = from; i < to; ++i) {
199 ObservableListWrapperShim.nextUpdate(this, i);
200 }
201 ObservableListWrapperShim.endChange(this);
202 }
203
204 public void updateAll() {
205 update(0, size());
206 }
207 }
208
209 @Test
210 public void testCustomMutableElements() {
211 Updater<Person> list = new Updater<>(Person.createPersonsFromNames(
212 "A0", "A1", "BB2", "B3", "A4", "A5", "BC6", "A7", "C8"));
213
214 FilteredList<Person> filtered = new FilteredList<>(list,
215 (Person p) -> p.name.get().length() > 2);
216 MockListObserver<Person> lo = new MockListObserver<>();
217 filtered.addListener(lo);
218
219 assertEquals(Person.createPersonsList("BB2", "BC6"), filtered);
220
221 list.updateAll();
|