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.

LabelDeclarativeTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.server.component.label;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.parser.Tag;
  19. import org.junit.Assert;
  20. import org.junit.Ignore;
  21. import org.junit.Test;
  22. import com.vaadin.shared.ui.ContentMode;
  23. import com.vaadin.tests.design.DeclarativeTestBase;
  24. import com.vaadin.ui.Label;
  25. import com.vaadin.ui.declarative.DesignContext;
  26. import com.vaadin.ui.declarative.DesignFormatter;
  27. /**
  28. * Tests declarative support for implementations of {@link Label}.
  29. *
  30. * @since 7.4
  31. * @author Vaadin Ltd
  32. */
  33. public class LabelDeclarativeTest extends DeclarativeTestBase<Label> {
  34. @Test
  35. public void testEmpty() {
  36. String design = "<vaadin-label />";
  37. Label l = new Label();
  38. l.setContentMode(ContentMode.HTML);
  39. testRead(design, l);
  40. testWrite(design, l);
  41. }
  42. @Test
  43. public void testDefault() {
  44. String design = "<vaadin-label>Hello world!</vaadin-label>";
  45. Label l = createLabel("Hello world!", null, true);
  46. testRead(design, l);
  47. testWrite(design, l);
  48. }
  49. @Test
  50. public void testRich() {
  51. String design = "<vaadin-label>This is <b><u>Rich</u></b> content!</vaadin-label>";
  52. Label l = createLabel("This is \n<b><u>Rich</u></b> content!", null,
  53. true);
  54. testRead(design, l);
  55. testWrite(design, l);
  56. }
  57. @Test
  58. public void testPlainText() {
  59. String design = "<vaadin-label plain-text>This is only &lt;b&gt;text&lt;/b&gt;"
  60. + " and will contain visible tags</vaadin-label>";
  61. Label l = createLabel(
  62. "This is only <b>text</b> and will contain visible tags", null,
  63. false);
  64. testRead(design, l);
  65. testWrite(design, l);
  66. }
  67. @Test
  68. public void testContentAndCaption() {
  69. String design = "<vaadin-label caption='This is a label'>This is <b><u>Rich</u></b> "
  70. + "content!</vaadin-label>";
  71. Label l = createLabel("This is \n<b><u>Rich</u></b> content!",
  72. "This is a label", true);
  73. testRead(design, l);
  74. testWrite(design, l);
  75. }
  76. @Test
  77. public void testCaption() {
  78. String design = "<vaadin-label caption='This is a label' />";
  79. Label l = createLabel(null, "This is a label", true);
  80. testRead(design, l);
  81. testWrite(design, l);
  82. }
  83. @Test
  84. public void testHtmlEntities() {
  85. String design = "<vaadin-label plain-text=\"true\">&gt; Test</vaadin-label>";
  86. Label read = read(design);
  87. Assert.assertEquals("> Test", read.getValue());
  88. design = design.replace("plain-text=\"true\"", "");
  89. read = read(design);
  90. Assert.assertEquals("&gt; Test", read.getValue());
  91. Label label = new Label("&amp; Test");
  92. label.setContentMode(ContentMode.TEXT);
  93. Element root = new Element(Tag.valueOf("vaadin-label"), "");
  94. label.writeDesign(root, new DesignContext());
  95. Assert.assertEquals("&amp;amp; Test", root.html());
  96. label.setContentMode(ContentMode.HTML);
  97. root = new Element(Tag.valueOf("vaadin-label"), "");
  98. label.writeDesign(root, new DesignContext());
  99. Assert.assertEquals("&amp; Test", root.html());
  100. }
  101. /**
  102. * FIXME Using another content mode than TEXT OR HTML is currently not
  103. * supported and will cause the content mode to fallback without the users
  104. * knowledge to HTML. This test can be enabled when
  105. * https://dev.vaadin.com/ticket/19435 is fixed.
  106. */
  107. @Test
  108. @Ignore("Test ignored due to https://dev.vaadin.com/ticket/19435")
  109. public void testContentModes() {
  110. String design = "<vaadin-label caption='This\n is a label' />";
  111. Label l = createLabel(null, "This\n is a label", true);
  112. l.setContentMode(ContentMode.PREFORMATTED);
  113. testRead(design, l);
  114. testWrite(design, l);
  115. }
  116. private Label createLabel(String content, String caption, boolean html) {
  117. Label label = new Label();
  118. label.setContentMode(html ? ContentMode.HTML : ContentMode.TEXT);
  119. if (content != null) {
  120. label.setValue(content);
  121. }
  122. if (caption != null) {
  123. label.setCaption(DesignFormatter.encodeForTextNode(caption));
  124. }
  125. return label;
  126. }
  127. }