< prev index next >

modules/base/src/test/java/test/javafx/collections/FilteredListTest.java

Print this page
rev 9769 : 8139848: SortedList should provide a way to map source index to view index

*** 23,34 **** * questions. */ package test.javafx.collections; - import test.javafx.collections.Person; - import test.javafx.collections.MockListObserver; import com.sun.javafx.collections.ObservableListWrapper; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.function.Predicate; --- 23,32 ----
*** 56,65 **** --- 54,84 ---- mlo = new MockListObserver<String>(); filteredList = new FilteredList<>(list, predicate); filteredList.addListener(mlo); } + private <E> void compareIndices(FilteredList<E> filtered) { + ObservableList<? extends E> source = filtered.getSource(); + for (int i = 0; i < filtered.size(); i++) { + // i as a view index + int sourceIndex = filtered.getSourceIndex(i); + assertEquals(i, filtered.getViewIndex(sourceIndex)); + assertSame(filtered.get(i), source.get(sourceIndex)); + } + for (int i = 0; i < source.size(); i++) { + // i as a source index + int viewIndex = filtered.getViewIndex(i); + if (viewIndex >= 0) { + assertEquals(i, filtered.getSourceIndex(viewIndex)); + assertSame(source.get(i), filtered.get(viewIndex)); + } + } + } + + private void compareIndices() { + compareIndices(filteredList); + } @Test public void testLiveMode() { assertEquals(Arrays.asList("a", "d"), filteredList); mlo.check0();
*** 71,104 **** --- 90,130 ---- mlo.clear(); assertEquals(Collections.emptyList(), filteredList); list.addAll("a", "c", "d", "c"); assertEquals(Arrays.asList("a", "d"), filteredList); mlo.check1AddRemove(filteredList, Collections.<String>emptyList(), 0, 2); + compareIndices(); + mlo.clear(); list.add("c"); mlo.check0(); list.add(1, "b"); assertEquals(Arrays.asList("a", "b", "d"), filteredList); mlo.check1AddRemove(filteredList, Collections.<String>emptyList(), 1, 2); + compareIndices(); } @Test public void testLiveMode_Remove() { list.removeAll(Arrays.asList("c")); assertEquals(Arrays.asList("a", "d"), filteredList); mlo.check0(); + compareIndices(); + mlo.clear(); list.remove("a"); assertEquals(Arrays.asList("d"), filteredList); mlo.check1AddRemove(filteredList, Arrays.asList("a"), 0, 0); + compareIndices(); } @Test public void testLiveMode_Permutation() { FXCollections.sort(list, (o1, o2) -> -o1.compareTo(o2)); mlo.check1Permutation(filteredList, new int[] {1, 0}); assertEquals(Arrays.asList("d", "a"), filteredList); + compareIndices(); } @Test public void testLiveMode_changeMatcher() { ObjectProperty<Predicate<String>> pProperty = new SimpleObjectProperty<>();
*** 108,117 **** --- 134,144 ---- filteredList.addListener(mlo); assertEquals(Arrays.asList("a", "d"), filteredList); mlo.check0(); pProperty.set((String s) -> !s.equals("d")); mlo.check1AddRemove(filteredList, Arrays.asList("a", "d"), 0, 3); + compareIndices(); } @Test public void testLiveMode_mutableElement() { ObservableList<Person> list = Person.createPersonsList("A", "BB", "C");
*** 120,143 **** --- 147,174 ---- (Person p) -> p.name.get().length() > 1); MockListObserver<Person> lo = new MockListObserver<>(); filtered.addListener(lo); assertEquals(Arrays.asList(new Person("BB")), filtered); + compareIndices(filtered); list.get(0).name.set("AA"); lo.check1AddRemove(filtered, Collections.EMPTY_LIST, 0, 1); assertEquals(Person.createPersonsList("AA", "BB"), filtered); + compareIndices(filtered); lo.clear(); list.get(1).name.set("BBB"); lo.check1Update(filtered, 1, 2); assertEquals(Person.createPersonsList("AA", "BBB"), filtered); + compareIndices(filtered); lo.clear(); list.get(1).name.set("B"); lo.check1AddRemove(filtered, Person.createPersonsList("B"), 1, 1); assertEquals(Person.createPersonsList("AA"), filtered); + compareIndices(filtered); } @Test public void testLiveMode_mutableElementEmptyList() { ObservableList<Person> list = Person.createPersonsList("A", "B", "C");
*** 146,159 **** --- 177,192 ---- (Person p) -> p.name.get().length() > 1); MockListObserver<Person> lo = new MockListObserver<>(); filtered.addListener(lo); assertEquals(Collections.EMPTY_LIST, filtered); + compareIndices(filtered); list.get(0).name.set("AA"); lo.check1AddRemove(filtered, Collections.EMPTY_LIST, 0, 1); assertEquals(Person.createPersonsList("AA"), filtered); + compareIndices(filtered); } @Test public void testLiveMode_mutableElements() { Person p1 = new Person("A");
*** 170,193 **** --- 203,229 ---- p1.name.set("AA"); lo.checkAddRemove(0, filtered, Collections.EMPTY_LIST, 0, 2); lo.checkAddRemove(1, filtered, Collections.EMPTY_LIST, 3, 5); lo.checkAddRemove(2, filtered, Collections.EMPTY_LIST, 6, 7); assertEquals(Person.createPersonsList("AA", "AA", "BB", "AA", "AA", "BC", "AA"), filtered); + compareIndices(filtered); lo.clear(); p1.name.set("AAA"); lo.checkUpdate(0, filtered, 0, 2); lo.checkUpdate(1, filtered, 3, 5); lo.checkUpdate(2, filtered, 6, 7); assertEquals(Person.createPersonsList("AAA", "AAA", "BB", "AAA", "AAA", "BC", "AAA"), filtered); + compareIndices(filtered); lo.clear(); p1.name.set("A"); lo.checkAddRemove(0, filtered, Person.createPersonsList("A", "A"), 0, 0); lo.checkAddRemove(1, filtered, Person.createPersonsList("A", "A"), 1, 1); lo.checkAddRemove(2, filtered, Person.createPersonsList("A"), 2, 2); assertEquals(Person.createPersonsList( "BB", "BC"), filtered); + compareIndices(filtered); } private static class Updater<E> extends ObservableListWrapper<E> { public Updater(List<E> list) { super(list);
*** 218,227 **** --- 254,264 ---- assertEquals(Person.createPersonsList("BB2", "BC6"), filtered); list.updateAll(); lo.checkUpdate(0, filtered, 0, filtered.size()); + compareIndices(filtered); lo.clear(); list.get(0).name.set("AA0"); list.get(3).name.set("BB3"); list.get(5).name.set("AA5");
*** 230,251 **** --- 267,291 ---- list.updateAll(); assertEquals(Person.createPersonsList("AA0", "BB2", "BB3", "AA5", "AA7"), filtered); lo.checkAddRemove(0, filtered, Collections.EMPTY_LIST, 0, 1); lo.checkAddRemove(1, filtered, Person.createPersonsList("B6"), 2, 5); lo.checkUpdate(2, filtered, 1, 2); + compareIndices(filtered); } @Test public void testNullPredicate() { filteredList.setPredicate(null); assertEquals(list.size(), filteredList.size()); assertEquals(list, filteredList); mlo.check1AddRemove(filteredList, Arrays.asList("a", "d"), 0, 4); + compareIndices(); } @Test public void testSingleArgConstructor() { filteredList = new FilteredList<>(list); assertEquals(list.size(), filteredList.size()); assertEquals(list, filteredList); + compareIndices(); } }
< prev index next >