From 6ff8263180966c96d22d49dbcff4b2e2ab5f98e9 Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Thu, 19 Dec 2019 10:56:05 +0200 Subject: [PATCH] Close window on ESC, when maximized button is clicked (#11840) (#11848) * Close window on ESC, when maximized button is clicked (#11840) Fixes #11838 Changes: 1. Close a window when maximized button is focused and ESC is pressed 2. Add additional check for a close button to react to the ESC key press 3. Rename a private method `onCloseClick` to `closeWindow` to allow code re-use --- .../java/com/vaadin/client/ui/VWindow.java | 14 +++++-- ...eWindowOnEscapeMaximizedButtonFocused.java | 41 +++++++++++++++++++ ...dowOnEscapeMaximizedButtonFocusedTest.java | 27 ++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 uitest/src/main/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocused.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocusedTest.java diff --git a/client/src/main/java/com/vaadin/client/ui/VWindow.java b/client/src/main/java/com/vaadin/client/ui/VWindow.java index e19a13c005..03cb7e16bf 100644 --- a/client/src/main/java/com/vaadin/client/ui/VWindow.java +++ b/client/src/main/java/com/vaadin/client/ui/VWindow.java @@ -1014,14 +1014,20 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, // if clicked or key ENTER or SPACE is pressed } else if (isClosable() && target == closeBox) { if (type == Event.ONCLICK || (type == Event.ONKEYUP - && isKeyEnterOrSpace(event.getKeyCode()))) { - onCloseClick(); + && (isKeyEnterOrSpace(event.getKeyCode())) + || event.getKeyCode() == KeyCodes.KEY_ESCAPE)) { + closeWindow(); } bubble = false; } else if (target == maximizeRestoreBox) { + // if ESC is pressed, close the window + if (type == Event.ONKEYUP + && event.getKeyCode() == KeyCodes.KEY_ESCAPE) { + closeWindow(); + } // handled in connector // if clicked or key ENTER or SPACE is pressed - if (type != Event.ONCLICK && !(type == Event.ONKEYUP + else if (type != Event.ONCLICK && !(type == Event.ONKEYUP && isKeyEnterOrSpace(event.getKeyCode()))) { bubble = false; } @@ -1097,7 +1103,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, } } - private void onCloseClick() { + private void closeWindow() { // Send the close event to the server client.updateVariable(id, "close", true, true); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocused.java b/uitest/src/main/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocused.java new file mode 100644 index 0000000000..d1c0f3aeec --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocused.java @@ -0,0 +1,41 @@ +package com.vaadin.tests.components.window; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.window.WindowMode; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Label; +import com.vaadin.ui.Window; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class CloseWindowOnEscapeMaximizedButtonFocused extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + Label instructions = new Label("Press Maximise button and then ESC. " + + " Window should be closed"); + Button openWindow = new Button("Open Window"); + openWindow.setId("openW"); + openWindow.addClickListener(e -> { + Window win = new Window("Window test", new Label("Some content")); + win.setWindowMode(WindowMode.NORMAL); + win.setWidth("300px"); + win.setHeight("300px"); + win.center(); + addWindow(win); + }); + addComponent(instructions); + addComponent(openWindow); + } + + @Override + public String getTestDescription() { + return "A window should be closed after the ESC button is pressed, when a maximize button is focused"; + }; + + @Override + public Integer getTicketNumber() { + return 11838; + }; +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocusedTest.java b/uitest/src/test/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocusedTest.java new file mode 100644 index 0000000000..e4be7413aa --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/window/CloseWindowOnEscapeMaximizedButtonFocusedTest.java @@ -0,0 +1,27 @@ +package com.vaadin.tests.components.window; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.WindowElement; +import com.vaadin.tests.tb3.MultiBrowserTest; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import static org.junit.Assert.assertTrue; + +public class CloseWindowOnEscapeMaximizedButtonFocusedTest + extends MultiBrowserTest { + + @Test + public void windowIsClosed() { + openTestURL(); + ButtonElement openWindow = $(ButtonElement.class).id("openW"); + openWindow.click(); + + WindowElement window = $(WindowElement.class).first(); + window.maximize(); + findElement(By.className("v-window-restorebox")).sendKeys(Keys.ESCAPE); + waitForElementNotPresent(By.className("v-window")); + assertTrue("Window should be removed after ESC key is pressed", + driver.findElements(By.className("v-window ")).isEmpty()); + } +} -- 2.39.5