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.

AbstractComponentTestCase.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.vaadin.tests.components;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Locale;
  6. import com.vaadin.data.HasValue;
  7. import com.vaadin.server.Resource;
  8. import com.vaadin.server.ThemeResource;
  9. import com.vaadin.server.UserError;
  10. import com.vaadin.server.VaadinRequest;
  11. import com.vaadin.ui.AbstractComponent;
  12. import com.vaadin.ui.Layout.SpacingHandler;
  13. import com.vaadin.v7.ui.AbstractLegacyComponent;
  14. import com.vaadin.v7.ui.Field;
  15. public abstract class AbstractComponentTestCase<T extends AbstractComponent>
  16. extends AbstractReindeerTestUI {
  17. protected static final ThemeResource ICON_16_HELP_PNG_CACHEABLE = cacheableThemeResource(
  18. "../runo/icons/16/help.png");
  19. protected static final ThemeResource ICON_16_FOLDER_PNG_CACHEABLE = cacheableThemeResource(
  20. "../runo/icons/16/folder.png");
  21. protected static final ThemeResource ICON_16_ERROR_PNG_CACHEABLE = cacheableThemeResource(
  22. "../runo/icons/16/error.png");
  23. protected static final ThemeResource ICON_16_USER_PNG_CACHEABLE = cacheableThemeResource(
  24. "../runo/icons/16/user.png");
  25. protected static final ThemeResource ICON_16_USER_PNG_UNCACHEABLE = uncacheableThemeResource(
  26. "../runo/icons/16/user.png");
  27. protected static final ThemeResource ICON_32_ATTENTION_PNG_CACHEABLE = cacheableThemeResource(
  28. "../runo/icons/32/attention.png");
  29. protected static final ThemeResource ICON_32_ATTENTION_PNG_UNCACHEABLE = uncacheableThemeResource(
  30. "../runo/icons/32/attention.png");
  31. protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_CACHEABLE = cacheableThemeResource(
  32. "../runo/icons/64/email-reply.png");
  33. protected static final ThemeResource ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE = uncacheableThemeResource(
  34. "../runo/icons/64/email-reply.png");
  35. private List<T> testComponents = new ArrayList<>();
  36. protected abstract Class<T> getTestClass();
  37. protected static ThemeResource uncacheableThemeResource(
  38. String resourceLocation) {
  39. return new ThemeResource(resourceLocation + "?" + new Date().getTime());
  40. }
  41. protected static ThemeResource cacheableThemeResource(
  42. String resourceLocation) {
  43. return new ThemeResource(resourceLocation);
  44. }
  45. protected abstract void initializeComponents();
  46. @Override
  47. protected void setup(VaadinRequest request) {
  48. ((SpacingHandler) getLayout()).setSpacing(true);
  49. // Create Components
  50. initializeComponents();
  51. }
  52. @Override
  53. protected Integer getTicketNumber() {
  54. return null;
  55. }
  56. protected void addTestComponent(T c) {
  57. testComponents.add(c);
  58. addComponent(c);
  59. }
  60. protected List<T> getTestComponents() {
  61. return testComponents;
  62. }
  63. public interface Command<T, VALUETYPE extends Object> {
  64. public void execute(T c, VALUETYPE value, Object data);
  65. }
  66. private String errorMessage = null;
  67. /* COMMANDS */
  68. protected Command<T, String> widthCommand = (t, value, data) -> t
  69. .setWidth(value);
  70. protected Command<T, String> heightCommand = (t, value, data) -> t
  71. .setHeight(value);
  72. protected Command<T, Boolean> enabledCommand = (c, enabled, data) -> c
  73. .setEnabled(enabled);
  74. protected Command<T, Boolean> errorIndicatorCommand = (c, enabled,
  75. data) -> {
  76. if (enabled) {
  77. c.setComponentError(new UserError(errorMessage));
  78. } else {
  79. c.setComponentError(null);
  80. }
  81. };
  82. protected Command<T, String> errorMessageCommand = (c, value, data) -> {
  83. errorMessage = value;
  84. if (c.getComponentError() != null) {
  85. errorIndicatorCommand.execute(c, true, null);
  86. }
  87. };
  88. // TODO Move to AbstractFieldTestCase
  89. protected Command<T, Boolean> requiredCommand = (c, enabled, data) -> {
  90. if (c instanceof HasValue) {
  91. ((HasValue) c).setRequiredIndicatorVisible(enabled);
  92. } else if (c instanceof Field) {
  93. ((Field) c).setRequired(enabled);
  94. } else {
  95. throw new IllegalArgumentException(c.getClass().getName()
  96. + " is not a field and cannot be set to required");
  97. }
  98. };
  99. protected Command<T, String> requiredErrorMessageCommand = (c, value,
  100. data) -> ((Field<?>) c).setRequiredError(value);
  101. protected Command<T, String> descriptionCommand = (c, value, data) -> c
  102. .setDescription(value);
  103. protected Command<T, Boolean> readonlyCommand = (c, enabled, data) -> {
  104. if (c instanceof HasValue) {
  105. ((HasValue) c).setReadOnly(enabled);
  106. } else if (c instanceof AbstractLegacyComponent) {
  107. ((AbstractLegacyComponent) c).setReadOnly(enabled);
  108. }
  109. };
  110. protected Command<T, Boolean> visibleCommand = (c, enabled, data) -> c
  111. .setVisible(enabled);
  112. protected Command<T, Resource> iconCommand = (c, value, data) -> c
  113. .setIcon(value);
  114. protected Command<T, String> captionCommand = (c, value, data) -> c
  115. .setCaption(value);
  116. protected Command<T, Locale> localeCommand = (c, value, data) -> c
  117. .setLocale(value);
  118. protected <VALUET> void doCommand(Command<T, VALUET> command,
  119. VALUET value) {
  120. doCommand(command, value, null);
  121. }
  122. protected <VALUET> void doCommand(Command<T, VALUET> command, VALUET value,
  123. Object data) {
  124. for (T c : getTestComponents()) {
  125. command.execute(c, value, data);
  126. }
  127. }
  128. protected <VALUET> void doCommand(String commandName,
  129. Command<T, VALUET> command, VALUET value) {
  130. doCommand(commandName, command, value, null);
  131. }
  132. protected <VALUET> void doCommand(String commandName,
  133. Command<T, VALUET> command, VALUET value, Object data) {
  134. doCommand(command, value, data);
  135. }
  136. protected Command<T, String> styleNameCommand = (c, value, data) -> c
  137. .setStyleName(value);
  138. protected Command<T, String> primaryStyleNameCommand = (c, value, data) -> c
  139. .setPrimaryStyleName(value);
  140. @Override
  141. protected String getTestDescription() {
  142. return "Generic test case for " + getTestClass().getSimpleName();
  143. }
  144. }