public String getSelectedText() { return getItemText(getSelectedIndex()); }
@Test public void propertyToListBoxUpdatesListBoxWhenPropertyChanges() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); s.set("b"); assertThat(listBox.getSelectedIndex(), is(2)); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); }
@Test public void propertyToListBoxDoesNotFailWhenThereAreNoOptions() { final StubListBox listBox = new StubListBox(); final ArrayList<Integer> values = new ArrayList<Integer>(); // s starts out null assertThat(s.get(), is(nullValue())); binder.bind(s).to(listBox, values, new IntegerAdaptor()); // and s is still null assertThat(s.get(), is(nullValue())); // and so we were unable to select anything assertThat(listBox.getSelectedIndex(), is(-1)); }
@Test public void propertyToListBoxWithLambdas() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "1", "2"); binder.bind(s).to(listBox, values, s -> "line " + s); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("line null")); s.set("2"); assertThat(listBox.getSelectedIndex(), is(2)); assertThat(listBox.getSelectedText(), is("line 2")); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("line null")); }
@Test public void bindEnumUpdatesListBoxOnPropertyChange() { final SetValue<Color> v = new SetValue<Color>("v", Color.BLUE); final EnumProperty<Color> e = enumProperty(v); final StubListBox box = new StubListBox(); binder.bind(e).to(box, Color.values()); assertThat(box.getSelectedIndex(), is(1)); e.set(Color.GREEN); assertThat(box.getSelectedIndex(), is(0)); }
@Test public void propertyToListBoxWithTwoLambdas() { final StubListBox listBox = new StubListBox(); final ArrayList<Integer> values = list(null, 1, 2); binder.bind(s).to(// listBox, values, i -> (i == null) ? "int null" : "int " + i, i -> i.toString()); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("int null")); s.set("2"); assertThat(listBox.getSelectedIndex(), is(2)); assertThat(listBox.getSelectedText(), is("int 2")); s.set(null); assertThat(listBox.getSelectedIndex(), is(0)); assertThat(listBox.getSelectedText(), is("int null")); }
@Test public void propertyToListBoxUpdatesListBoxWhenPropertyIsAdaptedAndInitiallySet() { final StubListBox listBox = new StubListBox(); final ArrayList<Integer> values = list(1, 2); // s starts out null assertThat(s.get(), is(nullValue())); binder.bind(s).to(listBox, values, new IntegerAdaptor()); // but is coerced to be a string when we bind assertThat(s.get(), is("1")); assertThat(s.isTouched(), is(false)); // and we made sure to set the listBox to the right value assertThat(listBox.getSelectedIndex(), is(0)); }
@Test public void propertyToListBoxHandlesNullValue() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list(null, "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); listBox.select("b"); assertThat(s.get(), is("b")); // null gets converted to "" (otherwise it shows up as "null"), so select that listBox.select(""); assertThat(s.get(), is(nullValue())); }
@Test public void propertyToListBoxHandlesEmptyString() { final StubListBox listBox = new StubListBox(); final ArrayList<String> values = list("", "a", "b"); binder.bind(s).to(listBox, values); assertThat(listBox.getSelectedIndex(), is(0)); listBox.select("b"); assertThat(s.get(), is("b")); listBox.select(""); assertThat(s.get(), is("")); }
@Test public void listPropertyToListBoxDoesNotResetPropertyWhenTheListIsNull() { final StubListBox listBox = new StubListBox(); // given s starts out set s.set("3"); // when we bind to a null list property final ListProperty<Integer> values = listProperty("values", null); binder.bind(s).to(listBox, values, new IntegerAdaptor()); // then s does not change assertThat(s.get(), is("3")); // and the select box is blank assertThat(listBox.getSelectedIndex(), is(-1)); // when later we do have values values.set(list(1, 2, 3)); // then s still does not change assertThat(s.get(), is("3")); // and we can show it's current value in the list assertThat(listBox.getSelectedIndex(), is(2)); }
@Test public void listPropertyToListBoxDoesNotFailWhenThereAreNoOptions() { final StubListBox listBox = new StubListBox(); final ListProperty<Integer> values = listProperty("values"); // s starts out null assertThat(s.get(), is(nullValue())); binder.bind(s).to(listBox, values, new IntegerAdaptor()); // and s is still null assertThat(s.get(), is(nullValue())); // and so we were unable to select anything assertThat(listBox.getSelectedIndex(), is(-1)); // when we finally have values values.set(list(2, 3)); // then since we're null, we get assigned to the first assertThat(s.get(), is("2")); }
@Test public void bindEnumAutoSelectsFirstValueIfNull() { final SetValue<Color> v = new SetValue<Color>("v", null); final EnumProperty<Color> e = enumProperty(v); final StubListBox box = new StubListBox(); binder.bind(e).to(box, Color.values()); assertThat(box.getSelectedIndex(), is(0)); assertThat(v.get(), is(Color.GREEN)); }
@Test public void bindEnumSetsInitialValueToOtherValue() { final SetValue<Color> v = new SetValue<Color>("v", Color.BLUE); final EnumProperty<Color> e = enumProperty(v); final StubListBox box = new StubListBox(); binder.bind(e).to(box, Color.values()); assertThat(box.getSelectedIndex(), is(1)); }
@Test public void bindEnumSetsInitialValue() { final SetValue<Color> v = new SetValue<Color>("v", Color.GREEN); final EnumProperty<Color> e = enumProperty(v); final StubListBox box = new StubListBox(); binder.bind(e).to(box, Color.values()); assertThat(box.getSelectedIndex(), is(0)); }
@Test public void listPropertyToListBoxResetsPropertyToNullWhenSelectedValueGoesAway() { final StubListBox listBox = new StubListBox(); // given we're binding against a list of values that includes null final ListProperty<Integer> values = listProperty("values", list(null, 1, 2)); // and s itself starts out null assertThat(s.get(), is(nullValue())); // when we bind binder.bind(s).to(listBox, values, new IntegerAdaptor()); // then s is left as is assertThat(s.get(), is(nullValue())); // when later we select something listBox.select("1"); assertThat(s.get(), is("1")); // and our selected value goes away values.set(list(null, 3, 4)); // then the property goes back to null assertThat(s.get(), is(nullValue())); assertThat(listBox.getSelectedIndex(), is(0)); }