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 /uitest | |
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
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/src/main/java/com/vaadin/tests/components/upload/UploadChangeListener.java | 31 | ||||
-rw-r--r-- | uitest/src/test/java/com/vaadin/tests/components/upload/UploadChangeListenerTest.java | 90 |
2 files changed, 121 insertions, 0 deletions
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()); + } + } +} |