]> source.dussan.org Git - vaadin-framework.git/blob
b294ffad72c633feaa5aabe0be53dc83043a0e8f
[vaadin-framework.git] /
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
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;
23
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;
35
36 /**
37  * Test cases for reading the properties of selection components.
38  * 
39  * @author Vaadin Ltd
40  */
41 public class AbstractSelectDeclarativeTest extends
42         DeclarativeTestBase<AbstractSelect> {
43
44     public String getDesignSingleSelectNewItemsAllowed() {
45         return "<v-combo-box new-items-allowed='' item-caption-mode='icon_only'"
46                 + " null-selection-item-id='nullIid'/>";
47
48     }
49
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");
56         return c;
57     }
58
59     public String getDesignMultiSelect() {
60         return "<v-list-select multi-select='' null-selection-allowed='false' new-items-allowed='' item-caption-mode='property' />";
61     }
62
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);
69         return c;
70     }
71
72     @Test
73     public void testReadSingleSelectNewItemsAllowed() {
74         testRead(getDesignSingleSelectNewItemsAllowed(),
75                 getExpectedSingleSelectNewItemsAllowed());
76     }
77
78     @Test
79     public void testWriteSingleSelectNewItemsAllowed() {
80         testWrite(getDesignSingleSelectNewItemsAllowed(),
81                 getExpectedSingleSelectNewItemsAllowed());
82     }
83
84     @Test
85     public void testReadMultiSelect() {
86         testRead(getDesignMultiSelect(), getExpectedMultiSelect());
87     }
88
89     @Test
90     public void testWriteMultiSelect() {
91         testWrite(getDesignMultiSelect(), getExpectedMultiSelect());
92     }
93
94     @Test
95     public void testReadInlineData() {
96         testRead(getDesignForInlineData(), getExpectedComponentForInlineData());
97     }
98
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);
103     }
104
105     @Test
106     public void testReadMultipleValuesForMultiSelect() {
107         ListSelect ls = new ListSelect();
108         ls.setMultiSelect(true);
109         ls.addItem("1");
110         ls.addItem("2");
111         ls.select("1");
112         ls.select("2");
113         testRead("<v-list-select multi-select>" + "<option selected>1</option>"
114                 + "<option selected>2</option>" + "</v-list-select>", ls);
115     }
116
117     @Test
118     public void testReadSingleValueForMultiSelect() {
119         ListSelect ls = new ListSelect();
120         ls.setMultiSelect(true);
121         ls.addItem("1");
122         ls.addItem("2");
123         ls.select("1");
124         testRead("<v-list-select multi-select>" + "<option selected>1</option>"
125                 + "<option>2</option>" + "</v-list-select>", ls);
126     }
127
128     @Test
129     public void testReadSingleValueForSingleSelect() {
130         ListSelect ls = new ListSelect();
131         ls.setMultiSelect(false);
132         ls.addItem("1");
133         ls.addItem("2");
134         ls.select("1");
135         testRead("<v-list-select>" + "<option selected>1</option>"
136                 + "<option>2</option>" + "</v-list-select>", ls);
137     }
138
139     @Test
140     public void testWriteInlineDataIgnored() {
141         // No data is written by default
142         testWrite(stripOptionTags(getDesignForInlineData()),
143                 getExpectedComponentForInlineData());
144     }
145
146     @Test
147     public void testWriteInlineData() {
148         testWrite(getDesignForInlineData(),
149                 getExpectedComponentForInlineData(), true);
150     }
151
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>";
157     }
158
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");
166         return as;
167     }
168
169     @Test
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());
191     }
192
193     @Test
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());
204     }
205
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);
216     }
217
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);
224     }
225
226     @Test
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",
232                 e.attr("caption"));
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"));
239         Assert.assertTrue(
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"));
245     }
246
247     @Test
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"));
254         Assert.assertTrue(
255                 "Multi select should be allowed.",
256                 "".equals(e.attr("multi-select"))
257                         || "true".equals(e.attr("multi-select")));
258     }
259
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");
271         return cb;
272     }
273
274     public ListSelect createMultiSelect() {
275         ListSelect ls = new ListSelect();
276         ls.setNullSelectionAllowed(false);
277         ls.setMultiSelect(true);
278         return ls;
279     }
280
281 }