]> source.dussan.org Git - vaadin-framework.git/blob
97646923458a63ec9c70183446bc0ebeb084e874
[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.abstractorderedlayout;
17
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.junit.Test;
22
23 import com.vaadin.shared.ui.label.ContentMode;
24 import com.vaadin.tests.server.component.DeclarativeMarginTestBase;
25 import com.vaadin.ui.AbstractOrderedLayout;
26 import com.vaadin.ui.Alignment;
27 import com.vaadin.ui.Button;
28 import com.vaadin.ui.Label;
29 import com.vaadin.ui.VerticalLayout;
30
31 /**
32  * Tests declarative support for AbstractOrderedLayout.
33  *
34  * @since
35  * @author Vaadin Ltd
36  */
37 public class AbstractOrderedLayoutDeclarativeTest
38         extends DeclarativeMarginTestBase<AbstractOrderedLayout> {
39
40     private List<String> defaultAlignments = Arrays.asList(":top", ":left");
41
42     @Test
43     public void testMargins() {
44         testMargins("vaadin-vertical-layout");
45     }
46
47     @Test
48     public void testExpandRatio() {
49         String design = getDesign(1);
50         AbstractOrderedLayout layout = getLayout(1, null);
51         testRead(design, layout);
52         testWrite(design, layout);
53         design = getDesign(0.25f);
54         layout = getLayout(0.25f, null);
55         testRead(design, layout);
56         testWrite(design, layout);
57     }
58
59     @Test
60     public void testAlignment() {
61         String design = getDesign(0, ":top", ":left");
62         AbstractOrderedLayout layout = getLayout(0, Alignment.TOP_LEFT);
63         testRead(design, layout);
64         testWrite(design, layout);
65         design = getDesign(0, ":middle", ":center");
66         layout = getLayout(0, Alignment.MIDDLE_CENTER);
67         testRead(design, layout);
68         testWrite(design, layout);
69         design = getDesign(0, ":bottom", ":right");
70         layout = getLayout(0, Alignment.BOTTOM_RIGHT);
71         testRead(design, layout);
72         testWrite(design, layout);
73     }
74
75     private String getDesign(float expandRatio, String... alignments) {
76         String result = "<vaadin-vertical-layout caption=test-layout>";
77         result += "<vaadin-label caption=test-label ";
78         String ratioString = expandRatio == 1.0f ? null
79                 : String.valueOf(expandRatio);
80         if (expandRatio != 0) {
81             if (ratioString == null) {
82                 result += ":expand";
83             } else {
84                 result += ":expand=" + ratioString;
85             }
86         }
87         for (String alignment : alignments) {
88             if (!defaultAlignments.contains(alignment)) {
89                 result += " " + alignment;
90             }
91         }
92         result += "></vaadin-label><vaadin-button ";
93         if (expandRatio != 0) {
94             if (ratioString == null) {
95                 result += ":expand";
96             } else {
97                 result += ":expand=" + ratioString;
98             }
99         }
100         for (String alignment : alignments) {
101             if (!defaultAlignments.contains(alignment)) {
102                 result += " " + alignment;
103             }
104         }
105         result += "></vaadin-button></vaadin-vertical-layout>";
106         return result;
107     }
108
109     private AbstractOrderedLayout getLayout(float expandRatio,
110             Alignment alignment) {
111         VerticalLayout layout = new VerticalLayout();
112         layout.setCaption("test-layout");
113         Label l = new Label();
114         l.setCaption("test-label");
115         l.setContentMode(ContentMode.HTML);
116         layout.addComponent(l);
117         layout.setExpandRatio(l, expandRatio);
118         Button b = new Button();
119         b.setCaptionAsHtml(true);
120         layout.addComponent(b);
121         layout.setExpandRatio(b, expandRatio);
122         if (alignment != null) {
123             layout.setComponentAlignment(l, alignment);
124             layout.setComponentAlignment(b, alignment);
125         }
126         return layout;
127     }
128 }