]> source.dussan.org Git - vaadin-framework.git/blob
d81bf62c027844a1c4dee6e8e15efc2e27951420
[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 org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22
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;
30
31 public class SingleSelectionModelTest {
32
33     private Object itemId1Present = "itemId1Present";
34     private Object itemId2Present = "itemId2Present";
35
36     private Object itemIdNotPresent = "itemIdNotPresent";
37     private Container.Indexed dataSource;
38     private SingleSelectionModel model;
39     private Grid grid;
40
41     private boolean expectingEvent = false;
42
43     @Before
44     public void setUp() {
45         dataSource = createDataSource();
46         grid = new Grid(dataSource);
47         grid.setSelectionMode(SelectionMode.SINGLE);
48         model = (SingleSelectionModel) grid.getSelectionModel();
49     }
50
51     @After
52     public void tearDown() {
53         Assert.assertFalse("Some expected event did not happen.",
54                 expectingEvent);
55     }
56
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());
63         }
64
65         return container;
66     }
67
68     @Test
69     public void testSelectAndDeselctRow() throws Throwable {
70         try {
71             expectEvent(itemId1Present, null);
72             model.select(itemId1Present);
73             expectEvent(null, itemId1Present);
74             model.select(null);
75         } catch (Exception e) {
76             throw e.getCause();
77         }
78     }
79
80     @Test
81     public void testSelectAndChangeSelectedRow() throws Throwable {
82         try {
83             expectEvent(itemId1Present, null);
84             model.select(itemId1Present);
85             expectEvent(itemId2Present, itemId1Present);
86             model.select(itemId2Present);
87         } catch (Exception e) {
88             throw e.getCause();
89         }
90     }
91
92     @Test
93     public void testRemovingSelectedRowAndThenDeselecting() throws Throwable {
94         try {
95             expectEvent(itemId2Present, null);
96             model.select(itemId2Present);
97             dataSource.removeItem(itemId2Present);
98             expectEvent(null, itemId2Present);
99             model.select(null);
100         } catch (Exception e) {
101             throw e.getCause();
102         }
103     }
104
105     @Test
106     public void testSelectAndReSelectRow() throws Throwable {
107         try {
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) {
114             throw e.getCause();
115         }
116         Assert.assertTrue("Should still wait for event", expectingEvent);
117         expectingEvent = false;
118     }
119
120     @Test(expected = IllegalArgumentException.class)
121     public void testSelectNonExistentRow() {
122         model.select(itemIdNotPresent);
123     }
124
125     private void expectEvent(final Object selected, final Object deselected) {
126         expectingEvent = true;
127         grid.addSelectionListener(new SelectionListener() {
128
129             @Override
130             public void select(SelectionEvent event) {
131                 if (selected != null) {
132                     Assert.assertTrue("Selection did not contain expected item",
133                             event.getAdded().contains(selected));
134                 } else {
135                     Assert.assertTrue("Unexpected selection",
136                             event.getAdded().isEmpty());
137                 }
138
139                 if (deselected != null) {
140                     Assert.assertTrue(
141                             "DeSelection did not contain expected item",
142                             event.getRemoved().contains(deselected));
143                 } else {
144                     Assert.assertTrue("Unexpected selection",
145                             event.getRemoved().isEmpty());
146                 }
147
148                 grid.removeSelectionListener(this);
149                 expectingEvent = false;
150             }
151         });
152     }
153 }