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.

TestSynchronizeToDesign.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2000-2014 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 junit.framework.TestCase;
  18. import org.jsoup.nodes.Attributes;
  19. import org.jsoup.nodes.Element;
  20. import org.jsoup.parser.Tag;
  21. import org.junit.Test;
  22. import com.vaadin.event.ShortcutAction.KeyCode;
  23. import com.vaadin.event.ShortcutAction.ModifierKey;
  24. import com.vaadin.ui.Button;
  25. import com.vaadin.ui.NativeButton;
  26. import com.vaadin.ui.declarative.DesignContext;
  27. /**
  28. * Tests generating html tree nodes corresponding to the contents of a Button
  29. * and a NativeButton.
  30. */
  31. public class TestSynchronizeToDesign extends TestCase {
  32. private DesignContext ctx;
  33. @Override
  34. protected void setUp() throws Exception {
  35. super.setUp();
  36. ctx = new DesignContext();
  37. }
  38. @Test
  39. public void testWithTextContent() {
  40. createAndTestButtons("Click me");
  41. }
  42. @Test
  43. public void testWithHtmlContent() {
  44. createAndTestButtons("<b>Click</b>");
  45. }
  46. @Test
  47. public void testAttributes() {
  48. Button button = new Button();
  49. button.setTabIndex(3);
  50. button.setIconAlternateText("OK");
  51. button.setClickShortcut(KeyCode.O, ModifierKey.CTRL, ModifierKey.SHIFT);
  52. Element e = new Element(Tag.valueOf("v-button"), "", new Attributes());
  53. button.synchronizeToDesign(e, ctx);
  54. assertEquals("3", e.attr("tabindex"));
  55. assertTrue("Button is plain text by default", e.hasAttr("plain-text"));
  56. assertEquals("OK", e.attr("icon-alt"));
  57. assertEquals("ctrl-shift-o", e.attr("click-shortcut"));
  58. }
  59. private void createAndTestButtons(String content) {
  60. Button b1 = new Button(content);
  61. // we need to set this on, since the plain-text attribute will appear
  62. // otherwise
  63. b1.setHtmlContentAllowed(true);
  64. Element e1 = ctx.createNode(b1);
  65. assertEquals("Wrong tag name for button.", "v-button", e1.tagName());
  66. assertEquals("Unexpected content in the v-button element.", content,
  67. e1.html());
  68. assertTrue("The v-button element should not have attributes.", e1
  69. .attributes().size() == 0);
  70. NativeButton b2 = new NativeButton(content);
  71. b2.setHtmlContentAllowed(true);
  72. Element e2 = ctx.createNode(b2);
  73. assertEquals("Wrong tag name for button.", "v-native-button",
  74. e2.tagName());
  75. assertEquals("Unexpected content in the v-button element.", content,
  76. e2.html());
  77. assertTrue("The v-button element should not have attributes.", e2
  78. .attributes().size() == 0);
  79. }
  80. }