diff options
author | Anna Koskinen <Ansku@users.noreply.github.com> | 2020-04-24 13:02:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-24 13:02:40 +0300 |
commit | 8f04d0324fde18a32625c0c75be1b5f85547d451 (patch) | |
tree | 424e73394d2e8544fb634ec7a650bc6a2d75b4b4 | |
parent | 616f24764b2659380c4f4c6ce1057c0056513199 (diff) | |
download | vaadin-framework-8f04d0324fde18a32625c0c75be1b5f85547d451.tar.gz vaadin-framework-8f04d0324fde18a32625c0c75be1b5f85547d451.zip |
Ensure ChangeListener still works after first upload. (#11936) (#11960)
* Ensure ChangeListener still works after first upload. (#11936)
Fixes #10420
3 files changed, 132 insertions, 0 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VUpload.java b/client/src/main/java/com/vaadin/client/ui/VUpload.java index 2f4b62a47f..e72fe3e838 100644 --- a/client/src/main/java/com/vaadin/client/ui/VUpload.java +++ b/client/src/main/java/com/vaadin/client/ui/VUpload.java @@ -40,6 +40,7 @@ import com.vaadin.client.ConnectorMap; import com.vaadin.client.StyleConstants; import com.vaadin.client.ui.upload.UploadConnector; import com.vaadin.client.ui.upload.UploadIFrameOnloadStrategy; +import com.vaadin.shared.EventId; import com.vaadin.shared.ui.upload.UploadServerRpc; /** @@ -264,6 +265,16 @@ public class VUpload extends SimplePanel { if (isImmediateMode()) { fu.sinkEvents(Event.ONCHANGE); } + fu.addChangeHandler(event -> { + if (client != null) { + UploadConnector connector = ((UploadConnector) ConnectorMap + .get(client).getConnector(VUpload.this)); + if (connector.hasEventListener(EventId.CHANGE)) { + connector.getRpcProxy(UploadServerRpc.class) + .change(fu.getFilename()); + } + } + }); } /** diff --git a/uitest/src/main/java/com/vaadin/tests/components/upload/UploadChangeListener.java b/uitest/src/main/java/com/vaadin/tests/components/upload/UploadChangeListener.java new file mode 100644 index 0000000000..14bf1a3bca --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/upload/UploadChangeListener.java @@ -0,0 +1,31 @@ +package com.vaadin.tests.components.upload; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Upload; + +public class UploadChangeListener extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + Upload upload = new Upload(); + upload.setImmediateMode(false); + upload.addChangeListener(e -> { + log("change"); + }); + upload.addFinishedListener(e -> { + log("finished"); + }); + addComponent(upload); + } + + @Override + protected String getTestDescription() { + return "Change listener should still work after first upload."; + }; + + @Override + protected Integer getTicketNumber() { + return 10420; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/upload/UploadChangeListenerTest.java b/uitest/src/test/java/com/vaadin/tests/components/upload/UploadChangeListenerTest.java new file mode 100644 index 0000000000..a7e8a4e3ce --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/upload/UploadChangeListenerTest.java @@ -0,0 +1,90 @@ +package com.vaadin.tests.components.upload; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.internal.WrapsElement; +import org.openqa.selenium.remote.LocalFileDetector; +import org.openqa.selenium.remote.RemoteWebElement; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class UploadChangeListenerTest extends MultiBrowserTest { + + @Test + public void changeListenerWorksAfterFirstUpload() throws IOException { + openTestURL(); + WebElement upload = findElement(By.className("v-button")); + + File tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath()); + + assertEquals("1. change", getLogRow(0)); + + upload.click(); + + assertEquals("2. finished", getLogRow(0)); + + tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath()); + + assertEquals("3. change", getLogRow(0)); + } + + /** + * @return The generated temp file handle + * @throws IOException + */ + private File createTempFile() throws IOException { + File tempFile = File.createTempFile("TestFileUpload", ".txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + writer.write(getTempFileContents()); + writer.close(); + tempFile.deleteOnExit(); + return tempFile; + } + + private String getTempFileContents() { + StringBuilder sb = new StringBuilder("This is a small test file."); + sb.append("\n"); + sb.append("Very small."); + return sb.toString(); + } + + private void fillPathToUploadInput(String tempFileName) { + // create a valid path in upload input element. Instead of selecting a + // file by some file browsing dialog, we use the local path directly. + WebElement input = getInput(); + setLocalFileDetector(input); + input.sendKeys(tempFileName); + } + + private WebElement getInput() { + return getDriver().findElement(By.className("gwt-FileUpload")); + } + + private void setLocalFileDetector(WebElement element) { + if (getRunLocallyBrowser() != null) { + return; + } + + if (element instanceof WrapsElement) { + element = ((WrapsElement) element).getWrappedElement(); + } + if (element instanceof RemoteWebElement) { + ((RemoteWebElement) element) + .setFileDetector(new LocalFileDetector()); + } else { + throw new IllegalArgumentException( + "Expected argument of type RemoteWebElement, received " + + element.getClass().getName()); + } + } +} |