]> source.dussan.org Git - vaadin-framework.git/blob
9dcbb31246d73b233ccfd32fc0ff02241c64909f
[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.tests.server.component.abstractsingleselect;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 import com.vaadin.tests.design.DeclarativeTestBaseBase;
27 import com.vaadin.tests.server.component.abstractlisting.AbstractListingDeclarativeTest;
28 import com.vaadin.ui.AbstractSingleSelect;
29 import com.vaadin.ui.ItemCaptionGenerator;
30 import com.vaadin.ui.declarative.DesignContext;
31
32 /**
33  * {@link AbstractSingleSelect} component declarative test.
34  * <p>
35  * Test inherits test methods from a {@link AbstractListingDeclarativeTest}
36  * class providing here only common cases for {@link AbstractSingleSelect}s.
37  *
38  * @author Vaadin Ltd
39  *
40  *
41  * @param <T>
42  *            a component type
43  */
44 @SuppressWarnings({ "unchecked", "rawtypes" })
45 public abstract class AbstractSingleSelectDeclarativeTest<T extends AbstractSingleSelect>
46         extends AbstractListingDeclarativeTest<T> {
47
48     @Override
49     @Test
50     public void dataSerialization() throws InstantiationException,
51             IllegalAccessException, InvocationTargetException {
52         List<String> items = Arrays.asList("foo", "bar", "foobar");
53
54         String type = "com.vaadin.SomeType";
55         String attribute = "data-type";
56
57         String design = String.format(
58                 "<%s %s='%s'>\n" + "<option item='foo'>foo</option>\n"
59                         + "<option item='bar' selected>bar</option>"
60                         + "<option item='foobar'>foobar</option></%s>",
61                 getComponentTag(), attribute, type, getComponentTag());
62         T component = getComponentClass().newInstance();
63         component.setItems(items);
64         component.setSelectedItem("bar");
65
66         DesignContext context = readComponentAndCompare(design, component);
67         assertEquals(type,
68                 context.getCustomAttributes(context.getRootComponent())
69                         .get(attribute));
70         context = new DesignContext();
71         context.setCustomAttribute(component, attribute, type);
72         context.setShouldWriteDataDelegate(
73                 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
74         testWrite(component, design, context);
75     }
76
77     @Override
78     @Test
79     public void valueSerialization() throws InstantiationException,
80             IllegalAccessException, InvocationTargetException {
81         List<String> items = Arrays.asList("foo", "bar", "foobar");
82
83         String type = "com.vaadin.SomeType";
84         String attribute = "data-type";
85
86         String design = String.format(
87                 "<%s  %s='%s'>\n" + "<option item='foo'>foo</option>\n"
88                         + "<option item='bar' selected>bar</option>"
89                         + "<option item='foobar'>foobar</option></%s>",
90                 getComponentTag(), attribute, type, getComponentTag());
91         T component = getComponentClass().newInstance();
92         component.setItems(items);
93         component.setValue("bar");
94
95         DesignContext context = readComponentAndCompare(design, component);
96         assertEquals(type,
97                 context.getCustomAttributes(context.getRootComponent())
98                         .get(attribute));
99         context = new DesignContext();
100         context.setCustomAttribute(component, attribute, type);
101         context.setShouldWriteDataDelegate(
102                 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
103         testWrite(component, design, context);
104     }
105
106     @Test
107     public void dataWithCaptionGeneratorSerialization()
108             throws InstantiationException, IllegalAccessException,
109             InvocationTargetException {
110         List<String> items = Arrays.asList("foo", "bar", "foobar");
111         T component = getComponentClass().newInstance();
112         Method setItemCaptionGenerator = getItemCaptionGeneratorMethod(
113                 component);
114         if (setItemCaptionGenerator == null) {
115             return;
116         }
117         String design = String.format(
118                 "<%s>\n" + "<option item='foo'>foo1</option>\n"
119                         + "<option item='bar' selected>bar1</option>"
120                         + "<option item='foobar'>foobar1</option></%s>",
121                 getComponentTag(), getComponentTag());
122         component.setItems(items);
123         component.setValue("bar");
124         ItemCaptionGenerator generator = item -> item + "1";
125         setItemCaptionGenerator.invoke(component, generator);
126
127         testRead(design, component);
128         testWrite(design, component, true);
129     }
130
131     @Override
132     @Test
133     public void readOnlySelection() throws InstantiationException,
134             IllegalAccessException, InvocationTargetException {
135         T component = getComponentClass().newInstance();
136
137         List<String> items = Arrays.asList("foo", "bar", "foobar");
138
139         String design = String.format(
140                 "<%s readonly>\n" + "<option item='foo'>foo</option>\n"
141                         + "<option item='bar'>bar</option>"
142                         + "<option item='foobar'>foobar</option>",
143                 getComponentTag(), getComponentTag());
144
145         component.setItems(items);
146         component.setReadOnly(true);
147
148         testRead(design, component);
149         testWrite(design, component, true);
150     }
151
152     private Method getItemCaptionGeneratorMethod(T component)
153             throws IllegalAccessException, InvocationTargetException {
154         try {
155             return component.getClass().getMethod("setItemCaptionGenerator",
156                     new Class[] { ItemCaptionGenerator.class });
157         } catch (NoSuchMethodException ignore) {
158             // ignore if there is no such method
159             return null;
160         }
161     }
162
163 }