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.

TestSizeableIncomponents.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package com.vaadin.tests;
  2. import java.io.File;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import com.vaadin.server.LegacyApplication;
  8. import com.vaadin.server.ThemeResource;
  9. import com.vaadin.ui.AbstractComponent;
  10. import com.vaadin.ui.Button;
  11. import com.vaadin.ui.Component;
  12. import com.vaadin.ui.ComponentContainer;
  13. import com.vaadin.ui.Embedded;
  14. import com.vaadin.ui.HorizontalLayout;
  15. import com.vaadin.ui.Label;
  16. import com.vaadin.ui.LegacyWindow;
  17. import com.vaadin.ui.Panel;
  18. import com.vaadin.ui.VerticalLayout;
  19. import com.vaadin.v7.data.Container;
  20. import com.vaadin.v7.data.util.IndexedContainer;
  21. import com.vaadin.v7.shared.ui.combobox.FilteringMode;
  22. import com.vaadin.v7.ui.AbstractSelect;
  23. import com.vaadin.v7.ui.ComboBox;
  24. import com.vaadin.v7.ui.Table;
  25. public class TestSizeableIncomponents extends LegacyApplication {
  26. private IndexedContainer cont;
  27. private ComboBox select;
  28. private Button prev;
  29. private Button next;
  30. private VerticalLayout testPanelLayout;
  31. private Panel testPanel;
  32. @Override
  33. public void init() {
  34. initComponentList();
  35. LegacyWindow w = new LegacyWindow();
  36. setMainWindow(w);
  37. setTheme("tests-components");
  38. final VerticalLayout main = new VerticalLayout();
  39. w.setContent(main);
  40. select = new ComboBox();
  41. select.setImmediate(true);
  42. select.setFilteringMode(FilteringMode.CONTAINS);
  43. select.setWidth("400px");
  44. prev = new Button("<<-|");
  45. prev.addClickListener(event -> {
  46. Object cur = select.getValue();
  47. Testable prev = (Testable) cont.prevItemId(cur);
  48. if (prev == null) {
  49. getMainWindow().showNotification("No more test cases");
  50. } else {
  51. getMainWindow().showNotification(
  52. "Selected test:" + prev.getTestableName());
  53. select.setValue(prev);
  54. select.markAsDirty();
  55. }
  56. });
  57. next = new Button("|->>");
  58. next.addClickListener(event -> {
  59. Object cur = select.getValue();
  60. Testable next = (Testable) cont.nextItemId(cur);
  61. if (next == null) {
  62. getMainWindow().showNotification("No more test cases");
  63. } else {
  64. getMainWindow().showNotification(
  65. "Selected test:" + next.getTestableName());
  66. select.setValue(next);
  67. select.markAsDirty();
  68. }
  69. });
  70. HorizontalLayout controllers = new HorizontalLayout();
  71. controllers.addComponent(prev);
  72. controllers.addComponent(select);
  73. controllers.addComponent(next);
  74. main.addComponent(controllers);
  75. select.setContainerDataSource(cont);
  76. select.addValueChangeListener(event -> {
  77. Testable t = (Testable) select.getValue();
  78. if (t != null) {
  79. testPanelLayout.removeAllComponents();
  80. try {
  81. Component c = t.getComponent();
  82. if (c != null) {
  83. testPanelLayout.addComponent(c);
  84. }
  85. } catch (InstantiationException | IllegalAccessException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. });
  90. testPanelLayout = new VerticalLayout();
  91. testPanel = new Panel(testPanelLayout);
  92. testPanel.setSizeFull();
  93. testPanel.setStyleName("testable");
  94. main.addComponent(testPanel);
  95. main.setExpandRatio(testPanel, 1);
  96. }
  97. private void initComponentList() {
  98. cont = new IndexedContainer();
  99. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  100. URL dir = cl.getResource("com/vaadin/ui");
  101. String[] list2 = (new File(dir.getFile())).list();
  102. for (String f : list2) {
  103. if (f.endsWith(".class") && (f.indexOf("CustomComponent") == -1)
  104. && (f.indexOf("Window") == -1)) {
  105. f = f.replaceAll(".class", "");
  106. String className = "com.vaadin.ui." + f;
  107. Class<?> c;
  108. try {
  109. c = Class.forName(className);
  110. Object o = c.newInstance();
  111. if (o instanceof Component) {
  112. Testable t = new Testable(c);
  113. cont.addItem(t);
  114. t = new Testable(c);
  115. t.addConfiguration(new Configuration("100px*100px") {
  116. @Override
  117. void configure(Component c) {
  118. c.setWidth("60px");
  119. c.setHeight("60px");
  120. }
  121. });
  122. t = new Testable(c);
  123. t.addConfiguration(new Configuration("Width 50em") {
  124. @Override
  125. void configure(Component c) {
  126. c.setWidth("50em");
  127. }
  128. });
  129. cont.addItem(t);
  130. t = new Testable(c);
  131. t.addConfiguration(new Configuration("Height 7cm") {
  132. @Override
  133. void configure(Component c) {
  134. c.setHeight("7cm");
  135. }
  136. });
  137. cont.addItem(t);
  138. t = new Testable(c) {
  139. @Override
  140. public Component getComponent()
  141. throws InstantiationException,
  142. IllegalAccessException {
  143. Component c = super.getComponent();
  144. VerticalLayout pl = new VerticalLayout();
  145. pl.setMargin(true);
  146. Panel p = new Panel(
  147. "Wrapper panel (400px*400px)", pl);
  148. p.setContent(new VerticalLayout());
  149. p.setWidth("400px");
  150. p.setHeight("400px");
  151. pl.addComponent(c);
  152. p.addStyleName("testablew");
  153. p.addStyleName("testable");
  154. return p;
  155. }
  156. };
  157. t.addConfiguration(new Configuration("100%*100%") {
  158. @Override
  159. void configure(Component c) {
  160. c.setSizeFull();
  161. }
  162. });
  163. cont.addItem(t);
  164. }
  165. } catch (ClassNotFoundException | InstantiationException
  166. | IllegalAccessException e) {
  167. // e.printStackTrace();
  168. }
  169. }
  170. }
  171. }
  172. class Testable {
  173. private Class<?> classToTest;
  174. private List<Configuration> configurations = new ArrayList<>();
  175. Testable(Class<?> c) {
  176. classToTest = c;
  177. }
  178. public void addConfiguration(Configuration conf) {
  179. configurations.add(conf);
  180. }
  181. public String getTestableName() {
  182. StringBuilder sb = new StringBuilder();
  183. sb.append(classToTest.getName().replaceAll("com.vaadin.ui.", ""));
  184. sb.append('[');
  185. for (Iterator<Configuration> i = configurations.iterator(); i
  186. .hasNext();) {
  187. sb.append((i.next()).getDescription());
  188. if (i.hasNext()) {
  189. sb.append(',');
  190. }
  191. }
  192. sb.append(']');
  193. return sb.toString();
  194. }
  195. /**
  196. * Instantiates and populates component with test data to be ready for
  197. * testing.
  198. *
  199. * @return
  200. * @throws InstantiationException
  201. * @throws IllegalAccessException
  202. */
  203. public Component getComponent()
  204. throws InstantiationException, IllegalAccessException {
  205. Component c = (Component) classToTest.newInstance();
  206. if (c instanceof Button) {
  207. ((AbstractComponent) c).setCaption("test");
  208. }
  209. if (AbstractSelect.class.isAssignableFrom(c.getClass())) {
  210. if (c instanceof Table) {
  211. Table new_name = (Table) c;
  212. new_name.setContainerDataSource(
  213. TestForTablesInitialColumnWidthLogicRendering
  214. .getTestTable(5, 100)
  215. .getContainerDataSource());
  216. } else {
  217. AbstractSelect new_name = (AbstractSelect) c;
  218. Container cont = TestForTablesInitialColumnWidthLogicRendering
  219. .getTestTable(2, 8).getContainerDataSource();
  220. new_name.setContainerDataSource(cont);
  221. new_name.setItemCaptionPropertyId(
  222. cont.getContainerPropertyIds().iterator().next());
  223. }
  224. } else if (c instanceof ComponentContainer) {
  225. ComponentContainer new_name = (ComponentContainer) c;
  226. new_name.addComponent(
  227. new Label("component 1 in test container"));
  228. new_name.addComponent(new Button("component 2"));
  229. } else if (c instanceof Embedded) {
  230. Embedded em = (Embedded) c;
  231. em.setSource(new ThemeResource("test.png"));
  232. } else if (c instanceof Label) {
  233. ((Label) c).setValue("Test label");
  234. }
  235. for (Configuration conf : configurations) {
  236. conf.configure(c);
  237. }
  238. return c;
  239. }
  240. @Override
  241. public String toString() {
  242. return getTestableName();
  243. }
  244. }
  245. public abstract class Configuration {
  246. private String description = "";
  247. Configuration(String description) {
  248. this.description = description;
  249. }
  250. public String getDescription() {
  251. return description;
  252. }
  253. abstract void configure(Component c);
  254. @Override
  255. public String toString() {
  256. return getDescription();
  257. }
  258. }
  259. }