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.abstractmultiselect;
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
23 import org.junit.Assert;
24 import org.junit.Test;
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;
32 * {@link AbstractMultiSelect} component declarative test.
34 * Test inherits test methods from a {@link AbstractListingDeclarativeTest}
35 * class providing here only common cases for {@link AbstractMultiSelect}s.
37 * @see AbstractListingDeclarativeTest
45 @SuppressWarnings({ "rawtypes", "unchecked" })
46 public abstract class AbstractMultiSelectDeclarativeTest<T extends AbstractMultiSelect>
47 extends AbstractListingDeclarativeTest<T> {
51 public void dataSerialization() throws InstantiationException,
52 IllegalAccessException, InvocationTargetException {
53 List<String> items = Arrays.asList("foo", "bar", "foobar");
55 String type = "com.vaadin.SomeType";
56 String attribute = "data-type";
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");
69 DesignContext context = readComponentAndCompare(design, component,
70 ctxt -> configureContext(type, attribute, component, ctxt));
72 context.getCustomAttributes(context.getRootComponent())
74 context = new DesignContext();
75 configureContext(type, attribute, component, context);
76 testWrite(component, design, context);
81 public void valueSerialization() throws InstantiationException,
82 IllegalAccessException, InvocationTargetException {
83 List<String> items = Arrays.asList("foo", "bar", "foobar");
85 String type = "com.vaadin.SomeType";
86 String attribute = "data-type";
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");
98 DesignContext context = readComponentAndCompare(design, component,
99 ctxt -> configureContext(type, attribute, component, ctxt));
101 context.getCustomAttributes(context.getRootComponent())
103 context = new DesignContext();
104 configureContext(type, attribute, component, context);
105 testWrite(component, design, context);
110 public void readOnlySelection() throws InstantiationException,
111 IllegalAccessException, InvocationTargetException {
112 T component = getComponentClass().newInstance();
114 List<String> items = Arrays.asList("foo", "bar", "foobar");
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());
122 component.setItems(items);
123 component.setReadOnly(true);
125 testRead(design, component, true);
126 testWrite(design, component, true);
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);