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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests;
  17. import java.io.File;
  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import com.vaadin.data.Container;
  22. import com.vaadin.data.Property.ValueChangeEvent;
  23. import com.vaadin.data.util.IndexedContainer;
  24. import com.vaadin.server.LegacyApplication;
  25. import com.vaadin.server.ThemeResource;
  26. import com.vaadin.shared.ui.combobox.FilteringMode;
  27. import com.vaadin.ui.AbstractComponent;
  28. import com.vaadin.ui.AbstractSelect;
  29. import com.vaadin.ui.Button;
  30. import com.vaadin.ui.Button.ClickEvent;
  31. import com.vaadin.ui.ComboBox;
  32. import com.vaadin.ui.Component;
  33. import com.vaadin.ui.ComponentContainer;
  34. import com.vaadin.ui.Embedded;
  35. import com.vaadin.ui.HorizontalLayout;
  36. import com.vaadin.ui.Label;
  37. import com.vaadin.ui.LegacyWindow;
  38. import com.vaadin.ui.Panel;
  39. import com.vaadin.ui.Table;
  40. import com.vaadin.ui.VerticalLayout;
  41. public class TestSizeableIncomponents extends LegacyApplication {
  42. private IndexedContainer cont;
  43. private ComboBox select;
  44. private Button prev;
  45. private Button next;
  46. private VerticalLayout testPanelLayout;
  47. private Panel testPanel;
  48. @Override
  49. public void init() {
  50. initComponentList();
  51. LegacyWindow w = new LegacyWindow();
  52. setMainWindow(w);
  53. setTheme("tests-components");
  54. final VerticalLayout main = new VerticalLayout();
  55. w.setContent(main);
  56. select = new ComboBox();
  57. select.setImmediate(true);
  58. select.setFilteringMode(FilteringMode.CONTAINS);
  59. select.setWidth("400px");
  60. prev = new Button("<<-|");
  61. prev.addListener(new Button.ClickListener() {
  62. @Override
  63. public void buttonClick(ClickEvent event) {
  64. Object cur = select.getValue();
  65. Testable prev = (Testable) cont.prevItemId(cur);
  66. if (prev == null) {
  67. getMainWindow().showNotification("No more test cases");
  68. } else {
  69. getMainWindow().showNotification(
  70. "Selected test:" + prev.getTestableName());
  71. select.setValue(prev);
  72. select.markAsDirty();
  73. }
  74. }
  75. });
  76. next = new Button("|->>");
  77. next.addListener(new Button.ClickListener() {
  78. @Override
  79. public void buttonClick(ClickEvent event) {
  80. Object cur = select.getValue();
  81. Testable next = (Testable) cont.nextItemId(cur);
  82. if (next == null) {
  83. getMainWindow().showNotification("No more test cases");
  84. } else {
  85. getMainWindow().showNotification(
  86. "Selected test:" + next.getTestableName());
  87. select.setValue(next);
  88. select.markAsDirty();
  89. }
  90. }
  91. });
  92. HorizontalLayout controllers = new HorizontalLayout();
  93. controllers.addComponent(prev);
  94. controllers.addComponent(select);
  95. controllers.addComponent(next);
  96. main.addComponent(controllers);
  97. select.setContainerDataSource(cont);
  98. select.addListener(new ComboBox.ValueChangeListener() {
  99. @Override
  100. public void valueChange(ValueChangeEvent event) {
  101. Testable t = (Testable) select.getValue();
  102. if (t != null) {
  103. testPanelLayout.removeAllComponents();
  104. try {
  105. Component c = t.getComponent();
  106. if (c != null) {
  107. testPanelLayout.addComponent(c);
  108. }
  109. } catch (InstantiationException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. } catch (IllegalAccessException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118. });
  119. testPanelLayout = new VerticalLayout();
  120. testPanel = new Panel(testPanelLayout);
  121. testPanel.setSizeFull();
  122. testPanel.setStyleName("testable");
  123. main.addComponent(testPanel);
  124. main.setExpandRatio(testPanel, 1);
  125. }
  126. private void initComponentList() {
  127. cont = new IndexedContainer();
  128. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  129. URL dir = cl.getResource("com/vaadin/ui");
  130. String[] list2 = (new File(dir.getFile())).list();
  131. for (int i = 0; i < list2.length; i++) {
  132. String f = list2[i];
  133. if (f.endsWith(".class") && (f.indexOf("CustomComponent") == -1)
  134. && (f.indexOf("Window") == -1)) {
  135. f = f.replaceAll(".class", "");
  136. String className = "com.vaadin.ui." + f;
  137. Class<?> c;
  138. try {
  139. c = Class.forName(className);
  140. Object o = c.newInstance();
  141. if (o instanceof Component) {
  142. Testable t = new Testable(c);
  143. cont.addItem(t);
  144. t = new Testable(c);
  145. t.addConfiguration(new Configuration("100px*100px") {
  146. @Override
  147. void configure(Component c) {
  148. c.setWidth("60px");
  149. c.setHeight("60px");
  150. }
  151. });
  152. t = new Testable(c);
  153. t.addConfiguration(new Configuration("Width 50em") {
  154. @Override
  155. void configure(Component c) {
  156. c.setWidth("50em");
  157. }
  158. });
  159. cont.addItem(t);
  160. t = new Testable(c);
  161. t.addConfiguration(new Configuration("Height 7cm") {
  162. @Override
  163. void configure(Component c) {
  164. c.setHeight("7cm");
  165. }
  166. });
  167. cont.addItem(t);
  168. t = new Testable(c) {
  169. @Override
  170. public Component getComponent()
  171. throws InstantiationException,
  172. IllegalAccessException {
  173. Component c = super.getComponent();
  174. VerticalLayout pl = new VerticalLayout();
  175. pl.setMargin(true);
  176. Panel p = new Panel(
  177. "Wrapper panel (400px*400px)", pl);
  178. p.setContent(new VerticalLayout());
  179. p.setWidth("400px");
  180. p.setHeight("400px");
  181. pl.addComponent(c);
  182. p.addStyleName("testablew");
  183. p.addStyleName("testable");
  184. return p;
  185. }
  186. };
  187. t.addConfiguration(new Configuration("100%*100%") {
  188. @Override
  189. void configure(Component c) {
  190. c.setSizeFull();
  191. }
  192. });
  193. cont.addItem(t);
  194. }
  195. } catch (ClassNotFoundException e) {
  196. // TODO Auto-generated catch block
  197. // e.printStackTrace();
  198. } catch (InstantiationException e) {
  199. // TODO Auto-generated catch block
  200. // e.printStackTrace();
  201. } catch (IllegalAccessException e) {
  202. // TODO Auto-generated catch block
  203. // e.printStackTrace();
  204. }
  205. }
  206. }
  207. }
  208. class Testable {
  209. private Class<?> classToTest;
  210. private ArrayList<Configuration> configurations = new ArrayList<Configuration>();
  211. Testable(Class<?> c) {
  212. classToTest = c;
  213. }
  214. public void addConfiguration(Configuration conf) {
  215. configurations.add(conf);
  216. }
  217. public String getTestableName() {
  218. StringBuffer sb = new StringBuffer();
  219. sb.append(classToTest.getName().replaceAll("com.vaadin.ui.", ""));
  220. sb.append("[");
  221. for (Iterator<Configuration> i = configurations.iterator(); i
  222. .hasNext();) {
  223. sb.append((i.next()).getDescription());
  224. if (i.hasNext()) {
  225. sb.append(",");
  226. }
  227. }
  228. sb.append("]");
  229. return sb.toString();
  230. }
  231. /**
  232. * Instantiates and populates component with test data to be ready for
  233. * testing.
  234. *
  235. * @return
  236. * @throws InstantiationException
  237. * @throws IllegalAccessException
  238. */
  239. public Component getComponent() throws InstantiationException,
  240. IllegalAccessException {
  241. Component c = (Component) classToTest.newInstance();
  242. if (c instanceof Button) {
  243. ((AbstractComponent) c).setCaption("test");
  244. }
  245. if (AbstractSelect.class.isAssignableFrom(c.getClass())) {
  246. if (c instanceof Table) {
  247. Table new_name = (Table) c;
  248. new_name.setContainerDataSource(TestForTablesInitialColumnWidthLogicRendering
  249. .getTestTable(5, 100).getContainerDataSource());
  250. } else {
  251. AbstractSelect new_name = (AbstractSelect) c;
  252. Container cont = TestForTablesInitialColumnWidthLogicRendering
  253. .getTestTable(2, 8).getContainerDataSource();
  254. new_name.setContainerDataSource(cont);
  255. new_name.setItemCaptionPropertyId(cont
  256. .getContainerPropertyIds().iterator().next());
  257. }
  258. } else if (c instanceof ComponentContainer) {
  259. ComponentContainer new_name = (ComponentContainer) c;
  260. new_name.addComponent(new Label("component 1 in test container"));
  261. new_name.addComponent(new Button("component 2"));
  262. } else if (c instanceof Embedded) {
  263. Embedded em = (Embedded) c;
  264. em.setSource(new ThemeResource("test.png"));
  265. } else if (c instanceof Label) {
  266. ((Label) c).setValue("Test label");
  267. }
  268. for (Iterator<Configuration> i = configurations.iterator(); i
  269. .hasNext();) {
  270. Configuration conf = i.next();
  271. conf.configure(c);
  272. }
  273. return c;
  274. }
  275. @Override
  276. public String toString() {
  277. return getTestableName();
  278. }
  279. }
  280. public abstract class Configuration {
  281. private String description = "";
  282. Configuration(String description) {
  283. this.description = description;
  284. }
  285. public String getDescription() {
  286. return description;
  287. }
  288. abstract void configure(Component c);
  289. @Override
  290. public String toString() {
  291. return getDescription();
  292. }
  293. }
  294. }