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.abstractsingleselect;
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.Arrays;
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.AbstractSingleSelect;
29 import com.vaadin.ui.ItemCaptionGenerator;
30 import com.vaadin.ui.declarative.DesignContext;
33 * {@link AbstractSingleSelect} component declarative test.
35 * Test inherits test methods from a {@link AbstractListingDeclarativeTest}
36 * class providing here only common cases for {@link AbstractSingleSelect}s.
44 @SuppressWarnings({ "unchecked", "rawtypes" })
45 public abstract class AbstractSingleSelectDeclarativeTest<T extends AbstractSingleSelect>
46 extends AbstractListingDeclarativeTest<T> {
50 public void dataSerialization() throws InstantiationException,
51 IllegalAccessException, InvocationTargetException {
52 List<String> items = Arrays.asList("foo", "bar", "foobar");
54 String type = "com.vaadin.SomeType";
55 String attribute = "data-type";
57 String design = String.format(
58 "<%s %s='%s'>\n" + "<option item='foo'>foo</option>\n"
59 + "<option item='bar' selected>bar</option>"
60 + "<option item='foobar'>foobar</option></%s>",
61 getComponentTag(), attribute, type, getComponentTag());
62 T component = getComponentClass().newInstance();
63 component.setItems(items);
64 component.setSelectedItem("bar");
66 DesignContext context = readComponentAndCompare(design, component);
68 context.getCustomAttributes(context.getRootComponent())
70 context = new DesignContext();
71 context.setCustomAttribute(component, attribute, type);
72 context.setShouldWriteDataDelegate(
73 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
74 testWrite(component, design, context);
79 public void valueSerialization() throws InstantiationException,
80 IllegalAccessException, InvocationTargetException {
81 List<String> items = Arrays.asList("foo", "bar", "foobar");
83 String type = "com.vaadin.SomeType";
84 String attribute = "data-type";
86 String design = String.format(
87 "<%s %s='%s'>\n" + "<option item='foo'>foo</option>\n"
88 + "<option item='bar' selected>bar</option>"
89 + "<option item='foobar'>foobar</option></%s>",
90 getComponentTag(), attribute, type, getComponentTag());
91 T component = getComponentClass().newInstance();
92 component.setItems(items);
93 component.setValue("bar");
95 DesignContext context = readComponentAndCompare(design, component);
97 context.getCustomAttributes(context.getRootComponent())
99 context = new DesignContext();
100 context.setCustomAttribute(component, attribute, type);
101 context.setShouldWriteDataDelegate(
102 DeclarativeTestBaseBase.ALWAYS_WRITE_DATA);
103 testWrite(component, design, context);
107 public void dataWithCaptionGeneratorSerialization()
108 throws InstantiationException, IllegalAccessException,
109 InvocationTargetException {
110 List<String> items = Arrays.asList("foo", "bar", "foobar");
111 T component = getComponentClass().newInstance();
112 Method setItemCaptionGenerator = getItemCaptionGeneratorMethod(
114 if (setItemCaptionGenerator == null) {
117 String design = String.format(
118 "<%s>\n" + "<option item='foo'>foo1</option>\n"
119 + "<option item='bar' selected>bar1</option>"
120 + "<option item='foobar'>foobar1</option></%s>",
121 getComponentTag(), getComponentTag());
122 component.setItems(items);
123 component.setValue("bar");
124 ItemCaptionGenerator generator = item -> item + "1";
125 setItemCaptionGenerator.invoke(component, generator);
127 testRead(design, component);
128 testWrite(design, component, true);
133 public void readOnlySelection() throws InstantiationException,
134 IllegalAccessException, InvocationTargetException {
135 T component = getComponentClass().newInstance();
137 List<String> items = Arrays.asList("foo", "bar", "foobar");
139 String design = String.format(
140 "<%s readonly>\n" + "<option item='foo'>foo</option>\n"
141 + "<option item='bar'>bar</option>"
142 + "<option item='foobar'>foobar</option>",
143 getComponentTag(), getComponentTag());
145 component.setItems(items);
146 component.setReadOnly(true);
148 testRead(design, component);
149 testWrite(design, component, true);
152 private Method getItemCaptionGeneratorMethod(T component)
153 throws IllegalAccessException, InvocationTargetException {
155 return component.getClass().getMethod("setItemCaptionGenerator",
156 new Class[] { ItemCaptionGenerator.class });
157 } catch (NoSuchMethodException ignore) {
158 // ignore if there is no such method