]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Flash expanding with percentage sizing (#8552)
authorOlli Tietäväinen <ollit@vaadin.com>
Tue, 21 Mar 2017 08:28:22 +0000 (10:28 +0200)
committerPekka Hyvönen <pekka@vaadin.com>
Tue, 21 Mar 2017 08:28:22 +0000 (10:28 +0200)
Allows Flash embed to expand if percentage dimensions given
Fixes #4035

client/src/main/java/com/vaadin/client/ui/VFlash.java
client/src/main/java/com/vaadin/client/ui/flash/FlashConnector.java
uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java [new file with mode: 0755]
uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java [new file with mode: 0755]

index 00fc881bc316018ab4c0ade04cf8eebe9a183201..fcfce928e5db661a9bc4687ca666fc3dc25ea50a 100644 (file)
@@ -37,6 +37,9 @@ public class VFlash extends HTML {
     protected String width;
     protected String height;
 
+    private int slotOffsetHeight = -1;
+    private int slotOffsetWidth = -1;
+
     public VFlash() {
         setStyleName(CLASSNAME);
     }
@@ -103,8 +106,7 @@ public class VFlash extends HTML {
 
     @Override
     public void setWidth(String width) {
-        // super.setWidth(height);
-
+        // explicitly not calling super here
         if (this.width != width) {
             this.width = width;
             needsRebuild = true;
@@ -113,8 +115,7 @@ public class VFlash extends HTML {
 
     @Override
     public void setHeight(String height) {
-        // super.setHeight(height);
-
+        // explicitly not calling super here
         if (this.height != height) {
             this.height = height;
             needsRebuild = true;
@@ -136,13 +137,34 @@ public class VFlash extends HTML {
         }
     }
 
+    /**
+     * Set dimensions of the containing layout slot so that the size of the
+     * embed object can be calculated from percentages if needed.
+     *
+     * Triggers embed resizing if percentage sizes are in use.
+     *
+     * @since
+     * @param slotOffsetHeight
+     *            offset height of the layout slot
+     * @param slotOffsetWidth
+     *            offset width of the layout slot
+     */
+    public void setSlotHeightAndWidth(int slotOffsetHeight,
+            int slotOffsetWidth) {
+        this.slotOffsetHeight = slotOffsetHeight;
+        this.slotOffsetWidth = slotOffsetWidth;
+        if (hasPercentageHeight() || hasPercentageWidth()) {
+            resizeEmbedElement();
+        }
+
+    }
+
     protected String createFlashEmbed() {
         /*
          * To ensure cross-browser compatibility we are using the twice-cooked
          * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
          * inside it a EMBED for all other browsers.
          */
-
         StringBuilder html = new StringBuilder();
 
         // Start the object tag
@@ -223,8 +245,19 @@ public class VFlash extends HTML {
         // Build inner EMBED tag
         html.append("<embed ");
         html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" ");
-        html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
-        html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
+        if (hasPercentageWidth() && slotOffsetWidth >= 0) {
+            html.append("width=\"" + getRelativePixelWidth() + "\" ");
+        } else {
+            html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
+        }
+
+        if (hasPercentageHeight() && slotOffsetHeight >= 0) {
+            html.append("height=\"" + getRelativePixelHeight() + "px\" ");
+        } else {
+            html.append(
+                    "height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
+        }
+
         html.append("type=\"application/x-shockwave-flash\" ");
 
         // Add the parameters to the Embed
@@ -250,4 +283,46 @@ public class VFlash extends HTML {
         return html.toString();
     }
 
+    private void resizeEmbedElement() {
+        // find <embed> element
+        com.google.gwt.dom.client.Element objectElem = getElement()
+                .getFirstChildElement();
+        com.google.gwt.dom.client.Element objectChild = objectElem
+                .getFirstChildElement();
+        while (!"EMBED".equalsIgnoreCase(objectChild.getTagName())) {
+            objectChild = objectChild.getNextSiblingElement();
+            if (objectChild == null) {
+                return;
+            }
+        }
+        // update height & width from slot offset, if percentage size is given
+        if (hasPercentageHeight() && slotOffsetHeight >= 0) {
+            objectChild.setAttribute("height", getRelativePixelHeight());
+        }
+        if (hasPercentageWidth() && slotOffsetWidth >= 0) {
+            objectChild.setAttribute("width", getRelativePixelWidth());
+        }
+
+    }
+
+    private String getRelativePixelWidth() {
+        float relative = WidgetUtil.parseRelativeSize(width);
+        int widthInPixels = (int) (relative / 100) * slotOffsetWidth;
+        return widthInPixels + "px";
+    }
+
+    private String getRelativePixelHeight() {
+        float relative = WidgetUtil.parseRelativeSize(height);
+        int heightInPixels = (int) (relative / 100) * slotOffsetHeight;
+        return heightInPixels + "px";
+    }
+
+    private boolean hasPercentageHeight() {
+        return ((height != null) && (height.indexOf('%') > 0));
+    }
+
+    private boolean hasPercentageWidth() {
+        return ((width != null) && (width.indexOf('%') > 0));
+    }
+
 }
index 856fc776a89466d9f5b8275a3a7439cdb78b3fde..7e74938036a998fa23e09c3cbb0283c46c2dbdc2 100644 (file)
  */
 package com.vaadin.client.ui.flash;
 
+import com.google.gwt.dom.client.Element;
 import com.vaadin.client.communication.StateChangeEvent;
 import com.vaadin.client.ui.AbstractComponentConnector;
 import com.vaadin.client.ui.VFlash;
+import com.vaadin.client.ui.layout.ElementResizeEvent;
+import com.vaadin.client.ui.layout.ElementResizeListener;
 import com.vaadin.shared.ui.AbstractEmbeddedState;
 import com.vaadin.shared.ui.Connect;
 import com.vaadin.shared.ui.flash.FlashState;
@@ -39,7 +42,6 @@ public class FlashConnector extends AbstractComponentConnector {
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
 
         super.onStateChanged(stateChangeEvent);
-
         getWidget().setSource(
                 getResourceUrl(AbstractEmbeddedState.SOURCE_RESOURCE));
         getWidget().setArchive(getState().archive);
@@ -52,4 +54,26 @@ public class FlashConnector extends AbstractComponentConnector {
 
         getWidget().rebuildIfNeeded();
     }
+
+    private final ElementResizeListener listener = new ElementResizeListener() {
+        public void onElementResize(ElementResizeEvent e) {
+            Element slot = e.getElement().getParentElement();
+            getWidget().setSlotHeightAndWidth(slot.getOffsetHeight(),
+                    slot.getOffsetWidth());
+        }
+    };
+
+    @Override
+    protected void init() {
+        super.init();
+        getLayoutManager().addElementResizeListener(getWidget().getElement(),
+                listener);
+    }
+
+    @Override
+    public void onUnregister() {
+        getLayoutManager().removeElementResizeListener(getWidget().getElement(),
+                listener);
+    }
+
 }
diff --git a/uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java b/uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java
new file mode 100755 (executable)
index 0000000..e504c1b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2000-2017 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.flash;
+
+import com.vaadin.server.ClassResource;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Flash;
+
+public class FlashExpansion extends TestBase {
+
+    Flash player = new Flash();
+
+    @Override
+    protected void setup() {
+
+        player.setWidth("400px");
+        player.setHeight("300px");
+        player.setSource(new ClassResource("simple.swf"));
+        addComponent(player);
+        Button button = new Button("click");
+        button.addClickListener(new Button.ClickListener() {
+
+            @Override
+            public void buttonClick(ClickEvent event) {
+                player.setSizeFull();
+
+            }
+        });
+        addComponent(button);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Flash object should expand according to percentile sizes";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 4035;
+    }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java b/uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java
new file mode 100755 (executable)
index 0000000..923ad2b
--- /dev/null
@@ -0,0 +1,45 @@
+package com.vaadin.tests.components.flash;
+
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.remote.DesiredCapabilities;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.FlashElement;
+import com.vaadin.testbench.parallel.Browser;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class FlashExpansionTest extends MultiBrowserTest {
+
+    @Override
+    public List<DesiredCapabilities> getBrowsersToTest() {
+        return getBrowsersSupportingFlash();
+    }
+
+    private final By locator = By.tagName("embed");
+
+    @Test
+    public void testFlashIsExpanded() throws Exception {
+        openTestURL();
+        /* Allow the flash plugin to load */
+        waitForElementPresent(locator);
+        WebElement embed = $(FlashElement.class).first().findElement(locator);
+        String width = embed.getAttribute("width");
+        Assert.assertTrue("Width is not 400.0px initially",
+                "400.0px".equals(width));
+        $(ButtonElement.class).first().click();
+        String widthAfterExpansion = embed.getAttribute("width");
+        Assert.assertFalse("Width is still 400.0px after expansion",
+                "400.0px".equals(widthAfterExpansion));
+    }
+
+    private List<DesiredCapabilities> getBrowsersSupportingFlash() {
+        // No Flash support in Chrome, FF, PhantomJS
+        return getBrowserCapabilities(Browser.IE8, Browser.IE9, Browser.IE10,
+                Browser.IE11);
+    }
+}