Skip to content

Commit

Permalink
Enhancement UI changes to ensure copy paste protections. (#162)
Browse files Browse the repository at this point in the history
Co-authored-by: Undid Iridium <[email protected]>
  • Loading branch information
2 people authored and soloturn committed Jan 2, 2025
1 parent fe7391c commit d6b8bf2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
42 changes: 30 additions & 12 deletions src/main/java/com/rarchives/ripme/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,8 @@ private void changeLocale() {
}

private void setupHandlers() {
ripButton.addActionListener(new RipButtonHandler());
ripTextfield.addActionListener(new RipButtonHandler());
ripButton.addActionListener(new RipButtonHandler(this));
ripTextfield.addActionListener(new RipButtonHandler(this));
ripTextfield.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
Expand Down Expand Up @@ -1390,10 +1390,25 @@ private boolean canRip(String urlString) {
}
}

class RipButtonHandler implements ActionListener {

public static JTextField getRipTextfield() {
return ripTextfield;
}

public static DefaultListModel<Object> getQueueListModel() {
return queueListModel;
}
static class RipButtonHandler implements ActionListener {

private MainWindow mainWindow;

public RipButtonHandler(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}
public void actionPerformed(ActionEvent event) {
String url = ripTextfield.getText();
if (!queueListModel.contains(url) && !url.equals("")) {
boolean url_not_empty = !url.equals("");
if (!queueListModel.contains(url) && url_not_empty) {
// Check if we're ripping a range of urls
if (url.contains("{")) {
// Make sure the user hasn't forgotten the closing }
Expand All @@ -1403,22 +1418,25 @@ public void actionPerformed(ActionEvent event) {
int rangeEnd = Integer.parseInt(rangeToParse.split("-")[1]);
for (int i = rangeStart; i < rangeEnd + 1; i++) {
String realURL = url.replaceAll("\\{\\S*\\}", Integer.toString(i));
if (canRip(realURL)) {
queueListModel.add(queueListModel.size(), realURL);
if (mainWindow.canRip(realURL)) {
queueListModel.addElement(realURL);
ripTextfield.setText("");
} else {
displayAndLogError("Can't find ripper for " + realURL, Color.RED);
mainWindow.displayAndLogError("Can't find ripper for " + realURL, Color.RED);
}
}
}
} else {
queueListModel.add(queueListModel.size(), ripTextfield.getText());
queueListModel.addElement(url);
ripTextfield.setText("");
}
} else {
if (!isRipping) {
ripNextAlbum();
}
} else if (url_not_empty) {
mainWindow.displayAndLogError("This URL is already in queue: " + url, Color.RED);
mainWindow.statusWithColor("This URL is already in queue: " + url, Color.ORANGE);
ripTextfield.setText("");
}
else if(!mainWindow.isRipping){
mainWindow.ripNextAlbum();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,18 @@
public class ChanRipperTest extends RippersTest {
@Test
@Tag("flaky")
public void testChanURLPasses() throws IOException, URISyntaxException {
public void testChanURLPasses() throws IOException, URISyntaxException {
List<URL> passURLs = new ArrayList<>();
// URLs that should work
passURLs.add(new URI("http://desuchan.net/v/res/7034.html").toURL());
passURLs.add(new URI("https://boards.4chan.org/hr/thread/3015701").toURL());
passURLs.add(new URI("https://boards.420chan.org/420/res/232066.php").toURL());
// passURLs.add(new URI("https://boards.420chan.org/420/res/232066.php").toURL()); - Dead link
passURLs.add(new URI("http://7chan.org/gif/res/25873.html").toURL());
passURLs.add(new URI("https://rbt.asia/g/thread/70643087/").toURL()); //must work with TLDs with len of 4
for (URL url : passURLs) {
ChanRipper ripper = new ChanRipper(url);
// Use CompletableFuture to run setup() asynchronously
CompletableFuture<Void> setupFuture = CompletableFuture.runAsync(() -> {
try {
ripper.setup();
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
});

try {
// Wait for up to 5 seconds for setup() to complete
setupFuture.get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException |
TimeoutException e) {
e.printStackTrace(); // Handle exceptions as needed
}
ripper.setup();
assert (ripper.canRip(url));
Assertions.assertNotNull(ripper.getWorkingDir(), "Ripper for " + url + " did not have a valid working directory.");
deleteDir(ripper.getWorkingDir());
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/rarchives/ripme/ui/RipButtonHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.rarchives.ripme.ui;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

public class RipButtonHandlerTest {


@Test
@Tag("flaky")
public void duplicateUrlTestCase() throws IOException {
// Simulating the MainWindow in our test
MainWindow testMainWindow = new MainWindow();
SwingUtilities.invokeLater(testMainWindow);

MainWindow.RipButtonHandler rbHandler = new MainWindow.RipButtonHandler(testMainWindow);
// Creating a RipButtonHandler instance - Changing fake text to cause github to rebuild 1.

// Add some URL to the model (assuming there's a method for adding URLs)
testMainWindow.getRipTextfield().setText("http://example.com");
rbHandler.actionPerformed(null);
testMainWindow.getRipTextfield().setText("http://example.com");
rbHandler.actionPerformed(null);

// Assuming your MainWindow or RipButtonHandler sets some flag or state
// indicating that a duplicate URL was encountered
assertEquals(testMainWindow.getRipTextfield().getText(), "");
}

}
5 changes: 5 additions & 0 deletions src/test/java/com/rarchives/ripme/ui/UIContextMenuTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,39 @@ void tearDown() {
}

@Test
@Tag("flaky")
void testCut() {
// Simulate a cut event
simulateCutEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testCopy() {
// Simulate a copy event
simulateCopyEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testPaste() {
// Simulate a paste event
simulatePasteEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testSelectAll() {
// Simulate a select all event
simulateSelectAllEvent();
// Add assertions if needed
}

@Test
@Tag("flaky")
void testUndo() {
// Simulate an undo event
simulateUndoEvent();
Expand Down

0 comments on commit d6b8bf2

Please sign in to comment.