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.

AbstractLegacyComponentDeclarativeTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.vaadin.v7.ui;
  2. import static org.junit.Assert.assertTrue;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.lang.reflect.Field;
  6. import java.nio.charset.Charset;
  7. import java.util.Locale;
  8. import org.jsoup.nodes.Attributes;
  9. import org.jsoup.nodes.Element;
  10. import org.jsoup.parser.Tag;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import com.vaadin.server.ExternalResource;
  14. import com.vaadin.server.FileResource;
  15. import com.vaadin.server.Responsive;
  16. import com.vaadin.server.ThemeResource;
  17. import com.vaadin.server.UserError;
  18. import com.vaadin.shared.ui.ErrorLevel;
  19. import com.vaadin.tests.design.DeclarativeTestBase;
  20. import com.vaadin.ui.AbstractComponent;
  21. import com.vaadin.ui.Label;
  22. import com.vaadin.ui.declarative.Design;
  23. import com.vaadin.ui.declarative.DesignContext;
  24. /**
  25. * Test cases for reading and writing the properties of AbstractComponent.
  26. *
  27. * @author Vaadin Ltd
  28. */
  29. public class AbstractLegacyComponentDeclarativeTest
  30. extends DeclarativeTestBase<AbstractLegacyComponent> {
  31. private AbstractLegacyComponent component;
  32. @Before
  33. public void setUp() {
  34. NativeSelect ns = new NativeSelect();
  35. component = ns;
  36. }
  37. @Test
  38. public void testEmptyDesign() {
  39. String design = "<vaadin7-native-select>";
  40. testRead(design, component);
  41. testWrite(design, component);
  42. }
  43. @Test
  44. public void testProperties() {
  45. String design = "<vaadin7-native-select id=\"testId\" primary-style-name=\"test-style\" "
  46. + "caption=\"test-caption\" locale=\"fi_FI\" description=\"test-description\" "
  47. + "error=\"<div>test-error</div>\" />";
  48. component.setId("testId");
  49. component.setPrimaryStyleName("test-style");
  50. component.setCaption("test-caption");
  51. component.setLocale(new Locale("fi", "FI"));
  52. component.setDescription("test-description");
  53. component.setComponentError(new UserError("<div>test-error</div>",
  54. com.vaadin.server.AbstractErrorMessage.ContentMode.HTML,
  55. ErrorLevel.ERROR));
  56. component.setImmediate(true);
  57. testRead(design, component);
  58. testWrite(design, component);
  59. }
  60. @Test
  61. public void testReadImmediate() {
  62. // Additional tests for the immediate property, including
  63. // explicit immediate values
  64. String[] design = { "<vaadin7-native-select/>",
  65. "<vaadin7-native-select immediate=\"false\"/>",
  66. "<vaadin7-native-select immediate=\"true\"/>",
  67. "<vaadin7-native-select immediate />" };
  68. Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE,
  69. Boolean.TRUE };
  70. boolean[] immediate = { true, false, true, true };
  71. for (int i = 0; i < design.length; i++) {
  72. component = (AbstractLegacyComponent) Design
  73. .read(new ByteArrayInputStream(
  74. design[i].getBytes(Charset.forName("UTF-8"))));
  75. assertEquals(immediate[i], component.isImmediate());
  76. assertEquals(explicitImmediate[i], getExplicitImmediate(component));
  77. }
  78. }
  79. @Test
  80. public void testExternalIcon() {
  81. String design = "<vaadin7-native-select icon=\"http://example.com/example.gif\"/>";
  82. component.setIcon(
  83. new ExternalResource("http://example.com/example.gif"));
  84. testRead(design, component);
  85. testWrite(design, component);
  86. }
  87. @Test
  88. public void testThemeIcon() {
  89. String design = "<vaadin7-native-select icon=\"theme://example.gif\"/>";
  90. component.setIcon(new ThemeResource("example.gif"));
  91. testRead(design, component);
  92. testWrite(design, component);
  93. }
  94. @Test
  95. public void testFileResourceIcon() {
  96. String design = "<vaadin7-native-select icon=\"img/example.gif\"/>";
  97. component.setIcon(new FileResource(new File("img/example.gif")));
  98. testRead(design, component);
  99. testWrite(design, component);
  100. }
  101. @Test
  102. public void testWidthAndHeight() {
  103. String design = "<vaadin7-native-select width=\"70%\" height=\"12px\"/>";
  104. component.setWidth("70%");
  105. component.setHeight("12px");
  106. testRead(design, component);
  107. testWrite(design, component);
  108. }
  109. @Test
  110. public void testSizeFull() {
  111. String design = "<vaadin7-native-select size-full />";
  112. component.setSizeFull();
  113. testRead(design, component);
  114. testWrite(design, component);
  115. }
  116. @Test
  117. public void testHeightFull() {
  118. String design = "<vaadin7-native-select height-full width=\"20px\"/>";
  119. component.setHeight("100%");
  120. component.setWidth("20px");
  121. testRead(design, component);
  122. testWrite(design, component);
  123. }
  124. @Test
  125. public void testWidthFull() {
  126. String design = "<vaadin7-native-select caption=\"Foo\" caption-as-html width-full height=\"20px\"></vaadin7-native-select>";
  127. AbstractLegacyComponent component = new NativeSelect();
  128. component.setCaptionAsHtml(true);
  129. component.setCaption("Foo");
  130. component.setHeight("20px");
  131. component.setWidth("100%");
  132. testRead(design, component);
  133. testWrite(design, component);
  134. }
  135. @Test
  136. public void testResponsive() {
  137. String design = "<vaadin7-native-select responsive />";
  138. Responsive.makeResponsive(component);
  139. testRead(design, component);
  140. testWrite(design, component);
  141. }
  142. @Test
  143. public void testResponsiveFalse() {
  144. String design = "<vaadin7-native-select responsive =\"false\"/>";
  145. // Only test read as the attribute responsive=false would not be written
  146. testRead(design, component);
  147. }
  148. @Test
  149. public void testReadAlreadyResponsive() {
  150. AbstractComponent component = new Label();
  151. Responsive.makeResponsive(component);
  152. Element design = createDesign(true);
  153. component.readDesign(design, new DesignContext());
  154. assertEquals("Component should have only one extension", 1,
  155. component.getExtensions().size());
  156. }
  157. @Test
  158. public void testUnknownProperties() {
  159. String design = "<vaadin7-native-select foo=\"bar\"/>";
  160. DesignContext context = readAndReturnContext(design);
  161. NativeSelect ns = (NativeSelect) context.getRootComponent();
  162. assertTrue("Custom attribute was preserved in custom attributes",
  163. context.getCustomAttributes(ns).containsKey("foo"));
  164. testWrite(ns, design, context);
  165. }
  166. private Element createDesign(boolean responsive) {
  167. Attributes attributes = new Attributes();
  168. attributes.put("responsive", responsive);
  169. Element node = new Element(Tag.valueOf("vaadin-label"), "", attributes);
  170. return node;
  171. }
  172. private Boolean getExplicitImmediate(AbstractLegacyComponent component) {
  173. try {
  174. Field immediate = AbstractLegacyComponent.class
  175. .getDeclaredField("explicitImmediateValue");
  176. immediate.setAccessible(true);
  177. return (Boolean) immediate.get(component);
  178. } catch (Exception e) {
  179. throw new RuntimeException(
  180. "Getting the field explicitImmediateValue failed.");
  181. }
  182. }
  183. }