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.

LocaleTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.ByteArrayInputStream;
  20. import java.util.Locale;
  21. import org.jsoup.nodes.Document;
  22. import org.jsoup.nodes.DocumentType;
  23. import org.jsoup.nodes.Element;
  24. import org.jsoup.nodes.Node;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import com.vaadin.ui.Button;
  28. import com.vaadin.ui.Component;
  29. import com.vaadin.ui.HorizontalLayout;
  30. import com.vaadin.ui.Label;
  31. import com.vaadin.ui.VerticalLayout;
  32. import com.vaadin.ui.declarative.Design;
  33. import com.vaadin.ui.declarative.DesignContext;
  34. /**
  35. * Tests the handling of the locale property in parsing and html generation.
  36. *
  37. * @since
  38. * @author Vaadin Ltd
  39. */
  40. public class LocaleTest {
  41. DesignContext ctx;
  42. @Before
  43. public void setUp() {
  44. ctx = new DesignContext();
  45. }
  46. /*
  47. * Checks that when the html corresponding to a component hierarchy is
  48. * constructed, the result only contains locale attributes for a component
  49. * if its locale differs from that of its parent.
  50. */
  51. @Test
  52. public void testHtmlGeneration() {
  53. // create a component hierarchy
  54. VerticalLayout vLayout = new VerticalLayout();
  55. vLayout.setLocale(Locale.US);
  56. HorizontalLayout hLayout = new HorizontalLayout();
  57. hLayout.setLocale(Locale.ITALY);
  58. vLayout.addComponent(hLayout);
  59. Button b1 = new Button();
  60. b1.setLocale(Locale.ITALY);
  61. Button b2 = new Button();
  62. b2.setLocale(Locale.US);
  63. hLayout.addComponent(b1);
  64. hLayout.addComponent(b2);
  65. HorizontalLayout hlayout2 = new HorizontalLayout();
  66. hlayout2.setLocale(Locale.US);
  67. vLayout.addComponent(hlayout2);
  68. Label l = new Label();
  69. l.setLocale(Locale.US);
  70. hlayout2.addComponent(l);
  71. Label l2 = new Label();
  72. l2.setLocale(Locale.CANADA);
  73. hlayout2.addComponent(l2);
  74. ctx.setRootComponent(vLayout);
  75. // create the html tree corresponding to the component hierarchy
  76. Document doc = componentToDoc(ctx);
  77. // check the created html
  78. Element body = doc.body();
  79. Element evLayout = body.child(0);
  80. assertEquals("Wrong locale information.", "en_US",
  81. evLayout.attr("locale"));
  82. Element ehLayout = evLayout.child(0);
  83. assertEquals("Wrong locale information.", "it_IT",
  84. ehLayout.attr("locale"));
  85. Element eb1 = ehLayout.child(0);
  86. assertTrue(
  87. "The element should not have a locale specification, found locale "
  88. + eb1.attr("locale"),
  89. "".equals(eb1.attr("locale")));
  90. Element eb2 = ehLayout.child(1);
  91. assertEquals("Wrong locale information.", "en_US", eb2.attr("locale"));
  92. Element ehLayout2 = evLayout.child(1);
  93. assertTrue(
  94. "The element should not have a locale specification, found locale "
  95. + ehLayout2.attr("locale"),
  96. "".equals(ehLayout2.attr("locale")));
  97. Element el1 = ehLayout2.child(0);
  98. assertTrue(
  99. "The element should not have a locale specification, found locale "
  100. + el1.attr("locale"),
  101. "".equals(el1.attr("locale")));
  102. Element el2 = ehLayout2.child(1);
  103. assertEquals("Wrong locale information.", "en_CA", el2.attr("locale"));
  104. }
  105. private Document componentToDoc(DesignContext dc) {
  106. // Create the html tree skeleton.
  107. Document doc = new Document("");
  108. DocumentType docType = new DocumentType("html", "", "", "");
  109. doc.appendChild(docType);
  110. Element html = doc.createElement("html");
  111. doc.appendChild(html);
  112. html.appendChild(doc.createElement("head"));
  113. Element body = doc.createElement("body");
  114. html.appendChild(body);
  115. dc.writePackageMappings(doc);
  116. // Append the design under <body> in the html tree. createNode
  117. // creates the entire component hierarchy rooted at the
  118. // given root node.
  119. Component root = dc.getRootComponent();
  120. Node rootNode = dc.createElement(root);
  121. body.appendChild(rootNode);
  122. return doc;
  123. }
  124. /*
  125. * Checks that the locale of a component is set when the html element
  126. * corresponding to the component specifies a locale.
  127. */
  128. @Test
  129. public void testParsing() {
  130. // create an html document
  131. Document doc = new Document("");
  132. DocumentType docType = new DocumentType("html", "", "", "");
  133. doc.appendChild(docType);
  134. Element html = doc.createElement("html");
  135. doc.appendChild(html);
  136. html.appendChild(doc.createElement("head"));
  137. Element body = doc.createElement("body");
  138. html.appendChild(body);
  139. Element evLayout = doc.createElement("vaadin-vertical-layout");
  140. evLayout.attr("locale", "en_US");
  141. body.appendChild(evLayout);
  142. Element ehLayout = doc.createElement("vaadin-horizontal-layout");
  143. evLayout.appendChild(ehLayout);
  144. Element eb1 = doc.createElement("vaadin-button");
  145. eb1.attr("locale", "en_US");
  146. ehLayout.appendChild(eb1);
  147. Element eb2 = doc.createElement("vaadin-button");
  148. eb2.attr("locale", "en_GB");
  149. ehLayout.appendChild(eb2);
  150. Element eb3 = doc.createElement("vaadin-button");
  151. ehLayout.appendChild(eb3);
  152. // parse the created document and check the constructed component
  153. // hierarchy
  154. String string = doc.html();
  155. VerticalLayout vLayout = (VerticalLayout) Design
  156. .read(new ByteArrayInputStream(string.getBytes()));
  157. assertEquals("Wrong locale.", new Locale("en", "US"),
  158. vLayout.getLocale());
  159. HorizontalLayout hLayout = (HorizontalLayout) vLayout.getComponent(0);
  160. assertEquals("The element should have the same locale as its parent.",
  161. vLayout.getLocale(), hLayout.getLocale());
  162. Button b1 = (Button) hLayout.getComponent(0);
  163. assertEquals("Wrong locale.", new Locale("en", "US"), b1.getLocale());
  164. Button b2 = (Button) hLayout.getComponent(1);
  165. assertEquals("Wrong locale.", new Locale("en", "GB"), b2.getLocale());
  166. Button b3 = (Button) hLayout.getComponent(2);
  167. assertEquals("The component should have the same locale as its parent.",
  168. hLayout.getLocale(), b3.getLocale());
  169. }
  170. }