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.v7.tests.server.component.grid;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
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;
35 public class MultiSelectionModelTest {
37 private Object itemId1Present = "itemId1Present";
38 private Object itemId2Present = "itemId2Present";
39 private Object itemId3Present = "itemId3Present";
41 private Object itemIdNotPresent = "itemIdNotPresent";
42 private Container.Indexed dataSource;
43 private MultiSelectionModel model;
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>();
53 dataSource = createDataSource();
54 grid = new Grid(dataSource);
55 grid.setSelectionMode(SelectionMode.MULTI);
56 model = (MultiSelectionModel) grid.getSelectionModel();
60 public void tearDown() {
61 Assert.assertFalse("Some expected select event did not happen.",
63 Assert.assertFalse("Some expected deselect event did not happen.",
64 expectingDeselectEvent);
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());
80 public void testSelectAndDeselectRow() throws Throwable {
82 expectSelectEvent(itemId1Present);
83 model.select(itemId1Present);
84 expectDeselectEvent(itemId1Present);
85 model.deselect(itemId1Present);
86 } catch (Exception e) {
90 verifyCurrentSelection();
94 public void testAddSelection() throws Throwable {
96 expectSelectEvent(itemId1Present);
97 model.select(itemId1Present);
98 expectSelectEvent(itemId2Present);
99 model.select(itemId2Present);
100 } catch (Exception e) {
104 verifyCurrentSelection(itemId1Present, itemId2Present);
108 public void testSettingSelection() throws Throwable {
110 expectSelectEvent(itemId2Present, itemId1Present);
111 model.setSelected(Arrays
112 .asList(new Object[] { itemId1Present, itemId2Present }));
113 verifyCurrentSelection(itemId1Present, itemId2Present);
115 expectDeselectEvent(itemId1Present);
116 expectSelectEvent(itemId3Present);
117 model.setSelected(Arrays
118 .asList(new Object[] { itemId3Present, itemId2Present }));
119 verifyCurrentSelection(itemId3Present, itemId2Present);
120 } catch (Exception e) {
125 private void expectSelectEvent(Object... selectArray) {
126 select = Arrays.asList(selectArray);
130 private void expectDeselectEvent(Object... deselectArray) {
131 deselect = Arrays.asList(deselectArray);
135 private void addListener() {
136 if (expectingEvent) {
140 expectingEvent = true;
141 grid.addSelectionListener(new SelectionListener() {
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>();
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>();
157 grid.removeSelectionListener(this);
158 expectingEvent = false;
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())) {
169 Assert.fail("Not all items were correctly selected");