summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDmitrij Abolichin <dirolwhite@gmail.com>2015-10-09 01:21:44 +0300
committerVaadin Code Review <review@vaadin.com>2015-10-20 04:34:53 +0000
commitdc196546c6e7de96b6d3027bd3a0509e71090ed9 (patch)
tree3e8242aad6a15db12edfe4024520b8beff9f28d9 /server
parent6539cb439f36c77aaa9cb80e6e0fc683f145a060 (diff)
downloadvaadin-framework-dc196546c6e7de96b6d3027bd3a0509e71090ed9.tar.gz
vaadin-framework-dc196546c6e7de96b6d3027bd3a0509e71090ed9.zip
Fix replacing of currently selected Tab (#14252)
Change-Id: I49a86d2fceac233fdfd9e31d8ade519b9f8460b6
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/ui/TabSheet.java6
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/tabsheet/TabSheetTest.java62
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());
+ }
}