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.tests.server.component.abstractcomponent;
18 import static org.junit.Assert.assertTrue;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.Locale;
25 import org.junit.Test;
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;
37 * Abstract test class which contains tests for declarative format for
38 * properties that are common for AbstractComponent.
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.
48 public abstract class AbstractComponentDeclarativeTestBase<T extends AbstractComponent>
49 extends DeclarativeTestBase<T> {
52 * Returns expected element tag for the tested component.
54 * @return expected element tag
56 protected abstract String getComponentTag();
59 * Returns component class which is a subject to test
61 * @return the component class
63 protected abstract Class<? extends T> getComponentClass();
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);
75 public void abstractComponentAttributesDeserialization()
76 throws InstantiationException, IllegalAccessException,
77 IllegalArgumentException, InvocationTargetException {
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;
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);
104 T component = getComponentClass().newInstance();
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,
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",
121 component.setResponsive(responsive);
122 component.setStyleName(styleName);
123 component.setVisible(visible);
125 testRead(design, component);
126 testWrite(design, component);
129 private void callBooleanSetter(boolean value, String setterName,
131 throws IllegalAccessException, InvocationTargetException {
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
142 public void externalIcon()
143 throws InstantiationException, IllegalAccessException {
144 String url = "http://example.com/example.gif";
146 String design = String.format("<%s icon='%s'/>", getComponentTag(),
149 T component = getComponentClass().newInstance();
151 component.setIcon(new ExternalResource(url));
152 testRead(design, component);
153 testWrite(design, component);
157 public void themeIcon()
158 throws InstantiationException, IllegalAccessException {
159 String path = "example.gif";
161 String design = String.format("<%s icon='theme://%s'/>",
162 getComponentTag(), path);
164 T component = getComponentClass().newInstance();
166 component.setIcon(new ThemeResource(path));
167 testRead(design, component);
168 testWrite(design, component);
172 public void sizeFullDeserialization()
173 throws InstantiationException, IllegalAccessException {
174 String design = String.format("<%s size-full/>", getComponentTag());
176 T component = getComponentClass().newInstance();
178 component.setSizeFull();
179 testRead(design, component);
180 testWrite(design, component);
184 public void widthFullDeserialization()
185 throws InstantiationException, IllegalAccessException {
186 String design = String.format("<%s width-full/>", getComponentTag());
188 T component = getComponentClass().newInstance();
190 component.setWidth("100%");
191 testRead(design, component);
192 testWrite(design, component);
196 public void heightFullDeserialization()
197 throws InstantiationException, IllegalAccessException {
198 String design = String.format("<%s height-full/>", getComponentTag());
200 T component = getComponentClass().newInstance();
202 component.setHeight("100%");
203 testRead(design, component);
204 testWrite(design, component);
208 public void sizeUnderfinedDeserialization()
209 throws InstantiationException, IllegalAccessException {
210 String design = String.format("<%s/>", getComponentTag());
212 T component = getComponentClass().newInstance();
214 component.setSizeUndefined();
215 testRead(design, component);
216 testWrite(design, component);
221 public void heightUnderfinedDeserialization()
222 throws InstantiationException, IllegalAccessException {
223 String design = String.format("<%s/>", getComponentTag());
225 T component = getComponentClass().newInstance();
227 component.setHeightUndefined();
228 testRead(design, component);
229 testWrite(design, component);
233 public void widthUnderfinedDeserialization()
234 throws InstantiationException, IllegalAccessException {
235 String design = String.format("<%s/>", getComponentTag());
237 T component = getComponentClass().newInstance();
239 component.setWidthUndefined();
240 testRead(design, component);
241 testWrite(design, component);
245 public void testUnknownAttribute() {
246 String value = "bar";
247 String design = String.format("<%s foo='%s'/>", getComponentTag(),
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"));
255 testWrite(label, design, context);