You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinderComponentTest.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.vaadin.data;
  2. import static org.junit.Assert.assertEquals;
  3. import java.util.Collections;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import com.vaadin.shared.ui.colorpicker.Color;
  7. import com.vaadin.ui.CheckBox;
  8. import com.vaadin.ui.CheckBoxGroup;
  9. import com.vaadin.ui.ColorPicker;
  10. import com.vaadin.ui.RichTextArea;
  11. import com.vaadin.ui.Slider;
  12. public class BinderComponentTest
  13. extends BinderTestBase<Binder<String>, String> {
  14. enum TestValues {
  15. TRUE, FALSE, FILE_NOT_FOUND
  16. }
  17. @Before
  18. public void setUp() {
  19. binder = new Binder<>();
  20. item = "Foo";
  21. }
  22. @Test
  23. public void slider_bind_null() {
  24. double minValue = 10.5d;
  25. double initialValue = 28.2d;
  26. Slider slider = new Slider();
  27. slider.setResolution(1);
  28. slider.setMin(minValue);
  29. testFieldNullRepresentation(initialValue, slider);
  30. }
  31. @Test
  32. public void colorpicker_bind_null() {
  33. testFieldNullRepresentation(new Color(123, 254, 213),
  34. new ColorPicker());
  35. }
  36. @Test
  37. public void richtextarea_bind_null() {
  38. testFieldNullRepresentation("Test text", new RichTextArea());
  39. }
  40. @Test
  41. public void checkbox_bind_null() {
  42. testFieldNullRepresentation(true, new CheckBox());
  43. }
  44. @Test
  45. public void checkboxgroup_bind_null() {
  46. CheckBoxGroup<TestValues> checkBoxGroup = new CheckBoxGroup<>();
  47. checkBoxGroup.setItems(TestValues.values());
  48. testFieldNullRepresentation(
  49. Collections.singleton(TestValues.FILE_NOT_FOUND),
  50. checkBoxGroup);
  51. }
  52. private <T> void testFieldNullRepresentation(T initialValue,
  53. HasValue<T> field) {
  54. binder.bind(field, t -> null, (str, val) -> {});
  55. field.setValue(initialValue);
  56. assertEquals("Initial value of field unexpected", initialValue,
  57. field.getValue());
  58. binder.setBean(item);
  59. assertEquals("Null representation for field failed",
  60. field.getEmptyValue(), field.getValue());
  61. }
  62. }