From 0907394c577c771a26195650f73d4cca0214cd76 Mon Sep 17 00:00:00 2001 From: Taras Hupalo Date: Mon, 29 Sep 2014 13:41:44 +0300 Subject: [PATCH] Removed the waitingForResponse flag from VTabsheet (#14710) Change-Id: Idd918197c2026a3c574cffd0e9595ff9f02eb2d8 --- .../src/com/vaadin/client/ui/VTabsheet.java | 8 +- .../client/ui/tabsheet/TabsheetConnector.java | 3 +- .../TabSelectionRevertedByServer.java | 83 +++++++++++++ .../TabSelectionRevertedByServerTest.java | 112 ++++++++++++++++++ 4 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServer.java create mode 100644 uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServerTest.java diff --git a/client/src/com/vaadin/client/ui/VTabsheet.java b/client/src/com/vaadin/client/ui/VTabsheet.java index 745f2bca61..9c3af5c568 100644 --- a/client/src/com/vaadin/client/ui/VTabsheet.java +++ b/client/src/com/vaadin/client/ui/VTabsheet.java @@ -694,9 +694,6 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware private final Element deco; - /** For internal use only. May be removed or replaced in the future. */ - public boolean waitingForResponse; - private String currentStyle; /** @@ -704,8 +701,7 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware */ private boolean canSelectTab(final int tabIndex) { Tab tab = tb.getTab(tabIndex); - if (getApplicationConnection() == null || disabled - || waitingForResponse) { + if (getApplicationConnection() == null || disabled) { return false; } if (!tab.isEnabledOnServer() || tab.isHiddenOnServer()) { @@ -739,8 +735,6 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware getRpcProxy().setSelected(tabKeys.get(tabIndex).toString()); - waitingForResponse = true; - tb.getTab(tabIndex).focus(); // move keyboard focus to active tab return true; diff --git a/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java b/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java index 8c6afd1c4f..94961a6a50 100644 --- a/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java +++ b/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java @@ -41,6 +41,7 @@ public class TabsheetConnector extends TabsheetBaseConnector implements final String key = getState().tabs.get(i).key; final boolean selected = key.equals(getState().selected); if (selected) { + getWidget().setActiveTabIndex(i); getWidget().selectTab(i); break; } @@ -92,8 +93,6 @@ public class TabsheetConnector extends TabsheetBaseConnector implements } getWidget().iLayout(); - - getWidget().waitingForResponse = false; } @Override diff --git a/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServer.java b/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServer.java new file mode 100644 index 0000000000..b51a8dde08 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServer.java @@ -0,0 +1,83 @@ +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.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.Component; +import com.vaadin.ui.Label; +import com.vaadin.ui.Notification; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.SelectedTabChangeEvent; +import com.vaadin.ui.TabSheet.SelectedTabChangeListener; + +/** + * TabSheet test in case user selects a tab and on the selection listener the + * selected tab is changed to another one. + * + * This test used to cause nonfunctional TabSheet if the current tab was 1, user + * selects 5, then the selection listener will revert the selected tab to 1. + * + * @since + * @author Vaadin Ltd + */ +public class TabSelectionRevertedByServer extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + + final TabSheet tabsheet = new TabSheet(); + tabsheet.setWidth("400px"); + + Component lastLabel = null; + + for (int i = 1; i <= 5; i++) { + String caption = "Tab " + i; + Label label = new Label(caption); + tabsheet.addTab(label, caption); + + lastLabel = label; + } + + tabsheet.setSelectedTab(0); + + final Component lastTab = lastLabel; + + tabsheet.addSelectedTabChangeListener(new SelectedTabChangeListener() { + + @Override + public void selectedTabChange(SelectedTabChangeEvent event) { + if (tabsheet.getSelectedTab().equals(lastTab)) { + + // Set focus back to first tab in tabsheet + tabsheet.setSelectedTab(0); + Notification.show("Focus set back to tab at position 0"); + } + } + }); + + addComponent(tabsheet); + + Button button = new Button("Select Last Tab"); + button.addClickListener(new ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + tabsheet.setSelectedTab(lastTab); + } + }); + addComponent(button); + } + + @Override + protected String getTestDescription() { + return "Clicking on Tab 5 will revert to Tab 1. The action is handled on the server side and will set the selected tab to 1 if Tab 5 is selected."; + } + + @Override + protected Integer getTicketNumber() { + return 14710; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServerTest.java b/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServerTest.java new file mode 100644 index 0000000000..64aee8768b --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/tabsheet/TabSelectionRevertedByServerTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2000-2014 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.components.tabsheet; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * If user selected the last tab the test will change it back to the first one + * from a server side selection listener. This test makes sure that actually + * happen. + * + * @since + * @author Vaadin Ltd + */ +public class TabSelectionRevertedByServerTest extends MultiBrowserTest { + + @Test + public void testFocus() throws InterruptedException, IOException { + openTestURL(); + + // Selects Tab 4 which should be selected. + click(4); + assertSelection(4, 1); + + // Select Tab 5 which should revert to Tab 1. + click(5); + assertSelection(1, 5); + + // Make sure after reverting the selection the tab selection still + // works. + click(3); + assertSelection(3, 1); + + } + + private void assertSelection(int expectedIndex, int wrongIndex) { + TestBenchElement tabExpected = tab(expectedIndex); + String attributeClassExpected = tabExpected.getAttribute("class"); + + Assert.assertTrue("Tab " + expectedIndex + " should be selected.", + attributeClassExpected + .contains("v-tabsheet-tabitemcell-selected")); + + TestBenchElement tabWrong = tab(wrongIndex); + String attributeClassWrong = tabWrong.getAttribute("class"); + + Assert.assertTrue("Tab " + wrongIndex + + " should be selected when click on Tab 4", + !attributeClassWrong + .contains("v-tabsheet-tabitemcell-selected")); + } + + /* + * Click on the element. + */ + private void click(int tabIndex) throws InterruptedException { + click(tab(tabIndex)); + } + + /* + * Click on the element. + */ + private void click(TestBenchElement element) throws InterruptedException { + + element.click(10, 10); + if (DELAY > 0) { + sleep(DELAY); + } + } + + /* + * Delay for PhantomJS. + */ + private final static int DELAY = 10; + + /* + * Provide the tab at specified index. + */ + private TestBenchElement tab(int index) { + By by = By.className("v-tabsheet-tabitemcell"); + + TestBenchElement element = (TestBenchElement) getDriver().findElements( + by).get(index - 1); + + String expected = "Tab " + index; + Assert.assertEquals(expected, + element.getText().substring(0, expected.length())); + + return element; + } + +} -- 2.39.5