@Test public void shownSuccessWhenDisplayBlock() { StubTextBox t = new StubTextBox(); t.getStyle().setDisplay(Display.BLOCK); assertThat(t, is(shown())); }
@Test public void testEventsCausedWithinHandlersFireImmediately() { final StubTextBox b = new StubTextBox(); b.fireAttached(); final boolean[] inClickHandler = { false }; final boolean[] wasInClickHandler = { false }; b.addClickHandler(new ClickHandler() { public void onClick(ClickEvent arg0) { inClickHandler[0] = true; WidgetUtils.focus(b); inClickHandler[0] = false; } }); b.addFocusHandler(new FocusHandler() { public void onFocus(FocusEvent arg0) { wasInClickHandler[0] = inClickHandler[0]; } }); b.click(); assertTrue(wasInClickHandler[0]); } }
@Test public void hasStyleSuccess() { StubTextBox t = new StubTextBox(); t.addStyleName("a"); t.addStyleName("b"); assertThat(t, hasStyle("a")); }
@Override public IsTextBox newTextBox() { return new StubTextBox(); }
@Test public void shouldOnlyUpdateSourceOnBlur() { final DoubleProperty p = new DoubleProperty(new SetValue<Double>("p")); binder.bind(p.asString()).to(box); box.focus(); box.typeEachWithoutBlur("123"); assertThat(box.getValue(), is("123")); assertThat(p.getValue(), is(123.0)); box.typeEachWithoutBlur("4"); assertThat(box.getValue(), is("1234")); assertThat(p.getValue(), is(1234.0)); box.blur(); assertThat(box.getValue(), is("1234.0")); assertThat(p.getValue(), is(1234.0)); }
@Test public void setBadStringThatIsBoundToATextBox() { IntegerProperty i = integerProperty("i", 1).req(); Binder binder = new Binder(); StubTextBox box = new StubTextBox(); binder.bind(i.asString()).to(box); listenTo(i); box.type("a"); assertThat(box.getText(), is("a")); assertMessages("I must be an integer"); assertThat(i.get(), is(1)); assertThat(box.getText(), is("a")); i.set(2); assertNoMessages(); box.type(""); assertMessages("I is required"); }
@Test public void onKeyDownFiltered() { final StubTextBox b = new StubTextBox(); binder.onKeyDown(b, KeyCodes.KEY_ENTER).set(s).to("asdf"); b.keyDown('a'); assertThat(s.get(), is(nullValue())); b.keyDown(KeyCodes.KEY_ENTER); assertThat(s.get(), is("asdf")); }
@Test public void debugId() { StubTextBox t = new StubTextBox(); TakesValue<String> id = TakesValues.debugId(t); id.setValue("foo"); assertThat(t.getIsElement().getAttribute("id"), is("foo")); assertThat(id.getValue(), is("foo")); }
@Test public void propertyToStringTrimsToNull() { binder.bind(s).to(box); box.typeEachWithoutBlur(" "); assertThat(box.getValue(), is(" ")); // to the property the value is still null, so it doesn't update the text box assertThat(s.get(), is(nullValue())); box.blur(); assertThat(box.getValue(), is("")); assertThat(s.get(), is(nullValue())); }
@Test public void enhanceIgnoresTabKeyUpEvent() { final StubTextBox b = new StubTextBox(); final StringProperty s = stringProperty("s"); binder.bind(s).to(b); binder.enhance(b); b.keyUp(KEY_TAB); assertThat(s.get(), is(nullValue())); }
@Test public void changedWithBinding() { Binder b = new Binder(); StringProperty s = stringProperty("s"); StubTextBox box = new StubTextBox(); b.bind(s).to(box); final Property<Boolean> c = s.changed(); assertThat(c.get(), is(false)); box.typeEach("foo"); assertThat(c.get(), is(true)); s.set("foo"); assertThat(c.get(), is(true)); } }
@Test public void shouldSetInitialValueEvenIfBlurred() { // given a property that is not set final DoubleProperty p = new DoubleProperty(new SetValue<Double>("p")); // and we bind to it binder.bind(p.asString()).to(box); // and immediately focus (e.g. because it's the first form field on the page) box.focus(); // when we bind the initial value from the server p.setInitialValue(2.0); // then we should go ahead and update the box value assertThat(box.getValue(), is("2.0")); }
@Test public void onClickRun() { final Boolean[] ran = { false }; binder.onClick(box).execute(new Runnable() { public void run() { ran[0] = true; } }); box.click(); assertThat(ran[0], is(true)); }
@Test public void shouldTouchPropertyOnBlur() { binder.bind(s).to(box); box.blur(); assertThat(s.isTouched(), is(true)); }
@Test public void onClickPreventDefault() { binder.onClick(box).preventDefault(); StubClickEvent e = new StubClickEvent(); box.fireEvent(e); assertThat(e.prevented, is(true)); }
@Test public void propertyToWidgetFiresWidgetChange() { binder.bind(s).to(box); final boolean[] changed = { false }; box.addValueChangeHandler(new ValueChangeHandler<String>() { public void onValueChange(final ValueChangeEvent<String> event) { changed[0] = true; } }); s.set("test"); assertThat(changed[0], is(true)); }
@Test public void shownSuccessWhenUnset() { StubTextBox t = new StubTextBox(); assertThat(t, is(shown())); }
@Test public void onKeyDown() { final StubTextBox b = new StubTextBox(); binder.onKeyDown(b).set(s).to("asdf"); b.keyDown('a'); assertThat(s.get(), is("asdf")); }
@Test public void clickableWidgetToProperty() { binder.onClick(box).set(s).to("gotclicked"); box.click(); assertThat(s.get(), is("gotclicked")); }
@Test public void widgetsAreChangedOnBlur() { employee.firstName.req(); p.add(new TextBoxFormLine(employee.firstName)); tb("mpv-hb-1").blur(); assertThat(errors("mpv-hb-2"), hasErrors("First Name is required")); }