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 (int i = 0; i < design.length; i++) {
101 component = (AbstractComponent) Design
102 .read(new ByteArrayInputStream(
103 design[i].getBytes(Charset.forName("UTF-8"))));
108 public void testExternalIcon() {
109 String design = "<vaadin-label icon=\"http://example.com/example.gif\"/>";
111 new ExternalResource("http://example.com/example.gif"));
112 testRead(design, component);
113 testWrite(design, component);
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);
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);
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);
142 public void testSizeFull() {
143 String design = "<vaadin-label size-full />";
144 component.setSizeFull();
145 testRead(design, component);
146 testWrite(design, component);
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);
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);
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);
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);
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);
202 public void testResponsive() {
203 String design = "<vaadin-label responsive />";
204 Responsive.makeResponsive(component);
205 testRead(design, component);
206 testWrite(design, component);
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);
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());
227 public void testUnknownProperties() {
228 String design = "<vaadin-label foo=\"bar\"/>";
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"));
235 testWrite(label, design, context);
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);