您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ComponentTestCase.java 5.5KB

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