diff options
author | Anna Koskinen <Ansku@users.noreply.github.com> | 2020-04-03 13:24:11 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-03 13:24:11 +0300 |
commit | 240cdce985bfc08a6e2869d1de52718227706640 (patch) | |
tree | d0696afd50c4d71e8304f0dbc0e1ed3f0fec7e40 /uitest/src | |
parent | 52013fc6c6ccc0891df79b7fa2c7cce1b9a4f036 (diff) | |
download | vaadin-framework-240cdce985bfc08a6e2869d1de52718227706640.tar.gz vaadin-framework-240cdce985bfc08a6e2869d1de52718227706640.zip |
Prevent upload if no file is selected. (#11939)
Fixes #10419
Diffstat (limited to 'uitest/src')
6 files changed, 191 insertions, 2 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/ui/MultiFileUploadTest.java b/uitest/src/main/java/com/vaadin/tests/components/ui/MultiFileUploadTest.java index 1e97980bc7..b4c0f1c038 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/ui/MultiFileUploadTest.java +++ b/uitest/src/main/java/com/vaadin/tests/components/ui/MultiFileUploadTest.java @@ -68,6 +68,7 @@ public class MultiFileUploadTest extends AbstractTestUIWithLog { private Upload createUpload() { Upload upload = new Upload(); + upload.setImmediateMode(false); upload.addChangeListener(changeListener); return upload; } diff --git a/uitest/src/main/java/com/vaadin/tests/components/upload/UploadNoSelection.java b/uitest/src/main/java/com/vaadin/tests/components/upload/UploadNoSelection.java index 81aeb03e0d..2267dec90f 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/upload/UploadNoSelection.java +++ b/uitest/src/main/java/com/vaadin/tests/components/upload/UploadNoSelection.java @@ -3,11 +3,16 @@ package com.vaadin.tests.components.upload; import java.io.ByteArrayOutputStream; import java.io.OutputStream; +import com.vaadin.annotations.Widgetset; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.widgetset.TestingWidgetSet; +import com.vaadin.tests.widgetset.server.upload.AllowUploadWithoutFilenameExtension; +import com.vaadin.ui.Button; import com.vaadin.ui.Upload; import com.vaadin.ui.Upload.Receiver; +@Widgetset(TestingWidgetSet.NAME) public class UploadNoSelection extends AbstractTestUIWithLog implements Receiver { @@ -27,7 +32,10 @@ public class UploadNoSelection extends AbstractTestUIWithLog @Override protected String getTestDescription() { - return "Uploading an empty selection (no file) will trigger FinishedEvent with 0-length file size and empty filename."; + return "Uploading an empty selection (no file) should not be possible by " + + "default. If the default behavior is overridden with a custom " + + "extension the upload attempt will trigger FinishedEvent with " + + "0-length file size and empty filename."; } @Override @@ -49,6 +57,16 @@ public class UploadNoSelection extends AbstractTestUIWithLog log(FILE_LENGTH_PREFIX + " " + event.getLength()); log(FILE_NAME_PREFIX + " " + event.getFilename()); }); + + Button progButton = new Button("Upload programmatically", + e -> u.submitUpload()); + progButton.setId("programmatic"); + addComponent(progButton); + + Button extButton = new Button("Allow upload without filename", + e -> AllowUploadWithoutFilenameExtension.wrap(u)); + extButton.setId("extend"); + addComponent(extButton); } @Override diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/upload/AllowUploadWithoutFilenameConnector.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/upload/AllowUploadWithoutFilenameConnector.java new file mode 100644 index 0000000000..d8e9b41610 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/upload/AllowUploadWithoutFilenameConnector.java @@ -0,0 +1,25 @@ +package com.vaadin.tests.widgetset.client.upload; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.ui.VUpload; +import com.vaadin.client.ui.upload.UploadConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.tests.widgetset.server.upload.AllowUploadWithoutFilenameExtension; + +@Connect(AllowUploadWithoutFilenameExtension.class) +public class AllowUploadWithoutFilenameConnector + extends AbstractExtensionConnector { + + @Override + protected void extend(ServerConnector target) { + UploadConnector connector = ((UploadConnector) target); + allowUploadWithoutFilename(connector.getWidget()); + } + + private native void allowUploadWithoutFilename(VUpload upload) + /*-{ + upload.@com.vaadin.client.ui.VUpload::allowUploadWithoutFilename = true; + }-*/; + +} diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/server/upload/AllowUploadWithoutFilenameExtension.java b/uitest/src/main/java/com/vaadin/tests/widgetset/server/upload/AllowUploadWithoutFilenameExtension.java new file mode 100644 index 0000000000..8f6620b43b --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/server/upload/AllowUploadWithoutFilenameExtension.java @@ -0,0 +1,13 @@ +package com.vaadin.tests.widgetset.server.upload; + +import com.vaadin.server.AbstractExtension; +import com.vaadin.ui.Upload; + +public class AllowUploadWithoutFilenameExtension extends AbstractExtension { + + public static AllowUploadWithoutFilenameExtension wrap(Upload upload) { + AllowUploadWithoutFilenameExtension extension = new AllowUploadWithoutFilenameExtension(); + extension.extend(upload); + return extension; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/ui/MultiFileUploadTestTest.java b/uitest/src/test/java/com/vaadin/tests/components/ui/MultiFileUploadTestTest.java new file mode 100644 index 0000000000..422c7b4f89 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/ui/MultiFileUploadTestTest.java @@ -0,0 +1,102 @@ +package com.vaadin.tests.components.ui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +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.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.UploadElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class MultiFileUploadTestTest extends MultiBrowserTest { + + @Test + public void changeListenerWorksAfterFirstUpload() throws IOException { + openTestURL(); + ButtonElement upload = $(ButtonElement.class).first(); + + File tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath(), + $(UploadElement.class).last()); + tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath(), + $(UploadElement.class).last()); + tempFile = createTempFile(); + fillPathToUploadInput(tempFile.getPath(), + $(UploadElement.class).last()); + + assertEquals("Unexpected amount of Upload components.", 4, + $(UploadElement.class).all().size()); + + upload.click(); + + // Last one doesn't have a file selected, and shouldn't trigger an + // event. + String logRow = getLogRow(0); + assertTrue("Unexpected upload log: " + logRow, + logRow.startsWith("3. Upload of ") + && logRow.endsWith(" complete")); + } + + /** + * @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, + UploadElement uploadElement) { + // 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(uploadElement); + setLocalFileDetector(input); + input.sendKeys(tempFileName); + } + + private WebElement getInput(UploadElement uploadElement) { + return uploadElement.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()); + } + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/upload/UploadNoSelectionTest.java b/uitest/src/test/java/com/vaadin/tests/components/upload/UploadNoSelectionTest.java index 9a4d3b5d1f..35f9577981 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/upload/UploadNoSelectionTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/upload/UploadNoSelectionTest.java @@ -1,11 +1,13 @@ package com.vaadin.tests.components.upload; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; +import com.vaadin.testbench.elements.ButtonElement; import com.vaadin.tests.tb3.MultiBrowserTest; public class UploadNoSelectionTest extends MultiBrowserTest { @@ -17,7 +19,24 @@ public class UploadNoSelectionTest extends MultiBrowserTest { // empty content is populated by com.vaadin.tests.util.Log assertEquals(" ", getLogRow(0)); - getSubmitButton().click(); + WebElement submitButton = getSubmitButton(); + assertTrue("Upload button should be disabled when no selection.", + hasCssClass(submitButton, "v-disabled")); + + submitButton.click(); + + // clicking the disabled default button doesn't do a thing + assertEquals(" ", getLogRow(0)); + + $(ButtonElement.class).id("programmatic").click(); + + // neither does triggering upload programmatically + assertEquals(" ", getLogRow(0)); + + // add an extension that allows upload without filename + $(ButtonElement.class).id("extend").click(); + + submitButton.click(); // expecting empty file name assertLogRow(0, 4, UploadNoSelection.FILE_NAME_PREFIX); @@ -25,6 +44,17 @@ public class UploadNoSelectionTest extends MultiBrowserTest { assertLogRow(1, 3, UploadNoSelection.FILE_LENGTH_PREFIX + " " + 0); assertLogRow(2, 2, UploadNoSelection.UPLOAD_FINISHED); assertLogRow(3, 1, UploadNoSelection.RECEIVING_UPLOAD); + + // and the same programmatically + $(ButtonElement.class).id("programmatic").click(); + + // expecting empty file name + assertLogRow(0, 8, UploadNoSelection.FILE_NAME_PREFIX); + // expecting 0-length file + assertLogRow(1, 7, UploadNoSelection.FILE_LENGTH_PREFIX + " " + 0); + assertLogRow(2, 6, UploadNoSelection.UPLOAD_FINISHED); + assertLogRow(3, 5, UploadNoSelection.RECEIVING_UPLOAD); + } private WebElement getSubmitButton() { |