]> source.dussan.org Git - vaadin-framework.git/blob
b8875c34fd89866f90cfa73153d3a11d60378df6
[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.ByteArrayInputStream;
21 import java.io.File;
22 import java.nio.charset.Charset;
23 import java.util.Locale;
24
25 import org.jsoup.nodes.Attributes;
26 import org.jsoup.nodes.Element;
27 import org.jsoup.parser.Tag;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import com.vaadin.server.ErrorMessage.ErrorLevel;
32 import com.vaadin.server.ExternalResource;
33 import com.vaadin.server.FileResource;
34 import com.vaadin.server.Responsive;
35 import com.vaadin.server.ThemeResource;
36 import com.vaadin.server.UserError;
37 import com.vaadin.shared.ui.label.ContentMode;
38 import com.vaadin.tests.design.DeclarativeTestBase;
39 import com.vaadin.ui.AbstractComponent;
40 import com.vaadin.ui.Button;
41 import com.vaadin.ui.HorizontalSplitPanel;
42 import com.vaadin.ui.Label;
43 import com.vaadin.ui.Panel;
44 import com.vaadin.ui.declarative.Design;
45 import com.vaadin.ui.declarative.DesignContext;
46
47 /**
48  * Test cases for reading and writing the properties of AbstractComponent.
49  *
50  * @since
51  * @author Vaadin Ltd
52  */
53 public class AbstractComponentDeclarativeTest
54         extends DeclarativeTestBase<AbstractComponent> {
55
56     private AbstractComponent component;
57
58     @Before
59     public void setUp() {
60         Label l = new Label();
61         l.setContentMode(ContentMode.HTML);
62         component = l;
63     }
64
65     @Test
66     public void testEmptyDesign() {
67         String design = "<vaadin-label>";
68         testRead(design, component);
69         testWrite(design, component);
70     }
71
72     @Test
73     public void testProperties() {
74         String design = "<vaadin-label id=\"testId\" primary-style-name=\"test-style\" "
75                 + "caption=\"test-caption\" locale=\"fi_FI\" description=\"test-description\" "
76                 + "error=\"<div>test-error</div>\" />";
77         component.setId("testId");
78         component.setPrimaryStyleName("test-style");
79         component.setCaption("test-caption");
80         component.setLocale(new Locale("fi", "FI"));
81         component.setDescription("test-description");
82         component.setComponentError(new UserError("<div>test-error</div>",
83                 com.vaadin.server.AbstractErrorMessage.ContentMode.HTML,
84                 ErrorLevel.ERROR));
85         testRead(design, component);
86         testWrite(design, component);
87     }
88
89     @Test
90     public void testReadImmediate() {
91         // Additional tests for the immediate property, including
92         // explicit immediate values
93         String[] design = { "<vaadin-label/>",
94                 "<vaadin-label immediate=\"false\"/>",
95                 "<vaadin-label immediate=\"true\"/>",
96                 "<vaadin-label immediate />" };
97         Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE,
98                 Boolean.TRUE };
99         boolean[] immediate = { true, false, true, true };
100         for (String designElements : design) {
101             component = (AbstractComponent) Design.read(new ByteArrayInputStream(designElements.getBytes(Charset.forName("UTF-8"))));
102         }
103     }
104
105     @Test
106     public void testExternalIcon() {
107         String design = "<vaadin-label icon=\"http://example.com/example.gif\"/>";
108         component.setIcon(
109                 new ExternalResource("http://example.com/example.gif"));
110         testRead(design, component);
111         testWrite(design, component);
112     }
113
114     @Test
115     public void testThemeIcon() {
116         String design = "<vaadin-label icon=\"theme://example.gif\"/>";
117         component.setIcon(new ThemeResource("example.gif"));
118         testRead(design, component);
119         testWrite(design, component);
120     }
121
122     @Test
123     public void testFileResourceIcon() {
124         String design = "<vaadin-label icon=\"img/example.gif\"/>";
125         component.setIcon(new FileResource(new File("img/example.gif")));
126         testRead(design, component);
127         testWrite(design, component);
128     }
129
130     @Test
131     public void testWidthAndHeight() {
132         String design = "<vaadin-label width=\"70%\" height=\"12px\"/>";
133         component.setWidth("70%");
134         component.setHeight("12px");
135         testRead(design, component);
136         testWrite(design, component);
137     }
138
139     @Test
140     public void testSizeFull() {
141         String design = "<vaadin-label size-full />";
142         component.setSizeFull();
143         testRead(design, component);
144         testWrite(design, component);
145     }
146
147     @Test
148     public void testSizeAuto() {
149         component = new Panel();
150         String design = "<vaadin-panel size-auto />";
151         component.setSizeUndefined();
152         testRead(design, component);
153         testWrite(design, component);
154     }
155
156     @Test
157     public void testHeightFull() {
158         String design = "<vaadin-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 = "<vaadin-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 = "<vaadin-button width-full height=\"20px\">Foo</vaadin-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         component = new Panel();
191         String design = "<vaadin-panel height=\"20px\"/ width-auto />";
192         component.setCaptionAsHtml(false);
193         component.setHeight("20px");
194         component.setWidth(null);
195         testRead(design, component);
196         testWrite(design, component);
197     }
198
199     @Test
200     public void testResponsive() {
201         String design = "<vaadin-label responsive />";
202         Responsive.makeResponsive(component);
203         testRead(design, component);
204         testWrite(design, component);
205     }
206
207     @Test
208     public void testResponsiveFalse() {
209         String design = "<vaadin-label responsive =\"false\"/>";
210         // Only test read as the attribute responsive=false would not be written
211         testRead(design, component);
212     }
213
214     @Test
215     public void testReadAlreadyResponsive() {
216         AbstractComponent component = new Label();
217         Responsive.makeResponsive(component);
218         Element design = createDesign(true);
219         component.readDesign(design, new DesignContext());
220         assertEquals("Component should have only one extension", 1,
221                 component.getExtensions().size());
222     }
223
224     @Test
225     public void testUnknownProperties() {
226         String design = "<vaadin-label foo=\"bar\"/>";
227
228         DesignContext context = readAndReturnContext(design);
229         Label label = (Label) context.getRootComponent();
230         assertTrue("Custom attribute was preserved in custom attributes",
231                 context.getCustomAttributes(label).containsKey("foo"));
232
233         testWrite(label, design, context);
234     }
235
236     private Element createDesign(boolean responsive) {
237         Attributes attributes = new Attributes();
238         attributes.put("responsive", responsive);
239         Element node = new Element(Tag.valueOf("vaadin-label"), "", attributes);
240         return node;
241     }
242
243 }