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.

ComponentTestCase.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.vaadin.tests.components;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import com.vaadin.data.HasValue;
  6. import com.vaadin.server.VaadinRequest;
  7. import com.vaadin.ui.AbstractComponent;
  8. import com.vaadin.ui.Alignment;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.CheckBox;
  11. import com.vaadin.ui.Component;
  12. import com.vaadin.ui.HorizontalLayout;
  13. import com.vaadin.v7.data.Item;
  14. import com.vaadin.v7.ui.Field;
  15. import com.vaadin.v7.ui.NativeSelect;
  16. public abstract class ComponentTestCase<T extends AbstractComponent>
  17. extends AbstractComponentTestCase<T> {
  18. protected static final Object CAPTION = "caption";
  19. private HorizontalLayout actionLayout;
  20. @Override
  21. protected final void setup(VaadinRequest request) {
  22. // Create action layout so it appears before the components
  23. actionLayout = createActionLayout();
  24. addComponent(actionLayout);
  25. super.setup(request);
  26. // Create actions and add to layout
  27. populateActionLayout();
  28. }
  29. protected void populateActionLayout() {
  30. for (Component c : createActions()) {
  31. addAction(c);
  32. }
  33. }
  34. private void addAction(Component c) {
  35. actionLayout.addComponent(c);
  36. actionLayout.setComponentAlignment(c, Alignment.BOTTOM_LEFT);
  37. }
  38. /**
  39. * Override to provide custom actions for the test case.
  40. *
  41. * @param actions
  42. * Array with default actions. Add custom actions to this. Never
  43. * null.
  44. */
  45. protected void createCustomActions(List<Component> actions) {
  46. }
  47. /**
  48. * Method that creates the "actions" shown in the upper part of the screen.
  49. * Override this only if you do not want the default actions. Custom actions
  50. * can be added through #createCustomActions();
  51. *
  52. * @return A List with actions to which more actions can be added.
  53. */
  54. protected List<Component> createActions() {
  55. List<Component> actions = new ArrayList<>();
  56. actions.add(createEnabledAction(true));
  57. actions.add(createReadonlyAction(false));
  58. actions.add(createErrorIndicatorAction(false));
  59. if (HasValue.class.isAssignableFrom(getTestClass())
  60. || Field.class.isAssignableFrom(getTestClass())) {
  61. actions.add(createRequiredAction(false));
  62. }
  63. createCustomActions(actions);
  64. return actions;
  65. }
  66. private HorizontalLayout createActionLayout() {
  67. HorizontalLayout actionLayout = new HorizontalLayout();
  68. actionLayout.setSpacing(true);
  69. actionLayout.setMargin(true);
  70. return actionLayout;
  71. }
  72. protected Component createErrorIndicatorAction(boolean initialState) {
  73. return createBooleanAction("Error indicators", initialState,
  74. errorIndicatorCommand);
  75. }
  76. protected Component createEnabledAction(boolean initialState) {
  77. return createBooleanAction("Enabled", initialState, enabledCommand);
  78. }
  79. protected Component createReadonlyAction(boolean initialState) {
  80. return createBooleanAction("Readonly", initialState, readonlyCommand);
  81. }
  82. protected Component createRequiredAction(boolean initialState) {
  83. return createBooleanAction("Required", initialState, requiredCommand);
  84. }
  85. protected Component createBooleanAction(String caption,
  86. boolean initialState, final Command<T, Boolean> command) {
  87. CheckBox checkBox = new CheckBox(caption);
  88. checkBox.addValueChangeListener(
  89. event -> doCommand(command, event.getValue()));
  90. checkBox.setValue(initialState);
  91. checkBox.setId("checkboxaction-" + caption);
  92. // Set default value for all components
  93. doCommand(command, initialState);
  94. return checkBox;
  95. }
  96. protected Component createButtonAction(String caption,
  97. final Command<T, Boolean> command) {
  98. Button button = new Button(caption);
  99. button.setData(Boolean.FALSE);
  100. button.addClickListener(event -> {
  101. Button b = event.getButton();
  102. boolean state = (Boolean) b.getData();
  103. b.setData(!state);
  104. doCommand(command, state);
  105. });
  106. button.setId("buttonaction-" + caption);
  107. return button;
  108. }
  109. protected <TYPE> Component createSelectAction(String caption,
  110. LinkedHashMap<String, TYPE> options, String initialValue,
  111. final Command<T, TYPE> command) {
  112. final String CAPTION = "caption";
  113. final String VALUE = "value";
  114. final NativeSelect select = new NativeSelect(caption);
  115. select.addContainerProperty(CAPTION, String.class, "");
  116. select.addContainerProperty(VALUE, Object.class, "");
  117. select.setItemCaptionPropertyId(CAPTION);
  118. select.setNullSelectionAllowed(false);
  119. select.addValueChangeListener(event -> {
  120. Object itemId = event.getProperty().getValue();
  121. Item item = select.getItem(itemId);
  122. @SuppressWarnings("unchecked")
  123. TYPE value = (TYPE) item.getItemProperty(VALUE).getValue();
  124. doCommand(command, value);
  125. });
  126. for (String itemCaption : options.keySet()) {
  127. Object itemId = new Object();
  128. Item i = select.addItem(itemId);
  129. i.getItemProperty(CAPTION).setValue(itemCaption);
  130. i.getItemProperty(VALUE).setValue(options.get(itemCaption));
  131. if (itemCaption.equals(initialValue)) {
  132. select.setValue(itemId);
  133. }
  134. }
  135. select.setId("selectaction-" + caption);
  136. select.setImmediate(true);
  137. return select;
  138. }
  139. }