Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TestSynchronizeFromDesign.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 java.lang.reflect.Field;
  18. import junit.framework.TestCase;
  19. import org.jsoup.nodes.Attributes;
  20. import org.jsoup.nodes.Element;
  21. import org.jsoup.parser.Tag;
  22. import org.junit.Test;
  23. import com.vaadin.event.ShortcutAction.KeyCode;
  24. import com.vaadin.event.ShortcutAction.ModifierKey;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Button.ClickShortcut;
  27. import com.vaadin.ui.NativeButton;
  28. import com.vaadin.ui.declarative.DesignContext;
  29. /**
  30. *
  31. * Test cases for reading the contents of a Button and a NativeButton from a
  32. * design.
  33. *
  34. */
  35. public class TestSynchronizeFromDesign extends TestCase {
  36. private DesignContext ctx;
  37. @Override
  38. protected void setUp() throws Exception {
  39. super.setUp();
  40. ctx = new DesignContext();
  41. }
  42. @Test
  43. public void testWithContent() {
  44. createAndTestButtons("Click", null);
  45. }
  46. @Test
  47. public void testWithHtmlCaption() {
  48. createAndTestButtons("<b>Click me</b>", null);
  49. }
  50. @Test
  51. public void testWithContentAndCaption() {
  52. createAndTestButtons("Click me", "caption");
  53. }
  54. @Test
  55. public void testWithCaption() {
  56. createAndTestButtons(null, "Click me");
  57. }
  58. @Test
  59. public void testAttributes() throws IllegalArgumentException,
  60. SecurityException, IllegalAccessException, NoSuchFieldException {
  61. Attributes attributes = new Attributes();
  62. attributes.put("tabindex", "3");
  63. attributes.put("plain-text", "");
  64. attributes.put("icon-alt", "OK");
  65. attributes.put("click-shortcut", "ctrl-shift-o");
  66. Button button = (Button) ctx
  67. .createChild(createButtonWithAttributes(attributes));
  68. assertEquals(3, button.getTabIndex());
  69. assertEquals(false, button.isHtmlContentAllowed());
  70. assertEquals("OK", button.getIconAlternateText());
  71. Field field = Button.class.getDeclaredField("clickShortcut");
  72. field.setAccessible(true);
  73. ClickShortcut value = (ClickShortcut) field.get(button);
  74. assertEquals(KeyCode.O, value.getKeyCode());
  75. assertEquals(ModifierKey.CTRL, value.getModifiers()[0]);
  76. assertEquals(ModifierKey.SHIFT, value.getModifiers()[1]);
  77. }
  78. /*
  79. * Test both Button and NativeButton. Caption should always be ignored. If
  80. * content is null, the created button should have empty content.
  81. */
  82. private void createAndTestButtons(String content, String caption) {
  83. Element e1 = createElement("v-button", content, caption);
  84. Button b1 = (Button) ctx.createChild(e1);
  85. Element e2 = createElement("v-native-button", content, caption);
  86. NativeButton b2 = (NativeButton) ctx.createChild(e2);
  87. if (content != null) {
  88. assertEquals("The button has the wrong text content.", content,
  89. b1.getCaption());
  90. assertEquals("The button has the wrong text content.", content,
  91. b2.getCaption());
  92. } else {
  93. assertTrue("The button has the wrong content.",
  94. b1.getCaption() == null || "".equals(b1.getCaption()));
  95. assertTrue("The button has the wrong content.",
  96. b2.getCaption() == null || "".equals(b2.getCaption()));
  97. }
  98. }
  99. private Element createButtonWithAttributes(Attributes attributes) {
  100. return new Element(Tag.valueOf("v-button"), "", attributes);
  101. }
  102. private Element createElement(String elementName, String content,
  103. String caption) {
  104. Attributes attributes = new Attributes();
  105. if (caption != null) {
  106. attributes.put("caption", caption);
  107. }
  108. Element node = new Element(Tag.valueOf(elementName), "", attributes);
  109. if (content != null) {
  110. node.html(content);
  111. }
  112. return node;
  113. }
  114. }