summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnna Koskinen <Ansku@users.noreply.github.com>2019-10-24 13:54:13 +0300
committerGitHub <noreply@github.com>2019-10-24 13:54:13 +0300
commit99320d6da42b39ad540e5cbd5b7f8ed5e4ac2944 (patch)
tree9b29d25ba34fa2dc4f5ce4391f8a7538e2eaa2fe
parent0e435f125cd7d57009aa6d938f6d11729c0e06e7 (diff)
downloadvaadin-framework-99320d6da42b39ad540e5cbd5b7f8ed5e4ac2944.tar.gz
vaadin-framework-99320d6da42b39ad540e5cbd5b7f8ed5e4ac2944.zip
Upload should preserve MIMEType after first selection (#11745) (#11759)
* Upload should preserve MIMEType after first selection Fix #11698 * Remove unused imports * Add sleep time in testing purposes * Getting a new instance of an input, after file is downloaded Exclude IE from tests. Throws https://stackoverflow.com/questions/23883071/unhandled-alert-exception-modal-dialog-present-selenium , but checked manually and it works
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VUpload.java6
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/upload/TestUploadMIMEType.java51
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/upload/TestUploadMIMETypeTest.java105
3 files changed, 162 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 56532b4a19..2f4b62a47f 100644
--- a/client/src/main/java/com/vaadin/client/ui/VUpload.java
+++ b/client/src/main/java/com/vaadin/client/ui/VUpload.java
@@ -121,6 +121,8 @@ public class VUpload extends SimplePanel {
private boolean immediateMode;
+ private String acceptMimeTypes;
+
private Hidden maxfilesize = new Hidden();
/** For internal use only. May be removed or replaced in the future. */
@@ -254,6 +256,9 @@ public class VUpload extends SimplePanel {
fu = new VFileUpload();
fu.setName(paintableId + "_file");
fu.getElement().setPropertyBoolean("disabled", !enabled);
+ if (acceptMimeTypes != null && !acceptMimeTypes.isEmpty()) {
+ InputElement.as(fu.getElement()).setAccept(acceptMimeTypes);
+ }
panel.add(fu);
panel.add(submitButton);
if (isImmediateMode()) {
@@ -424,5 +429,6 @@ public class VUpload extends SimplePanel {
} else {
InputElement.as(fu.getElement()).setAccept(acceptMimeTypes);
}
+ this.acceptMimeTypes = acceptMimeTypes;
}
}
diff --git a/uitest/src/main/java/com/vaadin/tests/components/upload/TestUploadMIMEType.java b/uitest/src/main/java/com/vaadin/tests/components/upload/TestUploadMIMEType.java
new file mode 100644
index 0000000000..f6b6ea788d
--- /dev/null
+++ b/uitest/src/main/java/com/vaadin/tests/components/upload/TestUploadMIMEType.java
@@ -0,0 +1,51 @@
+package com.vaadin.tests.components.upload;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.Upload.Receiver;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class TestUploadMIMEType extends AbstractTestUI {
+
+ public static final String TEST_MIME_TYPE = "application/pdf";
+ private UploadReceiver receiver = new UploadReceiver();
+
+ public static class UploadReceiver implements Receiver {
+
+ private String filename;
+
+ @Override
+ public OutputStream receiveUpload(String filename, String MIMEType) {
+ this.filename = filename;
+ return new ByteArrayOutputStream();
+ }
+
+ public String getFilename() {
+ return filename;
+ }
+
+ }
+
+ @Override
+ public String getDescription() {
+ return "MIME types for an Upload component should be preserved after the first upload";
+ }
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ Upload upload = new Upload("Upload a file", receiver);
+ upload.setAcceptMimeTypes(TEST_MIME_TYPE);
+ addComponent(upload);
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 119698;
+ }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/upload/TestUploadMIMETypeTest.java b/uitest/src/test/java/com/vaadin/tests/components/upload/TestUploadMIMETypeTest.java
new file mode 100644
index 0000000000..ca32e61234
--- /dev/null
+++ b/uitest/src/test/java/com/vaadin/tests/components/upload/TestUploadMIMETypeTest.java
@@ -0,0 +1,105 @@
+package com.vaadin.tests.components.upload;
+
+import com.vaadin.testbench.elements.UploadElement;
+import com.vaadin.testbench.parallel.Browser;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+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.DesiredCapabilities;
+import org.openqa.selenium.remote.LocalFileDetector;
+import org.openqa.selenium.remote.RemoteWebElement;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import static com.vaadin.tests.components.upload.TestUploadMIMEType.TEST_MIME_TYPE;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class TestUploadMIMETypeTest extends MultiBrowserTest {
+ @Override
+ public void setup() throws Exception {
+ super.setup();
+ openTestURL();
+ }
+
+ @Test
+ public void testAcceptAttribute() throws Exception {
+ WebElement input = getInput();
+ assertThat(input.getAttribute("accept"), is(TEST_MIME_TYPE));
+ uploadFile();
+ waitUntil(driver -> getSubmitButton().isEnabled());
+ //Previous element is removed, getting a new one
+ input = getInput();
+ assertThat(
+ String.format("Accept is expected to be %s , but was %s ",
+ TEST_MIME_TYPE, input.getAttribute("accept")),
+ input.getAttribute("accept"), is(TEST_MIME_TYPE));}
+
+ private void uploadFile() throws Exception {
+ File tempFile = createTempFile();
+ fillPathToUploadInput(tempFile.getPath());
+ getSubmitButton().click();
+ }
+
+ /**
+ * @return The generated temp file handle
+ * @throws IOException
+ */
+ private File createTempFile() throws IOException {
+ File tempFile = File.createTempFile("TestFileUpload", ".pdf");
+ BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
+ writer.write(getTempFileContents());
+ writer.close();
+ tempFile.deleteOnExit();
+ return tempFile;
+ }
+
+ private String getTempFileContents() {
+ return "This is a test file!\nRow 2\nRow3";
+ }
+
+ private void fillPathToUploadInput(String tempFileName) throws Exception {
+ // 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 getSubmitButton() {
+ UploadElement upload = $(UploadElement.class).first();
+ WebElement submitButton = upload.findElement(By.className("v-button"));
+ return submitButton;
+ }
+
+ private WebElement getInput() {
+ return getDriver().findElement(By.className("gwt-FileUpload"));
+ }
+
+ private void setLocalFileDetector(WebElement element) throws Exception {
+
+ 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());
+ }
+ }
+ @Override
+ public List<DesiredCapabilities> getBrowsersToTest() {
+ // IE11 throws an `Unhandled Alert Exception`
+ //https://stackoverflow.com/questions/23883071/unhandled-alert-exception-modal-dialog-present-selenium
+ return getBrowserCapabilities(Browser.CHROME, Browser.FIREFOX);
+ }
+}