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.

ButtonDeclarativeTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.button;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.parser.Tag;
  19. import org.junit.Assert;
  20. import org.junit.Test;
  21. import com.vaadin.event.ShortcutAction.KeyCode;
  22. import com.vaadin.event.ShortcutAction.ModifierKey;
  23. import com.vaadin.tests.design.DeclarativeTestBase;
  24. import com.vaadin.ui.Button;
  25. import com.vaadin.ui.NativeButton;
  26. import com.vaadin.ui.declarative.DesignContext;
  27. /**
  28. * Tests declarative support for implementations of {@link Button} and
  29. * {@link NativeButton}.
  30. *
  31. * @since 7.4
  32. * @author Vaadin Ltd
  33. */
  34. public class ButtonDeclarativeTest extends DeclarativeTestBase<Button> {
  35. @Test
  36. public void testEmptyPlainText() {
  37. String design = "<vaadin-button plain-text></vaadin-button>";
  38. testButtonAndNativeButton(design, false, "");
  39. }
  40. @Test
  41. public void testPlainTextCaption() {
  42. String design = "<vaadin-button plain-text>Click</vaadin-button>";
  43. testButtonAndNativeButton(design, false, "Click");
  44. }
  45. @Test
  46. public void testEmptyHtml() {
  47. String design = "<vaadin-button />";
  48. testButtonAndNativeButton(design, true, "");
  49. }
  50. @Test
  51. public void testHtmlCaption() {
  52. String design = "<vaadin-button><b>Click</b></vaadin-button>";
  53. testButtonAndNativeButton(design, true, "<b>Click</b>");
  54. }
  55. @Test
  56. public void testWithCaptionAttribute() {
  57. // The caption attribute should be ignored
  58. String design = "<vaadin-button caption=Caption>Click</vaadin-button>";
  59. String expectedWritten = "<vaadin-button>Click</vaadin-button>";
  60. testButtonAndNativeButton(design, true, "Click", expectedWritten);
  61. }
  62. @Test
  63. public void testWithOnlyCaptionAttribute() {
  64. String design = "<vaadin-button caption=Click/>";
  65. String expectedWritten = "<vaadin-button/>";
  66. testButtonAndNativeButton(design, true, "", expectedWritten);
  67. }
  68. @Test
  69. public void testHtmlEntitiesInCaption() {
  70. String designPlainText = "<vaadin-button plain-text=\"true\">&gt; One</vaadin-button>";
  71. String expectedCaptionPlainText = "> One";
  72. Button read = read(designPlainText);
  73. Assert.assertEquals(expectedCaptionPlainText, read.getCaption());
  74. designPlainText = designPlainText.replace("vaadin-button",
  75. "vaadin-native-button");
  76. Button nativeButton = read(designPlainText);
  77. Assert.assertEquals(expectedCaptionPlainText,
  78. nativeButton.getCaption());
  79. String designHtml = "<vaadin-button>&gt; One</vaadin-button>";
  80. String expectedCaptionHtml = "&gt; One";
  81. read = read(designHtml);
  82. Assert.assertEquals(expectedCaptionHtml, read.getCaption());
  83. designHtml = designHtml.replace("vaadin-button",
  84. "vaadin-native-button");
  85. nativeButton = read(designHtml);
  86. Assert.assertEquals(expectedCaptionHtml, nativeButton.getCaption());
  87. read = new Button("&amp; Test");
  88. read.setCaptionAsHtml(true);
  89. Element root = new Element(Tag.valueOf("vaadin-button"), "");
  90. read.writeDesign(root, new DesignContext());
  91. assertEquals("&amp; Test", root.html());
  92. read.setCaptionAsHtml(false);
  93. root = new Element(Tag.valueOf("vaadin-button"), "");
  94. read.writeDesign(root, new DesignContext());
  95. assertEquals("&amp;amp; Test", root.html());
  96. }
  97. public void testButtonAndNativeButton(String design, boolean html,
  98. String caption) {
  99. testButtonAndNativeButton(design, html, caption, design);
  100. }
  101. public void testButtonAndNativeButton(String design, boolean html,
  102. String caption, String expectedWritten) {
  103. // Test Button
  104. Button b = new Button();
  105. b.setCaptionAsHtml(html);
  106. b.setCaption(caption);
  107. testRead(expectedWritten, b);
  108. testWrite(expectedWritten, b);
  109. // Test NativeButton
  110. design = design.replace("vaadin-button", "vaadin-native-button");
  111. expectedWritten = expectedWritten.replace("vaadin-button",
  112. "vaadin-native-button");
  113. NativeButton nb = new NativeButton();
  114. nb.setCaptionAsHtml(html);
  115. nb.setCaption(caption);
  116. testRead(expectedWritten, nb);
  117. testWrite(expectedWritten, nb);
  118. }
  119. @Test
  120. public void testAttributes() {
  121. String design = "<vaadin-button tabindex=3 plain-text icon-alt=OK "
  122. + "click-shortcut=shift-ctrl-o></vaadin-button>";
  123. Button b = new Button("");
  124. b.setTabIndex(3);
  125. b.setIconAlternateText("OK");
  126. b.setClickShortcut(KeyCode.O, ModifierKey.CTRL, ModifierKey.SHIFT);
  127. testRead(design, b);
  128. testWrite(design, b);
  129. }
  130. }