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.

ParseLayoutTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.tests.design;
  17. import static org.hamcrest.CoreMatchers.is;
  18. import static org.hamcrest.CoreMatchers.not;
  19. import static org.hamcrest.CoreMatchers.nullValue;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertThat;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.ByteArrayOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import org.jsoup.Jsoup;
  28. import org.jsoup.nodes.Document;
  29. import org.jsoup.nodes.Element;
  30. import org.jsoup.nodes.Node;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import com.vaadin.ui.Component;
  34. import com.vaadin.ui.HorizontalLayout;
  35. import com.vaadin.ui.PasswordField;
  36. import com.vaadin.ui.TextField;
  37. import com.vaadin.ui.VerticalLayout;
  38. import com.vaadin.ui.declarative.Design;
  39. import com.vaadin.ui.declarative.DesignContext;
  40. import com.vaadin.ui.declarative.DesignException;
  41. /**
  42. * A test for checking that parsing a layout preserves the IDs and the mapping
  43. * from prefixes to package names (for example
  44. * <meta name=”package-mapping” content=”my:com.addon.mypackage” />)
  45. *
  46. * @since
  47. * @author Vaadin Ltd
  48. */
  49. public class ParseLayoutTest {
  50. // The context is used for accessing the created component hierarchy.
  51. private DesignContext ctx;
  52. @Before
  53. public void setUp() throws Exception {
  54. ctx = Design.read(getClass().getResourceAsStream("testFile.html"),
  55. null);
  56. }
  57. @Test
  58. public void buttonWithIdIsParsed() {
  59. Component button = ctx.getComponentByLocalId("firstButton");
  60. assertThat(ctx.getComponentByCaption("Native click me"), is(button));
  61. assertThat(button.getCaption(), is("Native click me"));
  62. }
  63. @Test
  64. public void buttonWithIdAndLocalIdIsParsed() {
  65. Component button = ctx.getComponentById("secondButton");
  66. assertThat(ctx.getComponentByCaption("Another button"), is(button));
  67. assertThat(ctx.getComponentByLocalId("localID"), is(button));
  68. assertThat(button.getCaption(), is("Another button"));
  69. }
  70. @Test
  71. public void buttonWithoutIdsIsParsed() {
  72. assertThat(ctx.getComponentByCaption("Yet another button"),
  73. is(not(nullValue())));
  74. }
  75. @Test
  76. public void serializationPreservesProperties() throws IOException {
  77. ByteArrayOutputStream out = serializeDesign(ctx);
  78. ctx = deSerializeDesign(out);
  79. assertButtonProperties();
  80. }
  81. @Test
  82. public void serializationPreservesHierarchy() throws IOException {
  83. ByteArrayOutputStream out = serializeDesign(ctx);
  84. ctx = deSerializeDesign(out);
  85. assertComponentHierarchy();
  86. }
  87. @Test
  88. public void designIsSerializedWithCorrectPrefixesAndPackageNames()
  89. throws IOException {
  90. ByteArrayOutputStream out = serializeDesign(ctx);
  91. // Check the mapping from prefixes to package names using the html tree
  92. String[] expectedPrefixes = { "my" };
  93. String[] expectedPackageNames = { "com.addon.mypackage" };
  94. int index = 0;
  95. Document doc = Jsoup.parse(out.toString("UTF-8"));
  96. Element head = doc.head();
  97. for (Node child : head.childNodes()) {
  98. if ("meta".equals(child.nodeName())) {
  99. String name = child.attributes().get("name");
  100. if ("package-mapping".equals(name)) {
  101. String content = child.attributes().get("content");
  102. String[] parts = content.split(":");
  103. assertEquals("Unexpected prefix.", expectedPrefixes[index],
  104. parts[0]);
  105. assertEquals("Unexpected package name.",
  106. expectedPackageNames[index], parts[1]);
  107. index++;
  108. }
  109. }
  110. }
  111. assertEquals("Unexpected number of prefix - package name pairs.", 1,
  112. index);
  113. }
  114. private DesignContext deSerializeDesign(ByteArrayOutputStream out) {
  115. ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  116. return Design.read(in, null);
  117. }
  118. private ByteArrayOutputStream serializeDesign(DesignContext context)
  119. throws IOException {
  120. ByteArrayOutputStream out = new ByteArrayOutputStream();
  121. Design.write(context, out);
  122. return out;
  123. }
  124. private void assertButtonProperties() {
  125. buttonWithIdAndLocalIdIsParsed();
  126. buttonWithIdIsParsed();
  127. buttonWithoutIdsIsParsed();
  128. }
  129. @Test
  130. public void fieldsAreBoundToATemplate() throws IOException {
  131. LayoutTemplate template = new LayoutTemplate();
  132. InputStream htmlFile = getClass().getResourceAsStream("testFile.html");
  133. Design.read(htmlFile, template);
  134. assertNotNull(template.getFirstButton());
  135. assertNotNull(template.getSecondButton());
  136. assertNotNull(template.getYetanotherbutton());
  137. assertNotNull(template.getClickme());
  138. assertEquals("Native click me", template.getFirstButton().getCaption());
  139. }
  140. @Test(expected = DesignException.class)
  141. public void fieldsCannotBeBoundToAnInvalidTemplate() throws IOException {
  142. InvalidLayoutTemplate template = new InvalidLayoutTemplate();
  143. InputStream htmlFile = getClass().getResourceAsStream("testFile.html");
  144. Design.read(htmlFile, template);
  145. }
  146. @Test
  147. public void rootHasCorrectComponents() {
  148. Component root = ctx.getRootComponent();
  149. VerticalLayout vlayout = (VerticalLayout) root;
  150. assertThat(vlayout.getComponentCount(), is(3));
  151. }
  152. @Test
  153. public void rootChildHasCorrectComponents() {
  154. Component root = ctx.getRootComponent();
  155. VerticalLayout vlayout = (VerticalLayout) root;
  156. HorizontalLayout hlayout = (HorizontalLayout) vlayout.getComponent(0);
  157. assertThat(hlayout.getComponentCount(), is(5));
  158. assertThat(hlayout.getComponent(0).getCaption(), is("FooBar"));
  159. assertThat(hlayout.getComponent(1).getCaption(), is("Native click me"));
  160. assertThat(hlayout.getComponent(2).getCaption(), is("Another button"));
  161. assertThat(hlayout.getComponent(3).getCaption(),
  162. is("Yet another button"));
  163. assertThat(hlayout.getComponent(4).getCaption(), is("Click me"));
  164. assertThat(hlayout.getComponent(4).getWidth(), is(150f));
  165. // Check the remaining two components of the vertical layout
  166. assertTextField(vlayout);
  167. assertPasswordField(vlayout);
  168. }
  169. private void assertComponentHierarchy() {
  170. rootHasCorrectComponents();
  171. rootChildHasCorrectComponents();
  172. }
  173. private void assertTextField(VerticalLayout vlayout) {
  174. TextField tf = (TextField) vlayout.getComponent(1);
  175. assertThat(tf.getCaption(), is("Text input"));
  176. }
  177. private void assertPasswordField(VerticalLayout layout) {
  178. PasswordField pf = (PasswordField) layout.getComponent(2);
  179. assertThat(pf.getCaption(), is("Password field"));
  180. assertThat(pf.getWidth(), is(300f));
  181. assertThat(pf.getHeight(), is(200f));
  182. }
  183. }