Skip to content

Commit c86bd35

Browse files
author
Jeanette Winzenburg
committed
8251941: ListCell: visual artifact when items contain null values
Reviewed-by: aghaisas
1 parent 23ad8f4 commit c86bd35

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

modules/javafx.controls/src/main/java/javafx/scene/control/ListCell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -483,7 +483,7 @@ private void updateItem(int oldIndex) {
483483
// refer to Ensemble8PopUpTree.png - in this case the arrows are being
484484
// shown as the new cells are instantiated with the arrows in the
485485
// children list, and are only hidden in updateItem.
486-
if ((!isEmpty && oldValue != null) || firstRun) {
486+
if (!isEmpty || firstRun) {
487487
updateItem(null, true);
488488
firstRun = false;
489489
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/ListCellTest.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -277,6 +277,93 @@ public class ListCellTest {
277277
assertEquals("Water", cell.getItem());
278278
}
279279

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+
280367
/*********************************************************************
281368
* Tests for the selection listener *
282369
********************************************************************/

0 commit comments

Comments
 (0)