Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add setSrc(AbstractStreamResource) #20933

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.vaadin.flow.component.PropertyDescriptor;
import com.vaadin.flow.component.PropertyDescriptors;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.server.AbstractStreamResource;
import com.vaadin.flow.server.StreamResource;

import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -144,6 +146,19 @@ public void setSrc(String src) {
set(srcDescriptor, src);
}

/**
* Sets the source of the iframe with a source URL with the URL of the given
* {@link StreamResource}.
*
* @see #setSrc(String)
*
* @param src
* the resource value, not null
*/
public void setSrc(AbstractStreamResource src) {
getElement().setAttribute("src", src);
}

/**
* Gets the source of the iframe.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package com.vaadin.flow.uitest.ui;

import java.io.ByteArrayInputStream;

import com.vaadin.flow.component.html.IFrame;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

/**
Expand All @@ -43,6 +46,7 @@ public IFrameContentView() {

public IFrame frame = new IFrame();
public NativeButton button;
public IFrame frameResource = new IFrame();

public IFrameView() {
content = "A";
Expand All @@ -56,10 +60,17 @@ public IFrameView() {
frame.setId("frame1");
button = new NativeButton("Reload", event -> handleButtonClick());
button.setId("Reload");

button.addClickListener(event -> handleButtonClick());
add(frame, button);

StreamResource dynamicResource = new StreamResource("dynamic-resource",
() -> new ByteArrayInputStream(
"<html><body><span id=\"content\">Dynamic</span></body></html>"
.getBytes()));
dynamicResource.setContentType("text/html");
frameResource.setSrc(dynamicResource);
frameResource.setId("frame2");

add(frame, button, frameResource);
}

public void handleButtonClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ public void testIFrameReload() {
waitUntil(webDriver -> "B"
.equals(findElement(By.id("Friday")).getText()));
}

@Test
public void testIFrameWithDynamicResource() {
open();

waitForElementPresent(By.id("frame2"));
getDriver().switchTo().frame("frame2");
Assert.assertEquals("Dynamic", findElement(By.id("content")).getText());
}
}
Loading