|
| 1 | +package com.tobiasdiez.easybind; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javafx.beans.Observable; |
| 6 | +import javafx.beans.property.IntegerProperty; |
| 7 | +import javafx.beans.property.SimpleIntegerProperty; |
| 8 | +import javafx.collections.FXCollections; |
| 9 | +import javafx.collections.ObservableList; |
| 10 | +import javafx.collections.transformation.SortedList; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | + |
| 16 | +public class MappedBackedListTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testMappedBackedListWithMappingOnUpdate() { |
| 20 | + ObservableList<IntegerProperty> list = FXCollections.observableArrayList(number -> new Observable[]{number}); |
| 21 | + ObservableList<Integer> mappedList = EasyBind.mapBacked(list, IntegerProperty::get, true); |
| 22 | + |
| 23 | + IntegerProperty number = new SimpleIntegerProperty(1); |
| 24 | + list.add(number); |
| 25 | + |
| 26 | + assertEquals(1, mappedList.get(0)); |
| 27 | + |
| 28 | + number.set(2); |
| 29 | + |
| 30 | + assertEquals(2, mappedList.get(0)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testMappedBackedListWithoutMappingOnUpdate() { |
| 35 | + ObservableList<IntegerProperty> list = FXCollections.observableArrayList(number -> new Observable[]{number}); |
| 36 | + ObservableList<Integer> mappedList = EasyBind.mapBacked(list, IntegerProperty::get, false); |
| 37 | + |
| 38 | + IntegerProperty number = new SimpleIntegerProperty(1); |
| 39 | + list.add(number); |
| 40 | + |
| 41 | + assertEquals(1, mappedList.get(0)); |
| 42 | + |
| 43 | + number.set(2); |
| 44 | + |
| 45 | + assertEquals(1, mappedList.get(0)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testUnSortedListUpdatesWithMappedBackedList() { |
| 50 | + ObservableList<IntegerProperty> list = FXCollections.observableArrayList(number -> new Observable[]{number}); |
| 51 | + ObservableList<Integer> mappedList = EasyBind.mapBacked(list, IntegerProperty::get); |
| 52 | + SortedList<Integer> sortedList = new SortedList<>(mappedList); |
| 53 | + |
| 54 | + IntegerProperty num1 = new SimpleIntegerProperty(1); |
| 55 | + IntegerProperty num2 = new SimpleIntegerProperty(3); |
| 56 | + IntegerProperty num3 = new SimpleIntegerProperty(2); |
| 57 | + |
| 58 | + list.addAll(num1, num2, num3); |
| 59 | + |
| 60 | + // list= [1, 3, 2], sortedList= [1, 3, 2] |
| 61 | + assertEquals(List.of(1, 3, 2), sortedList); |
| 62 | + |
| 63 | + num2.set(4); |
| 64 | + |
| 65 | + // list= [1, 4, 2], sortedList= [1, 4, 2] |
| 66 | + assertEquals(List.of(1, 4, 2), sortedList); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testSortedListUpdatesWithMappedBackedList() { |
| 72 | + ObservableList<IntegerProperty> list = FXCollections.observableArrayList(number -> new Observable[]{number}); |
| 73 | + ObservableList<Integer> mappedList = EasyBind.mapBacked(list, IntegerProperty::get); |
| 74 | + SortedList<Integer> sortedList = new SortedList<>(mappedList, Integer::compareTo); |
| 75 | + |
| 76 | + IntegerProperty num1 = new SimpleIntegerProperty(1); |
| 77 | + IntegerProperty num2 = new SimpleIntegerProperty(3); |
| 78 | + IntegerProperty num3 = new SimpleIntegerProperty(2); |
| 79 | + |
| 80 | + list.addAll(num1, num2, num3); |
| 81 | + |
| 82 | + // list= [1, 3, 2], sortedList= [1, 2, 3] |
| 83 | + assertEquals(List.of(1, 2, 3), sortedList); |
| 84 | + |
| 85 | + num2.set(4); |
| 86 | + |
| 87 | + // list= [1, 4, 2], sortedList= [1, 2, 4] |
| 88 | + assertEquals(List.of(1, 2, 4), sortedList); |
| 89 | + } |
| 90 | +} |
0 commit comments