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 java.util.Arrays; 29 import java.util.Collections; 30 import java.util.LinkedList; 31 import java.util.List; 32 import static org.junit.Assert.* ; 33 34 /** 35 * A mock observer that tracks calls to its onChanged() method, 36 * combined with utility methods to make assertions on the calls made. 37 * 38 */ 39 public class MockListObserver<E> implements ListChangeListener<E> { 40 private boolean tooManyCalls; 41 42 static class Call<E> { 43 ObservableList<? extends E> list; 44 List<? extends E> removed; 45 int from; 46 int to; 47 private int[] permutation; 48 private boolean update; 49 @Override 50 public String toString() { 51 return "removed: " + removed + ", from: " + from + ", to: " + to + ", permutation: " + Arrays.toString(permutation); 52 } 53 } 54 55 List<Call<E>> calls = new LinkedList<Call<E>>(); 56 57 @Override 58 public void onChanged(Change<? extends E> change) { 59 if (calls.isEmpty()) { 60 while (change.next()) { 61 Call<E> call = new Call<E>(); 62 call.list = change.getList(); 63 call.removed = change.getRemoved(); 64 call.from = change.getFrom(); 65 call.to = change.getTo(); 66 if (change.wasPermutated()) { 67 call.permutation = new int[call.to - call.from]; 68 for (int i = 0; i < call.permutation.length; ++i) { 69 call.permutation[i] = change.getPermutation(i + call.from); 70 } 71 } else { 72 call.permutation = new int[0]; 73 } 74 call.update = change.wasUpdated(); 75 calls.add(call); 76 77 // Check generic change assertions 78 assertFalse(change.wasPermutated() && change.wasUpdated()); 79 assertFalse((change.wasAdded() || change.wasRemoved()) && change.wasUpdated()); 80 assertFalse((change.wasAdded() || change.wasRemoved()) && change.wasPermutated()); 81 } 82 } else { 83 tooManyCalls = true; 84 } 85 } 86 87 public void check0() { 88 assertEquals(0, calls.size()); 89 } 90 91 public void check1AddRemove(ObservableList<E> list, 92 List<E> removed, 93 int from, 94 int to) { 95 assertFalse(tooManyCalls); 96 assertEquals(1, calls.size()); 97 checkAddRemove(0, list, removed, from, to); 98 } 99 100 public void checkAddRemove(int idx, ObservableList<E> list, 101 List<E> removed, 102 int from, 103 int to) { 104 if (removed == null) { 105 removed = Collections.<E>emptyList(); 106 } 107 assertFalse(tooManyCalls); 108 Call<E> call = calls.get(idx); 109 assertSame(call.list, list); 110 assertEquals(call.removed, removed); 111 assertEquals(call.from, from); 112 assertEquals(call.to, to); 113 assertEquals(call.permutation.length, 0); 114 } 115 116 public void check1Permutation(ObservableList<E> list, int[] perm) { 117 assertFalse(tooManyCalls); 118 assertEquals(1, calls.size()); 119 checkPermutation(0, list, 0, list.size(), perm); 120 } 121 122 public void check1Permutation(ObservableList<E> list, int from, int to, int[] perm) { 123 assertFalse(tooManyCalls); 124 assertEquals(1, calls.size()); 125 checkPermutation(0, list, from, to, perm); 126 } 127 128 public void checkPermutation(int idx, ObservableList<E> list, int from, int to, int[] perm) { 129 assertFalse(tooManyCalls); 130 Call<E> call = calls.get(idx); 131 assertEquals(list, call.list); 132 assertEquals(Collections.EMPTY_LIST, call.removed); 133 assertEquals(from, call.from); 134 assertEquals(to, call.to); 135 assertArrayEquals(perm, call.permutation); 136 } 137 138 public void check1Update(ObservableList<E> list, int from, int to) { 139 assertFalse(tooManyCalls); 140 assertEquals(1, calls.size()); 141 checkUpdate(0, list, from, to); 142 } 143 144 public void checkUpdate(int idx, ObservableList<E> list, int from, int to) { 145 assertFalse(tooManyCalls); 146 Call<E> call = calls.get(idx); 147 assertEquals(list, call.list); 148 assertEquals(Collections.EMPTY_LIST, call.removed); 149 assertArrayEquals(new int[0], call.permutation); 150 assertEquals(true, call.update); 151 assertEquals(from, call.from); 152 assertEquals(to, call.to); 153 } 154 155 public void check1() { 156 assertFalse(tooManyCalls); 157 assertEquals(1, calls.size()); 158 } 159 160 public void clear() { 161 calls.clear(); 162 tooManyCalls = false; 163 } 164 }