Skip to content

Commit 4ec163d

Browse files
Jeanette Winzenburgaghaisas
Jeanette Winzenburg
authored andcommitted
8242001: ChoiceBox: must update value on setting SelectionModel, part2
Reviewed-by: aghaisas
1 parent 236e2d6 commit 4ec163d

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ public ChoiceBox(ObservableList<T> items) {
182182
oldSM = sm;
183183
if (sm != null) {
184184
sm.selectedItemProperty().addListener(selectedItemListener);
185-
// FIXME JDK-8242001 - must sync to model state always
186-
if (sm.getSelectedItem() != null && ! valueProperty().isBound()) {
185+
if (!valueProperty().isBound()) {
187186
ChoiceBox.this.setValue(sm.getSelectedItem());
188187
}
189188
}

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

+38
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333

3434
import static org.junit.Assert.*;
3535

36+
import javafx.beans.property.SimpleStringProperty;
37+
import javafx.beans.property.StringProperty;
3638
import javafx.collections.FXCollections;
3739
import javafx.scene.Node;
3840
import javafx.scene.Scene;
3941
import javafx.scene.control.ChoiceBox;
42+
import javafx.scene.control.ChoiceBoxShim;
4043
import javafx.scene.control.ContextMenu;
4144
import javafx.scene.control.Control;
4245
import javafx.scene.control.MenuItem;
@@ -314,6 +317,41 @@ public void testSyncedClearSelectionUncontained() {
314317
assertEquals(uncontained, box.getValue());
315318
}
316319

320+
//------------- tests for JDK-8242001
321+
322+
/**
323+
* Testing JDK-8242001: box value not updated on replacing selection model.
324+
*
325+
* Happens if replacing.selectedItem == null
326+
*
327+
*/
328+
@Test
329+
public void testSyncedContainedValueReplaceSMEmpty() {
330+
box.setValue(box.getItems().get(1));
331+
SingleSelectionModel<String> replaceSM = ChoiceBoxShim.get_ChoiceBoxSelectionModel(box);
332+
assertNull(replaceSM.getSelectedItem());
333+
box.setSelectionModel(replaceSM);
334+
assertEquals(replaceSM.getSelectedItem(), box.getValue());
335+
}
336+
337+
@Test
338+
public void testSyncedUncontainedValueReplaceSMEmpty() {
339+
box.setValue(uncontained);
340+
SingleSelectionModel<String> replaceSM = ChoiceBoxShim.get_ChoiceBoxSelectionModel(box);
341+
assertNull(replaceSM.getSelectedItem());
342+
box.setSelectionModel(replaceSM);
343+
assertEquals(replaceSM.getSelectedItem(), box.getValue());
344+
}
345+
346+
@Test
347+
public void testSyncedBoundValueReplaceSMEmpty() {
348+
StringProperty valueSource = new SimpleStringProperty("stickyValue");
349+
box.valueProperty().bind(valueSource);
350+
SingleSelectionModel<String> replaceSM = ChoiceBoxShim.get_ChoiceBoxSelectionModel(box);
351+
assertNull(replaceSM.getSelectedItem());
352+
box.setSelectionModel(replaceSM);
353+
assertEquals(valueSource.get(), box.getValue());
354+
}
317355

318356
//----------- setup and sanity test for initial state
319357

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ protected void startApp(Parent root) {
342342
assertEquals("Orange", box.getValue());
343343
}
344344

345-
@Test public void ensureValueIsUpdatedByCorrectSelectionModelWhenSelectionModelIsChanged() {
345+
@Test
346+
public void ensureValueIsUpdatedByCorrectSelectionModelWhenSelectionModelIsChanged() {
346347
box.getItems().addAll("Apple", "Orange", "Banana");
347348
SelectionModel sm1 = box.getSelectionModel();
348349
sm1.select(1);
@@ -351,7 +352,10 @@ protected void startApp(Parent root) {
351352
box.setSelectionModel(sm2);
352353

353354
sm1.select(2); // value should not change as we are using old SM
354-
assertEquals("Orange", box.getValue());
355+
// was: incorrect test assumption
356+
// - setting the new model (with null selected item) changed the value to null
357+
// assertEquals("Orange", box.getValue());
358+
assertEquals(sm2.getSelectedItem(), box.getValue());
355359

356360
sm2.select(0); // value should change, as we are using new SM
357361
assertEquals("Apple", box.getValue());

0 commit comments

Comments
 (0)