From 029f47f1faaa9fd421e01b3b7fc664faf0fcae8f Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Wed, 18 Aug 2021 11:23:25 +0300 Subject: [PATCH] Ensure visible tab search only covers existing tabs. (#12373) Fixes #11673 --- .../java/com/vaadin/client/ui/VTabsheet.java | 2 + .../TabSheetScrolledRemoveAllButLast.java | 48 +++++++++++++++++++ .../TabSheetScrolledRemoveAllButLastTest.java | 29 +++++++++++ 3 files changed, 79 insertions(+) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLast.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLastTest.java diff --git a/client/src/main/java/com/vaadin/client/ui/VTabsheet.java b/client/src/main/java/com/vaadin/client/ui/VTabsheet.java index e994623d99..6297bbcde2 100644 --- a/client/src/main/java/com/vaadin/client/ui/VTabsheet.java +++ b/client/src/main/java/com/vaadin/client/ui/VTabsheet.java @@ -1046,6 +1046,7 @@ public class VTabsheet extends VTabsheetBase * @see VTabsheet#isClipped(Tab) */ private int getNextVisibleTab(int i) { + i = Math.max(i, -1); int tabs = getTabCount(); do { i++; @@ -1082,6 +1083,7 @@ public class VTabsheet extends VTabsheetBase * @see VTabsheet#isClipped(Tab) */ private int getPreviousVisibleTab(int i) { + i = Math.min(i, getTabCount()); do { i--; } while (i >= 0 && getTab(i).isHiddenOnServer()); diff --git a/uitest/src/main/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLast.java b/uitest/src/main/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLast.java new file mode 100644 index 0000000000..64ac3542a6 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLast.java @@ -0,0 +1,48 @@ +package com.vaadin.tests.components.tabsheet; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Label; +import com.vaadin.ui.TabSheet; + +public class TabSheetScrolledRemoveAllButLast extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + final TabSheet tabSheet = new TabSheet(); + tabSheet.setWidth("300px"); + + for (int i = 0; i < 10; i++) { + String caption = "Tab #" + (i + 1); + tabSheet.addTab(new Label(caption), caption); + } + // scroll + tabSheet.setSelectedTab(5); + + addComponent(tabSheet); + + Button button = new Button("Close all except last"); + button.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent clickEvent) { + int tabsCount = tabSheet.getComponentCount(); + for (int i = 0; i < tabsCount - 1; i++) { + TabSheet.Tab tab = tabSheet.getTab(0); + tabSheet.removeTab(tab); + } + } + }); + addComponent(button); + } + + @Override + protected String getTestDescription() { + return "Closing tabs shouldn't cause a client-side exception."; + } + + @Override + protected Integer getTicketNumber() { + return 11673; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLastTest.java b/uitest/src/test/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLastTest.java new file mode 100644 index 0000000000..2825330519 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLastTest.java @@ -0,0 +1,29 @@ +package com.vaadin.tests.components.tabsheet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class TabSheetScrolledRemoveAllButLastTest + extends MultiBrowserTest { + + @Test + public void closeTabs() { + openTestURL("debug"); + WebElement firstTab = findElement( + By.className("v-tabsheet-tabitemcell-first")); + assertNotEquals( + "Tab bar should be scrolled, unexpected first visible tab", + "Tab #1", firstTab.getText()); + + $(ButtonElement.class).first().click(); + assertEquals("Unexpected error notification(s),", 0, + findElements(By.className("v-Notification-error")).size()); + } +} -- 2.39.5