diff options
author | Dmitrij Abolichin <dirolwhite@gmail.com> | 2015-10-09 01:21:44 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2015-11-04 15:08:57 +0200 |
commit | b8846b314349cd71bc0c1340df92a6f22bad622c (patch) | |
tree | b92db7703c185dda51e3e41896927f386fdbcc00 | |
parent | 8e751f4a699da8ec7f271e41e0e33a6bfc4bfbf9 (diff) | |
download | vaadin-framework-b8846b314349cd71bc0c1340df92a6f22bad622c.tar.gz vaadin-framework-b8846b314349cd71bc0c1340df92a6f22bad622c.zip |
Fix replacing of currently selected Tab (#14252)
Change-Id: I49a86d2fceac233fdfd9e31d8ade519b9f8460b6
-rw-r--r-- | server/src/com/vaadin/ui/TabSheet.java | 6 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java | 62 |
2 files changed, 68 insertions, 0 deletions
diff --git a/server/src/com/vaadin/ui/TabSheet.java b/server/src/com/vaadin/ui/TabSheet.java index 592515c560..66784aaa02 100644 --- a/server/src/com/vaadin/ui/TabSheet.java +++ b/server/src/com/vaadin/ui/TabSheet.java @@ -736,6 +736,9 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, if (oldLocation == -1) { addComponent(newComponent); } else if (newLocation == -1) { + if (selected == oldComponent) { + setSelected(null); + } removeComponent(oldComponent); newTab = addTab(newComponent, oldLocation); @@ -752,6 +755,9 @@ public class TabSheet extends AbstractComponentContainer implements Focusable, if (selectAfterInserting) { setSelected(newComponent); + //SelectedTabChangeEvent should be fired here as selected Tab is changed. + //Other cases are handled implicitly by removeComponent() and addComponent()addTab() + fireSelectedTabChange(); } // Tab associations are not changed, but metadata is swapped between diff --git a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java index 29f12fa4b6..a45b2c5587 100644 --- a/server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java @@ -12,6 +12,8 @@ import org.junit.Test; import com.vaadin.ui.Component; import com.vaadin.ui.Label; import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.SelectedTabChangeEvent; +import com.vaadin.ui.TabSheet.SelectedTabChangeListener; import com.vaadin.ui.TabSheet.Tab; public class TabSheetTest { @@ -221,4 +223,64 @@ public class TabSheetTest { assertEquals("description", tab1.getDescription()); assertEquals(1, tabSheet.getTabPosition(tabSheet.getTab(lbl3))); } + + @Test + public void testSelectedTabChangeEvent_whenComponentReplaced() { + + // given + final class SelectedTabExpectedComponentListener + implements SelectedTabChangeListener { + + private Component actualComponent; + + @Override + public void selectedTabChange(SelectedTabChangeEvent event) { + actualComponent = event.getTabSheet().getSelectedTab(); + + } + + public void assertActualComponentIs(Component expectedComponent) { + assertEquals(expectedComponent, actualComponent); + actualComponent = null; + } + } + TabSheet tabSheet = new TabSheet(); + final Label lbl1 = new Label("aaa"); + final Label lbl2 = new Label("bbb"); + final Label lbl3 = new Label("ccc"); + final Label lbl4 = new Label("ddd"); + tabSheet.addComponent(lbl1); + tabSheet.addComponent(lbl2); + tabSheet.addComponent(lbl3); + tabSheet.setSelectedTab(lbl2); + SelectedTabExpectedComponentListener listener = new SelectedTabExpectedComponentListener(); + tabSheet.addSelectedTabChangeListener(listener); + + // when selected tab is replaced with new Component + tabSheet.replaceComponent(lbl2, lbl4); + + // then + listener.assertActualComponentIs(lbl4); + assertEquals(lbl4, tabSheet.getSelectedTab()); + + // when not selected tab is replaced with new Component + tabSheet.replaceComponent(lbl1, lbl2); + + // then + assertEquals(lbl4, tabSheet.getSelectedTab()); + + // when not selected tab is replaced with existing Component + tabSheet.replaceComponent(lbl2, lbl3); + + // then + assertEquals(lbl4, tabSheet.getSelectedTab()); + + // when selected tab is replaced with existing Component (locations are + // just swapped) + tabSheet.replaceComponent(lbl4, lbl3); + + // then + listener.assertActualComponentIs(lbl3); + assertEquals(lbl3, tabSheet.getSelectedTab()); + } } |