Browse Source

Fix declarative support for AbstractMedia (#16330 #16331 #16332)

Also fixes HTTPS resource handling (#17267)

Contains tests for both Audio and Video.

Change-Id: Id4a7325f0d29f6857ff2236757b28b9eb911e39a
tags/7.5.0.beta1
Miki 9 years ago
parent
commit
b96861c180

+ 0
- 1
server/src/com/vaadin/server/ExternalResource.java View File

@@ -124,5 +124,4 @@ public class ExternalResource implements Resource, Serializable {
public void setMIMEType(String mimeType) {
this.mimeType = mimeType;
}

}

+ 0
- 1
server/src/com/vaadin/server/FileResource.java View File

@@ -156,5 +156,4 @@ public class FileResource implements ConnectorResource {
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}

}

+ 52
- 0
server/src/com/vaadin/ui/AbstractMedia.java View File

@@ -18,12 +18,17 @@ package com.vaadin.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import com.vaadin.server.ConnectorResource;
import com.vaadin.server.DownloadStream;
import com.vaadin.server.Resource;
@@ -34,6 +39,8 @@ import com.vaadin.server.VaadinSession;
import com.vaadin.shared.communication.URLReference;
import com.vaadin.shared.ui.AbstractMediaState;
import com.vaadin.shared.ui.MediaControl;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;

/**
* Abstract base class for the HTML5 media components.
@@ -256,4 +263,49 @@ public abstract class AbstractMedia extends AbstractComponent {
getRpcProxy(MediaControl.class).play();
}

@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);

String altText = getAltText();
if (altText != null && !altText.isEmpty()) {
design.append(altText);
}

for (Resource r : getSources()) {
Attributes attr = design.appendElement("source").attributes();
DesignAttributeHandler.writeAttribute("href", attr, r, null,
Resource.class);
}
}

@Override
public void readDesign(Element design, DesignContext designContext) {
super.readDesign(design, designContext);

String altText = "";
for (Node child : design.childNodes()) {
if (child instanceof Element
&& ((Element) child).tagName().equals("source")
&& child.hasAttr("href")) {
addSource(DesignAttributeHandler.readAttribute("href",
child.attributes(), Resource.class));
} else {
altText += child.toString();
}
}

altText = altText.trim();
if (!altText.isEmpty()) {
setAltText(altText);
}
}

@Override
protected Collection<String> getCustomAttributes() {
Collection<String> result = super.getCustomAttributes();
result.add("alt-text");
return result;
}

}

+ 39
- 0
server/src/com/vaadin/ui/Video.java View File

@@ -16,9 +16,17 @@

package com.vaadin.ui;

import java.util.Collection;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.vaadin.server.Resource;
import com.vaadin.shared.ui.video.VideoConstants;
import com.vaadin.shared.ui.video.VideoState;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;

/**
* The Video component translates into an HTML5 &lt;video&gt; element and as
@@ -89,4 +97,35 @@ public class Video extends AbstractMedia {
return getResource(VideoConstants.POSTER_RESOURCE);
}

@Override
public void readDesign(Element design, DesignContext designContext) {
Elements elems = design.getElementsByTag("poster");
for (Element poster : elems) {
if (getPoster() == null && poster.hasAttr("href")) {
setPoster(DesignAttributeHandler.readAttribute("href",
poster.attributes(), Resource.class));
}
poster.remove();
}

// Poster is extracted so AbstractMedia does not include it in alt text
super.readDesign(design, designContext);
}

@Override
public void writeDesign(Element design, DesignContext designContext) {
super.writeDesign(design, designContext);
if (getPoster() != null) {
Attributes attr = design.appendElement("poster").attributes();
DesignAttributeHandler.writeAttribute("href", attr, getPoster(),
null, Resource.class);
}
}

@Override
protected Collection<String> getCustomAttributes() {
Collection<String> result = super.getCustomAttributes();
result.add("poster");
return result;
}
}

+ 2
- 1
server/src/com/vaadin/ui/declarative/converters/DesignResourceConverter.java View File

@@ -39,7 +39,8 @@ public class DesignResourceConverter implements Converter<String, Resource> {
public Resource convertToModel(String value,
Class<? extends Resource> targetType, Locale locale)
throws Converter.ConversionException {
if (value.startsWith("http://")) {
if (value.startsWith("http://") || value.startsWith("https://")
|| value.startsWith("ftp://") || value.startsWith("ftps://")) {
return new ExternalResource(value);
} else if (value.startsWith("theme://")) {
return new ThemeResource(value.substring(8));

+ 63
- 0
server/tests/src/com/vaadin/tests/server/component/audio/AudioDeclarativeTest.java View File

@@ -0,0 +1,63 @@
/*
* 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.audio;

import java.io.File;

import org.junit.Test;

import com.vaadin.server.ExternalResource;
import com.vaadin.server.FileResource;
import com.vaadin.tests.design.DeclarativeTestBase;
import com.vaadin.ui.Audio;

/**
* Tests specs of declarative support for abstract media and its
* implementations.
*
* @since 7.4
* @author Vaadin Ltd
*/
public class AudioDeclarativeTest extends DeclarativeTestBase<Audio> {

@Test
public void testEmptyAudio() {
String design = "<v-audio />";
Audio audio = new Audio();
testRead(design, audio);
testWrite(design, audio);
}

@Test
public void testAudioMultipleSources() {
String design = "<v-audio muted='true' show-controls='false'>"
+ "some <b>text</b>" //
+ "<source href='http://foo.pl' />"
+ "<source href='https://bar.pl' />" //
+ "<source href='ohai' />" //
+ "</v-audio>";
Audio audio = new Audio();
audio.setAltText("some <b>text</b>");
audio.setAutoplay(false);
audio.setMuted(true);
audio.setShowControls(false);
audio.setSources(new ExternalResource("http://foo.pl"),
new ExternalResource("https://bar.pl"), new FileResource(
new File("ohai")));
testRead(design, audio);
testWrite(design, audio);
}
}

+ 59
- 0
server/tests/src/com/vaadin/tests/server/component/audio/VideoDeclarativeTest.java View File

@@ -0,0 +1,59 @@
/*
* 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.audio;

import java.io.File;

import org.junit.Test;

import com.vaadin.server.ExternalResource;
import com.vaadin.server.FileResource;
import com.vaadin.tests.design.DeclarativeTestBase;
import com.vaadin.ui.Video;

public class VideoDeclarativeTest extends DeclarativeTestBase<Video> {

@Test
public void testEmptyVideo() {
String design = "<v-video />";
Video audio = new Video();
testRead(design, audio);
testWrite(design, audio);
}

@Test
public void testVideoMultipleSources() {
String design = "<v-video muted='true' show-controls='false'>"
+ "some <b>text</b>" //
+ "<source href='http://foo.pl' />"
+ "<source href='https://bar.pl' />" //
+ "<source href='ohai' />" //
+ "<poster href='http://foo.pl/poster' />" //
+ "</v-video>";
Video video = new Video();
video.setAltText("some <b>text</b>");
video.setAutoplay(false);
video.setMuted(true);
video.setShowControls(false);
video.setSources(new ExternalResource("http://foo.pl"),
new ExternalResource("https://bar.pl"), new FileResource(
new File("ohai")));
video.setPoster(new ExternalResource("http://foo.pl/poster"));
testRead(design, video);
testWrite(design, video);
}

}

Loading…
Cancel
Save