2 * Copyright 2000-2014 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 java.io.ByteArrayInputStream;
20 import java.lang.reflect.Field;
21 import java.nio.charset.Charset;
22 import java.util.Locale;
24 import org.jsoup.nodes.Attributes;
25 import org.jsoup.nodes.Element;
26 import org.jsoup.parser.Tag;
27 import org.junit.Before;
28 import org.junit.Test;
30 import com.vaadin.server.ErrorMessage.ErrorLevel;
31 import com.vaadin.server.ExternalResource;
32 import com.vaadin.server.FileResource;
33 import com.vaadin.server.Responsive;
34 import com.vaadin.server.ThemeResource;
35 import com.vaadin.server.UserError;
36 import com.vaadin.shared.ui.label.ContentMode;
37 import com.vaadin.tests.design.DeclarativeTestBase;
38 import com.vaadin.ui.AbstractComponent;
39 import com.vaadin.ui.Button;
40 import com.vaadin.ui.HorizontalSplitPanel;
41 import com.vaadin.ui.Label;
42 import com.vaadin.ui.declarative.Design;
43 import com.vaadin.ui.declarative.DesignContext;
46 * Test cases for reading and writing the properties of AbstractComponent.
51 public class AbstractComponentDeclarativeTest extends
52 DeclarativeTestBase<AbstractComponent> {
54 private AbstractComponent component;
58 Label l = new Label();
59 l.setContentMode(ContentMode.HTML);
64 public void testEmptyDesign() {
65 String design = "<v-label>";
66 testRead(design, component);
67 testWrite(design, component);
71 public void testProperties() {
72 String design = "<v-label id=\"testId\" primary-style-name=\"test-style\" "
73 + "caption=\"test-caption\" locale=\"fi_FI\" description=\"test-description\" "
74 + "error=\"<div>test-error</div>\" immediate=\"\"/>";
75 component.setId("testId");
76 component.setPrimaryStyleName("test-style");
77 component.setCaption("test-caption");
78 component.setLocale(new Locale("fi", "FI"));
79 component.setDescription("test-description");
80 component.setComponentError(new UserError("<div>test-error</div>",
81 com.vaadin.server.AbstractErrorMessage.ContentMode.HTML,
83 component.setImmediate(true);
84 testRead(design, component);
85 testWrite(design, component);
89 public void testReadImmediate() {
90 // Additional tests for the immediate property, including
91 // explicit immediate values
92 String[] design = { "<v-label/>", "<v-label immediate=\"false\"/>",
93 "<v-label immediate=\"true\"/>", "<v-label immediate=\"\"/>" };
94 Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE,
96 boolean[] immediate = { false, false, true, true };
97 for (int i = 0; i < design.length; i++) {
98 component = (AbstractComponent) Design
99 .read(new ByteArrayInputStream(design[i].getBytes(Charset
100 .forName("UTF-8"))));
101 assertEquals(immediate[i], component.isImmediate());
102 assertEquals(explicitImmediate[i], getExplicitImmediate(component));
107 public void testExternalIcon() {
108 String design = "<v-label icon=\"http://example.com/example.gif\"/>";
110 .setIcon(new ExternalResource("http://example.com/example.gif"));
111 testRead(design, component);
112 testWrite(design, component);
116 public void testThemeIcon() {
117 String design = "<v-label icon=\"theme://example.gif\"/>";
118 component.setIcon(new ThemeResource("example.gif"));
119 testRead(design, component);
120 testWrite(design, component);
124 public void testFileResourceIcon() {
125 String design = "<v-label icon=\"img/example.gif\"/>";
126 component.setIcon(new FileResource(new File("img/example.gif")));
127 testRead(design, component);
128 testWrite(design, component);
132 public void testWidthAndHeight() {
133 String design = "<v-label width=\"70%\" height=\"12px\"/>";
134 component.setWidth("70%");
135 component.setHeight("12px");
136 testRead(design, component);
137 testWrite(design, component);
141 public void testSizeFull() {
142 String design = "<v-label size-full=\"\"/>";
143 component.setSizeFull();
144 testRead(design, component);
145 testWrite(design, component);
149 public void testSizeAuto() {
150 String design = "<v-label size-auto=\"\"/>";
151 component.setSizeUndefined();
152 testRead(design, component);
153 testWrite(design, component);
157 public void testHeightFull() {
158 String design = "<v-label height-full=\"\"/ width=\"20px\"/>";
159 component.setHeight("100%");
160 component.setWidth("20px");
161 testRead(design, component);
162 testWrite(design, component);
166 public void testHeightAuto() {
167 String design = "<v-horizontal-split-panel height-auto=\"\"/ width=\"20px\" >";
168 // we need to have default height of 100% -> use split panel
169 AbstractComponent component = new HorizontalSplitPanel();
170 component.setHeight(null);
171 component.setWidth("20px");
172 testRead(design, component);
173 testWrite(design, component);
177 public void testWidthFull() {
178 String design = "<v-button width-full=\"\"/ height=\"20px\">Foo</button>";
179 AbstractComponent component = new Button();
180 component.setCaptionAsHtml(true);
181 component.setCaption("Foo");
182 component.setHeight("20px");
183 component.setWidth("100%");
184 testRead(design, component);
185 testWrite(design, component);
189 public void testWidthAuto() {
190 String design = "<v-label height=\"20px\"/ width-auto=\"\"/>";
191 component.setCaptionAsHtml(false);
192 component.setHeight("20px");
193 component.setWidth(null);
194 testRead(design, component);
195 testWrite(design, component);
199 public void testResponsive() {
200 String design = "<v-label responsive =\"\"/>";
201 Responsive.makeResponsive(component);
202 testRead(design, component);
203 testWrite(design, component);
207 public void testResponsiveFalse() {
208 String design = "<v-label responsive =\"false\"/>";
209 // Only test read as the attribute responsive=false would not be written
210 testRead(design, component);
214 public void testReadAlreadyResponsive() {
215 AbstractComponent component = new Label();
216 Responsive.makeResponsive(component);
217 Element design = createDesign("responsive", "");
218 component.readDesign(design, new DesignContext());
219 assertEquals("Component should have only one extension", 1, component
220 .getExtensions().size());
223 private Element createDesign(String key, String value) {
224 Attributes attributes = new Attributes();
225 attributes.put(key, value);
226 Element node = new Element(Tag.valueOf("v-label"), "", attributes);
230 private Boolean getExplicitImmediate(AbstractComponent component) {
232 Field immediate = AbstractComponent.class
233 .getDeclaredField("explicitImmediateValue");
234 immediate.setAccessible(true);
235 return (Boolean) immediate.get(component);
236 } catch (Exception e) {
237 throw new RuntimeException(
238 "Getting the field explicitImmediateValue failed.");