aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java
blob: fbc576758933872ce111a0834c6233bdc98f832d (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.ui;

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 static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;

import com.vaadin.data.HasValue.ValueChangeEvent;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.event.selection.MultiSelectionEvent;
import com.vaadin.event.selection.MultiSelectionListener;
import com.vaadin.server.ServerRpcManager;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.selection.MultiSelectServerRpc;

@RunWith(Parameterized.class)
public class AbstractMultiSelectTest<S extends AbstractMultiSelect<String>> {

    @Parameters(name = "{0}")
    public static Iterable<?> multiSelects() {
        return Arrays.asList(new CheckBoxGroup<>(), new TwinColSelect<>(),
                new ListSelect<>());
    }

    @Parameter
    public S selectToTest;

    private MultiSelectServerRpc rpc;

    private Registration registration;

    private List<Set<String>> values;

    private List<Set<String>> oldValues;

    @Before
    public void setUp() {
        selectToTest.deselectAll();
        // Intentional deviation from upcoming selection order
        selectToTest.setItems("3", "2", "1", "5", "8", "7", "4", "6");
        rpc = ServerRpcManager.getRpcProxy(selectToTest,
                MultiSelectServerRpc.class);

        values = new ArrayList<>();
        oldValues = new ArrayList<>();
        selectToTest
                .addValueChangeListener(event -> values.add(event.getValue()));
        selectToTest.addValueChangeListener(
                event -> oldValues.add(event.getOldValue()));
    }

    @After
    public void tearDown() {
        if (registration != null) {
            registration.remove();
            registration = null;
        }
    }

    @Test
    public void stableSelectionOrder() {
        selectToTest.select("1");
        selectToTest.select("2");
        selectToTest.select("3");

        assertSelectionOrder("1", "2", "3");

        selectToTest.deselect("1");
        assertSelectionOrder("2", "3");

        selectToTest.select("1");
        assertSelectionOrder("2", "3", "1");

        selectToTest.select("7", "8", "4");
        assertSelectionOrder("2", "3", "1", "7", "8", "4");

        selectToTest.deselect("2", "1", "4", "5");
        assertSelectionOrder("3", "7", "8");

        selectToTest.updateSelection(
                new LinkedHashSet<>(Arrays.asList("5", "2")),
                new LinkedHashSet<>(Arrays.asList("3", "8")));
        assertSelectionOrder("7", "5", "2");
        verifyValueChangeEvents();
    }

    @Test
    public void apiSelectionChange_notUserOriginated() {
        AtomicInteger listenerCount = new AtomicInteger(0);
        listenerCount.set(0);

        registration = selectToTest.addSelectionListener(event -> {
            listenerCount.incrementAndGet();
            assertFalse(event.isUserOriginated());
        });

        selectToTest.select("1");
        selectToTest.select("2");

        selectToTest.deselect("2");
        selectToTest.deselectAll();

        selectToTest.select("2", "3", "4");
        selectToTest.deselect("1", "4");

        assertEquals(6, listenerCount.get());

        // select partly selected
        selectToTest.select("2", "3", "4");
        assertEquals(7, listenerCount.get());

        // select completely selected
        selectToTest.select("2", "3", "4");
        assertEquals(7, listenerCount.get());

        // deselect partly not selected
        selectToTest.select("1", "4");
        assertEquals(8, listenerCount.get());

        // deselect completely not selected
        selectToTest.select("1", "4");
        assertEquals(8, listenerCount.get());
        verifyValueChangeEvents();
    }

    @Test
    public void rpcSelectionChange_userOriginated() {
        AtomicInteger listenerCount = new AtomicInteger(0);

        registration = selectToTest.addSelectionListener(event -> {
            listenerCount.incrementAndGet();
            assertTrue(event.isUserOriginated());
        });

        rpcSelect("1");
        assertSelectionOrder("1");

        rpcSelect("2");
        assertSelectionOrder("1", "2");
        rpcDeselectItems("2");
        assertSelectionOrder("1");
        rpcSelect("3", "6");
        assertSelectionOrder("1", "3", "6");
        rpcDeselectItems("1", "3");
        assertSelectionOrder("6");

        assertEquals(5, listenerCount.get());

        // select partly selected
        rpcSelect("2", "3", "4");
        assertEquals(6, listenerCount.get());
        assertSelectionOrder("6", "2", "3", "4");

        // select completely selected
        rpcSelect("2", "3", "4");
        assertEquals(6, listenerCount.get());
        assertSelectionOrder("6", "2", "3", "4");

        // deselect partly not selected
        rpcDeselectItems("1", "4");
        assertEquals(7, listenerCount.get());
        assertSelectionOrder("6", "2", "3");

        // deselect completely not selected
        rpcDeselectItems("1", "4");
        assertEquals(7, listenerCount.get());
        assertSelectionOrder("6", "2", "3");

        // select completely selected and deselect completely not selected
        rpcUpdateSelection(new String[] { "3" }, new String[] { "1", "4" });
        assertEquals(7, listenerCount.get());
        assertSelectionOrder("6", "2", "3");

        // select partly selected and deselect completely not selected
        rpcUpdateSelection(new String[] { "4", "2" },
                new String[] { "1", "8" });
        assertEquals(8, listenerCount.get());
        assertSelectionOrder("6", "2", "3", "4");

        // select completely selected and deselect partly not selected
        rpcUpdateSelection(new String[] { "4", "3" },
                new String[] { "1", "2" });
        assertEquals(9, listenerCount.get());
        assertSelectionOrder("6", "3", "4");

        // duplicate case - ignored
        rpcUpdateSelection(new String[] { "2" }, new String[] { "2" });
        assertEquals(9, listenerCount.get());
        assertSelectionOrder("6", "3", "4");

        // duplicate case - duplicate removed
        rpcUpdateSelection(new String[] { "2" }, new String[] { "2", "3" });
        assertEquals(10, listenerCount.get());
        assertSelectionOrder("6", "4");

        // duplicate case - duplicate removed
        rpcUpdateSelection(new String[] { "6", "8" }, new String[] { "6" });
        assertEquals(11, listenerCount.get());
        assertSelectionOrder("6", "4", "8");
        verifyValueChangeEvents();
    }

    @Test
    public void getValue() {
        selectToTest.select("1");

        assertEquals(Collections.singleton("1"), selectToTest.getValue());

        selectToTest.deselectAll();
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("1");
        set.add("5");
        selectToTest.select(set.toArray(new String[2]));
        assertEquals(set, selectToTest.getValue());

        set.add("3");
        selectToTest.select("3");
        assertEquals(set, selectToTest.getValue());
        verifyValueChangeEvents();
    }

    @Test
    @SuppressWarnings({ "serial", "unchecked" })
    public void getValue_isDelegatedTo_getSelectedItems() {
        Set<String> set = Mockito.mock(Set.class);
        AbstractMultiSelect<String> select = new AbstractMultiSelect<String>() {

            @Override
            public Set<String> getSelectedItems() {
                return set;
            }

            @Override
            public void setItems(Collection<String> items) {
                throw new UnsupportedOperationException(
                        "Not implemented for this test");
            }

            @Override
            public DataProvider<String, ?> getDataProvider() {
                return null;
            }
        };

        assertSame(set, select.getValue());
        verifyValueChangeEvents();
    }

    @Test
    public void setValue() {
        selectToTest.setValue(Collections.singleton("1"));

        assertEquals(Collections.singleton("1"),
                selectToTest.getSelectedItems());

        Set<String> set = new LinkedHashSet<>();
        set.add("4");
        set.add("3");
        selectToTest.setValue(set);

        assertEquals(set, selectToTest.getSelectedItems());
        verifyValueChangeEvents();
    }

    @Test
    @SuppressWarnings({ "unchecked", "rawtypes", "serial" })
    public void setValue_isDelegatedToDeselectAndUpdateSelection() {
        AbstractMultiSelect<String> select = Mockito
                .mock(AbstractMultiSelect.class);

        Set set = new LinkedHashSet<>();
        set.add("foo1");
        set.add("foo");
        Set selected = new LinkedHashSet<>();
        selected.add("bar1");
        selected.add("bar");
        selected.add("bar2");
        Mockito.when(select.getSelectedItems()).thenReturn(selected);
        Mockito.doCallRealMethod().when(select).setValue(Mockito.anySet());

        select.setValue(set);

        Mockito.verify(select).updateSelection(set, selected);
    }

    @SuppressWarnings({ "unchecked", "serial" })
    @Test
    public void addValueChangeListener() {
        AtomicReference<MultiSelectionListener<String>> selectionListener = new AtomicReference<>();
        Registration registration = Mockito.mock(Registration.class);
        Set<String> set = new HashSet<>();
        set.add("foo");
        set.add("bar");
        AbstractMultiSelect<String> select = new AbstractMultiSelect<String>() {
            @Override
            public Registration addSelectionListener(
                    MultiSelectionListener<String> listener) {
                selectionListener.set(listener);
                return registration;
            }

            @Override
            public Set<String> getValue() {
                return set;
            }

            @Override
            public void setItems(Collection<String> items) {
                throw new UnsupportedOperationException(
                        "Not implemented for this test");
            }

            @Override
            public DataProvider<String, ?> getDataProvider() {
                return null;
            }
        };

        AtomicReference<ValueChangeEvent<?>> event = new AtomicReference<>();
        Registration actualRegistration = select.addValueChangeListener(evt -> {
            assertNull(event.get());
            event.set(evt);
        });

        assertSame(registration, actualRegistration);

        selectionListener.get().selectionChange(new MultiSelectionEvent<>(
                select, Mockito.mock(Set.class), true));

        assertEquals(select, event.get().getComponent());
        assertEquals(set, event.get().getValue());
        assertTrue(event.get().isUserOriginated());
    }

    private void rpcSelect(String... keysToSelect) {
        rpcUpdateSelection(keysToSelect, new String[] {});
    }

    private void rpcDeselectItems(String... keysToDeselect) {
        rpcUpdateSelection(new String[] {}, keysToDeselect);
    }

    private void rpcUpdateSelection(String[] added, String[] removed) {
        rpc.updateSelection(
                new LinkedHashSet<>(Stream.of(added).map(this::getItemKey)
                        .collect(Collectors.toList())),
                new LinkedHashSet<>(Stream.of(removed).map(this::getItemKey)
                        .collect(Collectors.toList())));
    }

    private String getItemKey(String dataObject) {
        return selectToTest.getDataCommunicator().getKeyMapper()
                .key(dataObject);
    }

    private void assertSelectionOrder(String... selectionOrder) {
        assertEquals(Arrays.asList(selectionOrder),
                new ArrayList<>(selectToTest.getSelectedItems()));
    }

    private void verifyValueChangeEvents() {
        if (!oldValues.isEmpty()) {
            assertTrue(oldValues.get(0).isEmpty());
            assertEquals(values.size(), oldValues.size());
            for (int i = 0; i < oldValues.size() - 1; i++) {
                assertEquals(values.get(i), oldValues.get(i + 1));
            }
        }
    }
}