Skip to content

Commit

Permalink
(refactor) Extract interface from CaptureSupport.
Browse files Browse the repository at this point in the history
  • Loading branch information
hastebrot committed Feb 23, 2016
1 parent 65eb476 commit 218f54f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 55 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.testfx.service.finder.impl.NodeFinderImpl;
import org.testfx.service.finder.impl.WindowFinderImpl;
import org.testfx.service.support.CaptureSupport;
import org.testfx.service.support.impl.CaptureSupportImpl;
import org.testfx.service.support.WaitUntilSupport;

@Unstable(reason = "class was recently added")
Expand All @@ -39,7 +40,7 @@ public class FxServiceContext {

private BaseRobot baseRobot = new BaseRobotImpl();

private CaptureSupport captureSupport = new CaptureSupport(baseRobot);
private CaptureSupport captureSupport = new CaptureSupportImpl(baseRobot);

private WaitUntilSupport waitUntilSupport = new WaitUntilSupport();

Expand Down
Expand Up @@ -16,70 +16,34 @@
*/
package org.testfx.service.support;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.stage.Screen;

import org.testfx.api.annotation.Unstable;
import org.testfx.robot.BaseRobot;

@Unstable(reason = "needs more tests")
public class CaptureSupport {

//---------------------------------------------------------------------------------------------
// PRIVATE FIELDS.
//---------------------------------------------------------------------------------------------
import javafx.scene.shape.Shape;
import org.testfx.service.support.impl.MatchResult;

private BaseRobot baseRobot;

//---------------------------------------------------------------------------------------------
// CONSTRUCTORS.
//---------------------------------------------------------------------------------------------
import java.io.File;

public CaptureSupport(BaseRobot baseRobot) {
this.baseRobot = baseRobot;
}
public interface CaptureSupport {

//---------------------------------------------------------------------------------------------
// METHODS.
//---------------------------------------------------------------------------------------------

public void captureScreenToFile(Screen screen, File captureFile) {
Image captureImage = captureScreenToImage(screen);
writeCaptureImageToFile(captureImage, captureFile);
}

public void capturePrimaryScreenToFile(File captureFile) {
captureScreenToFile(Screen.getPrimary(), captureFile);
}

//---------------------------------------------------------------------------------------------
// PRIVATE METHODS.
//---------------------------------------------------------------------------------------------

private Image captureScreenToImage(Screen screen) {
Rectangle2D region = screen.getBounds();
return baseRobot.captureRegion(region);
}
Image captureRegion(Rectangle2D region);
Image captureNode(Node node);

private Image captureNodeToImage(Node node) {
return node.snapshot(null, null);
}
Image drawShape(Shape shape,
Image image);
Image blendImages(Image image0,
Image image1,
BlendMode blendMode);
MatchResult<Image> matchImages(Image image0,
Image image1,
MatchAlgorithm algorithm);

private void writeCaptureImageToFile(Image captureImage, File captureFile) {
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(captureImage, null);
try {
ImageIO.write(bufferedImage, "png", captureFile);
}
catch (IOException exception) {
exception.printStackTrace();
}
}
Image loadImage(File file);
void saveImage(Image image, File file);

}
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.Callable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;

Expand Down Expand Up @@ -186,7 +188,8 @@ public static <T> void waitUntil(Callable<T> callable, Matcher<? super T> condit

public static File captureScreenshot() {
File captureFile = new File("screenshot" + new Date().getTime() + ".png");
captureSupport.capturePrimaryScreenToFile(captureFile);
Image captureImage = captureSupport.captureRegion(Screen.getPrimary().getBounds());
captureSupport.saveImage(captureImage, captureFile);
return captureFile;
}

Expand Down

0 comments on commit 218f54f

Please sign in to comment.