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;
20 import java.io.ByteArrayInputStream;
22 import java.nio.charset.Charset;
23 import java.util.Locale;
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;
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;
48 * Test cases for reading and writing the properties of AbstractComponent.
53 public class AbstractComponentDeclarativeTest
54 extends DeclarativeTestBase<AbstractComponent> {
56 private AbstractComponent component;
60 Label l = new Label();
61 l.setContentMode(ContentMode.HTML);
66 public void testEmptyDesign() {
67 String design = "<vaadin-label>";
68 testRead(design, component);
69 testWrite(design, component);
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,
85 testRead(design, component);
86 testWrite(design, component);
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,
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"))));
106 public void testExternalIcon() {
107 String design = "<vaadin-label icon=\"http://example.com/example.gif\"/>";
109 new ExternalResource("http://example.com/example.gif"));
110 testRead(design, component);
111 testWrite(design, component);
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);
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);
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);
140 public void testSizeFull() {
141 String design = "<vaadin-label size-full />";
142 component.setSizeFull();
143 testRead(design, component);
144 testWrite(design, component);
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);
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);
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);
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);
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);
200 public void testResponsive() {
201 String design = "<vaadin-label responsive />";
202 Responsive.makeResponsive(component);
203 testRead(design, component);
204 testWrite(design, component);
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);
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());
225 public void testUnknownProperties() {
226 String design = "<vaadin-label foo=\"bar\"/>";
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"));
233 testWrite(label, design, context);
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);