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