diff options
author | Denis Anisimov <denis@vaadin.com> | 2014-08-17 16:20:32 +0300 |
---|---|---|
committer | Sauli Tähkäpää <sauli@vaadin.com> | 2014-10-16 15:51:34 +0300 |
commit | c91fee32e902bbc981056b428463c3548d491aad (patch) | |
tree | c10d170615aa51a12cbb58458a412f550a25a084 | |
parent | 4f71e74a2a4ae59885928c23d818737e9c9887c8 (diff) | |
download | vaadin-framework-c91fee32e902bbc981056b428463c3548d491aad.tar.gz vaadin-framework-c91fee32e902bbc981056b428463c3548d491aad.zip |
Postpone shortcut action handler initialization in PopupView (#14275).
Change-Id: I233a63fac4f1afe3f99105ac6dfbbbb38f9b9fad
3 files changed, 204 insertions, 9 deletions
diff --git a/client/src/com/vaadin/client/ui/VPopupView.java b/client/src/com/vaadin/client/ui/VPopupView.java index 931945e546..1a59501d38 100644 --- a/client/src/com/vaadin/client/ui/VPopupView.java +++ b/client/src/com/vaadin/client/ui/VPopupView.java @@ -20,6 +20,8 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; @@ -40,6 +42,7 @@ import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ComponentConnector; +import com.vaadin.client.DeferredWorker; import com.vaadin.client.Util; import com.vaadin.client.VCaptionWrapper; import com.vaadin.client.VConsole; @@ -48,7 +51,8 @@ import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner; import com.vaadin.client.ui.popupview.VisibilityChangeEvent; import com.vaadin.client.ui.popupview.VisibilityChangeHandler; -public class VPopupView extends HTML implements Iterable<Widget> { +public class VPopupView extends HTML implements Iterable<Widget>, + DeferredWorker { public static final String CLASSNAME = "v-popupview"; @@ -73,6 +77,8 @@ public class VPopupView extends HTML implements Iterable<Widget> { public final CustomPopup popup; private final Label loading = new Label(); + private boolean popupShowInProgress; + /** * loading constructor */ @@ -280,19 +286,33 @@ public class VPopupView extends HTML implements Iterable<Widget> { @Override public void show() { + popupShowInProgress = true; // Find the shortcut action handler that should handle keyboard // events from the popup. The events do not propagate automatically // because the popup is directly attached to the RootPanel. - Widget widget = VPopupView.this; - while (shortcutActionHandler == null && widget != null) { - if (widget instanceof ShortcutActionHandlerOwner) { - shortcutActionHandler = ((ShortcutActionHandlerOwner) widget) - .getShortcutActionHandler(); - } - widget = widget.getParent(); - } super.show(); + + /* + * Shortcut actions could be set (and currently in 7.2 they ARE SET + * via old style "updateFromUIDL" method, see f.e. UIConnector) + * AFTER method show() has been invoked (which is called as a + * reaction on change in component hierarchy). As a result there + * could be no shortcutActionHandler set yet. So let's postpone + * search of shortcutActionHandler. + */ + Scheduler.get().scheduleDeferred(new ScheduledCommand() { + @Override + public void execute() { + try { + if (shortcutActionHandler == null) { + shortcutActionHandler = findShortcutActionHandler(); + } + } finally { + popupShowInProgress = false; + } + } + }); } /** @@ -378,6 +398,18 @@ public class VPopupView extends HTML implements Iterable<Widget> { positionOrSizeUpdated(); } + private ShortcutActionHandler findShortcutActionHandler() { + Widget widget = VPopupView.this; + ShortcutActionHandler handler = null; + while (handler == null && widget != null) { + if (widget instanceof ShortcutActionHandlerOwner) { + handler = ((ShortcutActionHandlerOwner) widget) + .getShortcutActionHandler(); + } + widget = widget.getParent(); + } + return handler; + } }// class CustomPopup public HandlerRegistration addVisibilityChangeHandler( @@ -391,4 +423,9 @@ public class VPopupView extends HTML implements Iterable<Widget> { return Collections.singleton((Widget) popup).iterator(); } + @Override + public boolean isWorkPending() { + return popupShowInProgress; + } + }// class VPopupView diff --git a/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandler.java b/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandler.java new file mode 100644 index 0000000000..ca91597aa2 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandler.java @@ -0,0 +1,79 @@ +/* + * 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.popupview; + +import com.vaadin.event.ShortcutAction; +import com.vaadin.event.ShortcutListener; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Component; +import com.vaadin.ui.Label; +import com.vaadin.ui.PopupView; +import com.vaadin.ui.TextField; + +/** + * Test UI to check availability of shortcut action listener in the popup view + * oeverlay component. + * + * @author Vaadin Ltd + */ +public class PopupViewShortcutActionHandler extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + addComponent(new PopupView(new DemoPoupView())); + } + + @Override + protected String getTestDescription() { + return "Shortcut listener search should be executed in the end " + + "of request (after legacy UIDL request handling)."; + } + + @Override + protected Integer getTicketNumber() { + return 14275; + } + + private class DemoPoupView implements PopupView.Content { + + @Override + public String getMinimizedValueAsHTML() { + return "Click Me"; + } + + @Override + public Component getPopupComponent() { + TextField field = new TextField("Enter text"); + field.setImmediate(true); + field.addShortcutListener(new ShortcutListener("SearchAction", + ShortcutAction.KeyCode.ENTER, null) { + private static final long serialVersionUID = 1L; + + @Override + public void handleAction(Object sender, Object target) { + Label label = new Label( + "shortcut addedEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"); + label.addStyleName("shortcut-result"); + addComponent(label); + } + }); + return field; + } + + } + +} diff --git a/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandlerTest.java b/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandlerTest.java new file mode 100644 index 0000000000..f122e1a415 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/popupview/PopupViewShortcutActionHandlerTest.java @@ -0,0 +1,79 @@ +/* + * 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.popupview; + +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.phantomjs.PhantomJSDriver; +import org.openqa.selenium.remote.DesiredCapabilities; + +import com.vaadin.testbench.TestBench; +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * Check availability of shortcut action listener in the popup view. + * + * @author Vaadin Ltd + */ +public class PopupViewShortcutActionHandlerTest extends MultiBrowserTest { + + @Test + public void testShortcutHandling() { + openTestURL(); + + getDriver().findElement(By.className("v-popupview")).click(); + WebElement textField = getDriver().findElement( + By.className("v-textfield")); + textField.sendKeys("a", Keys.ENTER); + + Assert.assertTrue( + "Unable to find label component which is the result of" + + " shortcut action handling.", + isElementPresent(By.className("shortcut-result"))); + } + + @Override + protected void setupDriver() throws Exception { + System.setProperty("phantomjs.binary.path", + "C:\\tmp\\phantom\\phantomjs.exe"); + WebDriver dr = TestBench.createDriver(new PhantomJSDriver()); + setDriver(dr); + } + + @Override + protected String getScreenshotDirectory() { + return "C:\\tmp\\a"; + } + + @Override + protected void openTestURL() { + driver.get("http://localhost:8080/vaadin/run/PopupViewShortcutActionHandler"); + } + + @Override + public List<DesiredCapabilities> getBrowsersToTest() { + return Collections.singletonList(Browser.FIREFOX + .getDesiredCapabilities()); + } + +} |