|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.jface.conformance.databinding; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Arrays; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
| 19 |
import org.eclipse.core.databinding.observable.list.ListDiffEntry; |
| 20 |
import org.eclipse.jface.tests.databinding.EventTrackers.ChangeEventTracker; |
| 21 |
import org.eclipse.jface.tests.databinding.EventTrackers.ListChangeEventTracker; |
| 22 |
|
| 23 |
/** |
| 24 |
* Mutability tests for IObservableList. |
| 25 |
* |
| 26 |
* <p> |
| 27 |
* This class is experimental and can change at any time. It is recommended to |
| 28 |
* not subclass or assume the test names will not change. The only API that is |
| 29 |
* guaranteed to not change are the constructors. The tests will remain public |
| 30 |
* and not final in order to allow for consumers to turn off a test if needed by |
| 31 |
* subclassing. |
| 32 |
* </p> |
| 33 |
* |
| 34 |
* @since 3.2 |
| 35 |
*/ |
| 36 |
public class MutableObservableListContractTest extends |
| 37 |
MutableObservableCollectionContractTest { |
| 38 |
private IObservableCollectionContractDelegate delegate; |
| 39 |
|
| 40 |
private IObservableList list; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param delegate |
| 44 |
*/ |
| 45 |
public MutableObservableListContractTest( |
| 46 |
IObservableCollectionContractDelegate delegate) { |
| 47 |
super(delegate); |
| 48 |
this.delegate = delegate; |
| 49 |
} |
| 50 |
|
| 51 |
public MutableObservableListContractTest(String testName, |
| 52 |
IObservableCollectionContractDelegate delegate) { |
| 53 |
super(testName, delegate); |
| 54 |
this.delegate = delegate; |
| 55 |
} |
| 56 |
|
| 57 |
protected void setUp() throws Exception { |
| 58 |
super.setUp(); |
| 59 |
list = (IObservableList) getObservable(); |
| 60 |
} |
| 61 |
|
| 62 |
public void testAdd_ListChangeEvent() throws Exception { |
| 63 |
assertListChangeEventFired(new Runnable() { |
| 64 |
public void run() { |
| 65 |
list.add(delegate.createElement(list)); |
| 66 |
} |
| 67 |
}, "List.add(Object)", list); |
| 68 |
} |
| 69 |
|
| 70 |
public void testAdd_ListDiffEntry() throws Exception { |
| 71 |
list.add(delegate.createElement(list)); |
| 72 |
final Object element = delegate.createElement(list); |
| 73 |
|
| 74 |
assertAddDiffEntry(new Runnable() { |
| 75 |
public void run() { |
| 76 |
list.add(element); |
| 77 |
} |
| 78 |
}, "List.add(Object)", list, element, 1); |
| 79 |
} |
| 80 |
|
| 81 |
public void testAddAtIndex_ChangeEvent() throws Exception { |
| 82 |
assertChangeEventFired(new Runnable() { |
| 83 |
public void run() { |
| 84 |
list.add(0, delegate.createElement(list)); |
| 85 |
} |
| 86 |
}, "List.add(int, Object)", list); |
| 87 |
} |
| 88 |
|
| 89 |
public void testAddAtIndex_ListChangeEvent() throws Exception { |
| 90 |
assertListChangeEventFired(new Runnable() { |
| 91 |
public void run() { |
| 92 |
list.add(0, delegate.createElement(list)); |
| 93 |
} |
| 94 |
}, "List.add(int, Object)", list); |
| 95 |
} |
| 96 |
|
| 97 |
public void testAddAtIndex_ChangeEventFiredAfterElementIsAdded() |
| 98 |
throws Exception { |
| 99 |
final Object element = delegate.createElement(list); |
| 100 |
|
| 101 |
assertContainsDuringChangeEvent(new Runnable() { |
| 102 |
public void run() { |
| 103 |
list.add(0, element); |
| 104 |
} |
| 105 |
}, "List.add(int, Collection)", list, element); |
| 106 |
} |
| 107 |
|
| 108 |
public void testAddAtIndex_ListDiffEntry() throws Exception { |
| 109 |
list.add(delegate.createElement(list)); |
| 110 |
final Object element = delegate.createElement(list); |
| 111 |
|
| 112 |
assertAddDiffEntry(new Runnable() { |
| 113 |
public void run() { |
| 114 |
list.add(1, element); |
| 115 |
} |
| 116 |
}, "List.add(int, Object)", list, element, 1); |
| 117 |
} |
| 118 |
|
| 119 |
public void testAddAll_ListChangeEvent() throws Exception { |
| 120 |
assertListChangeEventFired(new Runnable() { |
| 121 |
public void run() { |
| 122 |
list.addAll(Arrays.asList(new Object[] { delegate |
| 123 |
.createElement(list) })); |
| 124 |
} |
| 125 |
}, "List.addAll(Collection", list); |
| 126 |
} |
| 127 |
|
| 128 |
public void testAddAll_ListDiffEntry() throws Exception { |
| 129 |
final Object element = delegate.createElement(list); |
| 130 |
|
| 131 |
assertAddDiffEntry(new Runnable() { |
| 132 |
public void run() { |
| 133 |
list.addAll(Arrays.asList(new Object[] { element })); |
| 134 |
} |
| 135 |
}, "List.addAll(Collection)", list, element, 0); |
| 136 |
} |
| 137 |
|
| 138 |
public void testAddAllAtIndex_ChangeEvent() throws Exception { |
| 139 |
assertChangeEventFired(new Runnable() { |
| 140 |
public void run() { |
| 141 |
list.addAll(0, Arrays.asList(new Object[] { delegate |
| 142 |
.createElement(list) })); |
| 143 |
} |
| 144 |
}, "List.addAll(int, Collection)", list); |
| 145 |
} |
| 146 |
|
| 147 |
public void testAddAllAtIndex_ListChangeEvent() throws Exception { |
| 148 |
assertListChangeEventFired(new Runnable() { |
| 149 |
public void run() { |
| 150 |
list.addAll(0, Arrays.asList(new Object[] { delegate |
| 151 |
.createElement(list) })); |
| 152 |
} |
| 153 |
}, "List.addAll(int, Collection)", list); |
| 154 |
} |
| 155 |
|
| 156 |
public void testAddAllAtIndex_ChangeEventFiredAfterElementIsAdded() |
| 157 |
throws Exception { |
| 158 |
final Object element = delegate.createElement(list); |
| 159 |
|
| 160 |
assertContainsDuringChangeEvent(new Runnable() { |
| 161 |
public void run() { |
| 162 |
list.addAll(0, Arrays.asList(new Object[] { element })); |
| 163 |
} |
| 164 |
}, "List.addAll(int, Collection)", list, element); |
| 165 |
} |
| 166 |
|
| 167 |
public void testAddAllAtIndex_ListDiffEntry() throws Exception { |
| 168 |
list.add(delegate.createElement(list)); |
| 169 |
final Object element = delegate.createElement(list); |
| 170 |
|
| 171 |
assertAddDiffEntry(new Runnable() { |
| 172 |
public void run() { |
| 173 |
list.addAll(1, Arrays.asList(new Object[] { element })); |
| 174 |
} |
| 175 |
}, "List.addAll(int, Collection)", list, element, 1); |
| 176 |
} |
| 177 |
|
| 178 |
public void testSet_ChangeEvent() throws Exception { |
| 179 |
list.add(delegate.createElement(list)); |
| 180 |
|
| 181 |
assertChangeEventFired(new Runnable() { |
| 182 |
public void run() { |
| 183 |
list.set(0, delegate.createElement(list)); |
| 184 |
} |
| 185 |
}, "List.set(int, Object)", list); |
| 186 |
} |
| 187 |
|
| 188 |
public void testSet_ListChangeEvent() throws Exception { |
| 189 |
list.add(delegate.createElement(list)); |
| 190 |
|
| 191 |
assertListChangeEventFired(new Runnable() { |
| 192 |
public void run() { |
| 193 |
list.set(0, delegate.createElement(list)); |
| 194 |
} |
| 195 |
}, "List.set(int, Object)", list); |
| 196 |
} |
| 197 |
|
| 198 |
public void testSet_ChangeEventFiredAfterElementIsSet() throws Exception { |
| 199 |
Object element1 = delegate.createElement(list); |
| 200 |
list.add(element1); |
| 201 |
final Object element2 = delegate.createElement(list); |
| 202 |
|
| 203 |
assertContainsDuringChangeEvent(new Runnable() { |
| 204 |
public void run() { |
| 205 |
list.set(0, element2); |
| 206 |
} |
| 207 |
}, "List.set(int, Object)", list, element2); |
| 208 |
} |
| 209 |
|
| 210 |
public void testSet_ListDiffEntry() throws Exception { |
| 211 |
list.add(delegate.createElement(list)); |
| 212 |
Object oldElement = delegate.createElement(list); |
| 213 |
list.add(oldElement); |
| 214 |
|
| 215 |
ListChangeEventTracker listener = new ListChangeEventTracker(); |
| 216 |
list.addListChangeListener(listener); |
| 217 |
|
| 218 |
Object newElement = delegate.createElement(list); |
| 219 |
list.set(1, newElement); |
| 220 |
|
| 221 |
ListDiffEntry[] entries = listener.event.diff.getDifferences(); |
| 222 |
assertEquals( |
| 223 |
"List.set(int, Object) should result in 2 list diff entries.", |
| 224 |
2, entries.length); |
| 225 |
|
| 226 |
ListDiffEntry add = null; |
| 227 |
ListDiffEntry remove = null; |
| 228 |
|
| 229 |
if (entries[0].isAddition() && !entries[1].isAddition()) { |
| 230 |
add = entries[0]; |
| 231 |
remove = entries[1]; |
| 232 |
} else if (!entries[0].isAddition() && entries[1].isAddition()) { |
| 233 |
add = entries[1]; |
| 234 |
remove = entries[0]; |
| 235 |
} else { |
| 236 |
fail("List.set(int, Object) should result in an add and a remove entry."); |
| 237 |
} |
| 238 |
|
| 239 |
assertEquals( |
| 240 |
"List.set(int, Object) removed element should be the old element.", |
| 241 |
oldElement, remove.getElement()); |
| 242 |
assertEquals( |
| 243 |
"List.set(int, Object) removed index should be the index the new element was set at.", |
| 244 |
1, remove.getPosition()); |
| 245 |
|
| 246 |
assertEquals( |
| 247 |
"List.set(int, Object) added element should be the set element.", |
| 248 |
newElement, add.getElement()); |
| 249 |
assertEquals( |
| 250 |
"List.set(int, Object) add index should be the index the new element was set at.", |
| 251 |
1, add.getPosition()); |
| 252 |
} |
| 253 |
|
| 254 |
public void testRemove_ListChangeEvent() throws Exception { |
| 255 |
final Object element = delegate.createElement(list); |
| 256 |
list.add(element); |
| 257 |
|
| 258 |
assertListChangeEventFired(new Runnable() { |
| 259 |
public void run() { |
| 260 |
list.remove(element); |
| 261 |
} |
| 262 |
}, "List.remove(Object)", list); |
| 263 |
} |
| 264 |
|
| 265 |
public void testRemove_ListDiffEntry() throws Exception { |
| 266 |
list.add(delegate.createElement(list)); |
| 267 |
final Object element = delegate.createElement(list); |
| 268 |
list.add(element); |
| 269 |
|
| 270 |
assertRemoveDiffEntry(new Runnable() { |
| 271 |
public void run() { |
| 272 |
list.remove(element); |
| 273 |
} |
| 274 |
}, "List.remove(Object)", list, element, 1); |
| 275 |
} |
| 276 |
|
| 277 |
public void testRemoveAtIndex_ChangeEvent() throws Exception { |
| 278 |
list.add(delegate.createElement(list)); |
| 279 |
|
| 280 |
assertChangeEventFired(new Runnable() { |
| 281 |
public void run() { |
| 282 |
list.remove(0); |
| 283 |
} |
| 284 |
}, "List.remove(int)", list); |
| 285 |
} |
| 286 |
|
| 287 |
public void testRemoveAtIndex_ListChangeEvent() throws Exception { |
| 288 |
list.add(delegate.createElement(list)); |
| 289 |
|
| 290 |
assertListChangeEventFired(new Runnable() { |
| 291 |
public void run() { |
| 292 |
list.remove(0); |
| 293 |
} |
| 294 |
}, "List.remove(int)", list); |
| 295 |
} |
| 296 |
|
| 297 |
public void testRemoveAtIndex_ChangeEventFiredAfterElementIsRemoved() |
| 298 |
throws Exception { |
| 299 |
final Object element = delegate.createElement(list); |
| 300 |
list.add(element); |
| 301 |
|
| 302 |
assertDoesNotContainDuringChangeEvent(new Runnable() { |
| 303 |
public void run() { |
| 304 |
list.remove(0); |
| 305 |
} |
| 306 |
}, "List.remove(int)", list, element); |
| 307 |
} |
| 308 |
|
| 309 |
public void testRemoveAtIndex_ListDiffEntry() throws Exception { |
| 310 |
list.add(delegate.createElement(list)); |
| 311 |
Object element = delegate.createElement(list); |
| 312 |
list.add(element); |
| 313 |
|
| 314 |
assertRemoveDiffEntry(new Runnable() { |
| 315 |
public void run() { |
| 316 |
list.remove(1); |
| 317 |
} |
| 318 |
}, "List.remove(int)", list, element, 1); |
| 319 |
} |
| 320 |
|
| 321 |
public void testRemoveAll_ListChangeEvent() throws Exception { |
| 322 |
final Object element = delegate.createElement(list); |
| 323 |
|
| 324 |
assertListChangeEventFired(new Runnable() { |
| 325 |
public void run() { |
| 326 |
list.removeAll(Arrays.asList(new Object[] { element })); |
| 327 |
} |
| 328 |
}, "List.removeAll(Collection)", list); |
| 329 |
} |
| 330 |
|
| 331 |
public void testRemoveAll_ListDiffEntry() throws Exception { |
| 332 |
final Object element = delegate.createElement(list); |
| 333 |
list.add(element); |
| 334 |
|
| 335 |
assertRemoveDiffEntry(new Runnable() { |
| 336 |
public void run() { |
| 337 |
list.removeAll(Arrays.asList(new Object[] { element })); |
| 338 |
} |
| 339 |
}, "List.removeAll(Collection)", list, element, 0); |
| 340 |
} |
| 341 |
|
| 342 |
public void testRetainAll_ListChangeEvent() throws Exception { |
| 343 |
final Object element1 = delegate.createElement(list); |
| 344 |
list.add(element1); |
| 345 |
list.add(delegate.createElement(list)); |
| 346 |
|
| 347 |
assertListChangeEventFired(new Runnable() { |
| 348 |
public void run() { |
| 349 |
list.retainAll(Arrays.asList(new Object[] { element1 })); |
| 350 |
} |
| 351 |
}, "List.retainAll(Collection", list); |
| 352 |
} |
| 353 |
|
| 354 |
public void testRetainAll_ListDiffEntry() throws Exception { |
| 355 |
final Object element1 = delegate.createElement(list); |
| 356 |
list.add(element1); |
| 357 |
Object element2 = delegate.createElement(list); |
| 358 |
list.add(delegate.createElement(list)); |
| 359 |
|
| 360 |
assertRemoveDiffEntry(new Runnable() { |
| 361 |
public void run() { |
| 362 |
list.retainAll(Arrays.asList(new Object[] { element1 })); |
| 363 |
} |
| 364 |
}, "List.retainAll(Collection)", list, element2, 1); |
| 365 |
} |
| 366 |
|
| 367 |
public void testClear_ListChangeEvent() throws Exception { |
| 368 |
list.add(delegate.createElement(list)); |
| 369 |
|
| 370 |
assertListChangeEventFired(new Runnable() { |
| 371 |
public void run() { |
| 372 |
list.clear(); |
| 373 |
} |
| 374 |
}, "List.clear()", list); |
| 375 |
} |
| 376 |
|
| 377 |
public void testClear_ListDiffEntry() throws Exception { |
| 378 |
Object element = delegate.createElement(list); |
| 379 |
list.add(element); |
| 380 |
|
| 381 |
assertRemoveDiffEntry(new Runnable() { |
| 382 |
public void run() { |
| 383 |
list.clear(); |
| 384 |
} |
| 385 |
}, "List.clear()", list, element, 0); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Asserts standard behaviors of firing list change events. |
| 390 |
* <ul> |
| 391 |
* <li>Event fires once.</li> |
| 392 |
* <li>Source of the event is the provided <code>list</code>. |
| 393 |
* <li>The list change event is fired after the change event.</li> |
| 394 |
* </ul> |
| 395 |
* |
| 396 |
* @param runnable |
| 397 |
* @param methodName |
| 398 |
* @param list |
| 399 |
*/ |
| 400 |
private void assertListChangeEventFired(Runnable runnable, |
| 401 |
String methodName, IObservableList list) { |
| 402 |
List queue = new ArrayList(); |
| 403 |
ListChangeEventTracker listListener = new ListChangeEventTracker(queue); |
| 404 |
ChangeEventTracker changeListener = new ChangeEventTracker(queue); |
| 405 |
|
| 406 |
list.addListChangeListener(listListener); |
| 407 |
list.addChangeListener(changeListener); |
| 408 |
|
| 409 |
runnable.run(); |
| 410 |
|
| 411 |
assertEquals(formatFail(methodName + " should fire one ListChangeEvent."), 1, |
| 412 |
listListener.count); |
| 413 |
assertEquals(formatFail(methodName |
| 414 |
+ "'s change event observable should be the created List."), |
| 415 |
list, listListener.event.getObservable()); |
| 416 |
|
| 417 |
assertEquals(formatFail("Two notifications should have been received."), 2, queue |
| 418 |
.size()); |
| 419 |
assertEquals("ChangeEvent of " + methodName |
| 420 |
+ " should have fired before the ListChangeEvent.", |
| 421 |
changeListener, queue.get(0)); |
| 422 |
assertEquals("ListChangeEvent of " + methodName |
| 423 |
+ " should have fired after the ChangeEvent.", listListener, |
| 424 |
queue.get(1)); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Asserts the list diff entry for a remove operation. |
| 429 |
* |
| 430 |
* @param runnable |
| 431 |
* @param methodName |
| 432 |
* @param list |
| 433 |
* @param element |
| 434 |
* @param index |
| 435 |
*/ |
| 436 |
private void assertRemoveDiffEntry(Runnable runnable, String methodName, |
| 437 |
IObservableList list, Object element, int index) { |
| 438 |
ListChangeEventTracker listener = new ListChangeEventTracker(); |
| 439 |
list.addListChangeListener(listener); |
| 440 |
|
| 441 |
runnable.run(); |
| 442 |
|
| 443 |
ListDiffEntry[] entries = listener.event.diff.getDifferences(); |
| 444 |
assertEquals(methodName + " should result in one diff entry.", 1, |
| 445 |
entries.length); |
| 446 |
|
| 447 |
ListDiffEntry entry = entries[0]; |
| 448 |
assertFalse(methodName |
| 449 |
+ " should result in a diff entry that is an removal.", entry |
| 450 |
.isAddition()); |
| 451 |
assertEquals(methodName |
| 452 |
+ " remove diff entry should have removed the element.", |
| 453 |
element, entry.getElement()); |
| 454 |
assertEquals( |
| 455 |
methodName |
| 456 |
+ " remove diff entry should have removed the element from the provided index.", |
| 457 |
index, entry.getPosition()); |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Asserts the list diff entry for an add operation. |
| 462 |
* |
| 463 |
* @param runnable |
| 464 |
* @param methodName |
| 465 |
* @param list |
| 466 |
* @param element |
| 467 |
* @param index |
| 468 |
*/ |
| 469 |
private void assertAddDiffEntry(Runnable runnable, String methodName, |
| 470 |
IObservableList list, Object element, int index) { |
| 471 |
ListChangeEventTracker listener = new ListChangeEventTracker(); |
| 472 |
list.addListChangeListener(listener); |
| 473 |
|
| 474 |
runnable.run(); |
| 475 |
|
| 476 |
ListDiffEntry[] entries = listener.event.diff.getDifferences(); |
| 477 |
assertEquals(methodName + " should result in one diff entry.", 1, |
| 478 |
entries.length); |
| 479 |
|
| 480 |
ListDiffEntry entry = entries[0]; |
| 481 |
assertTrue(methodName |
| 482 |
+ " should result in a diff entry that is an addition.", entry |
| 483 |
.isAddition()); |
| 484 |
assertEquals(methodName |
| 485 |
+ " add diff entry should have added the element.", element, |
| 486 |
entry.getElement()); |
| 487 |
assertEquals( |
| 488 |
methodName |
| 489 |
+ "add diff entry should have added the element at the provided index.", |
| 490 |
index, entry.getPosition()); |
| 491 |
} |
| 492 |
} |