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 org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
23 import com.vaadin.event.SelectionEvent;
24 import com.vaadin.event.SelectionEvent.SelectionListener;
25 import com.vaadin.v7.data.Container;
26 import com.vaadin.v7.data.util.IndexedContainer;
27 import com.vaadin.v7.ui.Grid;
28 import com.vaadin.v7.ui.Grid.SelectionMode;
29 import com.vaadin.v7.ui.Grid.SingleSelectionModel;
31 public class SingleSelectionModelTest {
33 private Object itemId1Present = "itemId1Present";
34 private Object itemId2Present = "itemId2Present";
36 private Object itemIdNotPresent = "itemIdNotPresent";
37 private Container.Indexed dataSource;
38 private SingleSelectionModel model;
41 private boolean expectingEvent = false;
45 dataSource = createDataSource();
46 grid = new Grid(dataSource);
47 grid.setSelectionMode(SelectionMode.SINGLE);
48 model = (SingleSelectionModel) grid.getSelectionModel();
52 public void tearDown() {
53 Assert.assertFalse("Some expected event did not happen.",
57 private IndexedContainer createDataSource() {
58 final IndexedContainer container = new IndexedContainer();
59 container.addItem(itemId1Present);
60 container.addItem(itemId2Present);
61 for (int i = 2; i < 10; i++) {
62 container.addItem(new Object());
69 public void testSelectAndDeselctRow() throws Throwable {
71 expectEvent(itemId1Present, null);
72 model.select(itemId1Present);
73 expectEvent(null, itemId1Present);
75 } catch (Exception e) {
81 public void testSelectAndChangeSelectedRow() throws Throwable {
83 expectEvent(itemId1Present, null);
84 model.select(itemId1Present);
85 expectEvent(itemId2Present, itemId1Present);
86 model.select(itemId2Present);
87 } catch (Exception e) {
93 public void testRemovingSelectedRowAndThenDeselecting() throws Throwable {
95 expectEvent(itemId2Present, null);
96 model.select(itemId2Present);
97 dataSource.removeItem(itemId2Present);
98 expectEvent(null, itemId2Present);
100 } catch (Exception e) {
106 public void testSelectAndReSelectRow() throws Throwable {
108 expectEvent(itemId1Present, null);
109 model.select(itemId1Present);
110 expectEvent(null, null);
111 // This is no-op. Nothing should happen.
112 model.select(itemId1Present);
113 } catch (Exception e) {
116 Assert.assertTrue("Should still wait for event", expectingEvent);
117 expectingEvent = false;
120 @Test(expected = IllegalArgumentException.class)
121 public void testSelectNonExistentRow() {
122 model.select(itemIdNotPresent);
125 private void expectEvent(final Object selected, final Object deselected) {
126 expectingEvent = true;
127 grid.addSelectionListener(new SelectionListener() {
130 public void select(SelectionEvent event) {
131 if (selected != null) {
132 Assert.assertTrue("Selection did not contain expected item",
133 event.getAdded().contains(selected));
135 Assert.assertTrue("Unexpected selection",
136 event.getAdded().isEmpty());
139 if (deselected != null) {
141 "DeSelection did not contain expected item",
142 event.getRemoved().contains(deselected));
144 Assert.assertTrue("Unexpected selection",
145 event.getRemoved().isEmpty());
148 grid.removeSelectionListener(this);
149 expectingEvent = false;