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
|
package com.vaadin.tests.components.grid;
import com.liferay.portal.kernel.atom.AtomEntryContent;
import com.vaadin.data.provider.CallbackDataProvider;
import com.vaadin.ui.Grid;
import com.vaadin.ui.components.grid.MultiSelectionModel;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class GridMultiSelectionModelProxyItemTest {
private List<String> data = IntStream.range(0, 100).boxed()
.map(i -> "String " + i).collect(Collectors.toList());
private Grid<AtomicReference<String>> proxyGrid = new Grid<>();
private MultiSelectionModel<AtomicReference<String>> model;
private AtomicReference<Set<AtomicReference<String>>> selectionEvent = new AtomicReference<>();
@Before
public void setup() {
proxyGrid.setDataProvider(new CallbackDataProvider<>(
q -> data.stream().map(AtomicReference::new).skip(q.getOffset())
.limit(q.getLimit()),
q -> data.size(), AtomicReference::get));
model = (MultiSelectionModel<AtomicReference<String>>) proxyGrid
.setSelectionMode(Grid.SelectionMode.MULTI);
model.addSelectionListener(e -> {
selectionEvent.set(e.getAllSelectedItems());
});
}
@Test
public void testSelectAllWithProxyDataProvider() {
model.selectAll();
assertEquals("Item count mismatch on first select all", 100,
getSelectionEvent().size());
model.deselect(model.getFirstSelectedItem().orElseThrow(
() -> new IllegalStateException("Items should be selected")));
assertEquals("Item count mismatch on deselect", 99,
getSelectionEvent().size());
model.selectAll();
assertEquals("Item count mismatch on second select all", 100,
getSelectionEvent().size());
}
@Test
public void testUpdateSelectionWithDuplicateEntries() {
List<String> selection = data.stream().filter(s -> s.contains("1"))
.collect(Collectors.toList());
model.updateSelection(selection.stream().map(AtomicReference::new)
.collect(Collectors.toSet()), Collections.emptySet());
assertEquals("Failure in initial selection", selection.size(),
getSelectionEvent().size());
String toRemove = model.getFirstSelectedItem().map(AtomicReference::get)
.orElseThrow(() -> new IllegalStateException(
"Items should be selected"));
model.updateSelection(
Stream.of(toRemove).map(AtomicReference::new)
.collect(Collectors.toSet()),
Stream.of(toRemove).map(AtomicReference::new)
.collect(Collectors.toSet()));
assertNull(
"Selection should not change when selecting and deselecting once",
selectionEvent.get());
Set<AtomicReference<String>> added = new LinkedHashSet<>();
Set<AtomicReference<String>> removed = new LinkedHashSet<>();
for (int i = 0; i < 20; ++i) {
added.add(new AtomicReference<>(toRemove));
removed.add(new AtomicReference<>(toRemove));
}
model.updateSelection(added, removed);
assertNull(
"Selection should not change when selecting and deselecting 20 times",
selectionEvent.get());
removed.add(new AtomicReference<>(toRemove));
model.updateSelection(added, removed);
assertEquals("Item should have been deselected", selection.size() - 1,
getSelectionEvent().size());
}
private Set<AtomicReference<String>> getSelectionEvent() {
Optional<Set<AtomicReference<String>>> eventOptional = Optional
.of(selectionEvent.get());
selectionEvent.set(null);
return eventOptional.orElseThrow(() -> new IllegalStateException(
"Expected selection event never happened"));
}
}
|