]> source.dussan.org Git - vaadin-framework.git/blob
71021a06e1f6f0a860638e7575f129d0b98c9555
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2014 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 java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.lang.reflect.Field;
21 import java.nio.charset.Charset;
22 import java.util.Locale;
23
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;
29
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;
44
45 /**
46  * Test cases for reading and writing the properties of AbstractComponent.
47  * 
48  * @since
49  * @author Vaadin Ltd
50  */
51 public class AbstractComponentDeclarativeTest extends
52         DeclarativeTestBase<AbstractComponent> {
53
54     private AbstractComponent component;
55
56     @Before
57     public void setUp() {
58         Label l = new Label();
59         l.setContentMode(ContentMode.HTML);
60         component = l;
61     }
62
63     @Test
64     public void testEmptyDesign() {
65         String design = "<v-label>";
66         testRead(design, component);
67         testWrite(design, component);
68     }
69
70     @Test
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,
82                 ErrorLevel.ERROR));
83         component.setImmediate(true);
84         testRead(design, component);
85         testWrite(design, component);
86     }
87
88     @Test
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,
95                 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));
103         }
104     }
105
106     @Test
107     public void testExternalIcon() {
108         String design = "<v-label icon=\"http://example.com/example.gif\"/>";
109         component
110                 .setIcon(new ExternalResource("http://example.com/example.gif"));
111         testRead(design, component);
112         testWrite(design, component);
113     }
114
115     @Test
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);
121     }
122
123     @Test
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);
129     }
130
131     @Test
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);
138     }
139
140     @Test
141     public void testSizeFull() {
142         String design = "<v-label size-full=\"\"/>";
143         component.setSizeFull();
144         testRead(design, component);
145         testWrite(design, component);
146     }
147
148     @Test
149     public void testSizeAuto() {
150         String design = "<v-label size-auto=\"\"/>";
151         component.setSizeUndefined();
152         testRead(design, component);
153         testWrite(design, component);
154     }
155
156     @Test
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);
163     }
164
165     @Test
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);
174     }
175
176     @Test
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);
186     }
187
188     @Test
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);
196     }
197
198     @Test
199     public void testResponsive() {
200         String design = "<v-label responsive =\"\"/>";
201         Responsive.makeResponsive(component);
202         testRead(design, component);
203         testWrite(design, component);
204     }
205
206     @Test
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);
211     }
212
213     @Test
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());
221     }
222
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);
227         return node;
228     }
229
230     private Boolean getExplicitImmediate(AbstractComponent component) {
231         try {
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.");
239         }
240     }
241 }