From: Artur Signell Date: Mon, 9 Feb 2015 18:56:05 +0000 (+0200) Subject: Declarative support for Image, Flash, BrowserFrame (#15551,#16327,#15552,#16325) X-Git-Tag: 7.4.1~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ba163b261c4791c6648d6c31841ec892a0246e8b;p=vaadin-framework.git Declarative support for Image, Flash, BrowserFrame (#15551,#16327,#15552,#16325) Change-Id: Icef0b3c4e652421265714c4fd29dee150bcc8cd0 --- diff --git a/server/src/com/vaadin/ui/AbstractEmbedded.java b/server/src/com/vaadin/ui/AbstractEmbedded.java index 66752aa5d7..d004ca7635 100644 --- a/server/src/com/vaadin/ui/AbstractEmbedded.java +++ b/server/src/com/vaadin/ui/AbstractEmbedded.java @@ -15,8 +15,14 @@ */ package com.vaadin.ui; +import java.util.Collection; + +import org.jsoup.nodes.Element; + import com.vaadin.server.Resource; import com.vaadin.shared.ui.AbstractEmbeddedState; +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; /** * Abstract base for embedding components. @@ -81,4 +87,27 @@ public abstract class AbstractEmbedded extends AbstractComponent { return getState(false).alternateText; } + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + if (design.hasAttr("alt")) { + setAlternateText(DesignAttributeHandler.readAttribute("alt", + design.attributes(), String.class)); + } + } + + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + AbstractEmbedded def = designContext.getDefaultInstance(this); + DesignAttributeHandler.writeAttribute("alt", design.attributes(), + getAlternateText(), def.getAlternateText(), String.class); + } + + @Override + protected Collection getCustomAttributes() { + Collection c = super.getCustomAttributes(); + c.add("alternate-text"); + return c; + } } diff --git a/server/src/com/vaadin/ui/Flash.java b/server/src/com/vaadin/ui/Flash.java index bbbd4e3285..cd7c00087e 100644 --- a/server/src/com/vaadin/ui/Flash.java +++ b/server/src/com/vaadin/ui/Flash.java @@ -15,10 +15,15 @@ */ package com.vaadin.ui; +import java.util.Collections; import java.util.HashMap; +import java.util.Map; + +import org.jsoup.nodes.Element; import com.vaadin.server.Resource; import com.vaadin.shared.ui.flash.FlashState; +import com.vaadin.ui.declarative.DesignContext; /** * A component for displaying Adobe® Flash® content. @@ -88,6 +93,17 @@ public class Flash extends AbstractEmbedded { } } + /** + * Returns the codebase. + * + * @see #setCodebase(String) + * @since 7.4 + * @return Current codebase. + */ + public String getCodebase() { + return getState(false).codebase; + } + /** * This attribute specifies the content type of data expected when * downloading the object specified by classid. This attribute is optional @@ -106,6 +122,17 @@ public class Flash extends AbstractEmbedded { } } + /** + * Returns the current codetype. + * + * @see #setCodetype(String) + * @since 7.4 + * @return Current codetype. + */ + public String getCodetype() { + return getState(false).codetype; + } + /** * This attribute may be used to specify a space-separated list of URIs for * archives containing resources relevant to the object, which may include @@ -126,6 +153,23 @@ public class Flash extends AbstractEmbedded { } } + /** + * Returns current archive. + * + * @see #setArchive(String) + * @since 7.4 + * @return Current archive. + */ + public String getArchive() { + return getState(false).archive; + } + + /** + * Sets standby. + * + * @param standby + * Standby string. + */ public void setStandby(String standby) { if (standby != getState().standby || (standby != null && !standby.equals(getState().standby))) { @@ -134,6 +178,16 @@ public class Flash extends AbstractEmbedded { } } + /** + * Returns standby. + * + * @since + * @return Standby string. + */ + public String getStandby() { + return getState(false).standby; + } + /** * Sets an object parameter. Parameters are optional information, and they * are passed to the instantiated object. Parameters are are stored as name @@ -179,4 +233,38 @@ public class Flash extends AbstractEmbedded { requestRepaint(); } + @Override + public void writeDesign(Element design, DesignContext designContext) { + super.writeDesign(design, designContext); + for (String param : getParameterNames()) { + design.appendElement("parameter").attr("name", param) + .attr("value", getParameter(param)); + } + } + + /** + * Returns an iterable with declared parameter names. + * + * @see #setParameter(String, String) + * @see #getParameter(String) + * @since 7.4 + * @return An iterable with declared parameter names. + */ + public Iterable getParameterNames() { + Map map = getState(false).embedParams; + if (map == null) { + return Collections.emptySet(); + } else { + return Collections.unmodifiableSet(map.keySet()); + } + } + + @Override + public void readDesign(Element design, DesignContext designContext) { + super.readDesign(design, designContext); + for (Element paramElement : design.getElementsByTag("parameter")) { + setParameter(paramElement.attr("name"), paramElement.attr("value")); + } + } + } diff --git a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java index 5bdca6e06a..cba981c947 100644 --- a/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java +++ b/server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java @@ -25,6 +25,7 @@ import java.util.Map; import org.junit.Assert; import com.vaadin.ui.Component; +import com.vaadin.ui.Flash; public abstract class DeclarativeTestBase extends DeclarativeTestBaseBase { @@ -84,6 +85,23 @@ public abstract class DeclarativeTestBase extends } } + { + comparators.put(Flash.class, new IntrospectorEqualsAsserter( + Flash.class) { + @Override + public void assertObjectEquals(Flash o1, Flash o2) { + super.assertObjectEquals(o1, o2); + assertEquals("parameterNames", o1.getParameterNames(), + o2.getParameterNames()); + for (String name : o1.getParameterNames()) { + assertEquals("Parameter " + name, o1.getParameter(name), + o2.getParameter(name)); + } + } + }); + + } + @Override protected EqualsAsserter getComparator(Class c) { com.vaadin.tests.design.DeclarativeTestBaseBase.EqualsAsserter comp = comparators diff --git a/server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java b/server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java new file mode 100644 index 0000000000..4c9e323948 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java @@ -0,0 +1,105 @@ +/* + * 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.design; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.junit.Test; + +import com.vaadin.server.ExternalResource; +import com.vaadin.shared.util.SharedUtil; +import com.vaadin.ui.AbstractEmbedded; +import com.vaadin.ui.BrowserFrame; +import com.vaadin.ui.Embedded; +import com.vaadin.ui.Flash; +import com.vaadin.ui.Image; +import com.vaadin.ui.declarative.Design; + +/** + * Tests declarative support for implementations of {@link AbstractEmbedded} and + * {@link Embedded}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class EmbeddedsTest { + + public static final boolean equals(ExternalResource obj, + ExternalResource other) { + return SharedUtil.equals(obj.getURL(), other.getURL()) + && SharedUtil.equals(obj.getMIMEType(), other.getMIMEType()); + } + + @Test + public void testAbstractEmbeddedsToFromDesign() throws Exception { + for (AbstractEmbedded ae : new AbstractEmbedded[] { new Image(), + new Flash(), new BrowserFrame() }) { + ae.setSource(new ExternalResource("http://www.example.org")); + ae.setAlternateText("some alternate text"); + ae.setCaption("some caption"); + ae.setCaptionAsHtml(true); + ae.setDescription("some description"); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Design.write(ae, bos); + AbstractEmbedded result = (AbstractEmbedded) Design + .read(new ByteArrayInputStream(bos.toByteArray())); + assertTrue(equals((ExternalResource) ae.getSource(), + (ExternalResource) result.getSource())); + assertEquals(ae.getAlternateText(), result.getAlternateText()); + assertEquals(ae.getCaption(), result.getCaption()); + assertEquals(ae.isCaptionAsHtml(), result.isCaptionAsHtml()); + assertEquals(ae.getDescription(), result.getDescription()); + } + } + + @Test + public void testFlashToFromDesign() throws Exception { + Flash ae = new Flash(); + ae.setSource(new ExternalResource("http://www.example.org")); + ae.setAlternateText("some alternate text"); + ae.setCaption("some caption"); + ae.setCaptionAsHtml(true); + ae.setDescription("some description"); + ae.setCodebase("codebase"); + ae.setArchive("archive"); + ae.setCodetype("codetype"); + ae.setParameter("foo", "bar"); + ae.setParameter("something", "else"); + ae.setStandby("foobar"); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + Design.write(ae, bos); + Flash result = (Flash) Design.read(new ByteArrayInputStream(bos + .toByteArray())); + assertTrue(equals((ExternalResource) ae.getSource(), + (ExternalResource) result.getSource())); + assertEquals(ae.getAlternateText(), result.getAlternateText()); + assertEquals(ae.getCaption(), result.getCaption()); + assertEquals(ae.isCaptionAsHtml(), result.isCaptionAsHtml()); + assertEquals(ae.getDescription(), result.getDescription()); + assertEquals(ae.getCodebase(), result.getCodebase()); + assertEquals(ae.getArchive(), result.getArchive()); + assertEquals(ae.getCodetype(), result.getCodetype()); + assertEquals(ae.getParameter("foo"), result.getParameter("foo")); + assertEquals(ae.getParameter("something"), + result.getParameter("something")); + assertEquals(ae.getStandby(), result.getStandby()); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java new file mode 100644 index 0000000000..f4f50560b4 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java @@ -0,0 +1,57 @@ +/* + * 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.server.component.browserframe; + +import org.junit.Test; + +import com.vaadin.server.ExternalResource; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.BrowserFrame; + +/** + * Tests declarative support for implementations of {@link BrowserFrame}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class BrowserFrameDeclarativeTest extends + DeclarativeTestBase { + + protected String getDesign() { + return ""; + } + + protected BrowserFrame getExpectedResult() { + BrowserFrame i = new BrowserFrame(); + i.setSource(new ExternalResource("http://foo.bar/some.html")); + return i; + }; + + @Test + public void read() { + testRead(getDesign(), getExpectedResult()); + } + + @Test + public void write() { + testWrite(getDesign(), getExpectedResult()); + } + + @Test + public void testEmpty() { + testRead("", new BrowserFrame()); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java new file mode 100644 index 0000000000..f16184d8b1 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java @@ -0,0 +1,67 @@ +/* + * 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.server.component.flash; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.AbstractEmbedded; +import com.vaadin.ui.Embedded; +import com.vaadin.ui.Flash; + +/** + * Tests declarative support for implementations of {@link AbstractEmbedded} and + * {@link Embedded}. + * + * @since + * @author Vaadin Ltd + */ +public class FlashDeclarativeTest extends DeclarativeTestBase { + + protected Flash getExpectedResult() { + Flash f = new Flash(); + f.setArchive("arch"); + f.setCodebase("foo"); + f.setCodetype("bar"); + f.setStandby("Please wait"); + f.setParameter("foo", "bar"); + f.setParameter("baz", "foo"); + return f; + }; + + protected String getDesign() { + return "" + + " \n" // + + " \n" // + + ""; // + } + + @Test + public void read() { + testRead(getDesign(), getExpectedResult()); + } + + @Test + public void write() { + testWrite(getDesign(), getExpectedResult()); + } + + @Test + public void testEmpty() { + testRead("", new Flash()); + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java new file mode 100644 index 0000000000..670b3beb3d --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java @@ -0,0 +1,58 @@ +/* + * 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.server.component.image; + +import org.junit.Test; + +import com.vaadin.server.ExternalResource; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Image; + +/** + * Tests declarative support for implementations of {@link Image}. + * + * @since + * @author Vaadin Ltd + */ +public class ImageDeclarativeTest extends DeclarativeTestBase { + + protected String getDesign() { + return ""; + } + + protected Image getExpectedResult() { + Image i = new Image(); + i.setSource(new ExternalResource("http://foo.bar/img.png")); + i.setAlternateText("Some random image from the theme"); + return i; + }; + + @Test + public void read() { + testRead(getDesign(), getExpectedResult()); + } + + @Test + public void write() { + testWrite(getDesign(), getExpectedResult()); + } + + @Test + public void testEmpty() { + testRead("", new Image()); + } + +}