|
1 | 1 | /* |
2 | | - * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -277,6 +277,93 @@ public class ListCellTest { |
277 | 277 | assertEquals("Water", cell.getItem()); |
278 | 278 | } |
279 | 279 |
|
| 280 | +//---------- tests around JDK-8251941: broken cleanup with null item |
| 281 | + |
| 282 | + /** |
| 283 | + * Transition not-empty -> empty by items modification |
| 284 | + */ |
| 285 | + @Test |
| 286 | + public void testNullItemRemoveAsLast() { |
| 287 | + model.add(null); |
| 288 | + cell.updateListView(list); |
| 289 | + int last = model.size() - 1; |
| 290 | + cell.updateIndex(last); |
| 291 | + model.remove(last); |
| 292 | + assertOffRangeState(last); |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Sanity: transition not-empty -> not empty by items modification |
| 297 | + */ |
| 298 | + @Test |
| 299 | + public void testNullItemRemoveAsFirst() { |
| 300 | + int first = 0; |
| 301 | + model.add(first, null); |
| 302 | + cell.updateListView(list); |
| 303 | + cell.updateIndex(first); |
| 304 | + model.remove(first); |
| 305 | + assertInRangeState(first); |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * Transition not-empty -> empty by updateIndex |
| 310 | + */ |
| 311 | + @Test |
| 312 | + public void testNullItemUpdateIndexOffRange() { |
| 313 | + model.add(0, null); |
| 314 | + cell.updateListView(list); |
| 315 | + cell.updateIndex(0); |
| 316 | + // update to off range > max |
| 317 | + cell.updateIndex(model.size()); |
| 318 | + assertOffRangeState(model.size()); |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * Transition not-empty -> empty by updateIndex |
| 323 | + */ |
| 324 | + @Test |
| 325 | + public void testNullItemUpdateIndexNegative() { |
| 326 | + model.add(0, null); |
| 327 | + cell.updateListView(list); |
| 328 | + cell.updateIndex(0); |
| 329 | + // update to off range < 0 |
| 330 | + cell.updateIndex(-1); |
| 331 | + assertOffRangeState(-1); |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * Sanity: in-range null item. |
| 336 | + */ |
| 337 | + @Test |
| 338 | + public void testNullItem() { |
| 339 | + // null item in range, verify state |
| 340 | + model.add(0, null); |
| 341 | + cell.updateListView(list); |
| 342 | + cell.updateIndex(0); |
| 343 | + assertInRangeState(0); |
| 344 | + } |
| 345 | + |
| 346 | + /** |
| 347 | + * Asserts state for the given off-range index. |
| 348 | + * @param index |
| 349 | + */ |
| 350 | + protected void assertOffRangeState(int index) { |
| 351 | + assertEquals("off range index", index, cell.getIndex()); |
| 352 | + assertNull("off range cell item must be null", cell.getItem()); |
| 353 | + assertTrue("off range cell must be empty", cell.isEmpty()); |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * Asserts state for the given in-range index. |
| 358 | + * @param index |
| 359 | + */ |
| 360 | + protected void assertInRangeState(int index) { |
| 361 | + assertEquals("in range index", index, cell.getIndex()); |
| 362 | + assertEquals("in range cell item must be same as model item", model.get(index), cell.getItem()); |
| 363 | + assertFalse("in range cell must not be empty", cell.isEmpty()); |
| 364 | + } |
| 365 | + |
| 366 | + |
280 | 367 | /********************************************************************* |
281 | 368 | * Tests for the selection listener * |
282 | 369 | ********************************************************************/ |
|
0 commit comments