]> source.dussan.org Git - vaadin-framework.git/commitdiff
Ensure visible tab search only covers existing tabs. (#12373)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Wed, 18 Aug 2021 08:23:25 +0000 (11:23 +0300)
committerGitHub <noreply@github.com>
Wed, 18 Aug 2021 08:23:25 +0000 (11:23 +0300)
Fixes #11673

client/src/main/java/com/vaadin/client/ui/VTabsheet.java
uitest/src/main/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLast.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/tabsheet/TabSheetScrolledRemoveAllButLastTest.java [new file with mode: 0644]

index e994623d99f1ed052b10675127ead2ea906c5a8d..6297bbcde2f5424fd392029291b2fecec0571624 100644 (file)
@@ -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 (file)
index 0000000..64ac354
--- /dev/null
@@ -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 (file)
index 0000000..2825330
--- /dev/null
@@ -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());
+    }
+}