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

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