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

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