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 00c8215
Show file tree
Hide file tree
Showing 6 changed files with 207 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);

}
@@ -0,0 +1,20 @@
/*
* Copyright 2013-2014 SmartBear Software
* Copyright 2014-2015 The TestFX Contributors
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence"); You may
* not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the Licence for the
* specific language governing permissions and limitations under the Licence.
*/
package org.testfx.service.support;

public interface MatchAlgorithm {
}
@@ -0,0 +1,113 @@
/*
* Copyright 2013-2014 SmartBear Software
* Copyright 2014-2015 The TestFX Contributors
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence"); You may
* not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the Licence for the
* specific language governing permissions and limitations under the Licence.
*/
package org.testfx.service.support.impl;

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.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Shape;

import org.testfx.api.annotation.Unstable;
import org.testfx.robot.BaseRobot;
import org.testfx.service.support.CaptureSupport;
import org.testfx.service.support.MatchAlgorithm;

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

//---------------------------------------------------------------------------------------------
// PRIVATE FIELDS.
//---------------------------------------------------------------------------------------------

private BaseRobot baseRobot;

//---------------------------------------------------------------------------------------------
// CONSTRUCTORS.
//---------------------------------------------------------------------------------------------

public CaptureSupportImpl(BaseRobot baseRobot) {
this.baseRobot = baseRobot;
}

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

@Override
public Image captureRegion(Rectangle2D region) {
return baseRobot.captureRegion(region);
}

@Override
public Image captureNode(Node node) {
return node.snapshot(null, null);
}

@Override
public Image drawShape(Shape shape,
Image image) {
throw new UnsupportedOperationException();
}

@Override
public Image blendImages(Image image0,
Image image1,
BlendMode blendMode) {
StackPane stackPane = new StackPane();
stackPane.setAlignment(Pos.TOP_LEFT);
stackPane.getChildren().add(new ImageView(image0));
stackPane.getChildren().add(new ImageView(image1));
stackPane.setBlendMode(blendMode);
return captureNode(stackPane);
}

@Override
public MatchResult<Image> matchImages(Image image0,
Image image1,
MatchAlgorithm algorithm) {
throw new UnsupportedOperationException();
}

@Override
public Image loadImage(File file) {
throw new UnsupportedOperationException();
}

@Override
public void saveImage(Image image,
File file) {
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(bufferedImage, "png", file);
}
catch (IOException exception) {
exception.printStackTrace();
}
}

}

@@ -0,0 +1,51 @@
/*
* Copyright 2013-2014 SmartBear Software
* Copyright 2014-2015 The TestFX Contributors
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence"); You may
* not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the Licence for the
* specific language governing permissions and limitations under the Licence.
*/
package org.testfx.service.support.impl;

public class MatchResult<T> {

//---------------------------------------------------------------------------------------------
// PRIVATE FIELDS.
//---------------------------------------------------------------------------------------------

private T result;

private int distance;

//---------------------------------------------------------------------------------------------
// CONSTRUCTORS.
//---------------------------------------------------------------------------------------------

public MatchResult(T result,
int distance) {
this.result = result;
this.distance = distance;
}

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

T getResult() {
return result;
}

int getDistance() {
return distance;
}

}
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 00c8215

Please sign in to comment.