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 9.5KB

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