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.

AbstractSelectDeclarativeTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.abstractselect;
  17. import org.jsoup.nodes.Attributes;
  18. import org.jsoup.nodes.Element;
  19. import org.jsoup.parser.Tag;
  20. import org.junit.Assert;
  21. import org.junit.Test;
  22. import com.vaadin.data.Container;
  23. import com.vaadin.data.util.IndexedContainer;
  24. import com.vaadin.server.ExternalResource;
  25. import com.vaadin.server.Resource;
  26. import com.vaadin.tests.design.DeclarativeTestBase;
  27. import com.vaadin.tests.design.DeclarativeTestBaseBase;
  28. import com.vaadin.ui.AbstractSelect;
  29. import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
  30. import com.vaadin.ui.ComboBox;
  31. import com.vaadin.ui.ListSelect;
  32. import com.vaadin.ui.declarative.DesignContext;
  33. import com.vaadin.ui.declarative.DesignException;
  34. /**
  35. * Test cases for reading the properties of selection components.
  36. *
  37. * @author Vaadin Ltd
  38. */
  39. public class AbstractSelectDeclarativeTest extends
  40. DeclarativeTestBase<AbstractSelect> {
  41. public String getDesignSingleSelectNewItemsAllowed() {
  42. return "<vaadin-combo-box new-items-allowed item-caption-mode='icon_only'"
  43. + " null-selection-item-id='nullIid'/>";
  44. }
  45. public AbstractSelect getExpectedSingleSelectNewItemsAllowed() {
  46. ComboBox c = new ComboBox();
  47. c.setNewItemsAllowed(true);
  48. c.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
  49. c.setNullSelectionAllowed(true);// Default
  50. c.setNullSelectionItemId("nullIid");
  51. return c;
  52. }
  53. public String getDesignMultiSelect() {
  54. return "<vaadin-list-select multi-select null-selection-allowed='false' new-items-allowed item-caption-mode='property' />";
  55. }
  56. public AbstractSelect getExpectedMultiSelect() {
  57. ListSelect c = new ListSelect();
  58. c.setNewItemsAllowed(true);
  59. c.setNullSelectionAllowed(false);
  60. c.setItemCaptionMode(ItemCaptionMode.PROPERTY);
  61. c.setMultiSelect(true);
  62. return c;
  63. }
  64. @Test
  65. public void testReadSingleSelectNewItemsAllowed() {
  66. testRead(getDesignSingleSelectNewItemsAllowed(),
  67. getExpectedSingleSelectNewItemsAllowed());
  68. }
  69. @Test
  70. public void testWriteSingleSelectNewItemsAllowed() {
  71. testWrite(getDesignSingleSelectNewItemsAllowed(),
  72. getExpectedSingleSelectNewItemsAllowed());
  73. }
  74. @Test
  75. public void testReadMultiSelect() {
  76. testRead(getDesignMultiSelect(), getExpectedMultiSelect());
  77. }
  78. @Test
  79. public void testWriteMultiSelect() {
  80. testWrite(getDesignMultiSelect(), getExpectedMultiSelect());
  81. }
  82. @Test
  83. public void testReadInlineData() {
  84. testRead(getDesignForInlineData(), getExpectedComponentForInlineData());
  85. }
  86. @Test(expected = DesignException.class)
  87. public void testReadMultipleValuesForSingleSelect() {
  88. testRead("<vaadin-list-select>" + "<option selected>1</option>"
  89. + "<option selected>2</option>" + "</vaadin-list-select>", null);
  90. }
  91. @Test
  92. public void testReadMultipleValuesForMultiSelect() {
  93. ListSelect ls = new ListSelect();
  94. ls.setMultiSelect(true);
  95. ls.addItem("1");
  96. ls.addItem("2");
  97. ls.select("1");
  98. ls.select("2");
  99. testRead("<vaadin-list-select multi-select>"
  100. + "<option selected>1</option>" + "<option selected>2</option>"
  101. + "</vaadin-list-select>", ls);
  102. }
  103. @Test
  104. public void testReadSingleValueForMultiSelect() {
  105. ListSelect ls = new ListSelect();
  106. ls.setMultiSelect(true);
  107. ls.addItem("1");
  108. ls.addItem("2");
  109. ls.select("1");
  110. testRead("<vaadin-list-select multi-select>"
  111. + "<option selected>1</option>" + "<option>2</option>"
  112. + "</vaadin-list-select>", ls);
  113. }
  114. @Test
  115. public void testReadSingleValueForSingleSelect() {
  116. ListSelect ls = new ListSelect();
  117. ls.setMultiSelect(false);
  118. ls.addItem("1");
  119. ls.addItem("2");
  120. ls.select("1");
  121. testRead("<vaadin-list-select>" + "<option selected>1</option>"
  122. + "<option>2</option>" + "</vaadin-list-select>", ls);
  123. }
  124. @Test
  125. public void testWriteInlineDataIgnored() {
  126. // No data is written by default
  127. testWrite(stripOptionTags(getDesignForInlineData()),
  128. getExpectedComponentForInlineData());
  129. }
  130. @Test
  131. public void testWriteInlineData() {
  132. testWrite(getDesignForInlineData(),
  133. getExpectedComponentForInlineData(), true);
  134. }
  135. private String getDesignForInlineData() {
  136. return "<vaadin-list-select>\n"
  137. + " <option icon='http://some.url/icon.png'>Value 1</option>\n" //
  138. + " <option selected>Value 2</option>\n"//
  139. + "</vaadin-list-select>";
  140. }
  141. private AbstractSelect getExpectedComponentForInlineData() {
  142. AbstractSelect as = new ListSelect();
  143. as.addItem("Value 1");
  144. as.setItemIcon("Value 1", new ExternalResource(
  145. "http://some.url/icon.png"));
  146. as.addItem("Value 2");
  147. as.setValue("Value 2");
  148. return as;
  149. }
  150. @Test
  151. public void testReadAttributesSingleSelect() {
  152. Element design = createDesignWithAttributesSingleSelect();
  153. ComboBox cb = new ComboBox();
  154. IndexedContainer container = new IndexedContainer();
  155. container.addContainerProperty("icon", Resource.class, null);
  156. container.addContainerProperty("name", String.class, null);
  157. cb.setContainerDataSource(container);
  158. cb.readDesign(design, new DesignContext());
  159. Assert.assertTrue("Adding new items should be allowed.",
  160. cb.isNewItemsAllowed());
  161. assertEquals("Wrong item caption mode.",
  162. AbstractSelect.ItemCaptionMode.PROPERTY,
  163. cb.getItemCaptionMode());
  164. assertEquals("Wrong item caption property id.", "name",
  165. cb.getItemCaptionPropertyId());
  166. assertEquals("Wrong item icon property id.", "icon",
  167. cb.getItemIconPropertyId());
  168. Assert.assertTrue("Null selection should be allowed.",
  169. cb.isNullSelectionAllowed());
  170. assertEquals("Wrong null selection item id.", "No items selected",
  171. cb.getNullSelectionItemId());
  172. }
  173. @Test
  174. public void testReadAttributesMultiSelect() {
  175. Element design = createDesignWithAttributesMultiSelect();
  176. ListSelect ls = new ListSelect();
  177. ls.readDesign(design, new DesignContext());
  178. Assert.assertTrue("Multi select should be allowed.", ls.isMultiSelect());
  179. assertEquals("Wrong caption mode.",
  180. AbstractSelect.ItemCaptionMode.EXPLICIT,
  181. ls.getItemCaptionMode());
  182. Assert.assertFalse("Null selection should not be allowed.",
  183. ls.isNullSelectionAllowed());
  184. }
  185. private Element createDesignWithAttributesSingleSelect() {
  186. Attributes attributes = new Attributes();
  187. attributes.put("new-items-allowed", true);
  188. attributes.put("multi-select", "false");
  189. attributes.put("item-caption-mode", "property");
  190. attributes.put("item-caption-property-id", "name");
  191. attributes.put("item-icon-property-id", "icon");
  192. attributes.put("null-selection-allowed", true);
  193. attributes.put("null-selection-item-id", "No items selected");
  194. return new Element(Tag.valueOf("vaadin-combo-box"), "", attributes);
  195. }
  196. private Element createDesignWithAttributesMultiSelect() {
  197. Attributes attributes = new Attributes();
  198. attributes.put("multi-select", true);
  199. attributes.put("item-caption-mode", "EXPLICIT");
  200. attributes.put("null-selection-allowed", "false");
  201. return new Element(Tag.valueOf("vaadin-list-select"), "", attributes);
  202. }
  203. @Test
  204. public void testWriteAttributesSingleSelect() {
  205. ComboBox cb = createSingleSelectWithOnlyAttributes();
  206. Element e = new Element(Tag.valueOf("vaadin-combo-box"), "");
  207. cb.writeDesign(e, new DesignContext());
  208. assertEquals("Wrong caption for the combo box.", "A combo box",
  209. e.attr("caption"));
  210. Assert.assertTrue("Adding new items should be allowed.",
  211. "".equals(e.attr("new-items-allowed")));
  212. assertEquals("Wrong item caption mode.", "icon_only",
  213. e.attr("item-caption-mode"));
  214. assertEquals("Wrong item icon property id.", "icon",
  215. e.attr("item-icon-property-id"));
  216. Assert.assertTrue(
  217. "Null selection should be allowed.",
  218. "".equals(e.attr("null-selection-allowed"))
  219. || "true".equals(e.attr("null-selection-allowed")));
  220. assertEquals("Wrong null selection item id.", "No item selected",
  221. e.attr("null-selection-item-id"));
  222. }
  223. @Test
  224. public void testWriteMultiListSelect() {
  225. ListSelect ls = createMultiSelect();
  226. Element e = new Element(Tag.valueOf("vaadin-list-select"), "");
  227. ls.writeDesign(e, new DesignContext());
  228. assertEquals("Null selection should not be allowed.", "false",
  229. e.attr("null-selection-allowed"));
  230. Assert.assertTrue(
  231. "Multi select should be allowed.",
  232. "".equals(e.attr("multi-select"))
  233. || "true".equals(e.attr("multi-select")));
  234. }
  235. @Test
  236. public void testHtmlEntities() {
  237. String design = "<vaadin-combo-box>"
  238. + " <option item-id=\"one\">&gt; One</option>"
  239. + " <option>&gt; Two</option>" + "</vaadin-combo-box>";
  240. AbstractSelect read = read(design);
  241. Assert.assertEquals("> One", read.getItemCaption("one"));
  242. AbstractSelect underTest = new ComboBox();
  243. underTest.addItem("> One");
  244. Element root = new Element(Tag.valueOf("vaadin-combo-box"), "");
  245. DesignContext dc = new DesignContext();
  246. dc.setShouldWriteDataDelegate(DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
  247. underTest.writeDesign(root, dc);
  248. Assert.assertEquals("&gt; One", root.getElementsByTag("option").first()
  249. .html());
  250. }
  251. public ComboBox createSingleSelectWithOnlyAttributes() {
  252. ComboBox cb = new ComboBox();
  253. Container dataSource = new IndexedContainer();
  254. dataSource.addContainerProperty("icon", Resource.class, null);
  255. cb.setContainerDataSource(dataSource);
  256. cb.setCaption("A combo box");
  257. cb.setNewItemsAllowed(true);
  258. cb.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
  259. cb.setItemIconPropertyId("icon");
  260. cb.setNullSelectionAllowed(true);
  261. cb.setNullSelectionItemId("No item selected");
  262. return cb;
  263. }
  264. public ListSelect createMultiSelect() {
  265. ListSelect ls = new ListSelect();
  266. ls.setNullSelectionAllowed(false);
  267. ls.setMultiSelect(true);
  268. return ls;
  269. }
  270. }