Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add file name to progress and file rejected listeners #5793

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ private void createSimpleUpload() {
eventsOutput.add("-succeeded");
});
upload.addAllFinishedListener(event -> eventsOutput.add("-finished"));
upload.addFileRejectedListener(event -> eventsOutput.add("-rejected"));
upload.addFileRejectedListener(event -> {
eventsOutput.add("-rejected");
output.add("REJECTED:" + event.getFileName());
});
upload.addFileRemovedListener(event -> {
eventsOutput.add("-removed");
output.add("REMOVED:" + event.getFileName());
});
upload.addProgressListener(
event -> output.add("PROGRESS:" + event.getFileName()));

NativeButton clearFileListBtn = new NativeButton("Clear file list",
e -> upload.clearFileList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public void testUploadAnyFile() throws Exception {

String content = uploadOutput.getText();

String expectedContent = tempFile.getName() + getTempFileContents();

Assert.assertEquals("Upload content does not match expected",
expectedContent, content);
Assert.assertTrue("Upload content does not contain file details",
content.contains(tempFile.getName() + getTempFileContents()));
Assert.assertTrue("Progress update event was not fired properly",
content.contains("PROGRESS:" + tempFile.getName()));
}

@Test
Expand Down Expand Up @@ -110,8 +110,14 @@ public void uploadInvalidFile_fileIsRejected() throws Exception {

getUpload().upload(invalidFile);

WebElement eventsOutput = getDriver()
.findElement(By.id("test-events-output"));
Assert.assertEquals("Invalid file was not rejected", "-rejected",
eventsOutput.getText());

WebElement uploadOutput = getDriver().findElement(By.id("test-output"));
Assert.assertTrue("Rejected file name was incorrect", uploadOutput
.getText().contains("REJECTED:" + invalidFile.getName()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/
public class FileRejectedEvent extends ComponentEvent<Upload> {

private String errorMessage;
private final String fileName;

private final String errorMessage;

/**
* Creates a new event using the given source and indicator whether the
Expand All @@ -35,10 +37,14 @@ public class FileRejectedEvent extends ComponentEvent<Upload> {
* the source component
* @param errorMessage
* the error message
* @param fileName
* the rejected file name
*/
public FileRejectedEvent(Upload source, String errorMessage) {
public FileRejectedEvent(Upload source, String errorMessage,
String fileName) {
super(source, true);
this.errorMessage = errorMessage;
this.fileName = fileName;
}

/**
Expand All @@ -49,4 +55,13 @@ public FileRejectedEvent(Upload source, String errorMessage) {
public String getErrorMessage() {
return errorMessage;
}

/**
* Get the file name.
*
* @return file name
*/
public String getFileName() {
return fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ProgressUpdateEvent extends ComponentEvent<Upload> {
*/
private final long contentLength;

/**
* Name of file currently being uploaded
*/
private final String fileName;

/**
* Event constructor method to construct a new progress event.
*
Expand All @@ -43,12 +48,15 @@ public class ProgressUpdateEvent extends ComponentEvent<Upload> {
* bytes transferred
* @param contentLength
* total size of file currently being uploaded, -1 if unknown
* @param fileName
* name of file currently being uploaded
*/
public ProgressUpdateEvent(Upload source, long readBytes,
long contentLength) {
long contentLength, String fileName) {
super(source, false);
this.readBytes = readBytes;
this.contentLength = contentLength;
this.fileName = fileName;
}

/**
Expand Down Expand Up @@ -77,4 +85,13 @@ public long getReadBytes() {
public long getContentLength() {
return contentLength;
}

/**
* Get the file name.
*
* @return file name
*/
public String getFileName() {
return fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ public UploadIcon() {
*/
public Upload() {
final String eventDetailError = "event.detail.error";
final String eventDetailFileName = "event.detail.file.name";

getElement().addEventListener("file-reject", event -> {
String detailError = event.getEventData()
.getString(eventDetailError);
fireEvent(new FileRejectedEvent(this, detailError));
}).addEventData(eventDetailError);
String detailFileName = event.getEventData()
.getString(eventDetailFileName);
fireEvent(new FileRejectedEvent(this, detailError, detailFileName));
}).addEventData(eventDetailError).addEventData(eventDetailFileName);

final String eventDetailFileName = "event.detail.file.name";
getElement().addEventListener("file-remove", event -> {
String detailFileName = event.getEventData()
.getString(eventDetailFileName);
Expand Down Expand Up @@ -489,9 +492,13 @@ private void fireAllFinish() {
* bytes received so far
* @param contentLength
* actual size of the file being uploaded, if known
* @param contentLength
* name of the file being uploaded
*/
protected void fireUpdateProgress(long totalBytes, long contentLength) {
fireEvent(new ProgressUpdateEvent(this, totalBytes, contentLength));
protected void fireUpdateProgress(long totalBytes, long contentLength,
String fileName) {
fireEvent(new ProgressUpdateEvent(this, totalBytes, contentLength,
fileName));
}

/**
Expand Down Expand Up @@ -744,7 +751,7 @@ public boolean listenProgress() {
@Override
public void onProgress(StreamVariable.StreamingProgressEvent event) {
upload.fireUpdateProgress(event.getBytesReceived(),
event.getContentLength());
event.getContentLength(), event.getFileName());
}

@Override
Expand Down