diff options
author | Zhe Sun <31067185+ZheSun88@users.noreply.github.com> | 2019-07-29 14:36:39 +0300 |
---|---|---|
committer | Zhe Sun <31067185+ZheSun88@users.noreply.github.com> | 2019-07-30 16:12:37 +0300 |
commit | 1b5d3c06690a17f60fcbe4488ce53e8b922c90df (patch) | |
tree | fc05218c6a83e8fa5380da2afbb19d9282b85482 | |
parent | ea9107525457eb142d91ec3e27257772979ba059 (diff) | |
download | vaadin-framework-1b5d3c06690a17f60fcbe4488ce53e8b922c90df.tar.gz vaadin-framework-1b5d3c06690a17f60fcbe4488ce53e8b922c90df.zip |
Ensure the upload button has been disabled when set Enabled calling from succeedL… (#11655)
* Ensure the upload button has been disabled when calling from succeedListener with Push enabled
* Add Tests to verify
3 files changed, 306 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 681eaaf2f2..7ec8fbb71f 100644 --- a/client/src/main/java/com/vaadin/client/ui/VUpload.java +++ b/client/src/main/java/com/vaadin/client/ui/VUpload.java @@ -226,6 +226,17 @@ public class VUpload extends SimplePanel { rebuildPanel(); submitted = false; } + ensureUploadButton(); + + } + + private void ensureUploadButton() { + //when push got enabled, need to ensure the upload is disabled properly + //check ticket #11652 + if (fu.getElement().getParentElement().getParentElement() + .hasClassName("v-disabled")) { + disableUpload(); + } } private void setEnabledForSubmitButton(boolean enabled) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/upload/DisablingUpload.java b/uitest/src/main/java/com/vaadin/tests/components/upload/DisablingUpload.java new file mode 100644 index 0000000000..8b88494231 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/upload/DisablingUpload.java @@ -0,0 +1,65 @@ +package com.vaadin.tests.components.upload; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import com.vaadin.annotations.Push; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.communication.PushMode; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Button; +import com.vaadin.ui.UI; +import com.vaadin.ui.Upload; + +@Push +public class DisablingUpload extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + Upload ul = new Upload( + "Uploading anything will disable the Upload on SucceededListener", + new Upload.Receiver() { + @Override + public OutputStream receiveUpload(String s, String s1) { + return new ByteArrayOutputStream(); + } + }); + Button button = new Button("Disable upload from Button click", e -> { + ul.setEnabled(!ul.isEnabled()); + }); + button.setId("button-id"); + + ul.addSucceededListener(e -> { + ul.setEnabled(false); + log("File has been uploaded."); + }); + + ul.addStartedListener(e -> { + log("File upload starts"); + }); + + Button pushButton = new Button("Set the Push Mode"); + pushButton.setId("push-button"); + + Button stateButton = new Button("" + ul.isEnabled()); + stateButton.setId("state-button"); + + stateButton.addClickListener(event -> { + stateButton.setCaption("" + ul.isEnabled()); + }); + pushButton.addClickListener(event -> { + if (UI.getCurrent().getPushConfiguration().getPushMode() + .isEnabled()) { + UI.getCurrent().getPushConfiguration() + .setPushMode(PushMode.DISABLED); + pushButton.setCaption("enable push mode"); + } else { + UI.getCurrent().getPushConfiguration() + .setPushMode(PushMode.AUTOMATIC); + pushButton.setCaption("disable push mode"); + } + }); + + addComponents(ul, button, pushButton, stateButton); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/upload/DisablingUploadTest.java b/uitest/src/test/java/com/vaadin/tests/components/upload/DisablingUploadTest.java new file mode 100644 index 0000000000..2df398313c --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/upload/DisablingUploadTest.java @@ -0,0 +1,230 @@ +package com.vaadin.tests.components.upload; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.junit.Test; +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.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.UploadElement; +import com.vaadin.tests.tb3.SingleBrowserTest; +import com.vaadin.tests.util.LoremIpsum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DisablingUploadTest extends SingleBrowserTest { + + ButtonElement button; + ButtonElement pushButton; + ButtonElement stateButton; + + @Override + public void setup() throws Exception { + super.setup(); + + openTestURL(); + } + + @Test + public void buttonWorksAsExpected() { + buttonGroup(); + + // Disable button is working + assertTrue("Upload button should be enabled", + getSubmitButton().isEnabled()); + button.click(); + assertFalse("Upload button should be disabled", + getSubmitButton().isEnabled()); + + // pushmode button is working + assertEquals("Set the Push Mode", pushButton.getCaption()); + pushButton.click(); + sleep(100); + assertEquals("enable push mode", pushButton.getCaption()); + pushButton.click(); + sleep(100); + assertEquals("disable push mode", pushButton.getCaption()); + + // upload button state is correct + assertEquals("true", stateButton.getCaption()); + stateButton.click(); + sleep(100); + assertEquals("false", stateButton.getCaption()); + } + + @Test + public void pushEnabled_uploadFile_uploadButtonDisabled() throws Exception { + buttonGroup(); + + uploadFile(false); + + String expected = "2. File has been uploaded."; + + String actual = getLogRow(0); + assertEquals("Upload log row does not match expected", expected, + actual); + + stateButton.click(); + sleep(100); + assertEquals("false", stateButton.getCaption()); + + uploadFile(false); + //assert no new log + assertEquals("Upload log row does not match expected", expected, + actual); + } + + @Test + public void pushDisabled_uploadFile_uploadButtonDisabled() throws Exception { + buttonGroup(); + + pushButton.click(); + + uploadFile(false); + + String expected = "2. File has been uploaded."; + + String actual = getLogRow(0); + assertEquals("Upload log row does not match expected", expected, + actual); + + stateButton.click(); + sleep(100); + assertEquals("false", stateButton.getCaption()); + + uploadFile(false); + //assert no new log + assertEquals("Upload log row does not match expected", expected, + actual); + } + + @Test + public void pushEnabled_uploadLargeFile_uploadButtonDisabled() throws Exception { + buttonGroup(); + + uploadFile(true); + + String expected = "2. File has been uploaded."; + + String actual = getLogRow(0); + assertEquals("Upload log row does not match expected", expected, + actual); + + stateButton.click(); + sleep(100); + assertEquals("false", stateButton.getCaption()); + + uploadFile(true); + //assert no new log + assertEquals("Upload log row does not match expected", expected, + actual); + } + + @Test + public void pushDisabled_uploadLargeFile_uploadButtonDisabled() throws Exception { + buttonGroup(); + + pushButton.click(); + + uploadFile(true); + + String expected = "2. File has been uploaded."; + + String actual = getLogRow(0); + assertEquals("Upload log row does not match expected", expected, + actual); + + stateButton.click(); + sleep(100); + assertEquals("false", stateButton.getCaption()); + + uploadFile(true); + //assert no new log + assertEquals("Upload log row does not match expected", expected, + actual); + } + + private void buttonGroup() { + button = $(ButtonElement.class).id("button-id"); + pushButton = $(ButtonElement.class).id("push-button"); + stateButton = $(ButtonElement.class).id("state-button"); + } + + private void uploadFile(boolean large) throws Exception { + File tempFile = createTempFile(large); + fillPathToUploadInput(tempFile.getPath()); + + getSubmitButton().click(); + sleep(100); + } + + /** + * @return The generated temp file handle + * @throws IOException + */ + private File createTempFile(boolean large) throws IOException { + File tempFile = File.createTempFile("TestFileUpload", ".txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); + if(large) { + writer.write(getLargeTempFileContents()); + } else { + writer.write(getTempFileContents()); + } + writer.close(); + tempFile.deleteOnExit(); + return tempFile; + } + + private String getTempFileContents() { + return "This is a test file!\nRow 2\nRow3"; + } + + private String getLargeTempFileContents() { + return LoremIpsum.get(); + } + + 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 (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()); + } + } +} |