@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 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 listPropertyToListBoxResetsPropertyWhenSelectedValueGoesAway() { final StubListBox listBox = new StubListBox(); // given we're binding against a list of values that does not include null final ListProperty<Integer> values = listProperty("values", list(1, 2)); // and s starts out null assertThat(s.get(), is(nullValue())); // when we bind binder.bind(s).to(listBox, values, new IntegerAdaptor()); // then s is set to the 1st option assertThat(s.get(), is("1")); assertThat(listBox.getSelectedText(), is("1")); // when later our selected value goes away values.set(list(3, 4)); // then the property is again set to the 1st option assertThat(s.get(), is("3")); assertThat(listBox.getSelectedText(), is("3")); }