]> source.dussan.org Git - vaadin-framework.git/blob
7a658fd31faf0f8728abc362626f015c1f3a41d7
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2016 Vaadin Ltd.
3  *
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
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
14  * the License.
15  */
16 package com.vaadin.v7.tests.server.component.grid;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import com.vaadin.event.SelectionEvent;
28 import com.vaadin.event.SelectionEvent.SelectionListener;
29 import com.vaadin.v7.data.Container;
30 import com.vaadin.v7.data.util.IndexedContainer;
31 import com.vaadin.v7.ui.Grid;
32 import com.vaadin.v7.ui.Grid.MultiSelectionModel;
33 import com.vaadin.v7.ui.Grid.SelectionMode;
34
35 public class MultiSelectionModelTest {
36
37     private Object itemId1Present = "itemId1Present";
38     private Object itemId2Present = "itemId2Present";
39     private Object itemId3Present = "itemId3Present";
40
41     private Object itemIdNotPresent = "itemIdNotPresent";
42     private Container.Indexed dataSource;
43     private MultiSelectionModel model;
44     private Grid grid;
45
46     private boolean expectingEvent = false;
47     private boolean expectingDeselectEvent;
48     private List<Object> select = new ArrayList<Object>();
49     private List<Object> deselect = new ArrayList<Object>();
50
51     @Before
52     public void setUp() {
53         dataSource = createDataSource();
54         grid = new Grid(dataSource);
55         grid.setSelectionMode(SelectionMode.MULTI);
56         model = (MultiSelectionModel) grid.getSelectionModel();
57     }
58
59     @After
60     public void tearDown() {
61         Assert.assertFalse("Some expected select event did not happen.",
62                 expectingEvent);
63         Assert.assertFalse("Some expected deselect event did not happen.",
64                 expectingDeselectEvent);
65     }
66
67     private IndexedContainer createDataSource() {
68         final IndexedContainer container = new IndexedContainer();
69         container.addItem(itemId1Present);
70         container.addItem(itemId2Present);
71         container.addItem(itemId3Present);
72         for (int i = 3; i < 10; i++) {
73             container.addItem(new Object());
74         }
75
76         return container;
77     }
78
79     @Test
80     public void testSelectAndDeselectRow() throws Throwable {
81         try {
82             expectSelectEvent(itemId1Present);
83             model.select(itemId1Present);
84             expectDeselectEvent(itemId1Present);
85             model.deselect(itemId1Present);
86         } catch (Exception e) {
87             throw e.getCause();
88         }
89
90         verifyCurrentSelection();
91     }
92
93     @Test
94     public void testAddSelection() throws Throwable {
95         try {
96             expectSelectEvent(itemId1Present);
97             model.select(itemId1Present);
98             expectSelectEvent(itemId2Present);
99             model.select(itemId2Present);
100         } catch (Exception e) {
101             throw e.getCause();
102         }
103
104         verifyCurrentSelection(itemId1Present, itemId2Present);
105     }
106
107     @Test
108     public void testSettingSelection() throws Throwable {
109         try {
110             expectSelectEvent(itemId2Present, itemId1Present);
111             model.setSelected(Arrays
112                     .asList(new Object[] { itemId1Present, itemId2Present }));
113             verifyCurrentSelection(itemId1Present, itemId2Present);
114
115             expectDeselectEvent(itemId1Present);
116             expectSelectEvent(itemId3Present);
117             model.setSelected(Arrays
118                     .asList(new Object[] { itemId3Present, itemId2Present }));
119             verifyCurrentSelection(itemId3Present, itemId2Present);
120         } catch (Exception e) {
121             throw e.getCause();
122         }
123     }
124
125     private void expectSelectEvent(Object... selectArray) {
126         select = Arrays.asList(selectArray);
127         addListener();
128     }
129
130     private void expectDeselectEvent(Object... deselectArray) {
131         deselect = Arrays.asList(deselectArray);
132         addListener();
133     }
134
135     private void addListener() {
136         if (expectingEvent) {
137             return;
138         }
139
140         expectingEvent = true;
141         grid.addSelectionListener(new SelectionListener() {
142
143             @Override
144             public void select(SelectionEvent event) {
145                 Assert.assertTrue("Selection did not contain expected items",
146                         event.getAdded().containsAll(select));
147                 Assert.assertTrue("Selection contained unexpected items",
148                         select.containsAll(event.getAdded()));
149                 select = new ArrayList<Object>();
150
151                 Assert.assertTrue("Deselection did not contain expected items",
152                         event.getRemoved().containsAll(deselect));
153                 Assert.assertTrue("Deselection contained unexpected items",
154                         deselect.containsAll(event.getRemoved()));
155                 deselect = new ArrayList<Object>();
156
157                 grid.removeSelectionListener(this);
158                 expectingEvent = false;
159             }
160         });
161     }
162
163     private void verifyCurrentSelection(Object... selection) {
164         final List<Object> selected = Arrays.asList(selection);
165         if (model.getSelectedRows().containsAll(selected)
166                 && selected.containsAll(model.getSelectedRows())) {
167             return;
168         }
169         Assert.fail("Not all items were correctly selected");
170     }
171 }