You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleSelectionModelTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.vaadin.v7.tests.server.component.grid;
  2. import static org.junit.Assert.assertFalse;
  3. import static org.junit.Assert.assertTrue;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import com.vaadin.ui.ComponentTest;
  8. import com.vaadin.v7.data.Container;
  9. import com.vaadin.v7.data.util.IndexedContainer;
  10. import com.vaadin.v7.event.SelectionEvent;
  11. import com.vaadin.v7.event.SelectionEvent.SelectionListener;
  12. import com.vaadin.v7.shared.ui.grid.selection.SingleSelectionModelServerRpc;
  13. import com.vaadin.v7.ui.Grid;
  14. import com.vaadin.v7.ui.Grid.SelectionMode;
  15. import com.vaadin.v7.ui.Grid.SelectionModel.HasUserSelectionAllowed;
  16. import com.vaadin.v7.ui.Grid.SingleSelectionModel;
  17. public class SingleSelectionModelTest {
  18. private Object itemId1Present = "itemId1Present";
  19. private Object itemId2Present = "itemId2Present";
  20. private Object itemIdNotPresent = "itemIdNotPresent";
  21. private Container.Indexed dataSource;
  22. private SingleSelectionModel model;
  23. private Grid grid;
  24. private boolean expectingEvent = false;
  25. @Before
  26. public void setUp() {
  27. dataSource = createDataSource();
  28. grid = new Grid(dataSource);
  29. grid.setSelectionMode(SelectionMode.SINGLE);
  30. model = (SingleSelectionModel) grid.getSelectionModel();
  31. }
  32. @After
  33. public void tearDown() {
  34. assertFalse("Some expected event did not happen.", expectingEvent);
  35. }
  36. private IndexedContainer createDataSource() {
  37. final IndexedContainer container = new IndexedContainer();
  38. container.addItem(itemId1Present);
  39. container.addItem(itemId2Present);
  40. for (int i = 2; i < 10; i++) {
  41. container.addItem(new Object());
  42. }
  43. return container;
  44. }
  45. @Test
  46. public void testSelectAndDeselctRow() throws Throwable {
  47. try {
  48. expectEvent(itemId1Present, null);
  49. model.select(itemId1Present);
  50. expectEvent(null, itemId1Present);
  51. model.select(null);
  52. } catch (Exception e) {
  53. throw e.getCause();
  54. }
  55. }
  56. @Test
  57. public void testSelectAndChangeSelectedRow() throws Throwable {
  58. try {
  59. expectEvent(itemId1Present, null);
  60. model.select(itemId1Present);
  61. expectEvent(itemId2Present, itemId1Present);
  62. model.select(itemId2Present);
  63. } catch (Exception e) {
  64. throw e.getCause();
  65. }
  66. }
  67. @Test
  68. public void testRemovingSelectedRowAndThenDeselecting() throws Throwable {
  69. try {
  70. expectEvent(itemId2Present, null);
  71. model.select(itemId2Present);
  72. dataSource.removeItem(itemId2Present);
  73. expectEvent(null, itemId2Present);
  74. model.select(null);
  75. } catch (Exception e) {
  76. throw e.getCause();
  77. }
  78. }
  79. @Test
  80. public void testSelectAndReSelectRow() throws Throwable {
  81. try {
  82. expectEvent(itemId1Present, null);
  83. model.select(itemId1Present);
  84. expectEvent(null, null);
  85. // This is no-op. Nothing should happen.
  86. model.select(itemId1Present);
  87. } catch (Exception e) {
  88. throw e.getCause();
  89. }
  90. assertTrue("Should still wait for event", expectingEvent);
  91. expectingEvent = false;
  92. }
  93. @Test(expected = IllegalArgumentException.class)
  94. public void testSelectNonExistentRow() {
  95. model.select(itemIdNotPresent);
  96. }
  97. private void expectEvent(final Object selected, final Object deselected) {
  98. expectingEvent = true;
  99. grid.addSelectionListener(new SelectionListener() {
  100. @Override
  101. public void select(SelectionEvent event) {
  102. if (selected != null) {
  103. assertTrue("Selection did not contain expected item",
  104. event.getAdded().contains(selected));
  105. } else {
  106. assertTrue("Unexpected selection",
  107. event.getAdded().isEmpty());
  108. }
  109. if (deselected != null) {
  110. assertTrue("DeSelection did not contain expected item",
  111. event.getRemoved().contains(deselected));
  112. } else {
  113. assertTrue("Unexpected selection",
  114. event.getRemoved().isEmpty());
  115. }
  116. grid.removeSelectionListener(this);
  117. expectingEvent = false;
  118. }
  119. });
  120. }
  121. @Test(expected = IllegalStateException.class)
  122. public void refuseSelectionWhenUserSelectionDisallowed() {
  123. ((HasUserSelectionAllowed) grid.getSelectionModel())
  124. .setUserSelectionAllowed(false);
  125. SingleSelectionModelServerRpc serverRpc = ComponentTest.getRpcProxy(
  126. grid.getSelectionModel(), SingleSelectionModelServerRpc.class);
  127. serverRpc.select("a");
  128. }
  129. }