]> source.dussan.org Git - vaadin-framework.git/blob
be2258369793404ffd38fb5d22b317074e5541c3
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 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.v7.tests.server.component.abstractselect;
17
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20
21 import org.jsoup.nodes.Attributes;
22 import org.jsoup.nodes.Element;
23 import org.jsoup.parser.Tag;
24 import org.junit.Test;
25
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;
38
39 /**
40  * Test cases for reading the properties of selection components.
41  *
42  * @author Vaadin Ltd
43  */
44 public class AbstractSelectDeclarativeTest
45         extends DeclarativeTestBase<AbstractSelect> {
46
47     public String getDesignSingleSelectNewItemsAllowed() {
48         return "<vaadin7-combo-box new-items-allowed item-caption-mode='icon_only'"
49                 + " null-selection-item-id='nullIid'/>";
50
51     }
52
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");
59         return c;
60     }
61
62     public String getDesignMultiSelect() {
63         return "<vaadin7-list-select multi-select null-selection-allowed='false' new-items-allowed item-caption-mode='property' />";
64     }
65
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);
72         return c;
73     }
74
75     @Test
76     public void testReadSingleSelectNewItemsAllowed() {
77         testRead(getDesignSingleSelectNewItemsAllowed(),
78                 getExpectedSingleSelectNewItemsAllowed());
79     }
80
81     @Test
82     public void testWriteSingleSelectNewItemsAllowed() {
83         testWrite(getDesignSingleSelectNewItemsAllowed(),
84                 getExpectedSingleSelectNewItemsAllowed());
85     }
86
87     @Test
88     public void testReadMultiSelect() {
89         testRead(getDesignMultiSelect(), getExpectedMultiSelect());
90     }
91
92     @Test
93     public void testWriteMultiSelect() {
94         testWrite(getDesignMultiSelect(), getExpectedMultiSelect());
95     }
96
97     @Test
98     public void testReadInlineData() {
99         testRead(getDesignForInlineData(), getExpectedComponentForInlineData());
100     }
101
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>",
106                 null);
107     }
108
109     @Test
110     public void testReadMultipleValuesForMultiSelect() {
111         ListSelect ls = new ListSelect();
112         ls.setMultiSelect(true);
113         ls.addItem("1");
114         ls.addItem("2");
115         ls.select("1");
116         ls.select("2");
117         testRead("<vaadin7-list-select multi-select>"
118                 + "<option selected>1</option>" + "<option selected>2</option>"
119                 + "</vaadin7-list-select>", ls);
120     }
121
122     @Test
123     public void testReadSingleValueForMultiSelect() {
124         ListSelect ls = new ListSelect();
125         ls.setMultiSelect(true);
126         ls.addItem("1");
127         ls.addItem("2");
128         ls.select("1");
129         testRead("<vaadin7-list-select multi-select>"
130                 + "<option selected>1</option>" + "<option>2</option>"
131                 + "</vaadin7-list-select>", ls);
132     }
133
134     @Test
135     public void testReadSingleValueForSingleSelect() {
136         ListSelect ls = new ListSelect();
137         ls.setMultiSelect(false);
138         ls.addItem("1");
139         ls.addItem("2");
140         ls.select("1");
141         testRead("<vaadin7-list-select>" + "<option selected>1</option>"
142                 + "<option>2</option>" + "</vaadin7-list-select>", ls);
143     }
144
145     @Test
146     public void testWriteInlineDataIgnored() {
147         // No data is written by default
148         testWrite(stripOptionTags(getDesignForInlineData()),
149                 getExpectedComponentForInlineData());
150     }
151
152     @Test
153     public void testWriteInlineData() {
154         testWrite(getDesignForInlineData(), getExpectedComponentForInlineData(),
155                 true);
156     }
157
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>";
163     }
164
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");
172         return as;
173     }
174
175     @Test
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());
197     }
198
199     @Test
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());
210     }
211
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);
222     }
223
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);
230     }
231
232     @Test
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",
238                 e.attr("caption"));
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"));
250     }
251
252     @Test
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")));
262     }
263
264     @Test
265     public void testHtmlEntities() {
266         String design = "<vaadin7-combo-box>"
267                 + "  <option item-id=\"one\">&gt; One</option>"
268                 + "  <option>&gt; Two</option>" + "</vaadin7-combo-box>";
269         AbstractSelect read = read(design);
270
271         assertEquals("> One", read.getItemCaption("one"));
272
273         AbstractSelect underTest = new ComboBox();
274         underTest.addItem("> One");
275
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);
281
282         assertEquals("&gt; One",
283                 root.getElementsByTag("option").first().html());
284     }
285
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");
297         return cb;
298     }
299
300     public ListSelect createMultiSelect() {
301         ListSelect ls = new ListSelect();
302         ls.setNullSelectionAllowed(false);
303         ls.setMultiSelect(true);
304         return ls;
305     }
306
307 }