]> source.dussan.org Git - vaadin-framework.git/blob
35f764704968e069f97dbf692325c769019eb8a5
[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.abstractmultiselect;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 import com.vaadin.tests.design.DeclarativeTestBaseBase;
27 import com.vaadin.tests.server.component.abstractlisting.AbstractListingDeclarativeTest;
28 import com.vaadin.ui.AbstractMultiSelect;
29 import com.vaadin.ui.declarative.DesignContext;
30
31 /**
32  * {@link AbstractMultiSelect} component declarative test.
33  * <p>
34  * Test inherits test methods from a {@link AbstractListingDeclarativeTest}
35  * class providing here only common cases for {@link AbstractMultiSelect}s.
36  *
37  * @see AbstractListingDeclarativeTest
38  *
39  * @author Vaadin Ltd
40  *
41  *
42  * @param <T>
43  *            a component type
44  */
45 @SuppressWarnings({ "rawtypes", "unchecked" })
46 public abstract class AbstractMultiSelectDeclarativeTest<T extends AbstractMultiSelect>
47         extends AbstractListingDeclarativeTest<T> {
48
49     @Override
50     @Test
51     public void dataSerialization() throws InstantiationException,
52             IllegalAccessException, InvocationTargetException {
53         List<String> items = Arrays.asList("foo", "bar", "foobar");
54
55         String type = "com.vaadin.SomeType";
56         String attribute = "data-type";
57
58         String design = String.format(
59                 "<%s %s='%s'>\n" + "<option item='foo' selected>foo1</option>\n"
60                         + "<option item='bar'>bar1</option>"
61                         + "<option item='foobar' selected>foobar1</option></%s>",
62                 getComponentTag(), attribute, type, getComponentTag());
63         T component = getComponentClass().newInstance();
64         component.setItems(items);
65         component.select("foo");
66         component.select("foobar");
67         component.setItemCaptionGenerator(item -> item + "1");
68
69         DesignContext context = readComponentAndCompare(design, component,
70                 ctxt -> configureContext(type, attribute, component, ctxt));
71         assertEquals(type,
72                 context.getCustomAttributes(context.getRootComponent())
73                         .get(attribute));
74         context = new DesignContext();
75         configureContext(type, attribute, component, context);
76         testWrite(component, design, context);
77     }
78
79     @Override
80     @Test
81     public void valueSerialization() throws InstantiationException,
82             IllegalAccessException, InvocationTargetException {
83         List<String> items = Arrays.asList("foo", "bar", "foobar");
84
85         String type = "com.vaadin.SomeType";
86         String attribute = "data-type";
87
88         String design = String.format(
89                 "<%s %s='%s'>\n" + "<option item='foo' selected>foo1</option>\n"
90                         + "<option item='bar'>bar1</option>"
91                         + "<option item='foobar' selected>foobar1</option></%s>",
92                 getComponentTag(), attribute, type, getComponentTag());
93         T component = getComponentClass().newInstance();
94         component.setItems(items);
95         component.setValue(new HashSet<>(Arrays.asList("foo", "foobar")));
96         component.setItemCaptionGenerator(item -> item + "1");
97
98         DesignContext context = readComponentAndCompare(design, component,
99                 ctxt -> configureContext(type, attribute, component, ctxt));
100         assertEquals(type,
101                 context.getCustomAttributes(context.getRootComponent())
102                         .get(attribute));
103         context = new DesignContext();
104         configureContext(type, attribute, component, context);
105         testWrite(component, design, context);
106     }
107
108     @Override
109     @Test
110     public void readOnlySelection() throws InstantiationException,
111             IllegalAccessException, InvocationTargetException {
112         T component = getComponentClass().newInstance();
113
114         List<String> items = Arrays.asList("foo", "bar", "foobar");
115
116         String design = String.format(
117                 "<%s readonly>\n" + "<option item='foo'>foo</option>\n"
118                         + "<option item='bar'>bar</option>"
119                         + "<option item='foobar'>foobar</option>",
120                 getComponentTag(), getComponentTag());
121
122         component.setItems(items);
123         component.setReadOnly(true);
124
125         testRead(design, component, true);
126         testWrite(design, component, true);
127     }
128
129     private void configureContext(String type, String attribute, T component,
130             DesignContext context) {
131         context.setCustomAttribute(component, attribute, type);
132         context.setShouldWriteDataDelegate(
133                 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
134     }
135
136 }