2 * Copyright 2000-2014 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.tests.server.component.abstractselect;
18 import org.jsoup.nodes.Attributes;
19 import org.jsoup.nodes.Element;
20 import org.jsoup.parser.Tag;
21 import org.junit.Assert;
22 import org.junit.Test;
24 import com.vaadin.data.Container;
25 import com.vaadin.data.util.IndexedContainer;
26 import com.vaadin.server.ExternalResource;
27 import com.vaadin.server.Resource;
28 import com.vaadin.tests.design.DeclarativeTestBase;
29 import com.vaadin.ui.AbstractSelect;
30 import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
31 import com.vaadin.ui.ComboBox;
32 import com.vaadin.ui.ListSelect;
33 import com.vaadin.ui.declarative.DesignContext;
34 import com.vaadin.ui.declarative.DesignException;
37 * Test cases for reading the properties of selection components.
41 public class AbstractSelectDeclarativeTest extends
42 DeclarativeTestBase<AbstractSelect> {
44 public String getDesignSingleSelectNewItemsAllowed() {
45 return "<v-combo-box new-items-allowed='' item-caption-mode='icon_only'"
46 + " null-selection-item-id='nullIid'/>";
50 public AbstractSelect getExpectedSingleSelectNewItemsAllowed() {
51 ComboBox c = new ComboBox();
52 c.setNewItemsAllowed(true);
53 c.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
54 c.setNullSelectionAllowed(true);// Default
55 c.setNullSelectionItemId("nullIid");
59 public String getDesignMultiSelect() {
60 return "<v-list-select multi-select='' null-selection-allowed='false' new-items-allowed='' item-caption-mode='property' />";
63 public AbstractSelect getExpectedMultiSelect() {
64 ListSelect c = new ListSelect();
65 c.setNewItemsAllowed(true);
66 c.setNullSelectionAllowed(false);
67 c.setItemCaptionMode(ItemCaptionMode.PROPERTY);
68 c.setMultiSelect(true);
73 public void testReadSingleSelectNewItemsAllowed() {
74 testRead(getDesignSingleSelectNewItemsAllowed(),
75 getExpectedSingleSelectNewItemsAllowed());
79 public void testWriteSingleSelectNewItemsAllowed() {
80 testWrite(getDesignSingleSelectNewItemsAllowed(),
81 getExpectedSingleSelectNewItemsAllowed());
85 public void testReadMultiSelect() {
86 testRead(getDesignMultiSelect(), getExpectedMultiSelect());
90 public void testWriteMultiSelect() {
91 testWrite(getDesignMultiSelect(), getExpectedMultiSelect());
95 public void testReadInlineData() {
96 testRead(getDesignForInlineData(), getExpectedComponentForInlineData());
99 @Test(expected = DesignException.class)
100 public void testReadMultipleValuesForSingleSelect() {
101 testRead("<v-list-select>" + "<option selected>1</option>"
102 + "<option selected>2</option>" + "</v-list-select>", null);
106 public void testReadMultipleValuesForMultiSelect() {
107 ListSelect ls = new ListSelect();
108 ls.setMultiSelect(true);
113 testRead("<v-list-select multi-select>" + "<option selected>1</option>"
114 + "<option selected>2</option>" + "</v-list-select>", ls);
118 public void testReadSingleValueForMultiSelect() {
119 ListSelect ls = new ListSelect();
120 ls.setMultiSelect(true);
124 testRead("<v-list-select multi-select>" + "<option selected>1</option>"
125 + "<option>2</option>" + "</v-list-select>", ls);
129 public void testReadSingleValueForSingleSelect() {
130 ListSelect ls = new ListSelect();
131 ls.setMultiSelect(false);
135 testRead("<v-list-select>" + "<option selected>1</option>"
136 + "<option>2</option>" + "</v-list-select>", ls);
140 public void testWriteInlineDataIgnored() {
141 // No data is written by default
142 testWrite(stripOptionTags(getDesignForInlineData()),
143 getExpectedComponentForInlineData());
147 public void testWriteInlineData() {
148 testWrite(getDesignForInlineData(),
149 getExpectedComponentForInlineData(), true);
152 private String getDesignForInlineData() {
153 return "<v-list-select>\n"
154 + " <option icon='http://some.url/icon.png'>Value 1</option>\n" //
155 + " <option selected>Value 2</option>\n"//
156 + "</v-list-select>";
159 private AbstractSelect getExpectedComponentForInlineData() {
160 AbstractSelect as = new ListSelect();
161 as.addItem("Value 1");
162 as.setItemIcon("Value 1", new ExternalResource(
163 "http://some.url/icon.png"));
164 as.addItem("Value 2");
165 as.setValue("Value 2");
170 public void testReadAttributesSingleSelect() {
171 Element design = createDesignWithAttributesSingleSelect();
172 ComboBox cb = new ComboBox();
173 IndexedContainer container = new IndexedContainer();
174 container.addContainerProperty("icon", Resource.class, null);
175 container.addContainerProperty("name", String.class, null);
176 cb.setContainerDataSource(container);
177 cb.readDesign(design, new DesignContext());
178 Assert.assertTrue("Adding new items should be allowed.",
179 cb.isNewItemsAllowed());
180 assertEquals("Wrong item caption mode.",
181 AbstractSelect.ItemCaptionMode.PROPERTY,
182 cb.getItemCaptionMode());
183 assertEquals("Wrong item caption property id.", "name",
184 cb.getItemCaptionPropertyId());
185 assertEquals("Wrong item icon property id.", "icon",
186 cb.getItemIconPropertyId());
187 Assert.assertTrue("Null selection should be allowed.",
188 cb.isNullSelectionAllowed());
189 assertEquals("Wrong null selection item id.", "No items selected",
190 cb.getNullSelectionItemId());
194 public void testReadAttributesMultiSelect() {
195 Element design = createDesignWithAttributesMultiSelect();
196 ListSelect ls = new ListSelect();
197 ls.readDesign(design, new DesignContext());
198 Assert.assertTrue("Multi select should be allowed.", ls.isMultiSelect());
199 assertEquals("Wrong caption mode.",
200 AbstractSelect.ItemCaptionMode.EXPLICIT,
201 ls.getItemCaptionMode());
202 Assert.assertFalse("Null selection should not be allowed.",
203 ls.isNullSelectionAllowed());
206 private Element createDesignWithAttributesSingleSelect() {
207 Attributes attributes = new Attributes();
208 attributes.put("new-items-allowed", "");
209 attributes.put("multi-select", "false");
210 attributes.put("item-caption-mode", "property");
211 attributes.put("item-caption-property-id", "name");
212 attributes.put("item-icon-property-id", "icon");
213 attributes.put("null-selection-allowed", "");
214 attributes.put("null-selection-item-id", "No items selected");
215 return new Element(Tag.valueOf("v-combo-box"), "", attributes);
218 private Element createDesignWithAttributesMultiSelect() {
219 Attributes attributes = new Attributes();
220 attributes.put("multi-select", "");
221 attributes.put("item-caption-mode", "EXPLICIT");
222 attributes.put("null-selection-allowed", "false");
223 return new Element(Tag.valueOf("v-list-select"), "", attributes);
227 public void testWriteAttributesSingleSelect() {
228 ComboBox cb = createSingleSelectWithOnlyAttributes();
229 Element e = new Element(Tag.valueOf("v-combo-box"), "");
230 cb.writeDesign(e, new DesignContext());
231 assertEquals("Wrong caption for the combo box.", "A combo box",
233 Assert.assertTrue("Adding new items should be allowed.",
234 "".equals(e.attr("new-items-allowed")));
235 assertEquals("Wrong item caption mode.", "icon_only",
236 e.attr("item-caption-mode"));
237 assertEquals("Wrong item icon property id.", "icon",
238 e.attr("item-icon-property-id"));
240 "Null selection should be allowed.",
241 "".equals(e.attr("null-selection-allowed"))
242 || "true".equals(e.attr("null-selection-allowed")));
243 assertEquals("Wrong null selection item id.", "No item selected",
244 e.attr("null-selection-item-id"));
248 public void testWriteMultiListSelect() {
249 ListSelect ls = createMultiSelect();
250 Element e = new Element(Tag.valueOf("v-list-select"), "");
251 ls.writeDesign(e, new DesignContext());
252 assertEquals("Null selection should not be allowed.", "false",
253 e.attr("null-selection-allowed"));
255 "Multi select should be allowed.",
256 "".equals(e.attr("multi-select"))
257 || "true".equals(e.attr("multi-select")));
260 public ComboBox createSingleSelectWithOnlyAttributes() {
261 ComboBox cb = new ComboBox();
262 Container dataSource = new IndexedContainer();
263 dataSource.addContainerProperty("icon", Resource.class, null);
264 cb.setContainerDataSource(dataSource);
265 cb.setCaption("A combo box");
266 cb.setNewItemsAllowed(true);
267 cb.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
268 cb.setItemIconPropertyId("icon");
269 cb.setNullSelectionAllowed(true);
270 cb.setNullSelectionItemId("No item selected");
274 public ListSelect createMultiSelect() {
275 ListSelect ls = new ListSelect();
276 ls.setNullSelectionAllowed(false);
277 ls.setMultiSelect(true);