]> source.dussan.org Git - vaadin-framework.git/blob
7733855e95fd6d4cde3728d07c80caba761e1d2d
[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.abstractcomponent;
17
18 import static org.junit.Assert.assertTrue;
19
20 import java.io.File;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Locale;
24
25 import org.junit.Test;
26
27 import com.vaadin.server.ErrorMessage.ErrorLevel;
28 import com.vaadin.server.ExternalResource;
29 import com.vaadin.server.FileResource;
30 import com.vaadin.server.ThemeResource;
31 import com.vaadin.server.UserError;
32 import com.vaadin.tests.design.DeclarativeTestBase;
33 import com.vaadin.ui.AbstractComponent;
34 import com.vaadin.ui.declarative.DesignContext;
35
36 /**
37  * Abstract test class which contains tests for declarative format for
38  * properties that are common for AbstractComponent.
39  * <p>
40  * It's an abstract so it's not supposed to be run as is. Instead each
41  * declarative test for a real component should extend it and implement abstract
42  * methods to be able to test the common properties. Components specific
43  * properties should be tested additionally in the subclasses implementations.
44  *
45  * @author Vaadin Ltd
46  *
47  */
48 public abstract class AbstractComponentDeclarativeTestBase<T extends AbstractComponent>
49         extends DeclarativeTestBase<T> {
50
51     /**
52      * Returns expected element tag for the tested component.
53      *
54      * @return expected element tag
55      */
56     protected abstract String getComponentTag();
57
58     /**
59      * Returns component class which is a subject to test
60      *
61      * @return the component class
62      */
63     protected abstract Class<? extends T> getComponentClass();
64
65     @Test
66     public void emptyAbstractComponentDeserialization()
67             throws InstantiationException, IllegalAccessException {
68         String design = String.format("<%s/> ", getComponentTag());
69         T component = getComponentClass().newInstance();
70         testRead(design, component);
71         testWrite(design, component);
72     }
73
74     @Test
75     public void abstractComponentAttributesDeserialization()
76             throws InstantiationException, IllegalAccessException,
77             IllegalArgumentException, InvocationTargetException {
78         String id = "testId";
79         String caption = "testCaption";
80         boolean captionAsHtml = true;
81         String description = "testDescription";
82         boolean enabled = false;
83         String error = "<div>testError</div>";
84         String height = "47%";
85         String width = "83px";
86         String icon = "img/example.gif";
87         Locale locale = new Locale("fi", "FI");
88         String primaryStyle = "testPrimaryStyle";
89         boolean readOnly = true;
90         boolean responsive = true;
91         String styleName = "testStyleName";
92         boolean visible = false;
93         boolean requiredIndicator = true;
94
95         String design = String.format(
96                 "<%s id='%s' caption='%s' caption-as-html description='%s' "
97                         + "error='%s' enabled='false' width='%s' height='%s' "
98                         + "icon='%s' locale='%s' primary-style-name='%s' "
99                         + "readonly responsive style-name='%s' visible='false' "
100                         + "required-indicator-visible/>",
101                 getComponentTag(), id, caption, description, error, width,
102                 height, icon, locale.toString(), primaryStyle, styleName);
103
104         T component = getComponentClass().newInstance();
105         component.setId(id);
106         component.setCaption(caption);
107         component.setCaptionAsHtml(captionAsHtml);
108         component.setDescription(description);
109         component.setEnabled(enabled);
110         component.setComponentError(new UserError(error,
111                 com.vaadin.server.AbstractErrorMessage.ContentMode.HTML,
112                 ErrorLevel.ERROR));
113         component.setHeight(height);
114         component.setWidth(width);
115         component.setIcon(new FileResource(new File(icon)));
116         component.setLocale(locale);
117         component.setPrimaryStyleName(primaryStyle);
118         callBooleanSetter(readOnly, "setReadOnly", component);
119         callBooleanSetter(requiredIndicator, "setRequiredIndicatorVisible",
120                 component);
121         component.setResponsive(responsive);
122         component.setStyleName(styleName);
123         component.setVisible(visible);
124
125         testRead(design, component);
126         testWrite(design, component);
127     }
128
129     private void callBooleanSetter(boolean value, String setterName,
130             T component)
131             throws IllegalAccessException, InvocationTargetException {
132         try {
133             Method method = component.getClass().getMethod(setterName,
134                     new Class[] { boolean.class });
135             method.invoke(component, value);
136         } catch (NoSuchMethodException ignore) {
137             // ignore if there is no such method
138         }
139     }
140
141     @Test
142     public void externalIcon()
143             throws InstantiationException, IllegalAccessException {
144         String url = "http://example.com/example.gif";
145
146         String design = String.format("<%s icon='%s'/>", getComponentTag(),
147                 url);
148
149         T component = getComponentClass().newInstance();
150
151         component.setIcon(new ExternalResource(url));
152         testRead(design, component);
153         testWrite(design, component);
154     }
155
156     @Test
157     public void themeIcon()
158             throws InstantiationException, IllegalAccessException {
159         String path = "example.gif";
160
161         String design = String.format("<%s icon='theme://%s'/>",
162                 getComponentTag(), path);
163
164         T component = getComponentClass().newInstance();
165
166         component.setIcon(new ThemeResource(path));
167         testRead(design, component);
168         testWrite(design, component);
169     }
170
171     @Test
172     public void sizeFullDeserialization()
173             throws InstantiationException, IllegalAccessException {
174         String design = String.format("<%s size-full/>", getComponentTag());
175
176         T component = getComponentClass().newInstance();
177
178         component.setSizeFull();
179         testRead(design, component);
180         testWrite(design, component);
181     }
182
183     @Test
184     public void widthFullDeserialization()
185             throws InstantiationException, IllegalAccessException {
186         String design = String.format("<%s width-full/>", getComponentTag());
187
188         T component = getComponentClass().newInstance();
189
190         component.setWidth("100%");
191         testRead(design, component);
192         testWrite(design, component);
193     }
194
195     @Test
196     public void heightFullDeserialization()
197             throws InstantiationException, IllegalAccessException {
198         String design = String.format("<%s height-full/>", getComponentTag());
199
200         T component = getComponentClass().newInstance();
201
202         component.setHeight("100%");
203         testRead(design, component);
204         testWrite(design, component);
205     }
206
207     @Test
208     public void sizeUnderfinedDeserialization()
209             throws InstantiationException, IllegalAccessException {
210         String design = String.format("<%s/>", getComponentTag());
211
212         T component = getComponentClass().newInstance();
213
214         component.setSizeUndefined();
215         testRead(design, component);
216         testWrite(design, component);
217
218     }
219
220     @Test
221     public void heightUnderfinedDeserialization()
222             throws InstantiationException, IllegalAccessException {
223         String design = String.format("<%s/>", getComponentTag());
224
225         T component = getComponentClass().newInstance();
226
227         component.setHeightUndefined();
228         testRead(design, component);
229         testWrite(design, component);
230     }
231
232     @Test
233     public void widthUnderfinedDeserialization()
234             throws InstantiationException, IllegalAccessException {
235         String design = String.format("<%s/>", getComponentTag());
236
237         T component = getComponentClass().newInstance();
238
239         component.setWidthUndefined();
240         testRead(design, component);
241         testWrite(design, component);
242     }
243
244     @Test
245     public void testUnknownAttribute() {
246         String value = "bar";
247         String design = String.format("<%s foo='%s'/>", getComponentTag(),
248                 value);
249
250         DesignContext context = readAndReturnContext(design);
251         T label = getComponentClass().cast(context.getRootComponent());
252         assertTrue("Custom attribute was preserved in custom attributes",
253                 context.getCustomAttributes(label).containsKey("foo"));
254
255         testWrite(label, design, context);
256     }
257
258 }