]> source.dussan.org Git - vaadin-framework.git/blob
b13499983e656b6d3c0086a68de903297a3fe605
[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 (int i = 0; i < design.length; i++) {
101             component = (AbstractComponent) Design
102                     .read(new ByteArrayInputStream(
103                             design[i].getBytes(Charset.forName("UTF-8"))));
104         }
105     }
106
107     @Test
108     public void testExternalIcon() {
109         String design = "<vaadin-label icon=\"http://example.com/example.gif\"/>";
110         component.setIcon(
111                 new ExternalResource("http://example.com/example.gif"));
112         testRead(design, component);
113         testWrite(design, component);
114     }
115
116     @Test
117     public void testThemeIcon() {
118         String design = "<vaadin-label icon=\"theme://example.gif\"/>";
119         component.setIcon(new ThemeResource("example.gif"));
120         testRead(design, component);
121         testWrite(design, component);
122     }
123
124     @Test
125     public void testFileResourceIcon() {
126         String design = "<vaadin-label icon=\"img/example.gif\"/>";
127         component.setIcon(new FileResource(new File("img/example.gif")));
128         testRead(design, component);
129         testWrite(design, component);
130     }
131
132     @Test
133     public void testWidthAndHeight() {
134         String design = "<vaadin-label width=\"70%\" height=\"12px\"/>";
135         component.setWidth("70%");
136         component.setHeight("12px");
137         testRead(design, component);
138         testWrite(design, component);
139     }
140
141     @Test
142     public void testSizeFull() {
143         String design = "<vaadin-label size-full />";
144         component.setSizeFull();
145         testRead(design, component);
146         testWrite(design, component);
147     }
148
149     @Test
150     public void testSizeAuto() {
151         component = new Panel();
152         String design = "<vaadin-panel size-auto />";
153         component.setSizeUndefined();
154         testRead(design, component);
155         testWrite(design, component);
156     }
157
158     @Test
159     public void testHeightFull() {
160         String design = "<vaadin-label height-full width=\"20px\"/>";
161         component.setHeight("100%");
162         component.setWidth("20px");
163         testRead(design, component);
164         testWrite(design, component);
165     }
166
167     @Test
168     public void testHeightAuto() {
169         String design = "<vaadin-horizontal-split-panel height-auto width=\"20px\" >";
170         // we need to have default height of 100% -> use split panel
171         AbstractComponent component = new HorizontalSplitPanel();
172         component.setHeight(null);
173         component.setWidth("20px");
174         testRead(design, component);
175         testWrite(design, component);
176     }
177
178     @Test
179     public void testWidthFull() {
180         String design = "<vaadin-button width-full height=\"20px\">Foo</vaadin-button>";
181         AbstractComponent component = new Button();
182         component.setCaptionAsHtml(true);
183         component.setCaption("Foo");
184         component.setHeight("20px");
185         component.setWidth("100%");
186         testRead(design, component);
187         testWrite(design, component);
188     }
189
190     @Test
191     public void testWidthAuto() {
192         component = new Panel();
193         String design = "<vaadin-panel height=\"20px\"/ width-auto />";
194         component.setCaptionAsHtml(false);
195         component.setHeight("20px");
196         component.setWidth(null);
197         testRead(design, component);
198         testWrite(design, component);
199     }
200
201     @Test
202     public void testResponsive() {
203         String design = "<vaadin-label responsive />";
204         Responsive.makeResponsive(component);
205         testRead(design, component);
206         testWrite(design, component);
207     }
208
209     @Test
210     public void testResponsiveFalse() {
211         String design = "<vaadin-label responsive =\"false\"/>";
212         // Only test read as the attribute responsive=false would not be written
213         testRead(design, component);
214     }
215
216     @Test
217     public void testReadAlreadyResponsive() {
218         AbstractComponent component = new Label();
219         Responsive.makeResponsive(component);
220         Element design = createDesign(true);
221         component.readDesign(design, new DesignContext());
222         assertEquals("Component should have only one extension", 1,
223                 component.getExtensions().size());
224     }
225
226     @Test
227     public void testUnknownProperties() {
228         String design = "<vaadin-label foo=\"bar\"/>";
229
230         DesignContext context = readAndReturnContext(design);
231         Label label = (Label) context.getRootComponent();
232         assertTrue("Custom attribute was preserved in custom attributes",
233                 context.getCustomAttributes(label).containsKey("foo"));
234
235         testWrite(label, design, context);
236     }
237
238     private Element createDesign(boolean responsive) {
239         Attributes attributes = new Attributes();
240         attributes.put("responsive", responsive);
241         Element node = new Element(Tag.valueOf("vaadin-label"), "", attributes);
242         return node;
243     }
244
245 }