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 6.1KB

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