diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2015-01-13 18:58:01 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-01-13 20:29:23 +0000 |
commit | 30f0e91195447140190f4c4feb232e9c13d02453 (patch) | |
tree | 8749703edb86736e2ef10b3d754f87e8e3b4bf9b /server | |
parent | e52e34c5d53cf108a6d738dc91e0b736873ecce8 (diff) | |
download | vaadin-framework-30f0e91195447140190f4c4feb232e9c13d02453.tar.gz vaadin-framework-30f0e91195447140190f4c4feb232e9c13d02453.zip |
Discard changes in server-side Grid.cancelEditor() (#16199)
Change-Id: Id1197d5e0b583fce13eacbb18f9891543f07c94c
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 4 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/grid/GridEditorTest.java | 69 |
2 files changed, 70 insertions, 3 deletions
diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index a4a78f4ff3..0843fb6ec1 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -4457,7 +4457,8 @@ public class Grid extends AbstractComponent implements SelectionNotifier, } /** - * Cancels the currently active edit if any. + * Cancels the currently active edit if any. Hides the editor and discards + * possible unsaved changes in the editor fields. */ public void cancelEditor() { if (isEditorActive()) { @@ -4469,6 +4470,7 @@ public class Grid extends AbstractComponent implements SelectionNotifier, protected void doCancelEditor() { editedItemId = null; + editorFieldGroup.discard(); } void resetEditor() { diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridEditorTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridEditorTest.java index c3817efc3f..3e52314fbc 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/GridEditorTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridEditorTest.java @@ -24,11 +24,14 @@ import static org.junit.Assert.assertTrue; import org.easymock.EasyMock; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.vaadin.data.Item; +import com.vaadin.data.Property; import com.vaadin.data.fieldgroup.FieldGroup; +import com.vaadin.data.fieldgroup.FieldGroup.CommitException; import com.vaadin.data.util.IndexedContainer; import com.vaadin.server.MockVaadinSession; import com.vaadin.server.VaadinService; @@ -41,6 +44,8 @@ public class GridEditorTest { private static final Object PROPERTY_NAME = "name"; private static final Object PROPERTY_AGE = "age"; + private static final String DEFAULT_NAME = "Some Valid Name"; + private static final Integer DEFAULT_AGE = 25; private static final Object ITEM_ID = new Object(); private Grid grid; @@ -57,8 +62,8 @@ public class GridEditorTest { Integer.valueOf(-1)); Item item = container.addItem(ITEM_ID); - item.getItemProperty(PROPERTY_NAME).setValue("Some Valid Name"); - item.getItemProperty(PROPERTY_AGE).setValue(Integer.valueOf(25)); + item.getItemProperty(PROPERTY_NAME).setValue(DEFAULT_NAME); + item.getItemProperty(PROPERTY_AGE).setValue(DEFAULT_AGE); grid = new Grid(container); @@ -125,6 +130,57 @@ public class GridEditorTest { public void editItem() throws Exception { startEdit(); assertEquals(ITEM_ID, grid.getEditedItemId()); + assertEquals(getEditedItem(), grid.getEditorFieldGroup() + .getItemDataSource()); + + assertEquals(DEFAULT_NAME, grid.getEditorField(PROPERTY_NAME) + .getValue()); + assertEquals(String.valueOf(DEFAULT_AGE), + grid.getEditorField(PROPERTY_AGE).getValue()); + } + + @Test + public void saveEditor() throws Exception { + startEdit(); + TextField field = (TextField) grid.getEditorField(PROPERTY_NAME); + + field.setValue("New Name"); + assertEquals(DEFAULT_NAME, field.getPropertyDataSource().getValue()); + + grid.saveEditor(); + assertTrue(grid.isEditorActive()); + assertFalse(field.isModified()); + assertEquals("New Name", field.getValue()); + assertEquals("New Name", getEditedProperty(PROPERTY_NAME).getValue()); + } + + @Test + public void saveEditorCommitFail() throws Exception { + startEdit(); + + ((TextField) grid.getEditorField(PROPERTY_AGE)).setValue("Invalid"); + try { + // Manual fail instead of @Test(expected=...) to check it is + // saveEditor that fails and not setValue + grid.saveEditor(); + Assert.fail("CommitException expected when saving an invalid field value"); + } catch (CommitException e) { + // expected + } + } + + @Test + public void cancelEditor() throws Exception { + startEdit(); + TextField field = (TextField) grid.getEditorField(PROPERTY_NAME); + field.setValue("New Name"); + + grid.cancelEditor(); + assertFalse(grid.isEditorActive()); + assertNull(grid.getEditedItemId()); + assertFalse(field.isModified()); + assertEquals(DEFAULT_NAME, field.getValue()); + assertEquals(DEFAULT_NAME, field.getPropertyDataSource().getValue()); } @Test(expected = IllegalArgumentException.class) @@ -206,4 +262,13 @@ public class GridEditorTest { grid.setEditorEnabled(true); grid.editItem(ITEM_ID); } + + private Item getEditedItem() { + assertNotNull(grid.getEditedItemId()); + return grid.getContainerDataSource().getItem(grid.getEditedItemId()); + } + + private Property<?> getEditedProperty(Object propertyId) { + return getEditedItem().getItemProperty(PROPERTY_NAME); + } } |