Skip to content

Commit

Permalink
Writing TestFx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arxa committed Oct 11, 2017
1 parent 792607a commit 11000e4
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 172 deletions.
21 changes: 14 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,23 @@ dependencies {
compile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.6'

testCompile group: "org.loadui", name: "testFx", version: "3.1.2"
testCompile 'org.testfx:testfx-core:4.0.1-alpha'
testRuntime 'org.testfx:openjfx-monocle:1.8.0_20'
// testCompile group: "org.loadui", name: "testFx", version: "3.1.2"
// testCompile 'org.testfx:testfx-core:4.0.1-alpha'
// testRuntime 'org.testfx:openjfx-monocle:1.8.0_20'
testCompile group: 'org.loadui', name: 'testFx', version: '3.1.2'

testCompile group: 'org.testfx', name: 'testfx-core', version: '4.0.8-alpha'

}
test {
testLogging.showStandardStreams = true
}

jfx {
mainClass = 'Main'
vendor = 'arxa'
//bundler = 'windows.app'
// bundleArguments = [
// 'runtime': 'C:\\Program Files\\Java\\jdk1.8.0_144\\jre'
// ]
bundler = 'windows.app'
bundleArguments = [
'runtime': 'C:\\Program Files\\Java\\jdk1.8.0_144\\jre'
]
}
2 changes: 2 additions & 0 deletions src/main/java/Entities/ApplicationPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ApplicationPaths
{
public static Caller CALLER;
public static String USER_DIR;
public static String TEST_RESOURCES;
public static String RESOURCES_NATIVES;
public static String RESOURCES_OCR;
public static String RESOURCES_OCR_TESSDATA;
Expand All @@ -32,6 +33,7 @@ public static void setApplicationPaths()
CALLER = Caller.IDE;
try {
USER_DIR = Paths.get(new File(".").getCanonicalPath(),"src","main","resources").toFile().getPath();
TEST_RESOURCES = Paths.get(new File(".").getCanonicalPath(),"src","test","resources").toFile().getPath();
} catch (IOException e) {
System.out.println("Couldn't create IDE directories");
Platform.exit();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void start(Stage stage) throws Exception
Parent root = loader.load();
stage.setTitle("VideoText Extractor");
stage.setScene(new Scene(root, 680, 450));
stage.setResizable(false);
stage.show();
stage.setOnCloseRequest(e -> Platform.exit());

Expand Down
32 changes: 23 additions & 9 deletions src/main/java/Processors/FileProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import Entities.Controllers;
import ViewControllers.MainController;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.Contract;
import org.opencv.core.Core;
Expand All @@ -27,6 +30,7 @@

public class FileProcessor
{
public static Node node;
/**
* Allows the user to choose a file through a file dialog
* and then validates if the File is valid, if it's playable
Expand All @@ -35,24 +39,31 @@ public class FileProcessor
*/
public static void validateVideoFile(File videoFile)
{
if (videoFile == null) return;
Alert alert;
if (!FileProcessor.validateVideoFileName(videoFile)){
new Alert(Alert.AlertType.WARNING, "ERROR on loading file\n"+
"Couldn't load file specified", ButtonType.OK).showAndWait();
alert = new Alert(Alert.AlertType.WARNING, "ERROR on loading file\n"+
"Couldn't load file specified", ButtonType.OK);
alert.showAndWait();
return;
}
if (!Player.playVideo(videoFile)){
new Alert(Alert.AlertType.WARNING, "ERROR on playing the video file\n"+
"Please choose a valid .mp4 video file", ButtonType.OK).showAndWait();
alert = new Alert(Alert.AlertType.WARNING, "ERROR on playing the video file",ButtonType.OK);
alert.showAndWait();
return;
}
if (!FileProcessor.createDirectories(videoFile)){
new Alert(Alert.AlertType.WARNING, "ERROR on creating directories\n"+
"Failed to create directories", ButtonType.OK).showAndWait();
alert = new Alert(Alert.AlertType.WARNING, "ERROR on creating directories\n"+
"Failed to create directories", ButtonType.OK);
alert.showAndWait();
return;
}
MainController.setCurrentVideoFile(videoFile);
MainController.resizeStageSlowly(1150, true);
MainController.setCurrentVideoFile(videoFile);
Controllers.getMainController().progressIndicator.setVisible(false);
Controllers.getMainController().progressBar.setVisible(false);
Controllers.getMainController().extractButton.setVisible(true);
Controllers.getMainController().textArea.setVisible(true);
Controllers.getMainController().textArea.clear();
}

/**
Expand Down Expand Up @@ -82,7 +93,8 @@ public static boolean createDirectories(File chosenFile)

@Contract("null -> false")
public static boolean validateVideoFileName(File filename) {
return filename != null && filename.exists();
return filename != null && filename.exists() && filename.canRead()
&& FilenameUtils.getExtension(filename.getPath()).equalsIgnoreCase("mp4");
}

/**
Expand Down Expand Up @@ -116,6 +128,8 @@ public static void loadLibraries()
Controllers.getLogController().logTextArea.appendText("Loaded OpenH264 for Windows 32 bit\n");
}
else if (bit == 64){
System.loadLibrary("opencv_ffmpeg320_64");
Controllers.getLogController().logTextArea.appendText("Loaded FFMPEG for Windows 64 bit\n");
System.loadLibrary("opencv_320_64");
Controllers.getLogController().logTextArea.appendText("Loaded OpenCV for Windows 64 bit\n");
System.loadLibrary("openh264-1.6.0-win64msvc");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Processors/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static boolean playVideo(File videoFile)
mediaView.fitWidthProperty().bind(Controllers.getMainController().videoPane.widthProperty());
mediaPlayer.play();
return true;
} catch (Exception e) {
} catch (Throwable e) {
return false;
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/Processors/VideoProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import ViewControllers.MainController;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.lept;
import org.bytedeco.javacpp.tesseract;
Expand Down Expand Up @@ -37,17 +39,22 @@ public class VideoProcessor
private static Thread thread;
private static boolean extractUniqueWords;
private static List<String> uniqueWords;
private static boolean frameIsOpened;

public static void processVideoFile(File videoFile)
{
VideoCapture cap = new VideoCapture(videoFile.getPath());
VideoCapture cap = new VideoCapture(videoFile.getAbsolutePath());
frameIsOpened = cap.read(input);

VideoWriter videoWriter = new VideoWriter(Paths.get(ApplicationPaths.RESOURCES_OUTPUTS,
ApplicationPaths.UNIQUE_FOLDER_NAME, "Video", "video.mp4").toAbsolutePath().toString(),
VideoWriter.fourcc('X', '2','6','4'),
cap.get(Videoio.CAP_PROP_FPS), new Size(cap.get(Videoio.CAP_PROP_FRAME_WIDTH), cap.get(Videoio.CAP_PROP_FRAME_HEIGHT)), true);

if (cap.isOpened())
{
if (!frameIsOpened){
new Alert(Alert.AlertType.ERROR, "ERROR: Failed to read video frames\nTry another video file. If the error persists, contact the developer",
ButtonType.OK).showAndWait();
} else {
Task<Void> task = new Task<Void>()
{
@Nullable
Expand All @@ -60,15 +67,14 @@ public static void processVideoFile(File videoFile)
OcrProcessor.initializeOcr(ocrApi);

double frames = (int) cap.get(Videoio.CAP_PROP_FRAME_COUNT);
boolean open1 = cap.read(input);
int currentFrame = 1;
input.copyTo(inputPainted);
Size kernel = StructuringElement.getStructuringElement(input.height()*input.width());
Mat structuringElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, kernel);
Platform.runLater(() -> {
Controllers.getLogController().logTextArea.appendText("[Structuring Element: "+kernel.height+" x "+kernel.width+"]\n");
});
while (open1 && !thread.isInterrupted())
while (frameIsOpened && !thread.isInterrupted())
{
ImageWriter.writeStep(input);
/*
Expand Down Expand Up @@ -166,7 +172,7 @@ default value (-1, -1) means that the anchor is at the element center.
// using the last detected text areas
videoWriter.write(inputPainted);
}
open1 = cap.read(input);
frameIsOpened = cap.read(input);
currentFrame++;
input.copyTo(inputPainted);

Expand Down
19 changes: 7 additions & 12 deletions src/main/java/ViewControllers/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public void initialize()
initializeViews();
ApplicationPaths.checkCaller();

textArea.setVisible(false); // TestFx will fail otherwise for some reason
videoIcon.setCursor(Cursor.HAND);
extractButton.setCursor(Cursor.HAND);

// When dragging something, allow it to be copied/moved only if it's a mp4 file
videoPane.setOnDragOver(event -> {
if (event.getDragboard().getFiles().get(0).isFile()){
if (FilenameUtils.getExtension(event.getDragboard().getFiles().get(0).getPath()).equals("mp4")) {
if (FilenameUtils.getExtension(event.getDragboard().getFiles().get(0).getPath()).equalsIgnoreCase("mp4")){
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
}
Expand All @@ -74,8 +75,6 @@ public void initialize()
VideoProcessor.getThread().interrupt();
}
}
textArea.clear();
progressIndicator.setVisible(false);
FileProcessor.validateVideoFile(event.getDragboard().getFiles().get(0));
event.setDropCompleted(true);
event.consume();
Expand All @@ -96,8 +95,6 @@ public void initialize()
VideoProcessor.getThread().interrupt();
}
}
textArea.clear();
progressIndicator.setVisible(false);
FileProcessor.validateVideoFile(FileProcessor.showFileDialog());
});

Expand Down Expand Up @@ -139,8 +136,12 @@ public void initialize()
videoPane.getChildren().clear();
videoPane.getChildren().add(videoIcon);
videoIcon.setVisible(true);
resizeStageSlowly(680, false);
textArea.clear();
textArea.setVisible(false);
progressIndicator.setVisible(false);
progressIndicator.setVisible(false);
extractButton.setVisible(false);
resizeStageSlowly(680, false);
});

playOriginal.setOnAction(event -> {
Expand All @@ -162,7 +163,6 @@ public void initialize()
* @param maximize Flag that indicates whether to expand or collapse the stage's width
*/
public static void resizeStageSlowly(double desiredSize, boolean maximize){
Controllers.getMainController().extractButton.setVisible(true);
Timer animTimer = new Timer();
animTimer.scheduleAtFixedRate(new TimerTask() {
@Override
Expand Down Expand Up @@ -218,11 +218,6 @@ public void initializeViews(){
settingsStage.setScene(new Scene(root, 400, 250));
}

@Contract(pure = true)
public static File getCurrentVideoFile() {
return currentVideoFile;
}

public static void setCurrentVideoFile(File currentVideoFile) {
MainController.currentVideoFile = currentVideoFile;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/Views/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</Menu>
</menus>
</MenuBar>
<TextArea fx:id="textArea" editable="false" layoutX="715.0" layoutY="151.0" opacity="0.39" prefHeight="200.0" prefWidth="369.0" wrapText="true" />
<TextArea fx:id="textArea" editable="false" layoutX="715.0" layoutY="151.0" opacity="0.39" prefHeight="200.0" prefWidth="369.0" visible="false" wrapText="true" />
<ProgressIndicator fx:id="progressIndicator" layoutX="838.0" layoutY="25.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="75.0" prefWidth="136.0" progress="0.0" visible="false" />
<Button fx:id="extractButton" layoutX="829.0" layoutY="52.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="168.0" text="Extract Text" visible="false" />
<ProgressBar fx:id="progressBar" layoutX="715.0" layoutY="126.0" prefHeight="8.0" prefWidth="369.0" progress="0.0" visible="false" />
Expand Down
Loading

0 comments on commit 11000e4

Please sign in to comment.