aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/GridAsSingleSelectInBinderTest.java
blob: 15b5bff007bc7367f2b2fb99aab231c14608fdf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.vaadin.data;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.vaadin.tests.data.bean.Person;
import com.vaadin.tests.data.bean.Sex;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.SingleSelect;
import com.vaadin.ui.components.grid.SingleSelectionModelImpl;

public class GridAsSingleSelectInBinderTest
        extends BinderTestBase<Binder<Person>, Person> {

    private class GridWithCustomSingleSelectionModel extends Grid<Sex> {
        @Override
        public void setSelectionModel(
                com.vaadin.ui.components.grid.GridSelectionModel<Sex> model) {
            super.setSelectionModel(model);
        }
    }

    private class CustomSingleSelectModel
            extends SingleSelectionModelImpl<Sex> {

        public void setSelectedFromClient(Sex item) {
            setSelectedFromClient(
                    getGrid().getDataCommunicator().getKeyMapper().key(item));
        }
    }

    private Grid<Sex> grid;
    private SingleSelect<Sex> select;

    @Before
    public void setup() {
        binder = new Binder<>();
        item = new Person();
        grid = new Grid<>();
        grid.setItems(Sex.values());
        select = grid.asSingleSelect();
    }

    @Test(expected = IllegalStateException.class)
    public void boundGridInBinder_selectionModelChanged_throws() {
        grid.setSelectionMode(SelectionMode.MULTI);

        select.setValue(Sex.MALE);
    }

    @Test
    public void personBound_bindSelectByShortcut_selectionUpdated() {
        item.setSex(Sex.FEMALE);
        binder.setBean(item);
        binder.bind(select, Person::getSex, Person::setSex);

        assertSame(Sex.FEMALE, select.getValue());
    }

    @Test
    public void personBound_bindSelect_selectionUpdated() {
        item.setSex(Sex.MALE);
        binder.setBean(item);
        binder.forField(select).bind(Person::getSex, Person::setSex);

        assertSame(Sex.MALE, select.getValue());
    }

    @Test
    public void selectBound_bindPersonWithNullSex_selectedItemNotPresent() {
        bindSex();

        assertFalse(select.getValue() != null);
    }

    @Test
    public void selectBound_bindPerson_selectionUpdated() {
        item.setSex(Sex.FEMALE);
        bindSex();

        assertSame(Sex.FEMALE, select.getValue());
    }

    @Test
    public void bound_setSelection_beanValueUpdated() {
        bindSex();

        select.setValue(Sex.MALE);

        assertSame(Sex.MALE, item.getSex());
    }

    @Test
    public void bound_deselect_beanValueUpdatedToNull() {
        item.setSex(Sex.MALE);
        bindSex();

        select.setValue(null);

        assertNull(item.getSex());
    }

    @Test
    public void unbound_changeSelection_beanValueNotUpdated() {
        item.setSex(Sex.UNKNOWN);
        bindSex();
        binder.removeBean();

        select.setValue(Sex.FEMALE);

        assertSame(Sex.UNKNOWN, item.getSex());
    }

    @Test
    public void addValueChangeListener_selectionUpdated_eventTriggeredForSelect() {
        GridWithCustomSingleSelectionModel grid = new GridWithCustomSingleSelectionModel();
        CustomSingleSelectModel model = new CustomSingleSelectModel();
        grid.setSelectionModel(model);
        grid.setItems(Sex.values());
        select = grid.asSingleSelect();

        List<Sex> selected = new ArrayList<>();
        List<Sex> oldSelected = new ArrayList<>();
        List<Boolean> userOriginated = new ArrayList<>();
        select.addValueChangeListener(event -> {
            selected.add(event.getValue());
            oldSelected.add(event.getOldValue());
            userOriginated.add(event.isUserOriginated());
            assertSame(grid, event.getComponent());
            // cannot compare that the event source is the select since a new
            // SingleSelect wrapper object has been created for the event
            assertSame(select.getValue(), event.getValue());
        });

        grid.getSelectionModel().select(Sex.UNKNOWN);
        // simulates client side selection
        model.setSelectedFromClient(Sex.MALE);
        grid.getSelectionModel().select(Sex.MALE); // NOOP
        grid.getSelectionModel().deselect(Sex.UNKNOWN); // NOOP
        // simulates deselect from client side
        model.setSelectedFromClient(null);
        grid.getSelectionModel().select(Sex.FEMALE);

        assertEquals(Arrays.asList(Sex.UNKNOWN, Sex.MALE, null, Sex.FEMALE),
                selected);
        assertEquals(Arrays.asList(null, Sex.UNKNOWN, Sex.MALE, null),
                oldSelected);
        assertEquals(Arrays.asList(false, true, true, false), userOriginated);
    }

    protected void bindSex() {
        binder.forField(select).bind(Person::getSex, Person::setSex);
        binder.setBean(item);
    }
}