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.3KB

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