1 /* 2 * Copyright (c) 2012, 2014, 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 com.sun.javafx.collections.ElementObservableListDecorator; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.Collection; 32 import java.util.Collections; 33 import java.util.List; 34 import java.util.ListIterator; 35 import javafx.beans.Observable; 36 import javafx.collections.FXCollections; 37 import javafx.collections.ObservableList; 38 import javafx.util.Callback; 39 import org.junit.Before; 40 import org.junit.Test; 41 import static org.junit.Assert.*; 42 import org.junit.runner.RunWith; 43 import org.junit.runners.Parameterized; 44 import org.junit.runners.Parameterized.Parameters; 45 46 @RunWith(Parameterized.class) 47 public class ObservableListWithExtractor { 48 private final Mode mode; 49 50 public static enum Mode { 51 OBSERVABLE_LIST_WRAPPER, 52 DECORATOR 53 } 54 55 @Parameters 56 public static Collection<Object[]> data() { 57 return Arrays.asList(new Object[][]{{Mode.OBSERVABLE_LIST_WRAPPER}, {Mode.DECORATOR}}); 58 } 59 60 private ObservableList<Person> modifiedList; 61 private ObservableList<Person> observedList; 62 private MockListObserver obs; 63 private Person p0; 64 65 public ObservableListWithExtractor(Mode mode) { 66 this.mode = mode; 67 } 68 69 private void updateP0() { 70 p0.name.set("bar"); 71 } 72 73 74 @Before 75 public void setUp() { 76 p0 = new Person(); 77 obs = new MockListObserver(); 78 Callback<Person, Observable[]> callback = param -> new Observable[]{param.name}; 79 if (mode == Mode.OBSERVABLE_LIST_WRAPPER) { 80 observedList = modifiedList = FXCollections.observableArrayList(callback); 81 } else { 82 modifiedList = FXCollections.observableArrayList(); 83 observedList = new ElementObservableListDecorator<Person>(modifiedList, callback); 84 } 85 86 modifiedList.add(p0); 87 observedList.addListener(obs); 88 } 89 90 @Test 91 public void testUpdate_add() { 92 updateP0(); 93 obs.check1Update(modifiedList, 0, 1); 94 } 95 96 @Test 97 public void testUpdate_add1() { 98 modifiedList.clear(); 99 modifiedList.add(0, p0); 100 obs.clear(); 101 updateP0(); 102 obs.check1Update(observedList, 0, 1); 103 } 104 105 @Test 106 public void testUpdate_addAll() { 107 modifiedList.clear(); 108 modifiedList.addAll(Arrays.asList(p0, p0)); 109 obs.clear(); 110 updateP0(); 111 obs.check1Update(observedList, 0, 2); 112 } 113 114 @Test 115 public void testUpdate_addAll1() { 116 modifiedList.clear(); 117 modifiedList.addAll(0, Arrays.asList(p0, p0)); 118 obs.clear(); 119 updateP0(); 120 obs.check1Update(observedList, 0, 2); 121 } 122 123 @Test 124 public void testUpdate_addAll2() { 125 modifiedList.clear(); 126 modifiedList.addAll(p0, p0); 127 obs.clear(); 128 updateP0(); 129 obs.check1Update(observedList, 0, 2); 130 } 131 132 @Test 133 public void testUpdate_set() { 134 Person p1 = new Person(); 135 modifiedList.set(0, p1); 136 obs.clear(); 137 updateP0(); 138 obs.check0(); 139 p1.name.set("bar"); 140 obs.check1Update(observedList, 0, 1); 141 } 142 143 @Test 144 public void testUpdate_setAll() { 145 Person p1 = new Person(); 146 modifiedList.setAll(p1); 147 obs.clear(); 148 updateP0(); 149 obs.check0(); 150 p1.name.set("bar"); 151 obs.check1Update(observedList, 0, 1); 152 } 153 154 @Test 155 public void testUpdate_remove() { 156 modifiedList.remove(p0); 157 obs.clear(); 158 updateP0(); 159 obs.check0(); 160 } 161 162 @Test 163 public void testUpdate_remove1() { 164 modifiedList.remove(0); 165 obs.clear(); 166 updateP0(); 167 obs.check0(); 168 } 169 170 @Test 171 public void testUpdate_removeAll() { 172 modifiedList.removeAll(p0); 173 obs.clear(); 174 updateP0(); 175 obs.check0(); 176 } 177 178 @Test 179 public void testUpdate_retainAll() { 180 modifiedList.retainAll(); 181 obs.clear(); 182 updateP0(); 183 obs.check0(); 184 } 185 186 @Test 187 public void testUpdate_iterator_add() { 188 modifiedList.clear(); 189 modifiedList.listIterator().add(p0); 190 obs.clear(); 191 updateP0(); 192 obs.check1Update(observedList, 0, 1); 193 } 194 195 @Test 196 public void testUpdate_iterator_set() { 197 Person p1 = new Person(); 198 ListIterator<Person> listIterator = modifiedList.listIterator(); 199 listIterator.next(); 200 listIterator.set(p1); 201 obs.clear(); 202 updateP0(); 203 obs.check0(); 204 p1.name.set("bar"); 205 obs.check1Update(observedList, 0, 1); 206 } 207 208 @Test 209 public void testUpdate_sublist_add() { 210 List<Person> sublist = modifiedList.subList(0, 1); 211 sublist.add(p0); 212 obs.clear(); 213 updateP0(); 214 obs.check1Update(observedList, 0, 2); 215 } 216 217 @Test 218 public void testUpdate_sublist_add1() { 219 List<Person> sublist = modifiedList.subList(0, 1); 220 sublist.clear(); 221 sublist.add(0, p0); 222 obs.clear(); 223 updateP0(); 224 obs.check1Update(observedList, 0, 1); 225 } 226 227 @Test 228 public void testUpdate_sublist_addAll() { 229 List<Person> sublist = modifiedList.subList(0, 1); 230 sublist.clear(); 231 sublist.addAll(Arrays.asList(p0, p0)); 232 obs.clear(); 233 updateP0(); 234 obs.check1Update(observedList, 0, 2); 235 } 236 237 @Test 238 public void testUpdate_sublist_addAll1() { 239 List<Person> sublist = modifiedList.subList(0, 1); 240 sublist.clear(); 241 sublist.addAll(0, Arrays.asList(p0, p0)); 242 obs.clear(); 243 updateP0(); 244 obs.check1Update(observedList, 0, 2); 245 } 246 247 @Test 248 public void testUpdate_sublist_set() { 249 List<Person> sublist = modifiedList.subList(0, 1); 250 Person p1 = new Person(); 251 sublist.set(0, p1); 252 obs.clear(); 253 updateP0(); 254 obs.check0(); 255 p1.name.set("bar"); 256 obs.check1Update(observedList, 0, 1); 257 } 258 259 @Test 260 public void testUpdate_sublist_remove() { 261 List<Person> sublist = modifiedList.subList(0, 1); 262 sublist.remove(p0); 263 obs.clear(); 264 updateP0(); 265 obs.check0(); 266 } 267 268 @Test 269 public void testUpdate_sublist_remove1() { 270 List<Person> sublist = modifiedList.subList(0, 1); 271 sublist.remove(0); 272 obs.clear(); 273 updateP0(); 274 obs.check0(); 275 } 276 277 @Test 278 public void testUpdate_sublist_removeAll() { 279 List<Person> sublist = modifiedList.subList(0, 1); 280 sublist.removeAll(Arrays.asList(p0)); 281 obs.clear(); 282 updateP0(); 283 obs.check0(); 284 } 285 286 @Test 287 public void testUpdate_sublist_retainAll() { 288 List<Person> sublist = modifiedList.subList(0, 1); 289 sublist.retainAll(Collections.<Person>emptyList()); 290 obs.clear(); 291 updateP0(); 292 obs.check0(); 293 } 294 295 @Test 296 public void testUpdate_iterator_sublist_add() { 297 List<Person> sublist = modifiedList.subList(0, 1); 298 sublist.clear(); 299 sublist.listIterator().add(p0); 300 obs.clear(); 301 updateP0(); 302 obs.check1Update(observedList, 0, 1); 303 } 304 305 @Test 306 public void testUpdate_iterator_sublist_set() { 307 List<Person> sublist = modifiedList.subList(0, 1); 308 Person p1 = new Person(); 309 ListIterator<Person> listIterator = sublist.listIterator(); 310 listIterator.next(); 311 listIterator.set(p1); 312 obs.clear(); 313 updateP0(); 314 obs.check0(); 315 p1.name.set("bar"); 316 obs.check1Update(observedList, 0, 1); 317 } 318 319 @Test 320 public void testMultipleUpdate() { 321 322 modifiedList.add(new Person()); 323 modifiedList.addAll(p0, p0); 324 325 obs.clear(); 326 327 updateP0(); 328 329 obs.checkUpdate(0, modifiedList, 0, 1); 330 obs.checkUpdate(1, modifiedList, 2, 4); 331 assertEquals(2, obs.calls.size()); 332 333 } 334 335 @Test 336 public void testPreFilledList() { 337 ArrayList<Person> arrayList = new ArrayList<Person>(); 338 arrayList.add(p0); 339 obs = new MockListObserver(); 340 Callback<Person, Observable[]> callback = param -> new Observable[]{param.name}; 341 if (mode == Mode.OBSERVABLE_LIST_WRAPPER) { 342 observedList = modifiedList = FXCollections.observableList(arrayList, callback); 343 } else { 344 modifiedList = FXCollections.observableArrayList(arrayList); 345 observedList = new ElementObservableListDecorator<Person>(modifiedList, callback); 346 } 347 348 observedList.addListener(obs); 349 350 updateP0(); 351 352 obs.checkUpdate(0, observedList, 0, 1); 353 assertEquals(1, obs.calls.size()); 354 } 355 356 357 }