diff options
author | Denis <denis@vaadin.com> | 2017-02-02 22:07:21 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-02-02 22:07:21 +0200 |
commit | 6456428004199f7f4d887579b460df96530c42b0 (patch) | |
tree | b54ee2ce835e69859ddcd1f3cb407baa0d81cd6f /server/src/test | |
parent | ab207e8bbeb6f3bf5b6a45e01ca5a4fb0e0c4f6a (diff) | |
download | vaadin-framework-6456428004199f7f4d887579b460df96530c42b0.tar.gz vaadin-framework-6456428004199f7f4d887579b460df96530c42b0.zip |
Add Grid editor events. (#8365)
* Migrate v7 Grid tests. Next round.
Fixes #8419
* Add Grid editor events.
Fixes #8202.
Diffstat (limited to 'server/src/test')
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/grid/EditorImplTest.java | 165 |
1 files changed, 165 insertions, 0 deletions
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<Object> implements + EditorSaveListener<Object>, EditorCancelListener<Object> { + + public TestEditorImpl() { + super(new PropertySet<Object>() { + @Override + public Stream<PropertyDefinition<Object, ?>> getProperties() { + return null; + } + + @Override + public Optional<PropertyDefinition<Object, ?>> getProperty( + String name) { + return null; + } + }); + } + + EditorCancelEvent<Object> cancelEvent; + + EditorSaveEvent<Object> 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<Object> event) { + Assert.assertNull(cancelEvent); + cancelEvent = event; + } + + @Override + public void onEditorSave(EditorSaveEvent<Object> event) { + Assert.assertNull(saveEvent); + saveEvent = event; + } + + @Override + protected <T extends ServerRpc> void registerRpc(T implementation) { + if (implementation instanceof EditorServerRpc) { + rpc = (EditorServerRpc) implementation; + } + super.registerRpc(implementation); + } + } + + private TestEditorImpl editor; + private Binder<Object> 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); + } +} |