< prev index next >

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

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


  68         assertEquals(Arrays.asList("a", "c", "c", "d"), sortedList);
  69         mockListObserver.check0();
  70 
  71         compareIndices();
  72     }
  73 
  74     @Test
  75     public void testAdd() {
  76         list.clear();
  77         mockListObserver.clear();
  78         assertEquals(Collections.emptyList(), sortedList);
  79         list.addAll("a", "c", "d", "c");
  80         assertEquals(Arrays.asList("a", "c", "c", "d"), sortedList);
  81         mockListObserver.check1AddRemove(sortedList, Collections.<String>emptyList(), 0, 4);
  82         assertEquals(0, sortedList.getSourceIndex(0));
  83         assertEquals(2, sortedList.getSourceIndex(3));
  84 
  85         compareIndices();
  86     }
  87 
  88     //TODO: replace with sorted.getViewIndex when JDK-8139848 is fixed
  89     private <E> int getViewIndex(SortedList<E> sorted, int sourceIndex) {
  90         for (int i = 0; i < sorted.size(); i++) {
  91             if (sourceIndex == sorted.getSourceIndex(i)) {
  92                 return i;
  93             }
  94         }
  95         return -1;
  96     }
  97 
  98     private <E> void compareIndices(SortedList<E> sorted) {
  99         ObservableList<? extends E> source = sorted.getSource();
 100         for (int i = 0; i < sorted.size(); i++) {
 101             // i as a view index
 102             int sourceIndex = sorted.getSourceIndex(i);
 103             assertEquals(i, getViewIndex(sorted, sourceIndex));
 104             assertSame(sorted.get(i), source.get(sourceIndex));
 105 
 106             // i as a source index
 107             int viewIndex = getViewIndex(sorted, i);
 108             assertEquals(i, sorted.getSourceIndex(viewIndex));
 109             assertSame(source.get(i), sorted.get(viewIndex));
 110         }
 111     }
 112 
 113     private void compareIndices() {
 114         compareIndices(sortedList);
 115     }
 116 
 117     @Test
 118     public void testAddSingle() {
 119         list.add("b");
 120         assertEquals(Arrays.asList("a", "b", "c", "c", "d"), sortedList);
 121         mockListObserver.check1AddRemove(sortedList, Collections.<String>emptyList(), 1, 2);
 122         assertEquals(0, sortedList.getSourceIndex(0));
 123         assertEquals(4, sortedList.getSourceIndex(1));
 124         assertEquals(1, sortedList.getSourceIndex(2));
 125         assertEquals(3, sortedList.getSourceIndex(3));
 126         assertEquals(2, sortedList.getSourceIndex(4));
 127 




  68         assertEquals(Arrays.asList("a", "c", "c", "d"), sortedList);
  69         mockListObserver.check0();
  70 
  71         compareIndices();
  72     }
  73 
  74     @Test
  75     public void testAdd() {
  76         list.clear();
  77         mockListObserver.clear();
  78         assertEquals(Collections.emptyList(), sortedList);
  79         list.addAll("a", "c", "d", "c");
  80         assertEquals(Arrays.asList("a", "c", "c", "d"), sortedList);
  81         mockListObserver.check1AddRemove(sortedList, Collections.<String>emptyList(), 0, 4);
  82         assertEquals(0, sortedList.getSourceIndex(0));
  83         assertEquals(2, sortedList.getSourceIndex(3));
  84 
  85         compareIndices();
  86     }
  87 










  88     private <E> void compareIndices(SortedList<E> sorted) {
  89         ObservableList<? extends E> source = sorted.getSource();
  90         for (int i = 0; i < sorted.size(); i++) {
  91             // i as a view index
  92             int sourceIndex = sorted.getSourceIndex(i);
  93             assertEquals(i, sorted.getViewIndex(sourceIndex));
  94             assertSame(sorted.get(i), source.get(sourceIndex));
  95 
  96             // i as a source index
  97             int viewIndex = sorted.getViewIndex(i);
  98             assertEquals(i, sorted.getSourceIndex(viewIndex));
  99             assertSame(source.get(i), sorted.get(viewIndex));
 100         }
 101     }
 102 
 103     private void compareIndices() {
 104         compareIndices(sortedList);
 105     }
 106 
 107     @Test
 108     public void testAddSingle() {
 109         list.add("b");
 110         assertEquals(Arrays.asList("a", "b", "c", "c", "d"), sortedList);
 111         mockListObserver.check1AddRemove(sortedList, Collections.<String>emptyList(), 1, 2);
 112         assertEquals(0, sortedList.getSourceIndex(0));
 113         assertEquals(4, sortedList.getSourceIndex(1));
 114         assertEquals(1, sortedList.getSourceIndex(2));
 115         assertEquals(3, sortedList.getSourceIndex(3));
 116         assertEquals(2, sortedList.getSourceIndex(4));
 117 


< prev index next >