From 6456428004199f7f4d887579b460df96530c42b0 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 2 Feb 2017 22:07:21 +0200 Subject: [PATCH] Add Grid editor events. (#8365) * Migrate v7 Grid tests. Next round. Fixes #8419 * Add Grid editor events. Fixes #8202. --- .../connectors/grid/EditorConnector.java | 5 +- .../client/widget/grid/EditorHandler.java | 5 +- .../java/com/vaadin/client/widgets/Grid.java | 8 +- .../com/vaadin/ui/components/grid/Editor.java | 27 +++ .../ui/components/grid/EditorCancelEvent.java | 60 +++++++ .../components/grid/EditorCancelListener.java | 44 +++++ .../vaadin/ui/components/grid/EditorImpl.java | 36 +++- .../ui/components/grid/EditorSaveEvent.java | 60 +++++++ .../components/grid/EditorSaveListener.java | 41 +++++ .../server/component/grid/EditorImplTest.java | 165 ++++++++++++++++++ .../ui/grid/editor/EditorServerRpc.java | 10 +- .../components/grid/GridEditorEvents.java | 58 ++++++ .../components/grid/GridEditorEventsTest.java | 68 ++++++++ 13 files changed, 576 insertions(+), 11 deletions(-) create mode 100644 server/src/main/java/com/vaadin/ui/components/grid/EditorCancelEvent.java create mode 100644 server/src/main/java/com/vaadin/ui/components/grid/EditorCancelListener.java create mode 100644 server/src/main/java/com/vaadin/ui/components/grid/EditorSaveEvent.java create mode 100644 server/src/main/java/com/vaadin/ui/components/grid/EditorSaveListener.java create mode 100644 server/src/test/java/com/vaadin/tests/server/component/grid/EditorImplTest.java create mode 100644 uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java index fb76bc8c03..50704c4262 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java @@ -103,11 +103,12 @@ public class EditorConnector extends AbstractExtensionConnector { } @Override - public void cancel(EditorRequest request) { + public void cancel(EditorRequest request, + boolean afterBeingSaved) { if (!handleServerInitiated(request)) { // No startRequest as we don't get (or need) // a confirmation from the server - rpc.cancel(); + rpc.cancel(afterBeingSaved); } } diff --git a/client/src/main/java/com/vaadin/client/widget/grid/EditorHandler.java b/client/src/main/java/com/vaadin/client/widget/grid/EditorHandler.java index 97e8cbf297..6c23d8af87 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/EditorHandler.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/EditorHandler.java @@ -156,10 +156,13 @@ public interface EditorHandler { * * @param request * the cancel request + * @param afterBeingSaved + * if {@code true} then this method is called to close editor + * after save action, otherwise it represents a cancel action * * @see Grid#cancelEditor() */ - public void cancel(EditorRequest request); + public void cancel(EditorRequest request, boolean afterBeingSaved); /** * Commits changes in the currently active edit to the data source. Called diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 19dfb79df6..9974505bd4 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -1388,7 +1388,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, public void onSuccess(EditorRequest request) { if (state == State.SAVING) { cleanup(); - cancel(); + cancel(true); grid.clearSortOrder(); } } @@ -1640,6 +1640,10 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, * if this editor is not in edit mode */ public void cancel() { + cancel(false); + } + + private void cancel(boolean afterSave) { if (!enabled) { throw new IllegalStateException( "Cannot cancel edit: editor is not enabled"); @@ -1649,7 +1653,7 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, "Cannot cancel edit: editor is not in edit mode"); } handler.cancel(new EditorRequestImpl<>(grid, rowIndex, - focusedColumnIndex, null)); + focusedColumnIndex, null), afterSave); doCancel(); } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java index 823c5cd849..0eff213c7e 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java @@ -18,6 +18,8 @@ package com.vaadin.ui.components.grid; import java.io.Serializable; import com.vaadin.data.Binder; +import com.vaadin.shared.Registration; +import com.vaadin.ui.Grid; /** * An editor in a Grid. @@ -155,4 +157,29 @@ public interface Editor extends Serializable { * @see EditorErrorGenerator */ public EditorErrorGenerator getErrorGenerator(); + + /** + * Adds an editor save {@code listener}. + * + * @param listener + * save listener + * @return a registration object for removing the listener + */ + public Registration addSaveListener(EditorSaveListener listener); + + /** + * Adds an editor cancel {@code listener}. + * + * @param listener + * cancel listener + * @return a registration object for removing the listener + */ + public Registration addCancelListener(EditorCancelListener listener); + + /** + * Gets the Grid instance which this editor belongs to. + * + * @return the grid which owns the editor + */ + public Grid getGrid(); } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelEvent.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelEvent.java new file mode 100644 index 0000000000..b2af155977 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelEvent.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.components.grid; + +import java.util.EventObject; + +import com.vaadin.ui.Grid; + +/** + * An event that is fired when a Grid editor is cancelled. + * + * @author Vaadin Ltd + * @since 8.0 + * + * @see EditorCancelListener + * @see Editor#addCancelListener(EditorCancelListener) + * + * @param + * the bean type + */ +public class EditorCancelEvent extends EventObject { + + /** + * Constructor for a editor cancel event. + * + * @param editor + * the source of the event + */ + public EditorCancelEvent(Editor editor) { + super(editor); + } + + @SuppressWarnings("unchecked") + @Override + public Editor getSource() { + return (Editor) super.getSource(); + } + + /** + * Gets the editors' grid. + * + * @return the editors' grid + */ + public Grid getGrid() { + return getSource().getGrid(); + } +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelListener.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelListener.java new file mode 100644 index 0000000000..0197a59f12 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorCancelListener.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.components.grid; + +import java.io.Serializable; + +import com.vaadin.ui.Grid; + +/** + * An event listener for a {@link Grid} editor cancel events. + * + * @author Vaadin Ltd + * @since 8.0 + * + * @see EditorCancelEvent + * @see Editor#addCancelListener(EditorCancelListener) + * + * @param + * the bean type + */ +@FunctionalInterface +public interface EditorCancelListener extends Serializable { + + /** + * Called when the editor is cancelled. + * + * @param event + * cancel event + */ + public void onEditorCancel(EditorCancelEvent event); +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java index dae7c61ee7..51cf1433b2 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java @@ -28,10 +28,13 @@ import com.vaadin.data.Binder.Binding; import com.vaadin.data.BinderValidationStatus; import com.vaadin.data.BinderValidationStatusHandler; import com.vaadin.data.PropertySet; +import com.vaadin.event.EventRouter; +import com.vaadin.shared.Registration; import com.vaadin.shared.ui.grid.editor.EditorClientRpc; import com.vaadin.shared.ui.grid.editor.EditorServerRpc; import com.vaadin.shared.ui.grid.editor.EditorState; import com.vaadin.ui.Component; +import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.AbstractGridExtension; import com.vaadin.ui.Grid.Column; @@ -92,6 +95,8 @@ public class EditorImpl extends AbstractGridExtension private T edited; private boolean saving = false; private EditorClientRpc rpc; + private EventRouter eventRouter = new EventRouter(); + private EditorErrorGenerator errorGenerator = (fieldToColumn, status) -> { String message = status.getFieldValidationErrors().stream() @@ -128,8 +133,8 @@ public class EditorImpl extends AbstractGridExtension } @Override - public void cancel() { - doClose(); + public void cancel(boolean afterBeingSaved) { + doCancel(afterBeingSaved); } @Override @@ -240,6 +245,7 @@ public class EditorImpl extends AbstractGridExtension binder.validate(); if (binder.writeBeanIfValid(edited)) { refresh(edited); + eventRouter.fireEvent(new EditorSaveEvent<>(this)); return true; } } @@ -253,10 +259,17 @@ public class EditorImpl extends AbstractGridExtension @Override public void cancel() { - doClose(); + doCancel(false); rpc.cancel(); } + private void doCancel(boolean afterBeingSaved) { + doClose(); + if (!afterBeingSaved) { + eventRouter.fireEvent(new EditorCancelEvent<>(this)); + } + } + /** * Handles clean up for closing the Editor. */ @@ -317,4 +330,21 @@ public class EditorImpl extends AbstractGridExtension public EditorErrorGenerator getErrorGenerator() { return errorGenerator; } + + @Override + public Registration addSaveListener(EditorSaveListener listener) { + return eventRouter.addListener(EditorSaveEvent.class, listener, + EditorSaveListener.class.getDeclaredMethods()[0]); + } + + @Override + public Registration addCancelListener(EditorCancelListener listener) { + return eventRouter.addListener(EditorCancelEvent.class, listener, + EditorCancelListener.class.getDeclaredMethods()[0]); + } + + @Override + public Grid getGrid() { + return getParent(); + } } \ No newline at end of file diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveEvent.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveEvent.java new file mode 100644 index 0000000000..e3d29f38a2 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveEvent.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.components.grid; + +import java.util.EventObject; + +import com.vaadin.ui.Grid; + +/** + * An event that is fired when a Grid editor is saved. + * + * @author Vaadin Ltd + * @since 8.0 + * + * @see EditorSaveListener + * @see Editor#addSaveListener(EditorSaveListener) + * + * @param + * the bean type + */ +public class EditorSaveEvent extends EventObject { + + /** + * Constructor for a editor save event. + * + * @param editor + * the source of the event + */ + public EditorSaveEvent(Editor editor) { + super(editor); + } + + @SuppressWarnings("unchecked") + @Override + public Editor getSource() { + return (Editor) super.getSource(); + } + + /** + * Gets the editors' grid. + * + * @return the editors' grid + */ + public Grid getGrid() { + return getSource().getGrid(); + } +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveListener.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveListener.java new file mode 100644 index 0000000000..329fde521b --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorSaveListener.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui.components.grid; + +import java.io.Serializable; + +import com.vaadin.ui.Grid; + +/** + * An event listener for a {@link Grid} editor save events. + * + * @author Vaadin Ltd + * @since 8.0 + * + * @see EditorSaveEvent + * @see Editor#addSaveListener(EditorSaveListener) + */ +@FunctionalInterface +public interface EditorSaveListener extends Serializable { + + /** + * Called when the editor is saved. + * + * @param event + * save event + */ + public void onEditorSave(EditorSaveEvent event); +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/EditorImplTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/EditorImplTest.java new file mode 100644 index 0000000000..de3c6569e5 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/EditorImplTest.java @@ -0,0 +1,165 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.grid; + +import java.util.Optional; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import com.vaadin.data.Binder; +import com.vaadin.data.PropertyDefinition; +import com.vaadin.data.PropertySet; +import com.vaadin.shared.communication.ServerRpc; +import com.vaadin.shared.ui.grid.editor.EditorServerRpc; +import com.vaadin.ui.components.grid.EditorCancelEvent; +import com.vaadin.ui.components.grid.EditorCancelListener; +import com.vaadin.ui.components.grid.EditorImpl; +import com.vaadin.ui.components.grid.EditorSaveEvent; +import com.vaadin.ui.components.grid.EditorSaveListener; + +/** + * @author Vaadin Ltd + * + */ +public class EditorImplTest { + + public static class TestEditorImpl extends EditorImpl implements + EditorSaveListener, EditorCancelListener { + + public TestEditorImpl() { + super(new PropertySet() { + @Override + public Stream> getProperties() { + return null; + } + + @Override + public Optional> getProperty( + String name) { + return null; + } + }); + } + + EditorCancelEvent cancelEvent; + + EditorSaveEvent saveEvent; + + EditorServerRpc rpc; + + @Override + public boolean isOpen() { + return true; + } + + @Override + public boolean isBuffered() { + return true; + } + + @Override + protected void refresh(Object item) { + } + + @Override + public void onEditorCancel(EditorCancelEvent event) { + Assert.assertNull(cancelEvent); + cancelEvent = event; + } + + @Override + public void onEditorSave(EditorSaveEvent event) { + Assert.assertNull(saveEvent); + saveEvent = event; + } + + @Override + protected void registerRpc(T implementation) { + if (implementation instanceof EditorServerRpc) { + rpc = (EditorServerRpc) implementation; + } + super.registerRpc(implementation); + } + } + + private TestEditorImpl editor; + private Binder binder; + + @Before + public void setUp() { + editor = new TestEditorImpl(); + editor.addSaveListener(editor); + editor.addCancelListener(editor); + binder = Mockito.mock(Binder.class); + editor.setBinder(binder); + } + + @Test + public void save_eventIsFired() { + Mockito.when(binder.writeBeanIfValid(Mockito.any())).thenReturn(true); + + editor.save(); + + Assert.assertNotNull(editor.saveEvent); + Assert.assertNull(editor.cancelEvent); + + Assert.assertEquals(editor, editor.saveEvent.getSource()); + } + + @Test + public void cancel_eventIsFired() { + editor.cancel(); + + Assert.assertNull(editor.saveEvent); + Assert.assertNotNull(editor.cancelEvent); + + Assert.assertEquals(editor, editor.cancelEvent.getSource()); + } + + @Test + public void saveFromClient_eventIsFired() { + Mockito.when(binder.writeBeanIfValid(Mockito.any())).thenReturn(true); + + editor.rpc.save(); + + Assert.assertNotNull(editor.saveEvent); + Assert.assertNull(editor.cancelEvent); + + Assert.assertEquals(editor, editor.saveEvent.getSource()); + } + + @Test + public void cancelFromClient_eventIsFired() { + editor.rpc.cancel(false); + + Assert.assertNull(editor.saveEvent); + Assert.assertNotNull(editor.cancelEvent); + + Assert.assertEquals(editor, editor.cancelEvent.getSource()); + } + + @Test + public void cancelAfterSaveFromClient_eventIsNotFired() { + editor.rpc.cancel(true); + + Assert.assertNull(editor.saveEvent); + Assert.assertNull(editor.cancelEvent); + } +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/editor/EditorServerRpc.java b/shared/src/main/java/com/vaadin/shared/ui/grid/editor/EditorServerRpc.java index 8f338aa1a0..a19d94d9d6 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/editor/EditorServerRpc.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/editor/EditorServerRpc.java @@ -28,8 +28,8 @@ public interface EditorServerRpc extends ServerRpc { /** * Asks the server to open the editor and bind data to it. When a bind * request is sent, it must be acknowledged with a - * {@link EditorClientRpc#confirmBind(boolean) confirm call} before the client can - * open the editor. + * {@link EditorClientRpc#confirmBind(boolean) confirm call} before the + * client can open the editor. * * @param key * the identifier key for edited item @@ -47,6 +47,10 @@ public interface EditorServerRpc extends ServerRpc { * Tells the server to cancel editing. When sending a cancel request, the * client does not need to wait for confirmation by the server before hiding * the editor. + * + * @param afterBeingSaved + * if {@code true} then this method is called to close editor + * after save action, otherwise it represents a cancel action */ - void cancel(); + void cancel(boolean afterBeingSaved); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java new file mode 100644 index 0000000000..bd799a9925 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorEvents.java @@ -0,0 +1,58 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +import com.vaadin.data.Binder; +import com.vaadin.data.Binder.Binding; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.data.bean.Person; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.Column; +import com.vaadin.ui.TextField; + +/** + * @author Vaadin Ltd + * + */ +public class GridEditorEvents extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + Grid grid = new Grid<>(); + Person person1 = new Person(); + person1.setFirstName(""); + + Person person2 = new Person(); + person2.setFirstName("foo"); + + grid.setItems(person1, person2); + Column column = grid.addColumn(Person::getFirstName); + + Binder binder = grid.getEditor().getBinder(); + grid.getEditor().setEnabled(true); + + TextField field = new TextField(); + Binding binding = binder.bind(field, + Person::getFirstName, Person::setFirstName); + column.setEditorBinding(binding); + + grid.getEditor().addCancelListener(event -> log("editor is canceled")); + grid.getEditor().addSaveListener(event -> log("editor is saved")); + addComponent(grid); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java new file mode 100644 index 0000000000..43cfd66c34 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorEventsTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.grid; + +import static org.junit.Assert.assertEquals; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.testbench.elements.GridElement.GridEditorElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * @author Vaadin Ltd + * + */ +public class GridEditorEventsTest extends MultiBrowserTest { + + @Test + public void editorEvents() throws InterruptedException { + openTestURL(); + + GridElement grid = $(GridElement.class).first(); + + assertEditorEvents(0, grid); + assertEditorEvents(1, grid); + } + + private void assertEditorEvents(int index, GridElement grid) { + GridEditorElement editor = updateField(index, grid, "foo"); + editor.save(); + + Assert.assertEquals((index * 2 + 1) + ". editor is saved", + getLogRow(0)); + + editor = updateField(index, grid, "bar"); + editor.cancel(); + + Assert.assertEquals((index * 2 + 2) + ". editor is canceled", + getLogRow(0)); + } + + private GridEditorElement updateField(int index, GridElement grid, + String text) { + grid.getRow(index).doubleClick(); + + GridEditorElement editor = grid.getEditor(); + WebElement focused = getFocusedElement(); + assertEquals("input", focused.getTagName()); + focused.sendKeys(text); + return editor; + } +} -- 2.39.5