diff options
author | Edoardo Vacchi <uncommonnonsense@gmail.com> | 2014-11-24 17:23:29 +0100 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-05-17 12:26:04 +0000 |
commit | 31709daa2d13fbf7b25456f8ad99053fe81daf38 (patch) | |
tree | 383ec9cabd5faca5bfda48bcf7c8d431c2036b06 /uitest/src | |
parent | 107c1b84d493af79c3735745143fd9dd7740fc6f (diff) | |
download | vaadin-framework-31709daa2d13fbf7b25456f8ad99053fe81daf38.tar.gz vaadin-framework-31709daa2d13fbf7b25456f8ad99053fe81daf38.zip |
Remove shortcut listener from subclasses of AbstractComponent (#16498).
e.g., Panel shadows actionManager with its own instance field
* solution: always invoke getActionManager()
Change-Id: Ibeca2ad53cb1175ae1ebcfb4d68b3d2bf36ea8b2
Diffstat (limited to 'uitest/src')
2 files changed, 198 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListener.java b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListener.java new file mode 100644 index 0000000000..28ebf0e2b2 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListener.java @@ -0,0 +1,84 @@ +/* + * 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.panel; + +import com.vaadin.event.ShortcutAction.KeyCode; +import com.vaadin.event.ShortcutListener; +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.Label; +import com.vaadin.ui.Panel; +import com.vaadin.ui.TextField; + +/** + * Test for removing a shortcut listener from Panel. + * + * @author Vaadin Ltd + */ +public class PanelRemoveShortcutListener extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + final Panel panel = new Panel("No shortcut effects (press 'A')"); + panel.setWidth("200px"); + addComponent(panel); + TextField textField = new TextField(); + panel.setContent(textField); + ShortcutListener shortcut = createShortcutListener(panel); + panel.addShortcutListener(shortcut); + addButton("Remove shortcut", createClickListener(panel, shortcut)); + } + + private ShortcutListener createShortcutListener(final Panel panel) { + return new ShortcutListener("A", KeyCode.A, null) { + + @Override + public void handleAction(Object sender, Object target) { + if ("A on".equals(panel.getCaption())) { + panel.setCaption("A off"); + } else { + panel.setCaption("A on"); + } + } + }; + } + + private ClickListener createClickListener(final Panel panel, + final ShortcutListener shortcut) { + return new Button.ClickListener() { + + @Override + public void buttonClick(ClickEvent event) { + panel.removeShortcutListener(shortcut); + addComponent(new Label("shortcut removed")); + } + }; + } + + @Override + protected Integer getTicketNumber() { + return 16498; + } + + @Override + protected String getTestDescription() { + return "Should be possible to remove shortcut listener from Panel"; + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListenerTest.java b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListenerTest.java new file mode 100644 index 0000000000..872abe9191 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/panel/PanelRemoveShortcutListenerTest.java @@ -0,0 +1,114 @@ +/* + * 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.panel; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.support.ui.ExpectedCondition; + +import com.google.common.base.Objects; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.PanelElement; +import com.vaadin.testbench.elements.TextFieldElement; +import com.vaadin.testbench.parallel.Browser; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Test for removing a shortcut listener from Panel. + * + * @author Vaadin Ltd + */ +public class PanelRemoveShortcutListenerTest extends MultiBrowserTest { + + private PanelElement panel; + + @Override + public void setup() throws Exception { + super.setup(); + openTestURL(); + waitForElementPresent(By.className("v-panel")); + panel = $(PanelElement.class).first(); + } + + @Override + public List<DesiredCapabilities> getBrowsersToTest() { + List<DesiredCapabilities> list = super.getBrowsersToTest(); + // For some reason the shortcut isn't working for these browsers when + // tested through TestBench: + list.remove(Browser.IE8.getDesiredCapabilities()); + list.remove(Browser.FIREFOX.getDesiredCapabilities()); + list.remove(Browser.CHROME.getDesiredCapabilities()); + return list; + } + + @Test + public void testToggleWithShortcut() { + assertThat(panel.findElement(By.className("v-panel-caption")) + .findElement(By.tagName("span")).getText(), + is("No shortcut effects (press 'A')")); + + attemptShortcut("A on"); + attemptShortcut("A off"); + } + + @Test + public void testShortcutGetsRemoved() { + attemptShortcut("A on"); + + $(ButtonElement.class).first().click(); + waitForElementPresent(By.className("v-label")); + + attemptShortcut("A on"); + + // add a bit more delay to make sure the caption doesn't change later + try { + sleep(2000); + } catch (InterruptedException ignore) { + } + + assertThat(panel.findElement(By.className("v-panel-caption")) + .findElement(By.tagName("span")).getText(), is("A on")); + } + + private void attemptShortcut(final String expectedCaption) { + $(TextFieldElement.class).first().sendKeys("A"); + waitUntil(new ExpectedCondition<Boolean>() { + private String actualCaption; + + @Override + public Boolean apply(WebDriver input) { + actualCaption = panel + .findElement(By.className("v-panel-caption")) + .findElement(By.tagName("span")).getText(); + return Objects.equal(actualCaption, expectedCaption); + } + + @Override + public String toString() { + // Timed out after 10 seconds waiting for ... + return "panel's caption to become " + expectedCaption + + " (was: " + actualCaption + ")"; + } + }); + } +} |