/** Shows {@code hasCss} only when the command is active. */ public UiCommandBinder spin(final HasCss hasCss) { if (command instanceof HasActive) { Property<Boolean> active = ((HasActive) command).active(); b.when(active).is(true).visible(hasCss); } else { throw new IllegalStateException("Not a command that has .active() available"); } return this; }
/** Bind our property to source, two-way, and shows view when our property is true. */ public void to(final HasValue<Boolean> source, HasCss view) { super.to(source); b.when(bp).is(true).show(view); }
@Test public void whenTrueRemovesFromInitialSet() { final BooleanProperty b = booleanProperty("b", false); final ArrayList<String> list = new ArrayList<String>(); list.add("foo"); binder.when(b).is(true).add("foo").to(list); assertThat(list, not(hasItem("foo"))); }
@Test public void whenTrueAddDoesInitialSet() { final BooleanProperty b = booleanProperty("b", true); final ArrayList<String> list = new ArrayList<String>(); binder.when(b).is(true).add("foo").to(list); assertThat(list, contains("foo")); }
@Test public void whenValueAddToListInitializesProperty() { final EnumProperty<Color> color = enumProperty("color"); // null so we'll do the initial set final ArrayList<String> list = new ArrayList<String>(); list.add("foo"); binder.when(color).is(Color.BLUE).add("foo").to(list); assertThat(color.get(), is(Color.BLUE)); assertThat(color.isTouched(), is(false)); assertThat(list, contains("foo")); }
@Test public void whenValueAddToListDoesNotInitializeProperty() { final EnumProperty<Color> color = enumProperty("color"); final ArrayList<String> list = new ArrayList<String>(); // foo isn't in the list, so we leave color alone binder.when(color).is(Color.BLUE).add("foo").to(list); assertThat(color.get(), is(nullValue())); assertThat(color.isTouched(), is(false)); }
@Test public void whenIsFadeInStartsFalse() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).fadeIn(w); // if condition is false, jump right to display none assertThat(w, is(hidden())); assertThat(w.getStyle().getOpacity(), is(nullValue())); }
@Test public void whenTrueEnableLeavesEnabled() { final BooleanProperty b = booleanProperty("b", true); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).enable(w); assertThat(w.isEnabled(), is(true)); }
@Test public void whenTrueEnableIsFalseThenSetsDisabled() { final BooleanProperty b = booleanProperty("b", false); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).enable(w); assertThat(w.isEnabled(), is(false)); }
@Test public void whenTrueDisabledChangesToDisabled() { final BooleanProperty b = booleanProperty("b", true); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).disable(w); assertThat(w.isEnabled(), is(false)); }
@Test public void whenSetToTakesValuesDoesNotTouchOnInitialEvaluation() { final BooleanProperty b = booleanProperty("b", true); final StringProperty s = stringProperty("s"); binder.when(b).is(true).set(s).to("t"); assertThat(s.get(), is("t")); assertThat(s.isTouched(), is(false)); }
@Test public void whenTrueDoesNotFireInitialValueWhenFalse() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).set("c").on(w); assertThat(w, not(hasStyle("c"))); }
@Test public void whenSetToOrElse() { final BooleanProperty b = booleanProperty("b"); binder.when(b).is(true).set(s).toOrElse("a", "b"); assertThat(s.get(), is("b")); b.set(true); assertThat(s.get(), is("a")); b.set(false); assertThat(s.get(), is("b")); }
@Test public void whenTrueEnableChangesToFalseThenSetsDisabled() { final BooleanProperty b = booleanProperty("b", true); final StubFocusWidget w = new StubFocusWidget(); binder.when(b).is(true).enable(w); b.set(false); assertThat(w.isEnabled(), is(false)); }
@Test public void whenSetToHasValue() { final BooleanProperty b = booleanProperty("b", false); binder.when(b).is(true).set(box).to(s); s.set("some new value"); assertThat(box.getValue(), is("")); b.set(true); assertThat(box.getValue(), is("some new value")); }
@Test public void whenIsOrElseOnAttributes() { final BooleanProperty b = booleanProperty("b", false); final StubElement e = new StubElement(); binder.when(b).is(true).setOrElse("c", "d").onAttribute(e, "foo"); assertThat(e.getAttribute("foo"), is("d")); b.set(true); assertThat(e.getAttribute("foo"), is("c")); }
@Test public void whenTrueSetAnotherProperty() { final BooleanProperty b = booleanProperty("b", false); final IntegerProperty i = integerProperty("i", 1); binder.when(b).is(true).set(i).to(10); b.set(true); assertThat(i.get(), is(10)); }
@Test public void whenTrueFiresInitialValueWhenTrue() { final BooleanProperty b = booleanProperty("b", true); final StubWidget w = new StubWidget(); binder.when(b).is(true).set("c").on(w); assertThat(w, hasStyle("c")); }
@Test public void whenIsOrElse() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).setOrElse("c", "d").on(w); assertThat(w, hasStyle("d")); assertThat(w, not(hasStyle("c"))); b.set(true); assertThat(w, hasStyle("c")); assertThat(w, not(hasStyle("d"))); }
@Test public void whenTrueFiresWhenFalseChangesToTrue() { final BooleanProperty b = booleanProperty("b", false); final StubWidget w = new StubWidget(); binder.when(b).is(true).set("c").on(w); b.set(true); assertThat(w, hasStyle("c")); }