2 * Copyright 2000-2016 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.v7.tests.server.component.abstractselect;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
21 import org.jsoup.nodes.Attributes;
22 import org.jsoup.nodes.Element;
23 import org.jsoup.parser.Tag;
24 import org.junit.Test;
26 import com.vaadin.server.ExternalResource;
27 import com.vaadin.server.Resource;
28 import com.vaadin.tests.design.DeclarativeTestBase;
29 import com.vaadin.tests.design.DeclarativeTestBaseBase;
30 import com.vaadin.ui.declarative.DesignContext;
31 import com.vaadin.ui.declarative.DesignException;
32 import com.vaadin.v7.data.Container;
33 import com.vaadin.v7.data.util.IndexedContainer;
34 import com.vaadin.v7.ui.AbstractSelect;
35 import com.vaadin.v7.ui.AbstractSelect.ItemCaptionMode;
36 import com.vaadin.v7.ui.ComboBox;
37 import com.vaadin.v7.ui.ListSelect;
40 * Test cases for reading the properties of selection components.
44 public class AbstractSelectDeclarativeTest
45 extends DeclarativeTestBase<AbstractSelect> {
47 public String getDesignSingleSelectNewItemsAllowed() {
48 return "<vaadin7-combo-box new-items-allowed item-caption-mode='icon_only'"
49 + " null-selection-item-id='nullIid'/>";
53 public AbstractSelect getExpectedSingleSelectNewItemsAllowed() {
54 ComboBox c = new ComboBox();
55 c.setNewItemsAllowed(true);
56 c.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
57 c.setNullSelectionAllowed(true);// Default
58 c.setNullSelectionItemId("nullIid");
62 public String getDesignMultiSelect() {
63 return "<vaadin7-list-select multi-select null-selection-allowed='false' new-items-allowed item-caption-mode='property' />";
66 public AbstractSelect getExpectedMultiSelect() {
67 ListSelect c = new ListSelect();
68 c.setNewItemsAllowed(true);
69 c.setNullSelectionAllowed(false);
70 c.setItemCaptionMode(ItemCaptionMode.PROPERTY);
71 c.setMultiSelect(true);
76 public void testReadSingleSelectNewItemsAllowed() {
77 testRead(getDesignSingleSelectNewItemsAllowed(),
78 getExpectedSingleSelectNewItemsAllowed());
82 public void testWriteSingleSelectNewItemsAllowed() {
83 testWrite(getDesignSingleSelectNewItemsAllowed(),
84 getExpectedSingleSelectNewItemsAllowed());
88 public void testReadMultiSelect() {
89 testRead(getDesignMultiSelect(), getExpectedMultiSelect());
93 public void testWriteMultiSelect() {
94 testWrite(getDesignMultiSelect(), getExpectedMultiSelect());
98 public void testReadInlineData() {
99 testRead(getDesignForInlineData(), getExpectedComponentForInlineData());
102 @Test(expected = DesignException.class)
103 public void testReadMultipleValuesForSingleSelect() {
104 testRead("<vaadin7-list-select>" + "<option selected>1</option>"
105 + "<option selected>2</option>" + "</vaadin7-list-select>",
110 public void testReadMultipleValuesForMultiSelect() {
111 ListSelect ls = new ListSelect();
112 ls.setMultiSelect(true);
117 testRead("<vaadin7-list-select multi-select>"
118 + "<option selected>1</option>" + "<option selected>2</option>"
119 + "</vaadin7-list-select>", ls);
123 public void testReadSingleValueForMultiSelect() {
124 ListSelect ls = new ListSelect();
125 ls.setMultiSelect(true);
129 testRead("<vaadin7-list-select multi-select>"
130 + "<option selected>1</option>" + "<option>2</option>"
131 + "</vaadin7-list-select>", ls);
135 public void testReadSingleValueForSingleSelect() {
136 ListSelect ls = new ListSelect();
137 ls.setMultiSelect(false);
141 testRead("<vaadin7-list-select>" + "<option selected>1</option>"
142 + "<option>2</option>" + "</vaadin7-list-select>", ls);
146 public void testWriteInlineDataIgnored() {
147 // No data is written by default
148 testWrite(stripOptionTags(getDesignForInlineData()),
149 getExpectedComponentForInlineData());
153 public void testWriteInlineData() {
154 testWrite(getDesignForInlineData(), getExpectedComponentForInlineData(),
158 private String getDesignForInlineData() {
159 return "<vaadin7-list-select>\n"
160 + " <option icon='http://some.url/icon.png'>Value 1</option>\n" //
161 + " <option selected>Value 2</option>\n"//
162 + "</vaadin7-list-select>";
165 private AbstractSelect getExpectedComponentForInlineData() {
166 AbstractSelect as = new ListSelect();
167 as.addItem("Value 1");
168 as.setItemIcon("Value 1",
169 new ExternalResource("http://some.url/icon.png"));
170 as.addItem("Value 2");
171 as.setValue("Value 2");
176 public void testReadAttributesSingleSelect() {
177 Element design = createDesignWithAttributesSingleSelect();
178 ComboBox cb = new ComboBox();
179 IndexedContainer container = new IndexedContainer();
180 container.addContainerProperty("icon", Resource.class, null);
181 container.addContainerProperty("name", String.class, null);
182 cb.setContainerDataSource(container);
183 cb.readDesign(design, new DesignContext());
184 assertTrue("Adding new items should be allowed.",
185 cb.isNewItemsAllowed());
186 assertEquals("Wrong item caption mode.",
187 AbstractSelect.ItemCaptionMode.PROPERTY,
188 cb.getItemCaptionMode());
189 assertEquals("Wrong item caption property id.", "name",
190 cb.getItemCaptionPropertyId());
191 assertEquals("Wrong item icon property id.", "icon",
192 cb.getItemIconPropertyId());
193 assertTrue("Null selection should be allowed.",
194 cb.isNullSelectionAllowed());
195 assertEquals("Wrong null selection item id.", "No items selected",
196 cb.getNullSelectionItemId());
200 public void testReadAttributesMultiSelect() {
201 Element design = createDesignWithAttributesMultiSelect();
202 ListSelect ls = new ListSelect();
203 ls.readDesign(design, new DesignContext());
204 assertTrue("Multi select should be allowed.", ls.isMultiSelect());
205 assertEquals("Wrong caption mode.",
206 AbstractSelect.ItemCaptionMode.EXPLICIT,
207 ls.getItemCaptionMode());
208 assertFalse("Null selection should not be allowed.",
209 ls.isNullSelectionAllowed());
212 private Element createDesignWithAttributesSingleSelect() {
213 Attributes attributes = new Attributes();
214 attributes.put("new-items-allowed", true);
215 attributes.put("multi-select", "false");
216 attributes.put("item-caption-mode", "property");
217 attributes.put("item-caption-property-id", "name");
218 attributes.put("item-icon-property-id", "icon");
219 attributes.put("null-selection-allowed", true);
220 attributes.put("null-selection-item-id", "No items selected");
221 return new Element(Tag.valueOf("vaadin-combo-box"), "", attributes);
224 private Element createDesignWithAttributesMultiSelect() {
225 Attributes attributes = new Attributes();
226 attributes.put("multi-select", true);
227 attributes.put("item-caption-mode", "EXPLICIT");
228 attributes.put("null-selection-allowed", "false");
229 return new Element(Tag.valueOf("vaadin-list-select"), "", attributes);
233 public void testWriteAttributesSingleSelect() {
234 ComboBox cb = createSingleSelectWithOnlyAttributes();
235 Element e = new Element(Tag.valueOf("vaadin-combo-box"), "");
236 cb.writeDesign(e, new DesignContext());
237 assertEquals("Wrong caption for the combo box.", "A combo box",
239 assertTrue("Adding new items should be allowed.",
240 "".equals(e.attr("new-items-allowed")));
241 assertEquals("Wrong item caption mode.", "icon_only",
242 e.attr("item-caption-mode"));
243 assertEquals("Wrong item icon property id.", "icon",
244 e.attr("item-icon-property-id"));
245 assertTrue("Null selection should be allowed.",
246 "".equals(e.attr("null-selection-allowed"))
247 || "true".equals(e.attr("null-selection-allowed")));
248 assertEquals("Wrong null selection item id.", "No item selected",
249 e.attr("null-selection-item-id"));
253 public void testWriteMultiListSelect() {
254 ListSelect ls = createMultiSelect();
255 Element e = new Element(Tag.valueOf("vaadin-list-select"), "");
256 ls.writeDesign(e, new DesignContext());
257 assertEquals("Null selection should not be allowed.", "false",
258 e.attr("null-selection-allowed"));
259 assertTrue("Multi select should be allowed.",
260 "".equals(e.attr("multi-select"))
261 || "true".equals(e.attr("multi-select")));
265 public void testHtmlEntities() {
266 String design = "<vaadin7-combo-box>"
267 + " <option item-id=\"one\">> One</option>"
268 + " <option>> Two</option>" + "</vaadin7-combo-box>";
269 AbstractSelect read = read(design);
271 assertEquals("> One", read.getItemCaption("one"));
273 AbstractSelect underTest = new ComboBox();
274 underTest.addItem("> One");
276 Element root = new Element(Tag.valueOf("vaadin-combo-box"), "");
277 DesignContext dc = new DesignContext();
278 dc.setShouldWriteDataDelegate(
279 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
280 underTest.writeDesign(root, dc);
282 assertEquals("> One",
283 root.getElementsByTag("option").first().html());
286 public ComboBox createSingleSelectWithOnlyAttributes() {
287 ComboBox cb = new ComboBox();
288 Container dataSource = new IndexedContainer();
289 dataSource.addContainerProperty("icon", Resource.class, null);
290 cb.setContainerDataSource(dataSource);
291 cb.setCaption("A combo box");
292 cb.setNewItemsAllowed(true);
293 cb.setItemCaptionMode(ItemCaptionMode.ICON_ONLY);
294 cb.setItemIconPropertyId("icon");
295 cb.setNullSelectionAllowed(true);
296 cb.setNullSelectionItemId("No item selected");
300 public ListSelect createMultiSelect() {
301 ListSelect ls = new ListSelect();
302 ls.setNullSelectionAllowed(false);
303 ls.setMultiSelect(true);