Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractLegacyComponentDeclarativeTest.java 7.8KB

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