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

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