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.

GridEditorTest.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package com.vaadin.v7.tests.server.component.grid;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertNull;
  6. import static org.junit.Assert.assertSame;
  7. import static org.junit.Assert.assertTrue;
  8. import static org.junit.Assert.fail;
  9. import java.lang.reflect.Method;
  10. import org.easymock.EasyMock;
  11. import org.junit.After;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import com.vaadin.server.MockVaadinSession;
  15. import com.vaadin.server.VaadinService;
  16. import com.vaadin.server.VaadinSession;
  17. import com.vaadin.v7.data.Item;
  18. import com.vaadin.v7.data.Property;
  19. import com.vaadin.v7.data.fieldgroup.FieldGroup;
  20. import com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException;
  21. import com.vaadin.v7.data.util.IndexedContainer;
  22. import com.vaadin.v7.ui.Field;
  23. import com.vaadin.v7.ui.Grid;
  24. import com.vaadin.v7.ui.TextField;
  25. public class GridEditorTest {
  26. private static final Object PROPERTY_NAME = "name";
  27. private static final Object PROPERTY_AGE = "age";
  28. private static final String DEFAULT_NAME = "Some Valid Name";
  29. private static final Integer DEFAULT_AGE = 25;
  30. private static final Object ITEM_ID = new Object();
  31. // Explicit field for the test session to save it from GC
  32. private VaadinSession session;
  33. private final Grid grid = new Grid();
  34. private Method doEditMethod;
  35. @Before
  36. @SuppressWarnings("unchecked")
  37. public void setup() throws SecurityException, NoSuchMethodException {
  38. IndexedContainer container = new IndexedContainer();
  39. container.addContainerProperty(PROPERTY_NAME, String.class, "[name]");
  40. container.addContainerProperty(PROPERTY_AGE, Integer.class,
  41. Integer.valueOf(-1));
  42. Item item = container.addItem(ITEM_ID);
  43. item.getItemProperty(PROPERTY_NAME).setValue(DEFAULT_NAME);
  44. item.getItemProperty(PROPERTY_AGE).setValue(DEFAULT_AGE);
  45. grid.setContainerDataSource(container);
  46. // VaadinSession needed for ConverterFactory
  47. VaadinService mockService = EasyMock
  48. .createNiceMock(VaadinService.class);
  49. session = new MockVaadinSession(mockService);
  50. VaadinSession.setCurrent(session);
  51. session.lock();
  52. // Access to method for actual editing.
  53. doEditMethod = Grid.class.getDeclaredMethod("doEditItem");
  54. doEditMethod.setAccessible(true);
  55. }
  56. @After
  57. public void tearDown() {
  58. session.unlock();
  59. session = null;
  60. VaadinSession.setCurrent(null);
  61. }
  62. @Test
  63. public void testInitAssumptions() throws Exception {
  64. assertFalse(grid.isEditorEnabled());
  65. assertNull(grid.getEditedItemId());
  66. assertNotNull(grid.getEditorFieldGroup());
  67. }
  68. @Test
  69. public void testSetEnabled() throws Exception {
  70. assertFalse(grid.isEditorEnabled());
  71. grid.setEditorEnabled(true);
  72. assertTrue(grid.isEditorEnabled());
  73. }
  74. @Test
  75. public void testSetDisabled() throws Exception {
  76. assertFalse(grid.isEditorEnabled());
  77. grid.setEditorEnabled(true);
  78. grid.setEditorEnabled(false);
  79. assertFalse(grid.isEditorEnabled());
  80. }
  81. @Test
  82. public void testSetReEnabled() throws Exception {
  83. assertFalse(grid.isEditorEnabled());
  84. grid.setEditorEnabled(true);
  85. grid.setEditorEnabled(false);
  86. grid.setEditorEnabled(true);
  87. assertTrue(grid.isEditorEnabled());
  88. }
  89. @Test
  90. public void testDetached() throws Exception {
  91. FieldGroup oldFieldGroup = grid.getEditorFieldGroup();
  92. grid.removeAllColumns();
  93. grid.setContainerDataSource(new IndexedContainer());
  94. assertFalse(oldFieldGroup == grid.getEditorFieldGroup());
  95. }
  96. @Test(expected = IllegalStateException.class)
  97. public void testDisabledEditItem() throws Exception {
  98. grid.editItem(ITEM_ID);
  99. }
  100. @Test
  101. public void testEditItem() throws Exception {
  102. startEdit();
  103. assertEquals(ITEM_ID, grid.getEditedItemId());
  104. assertEquals(getEditedItem(),
  105. grid.getEditorFieldGroup().getItemDataSource());
  106. assertEquals(DEFAULT_NAME,
  107. grid.getColumn(PROPERTY_NAME).getEditorField().getValue());
  108. assertEquals(String.valueOf(DEFAULT_AGE),
  109. grid.getColumn(PROPERTY_AGE).getEditorField().getValue());
  110. }
  111. @Test
  112. public void testSaveEditor() throws Exception {
  113. startEdit();
  114. TextField field = (TextField) grid.getColumn(PROPERTY_NAME)
  115. .getEditorField();
  116. field.setValue("New Name");
  117. assertEquals(DEFAULT_NAME, field.getPropertyDataSource().getValue());
  118. grid.saveEditor();
  119. assertTrue(grid.isEditorActive());
  120. assertFalse(field.isModified());
  121. assertEquals("New Name", field.getValue());
  122. assertEquals("New Name", getEditedProperty(PROPERTY_NAME).getValue());
  123. }
  124. @Test
  125. public void testSaveEditorCommitFail() throws Exception {
  126. startEdit();
  127. ((TextField) grid.getColumn(PROPERTY_AGE).getEditorField())
  128. .setValue("Invalid");
  129. try {
  130. // Manual fail instead of @Test(expected=...) to check it is
  131. // saveEditor that fails and not setValue
  132. grid.saveEditor();
  133. fail("CommitException expected when saving an invalid field value");
  134. } catch (CommitException e) {
  135. // expected
  136. }
  137. }
  138. @Test
  139. public void testCancelEditor() throws Exception {
  140. startEdit();
  141. TextField field = (TextField) grid.getColumn(PROPERTY_NAME)
  142. .getEditorField();
  143. field.setValue("New Name");
  144. Property<?> datasource = field.getPropertyDataSource();
  145. grid.cancelEditor();
  146. assertFalse(grid.isEditorActive());
  147. assertNull(grid.getEditedItemId());
  148. assertFalse(field.isModified());
  149. assertEquals("", field.getValue());
  150. assertEquals(DEFAULT_NAME, datasource.getValue());
  151. assertNull(field.getPropertyDataSource());
  152. assertNull(grid.getEditorFieldGroup().getItemDataSource());
  153. }
  154. @Test(expected = IllegalArgumentException.class)
  155. public void testNonexistentEditItem() throws Exception {
  156. grid.setEditorEnabled(true);
  157. grid.editItem(new Object());
  158. }
  159. @Test
  160. public void testGetField() throws Exception {
  161. startEdit();
  162. assertNotNull(grid.getColumn(PROPERTY_NAME).getEditorField());
  163. }
  164. @Test
  165. public void testGetFieldWithoutItem() throws Exception {
  166. grid.setEditorEnabled(true);
  167. assertNotNull(grid.getColumn(PROPERTY_NAME).getEditorField());
  168. }
  169. @Test
  170. public void testCustomBinding() {
  171. TextField textField = new TextField();
  172. grid.getColumn(PROPERTY_NAME).setEditorField(textField);
  173. startEdit();
  174. assertSame(textField, grid.getColumn(PROPERTY_NAME).getEditorField());
  175. }
  176. @Test(expected = IllegalStateException.class)
  177. public void testDisableWhileEditing() {
  178. startEdit();
  179. grid.setEditorEnabled(false);
  180. }
  181. @Test
  182. public void testFieldIsNotReadonly() {
  183. startEdit();
  184. Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
  185. assertFalse(field.isReadOnly());
  186. }
  187. @Test
  188. public void testFieldIsReadonlyWhenFieldGroupIsReadonly() {
  189. startEdit();
  190. grid.getEditorFieldGroup().setReadOnly(true);
  191. Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
  192. assertTrue(field.isReadOnly());
  193. }
  194. @Test
  195. public void testColumnRemoved() {
  196. Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
  197. assertSame("field should be attached to ", grid, field.getParent());
  198. grid.removeColumn(PROPERTY_NAME);
  199. assertNull("field should be detached from ", field.getParent());
  200. }
  201. @Test
  202. public void testSetFieldAgain() {
  203. TextField field = new TextField();
  204. grid.getColumn(PROPERTY_NAME).setEditorField(field);
  205. field = new TextField();
  206. grid.getColumn(PROPERTY_NAME).setEditorField(field);
  207. assertSame("new field should be used.", field,
  208. grid.getColumn(PROPERTY_NAME).getEditorField());
  209. }
  210. private void startEdit() {
  211. grid.setEditorEnabled(true);
  212. grid.editItem(ITEM_ID);
  213. // Simulate succesful client response to actually start the editing.
  214. try {
  215. doEditMethod.invoke(grid);
  216. } catch (Exception e) {
  217. fail("Editing item " + ITEM_ID + " failed. Cause: "
  218. + e.getCause().toString());
  219. }
  220. }
  221. private Item getEditedItem() {
  222. assertNotNull(grid.getEditedItemId());
  223. return grid.getContainerDataSource().getItem(grid.getEditedItemId());
  224. }
  225. private Property<?> getEditedProperty(Object propertyId) {
  226. return getEditedItem().getItemProperty(PROPERTY_NAME);
  227. }
  228. }