summaryrefslogtreecommitdiffstats
path: root/uitest
diff options
context:
space:
mode:
authorZhe Sun <31067185+ZheSun88@users.noreply.github.com>2019-07-29 14:36:39 +0300
committerGitHub <noreply@github.com>2019-07-29 14:36:39 +0300
commit3a1f595b82dd031fecdf8c43f921f9b0b5c96853 (patch)
treed0fad563b97682cf2aa9a31b1ed4eb914f913245 /uitest
parente53237061362c5be5bc0d623c98b8f775b8ad2d4 (diff)
downloadvaadin-framework-3a1f595b82dd031fecdf8c43f921f9b0b5c96853.tar.gz
vaadin-framework-3a1f595b82dd031fecdf8c43f921f9b0b5c96853.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
Diffstat (limited to 'uitest')
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/upload/DisablingUpload.java65
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/upload/DisablingUploadTest.java230
2 files changed, 295 insertions, 0 deletions
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());
+ }
+ }
+}