# HG changeset patch # User arusakov # Date 1468859185 -10800 # Mon Jul 18 19:26:25 2016 +0300 # Node ID b9945eb761e345a57f605e06ffc7e04475f24f68 # Parent 7562f73c8642bbbe01cd5c24805d7c51c1b14b5c 8151500: [TEST] Implement multiple golden image support Summary: Makes possible using any number of golden images. diff --git a/functional/ControlsTests/test/javafx/commons/ControlsTest.java b/functional/ControlsTests/test/javafx/commons/ControlsTest.java --- a/functional/ControlsTests/test/javafx/commons/ControlsTest.java +++ b/functional/ControlsTests/test/javafx/commons/ControlsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -45,8 +45,7 @@ import org.jemmy.timing.State; import org.jemmy.timing.Waiter; import org.junit.Assert; -import test.javaclient.shared.screenshots.ImagesManager; - +import test.javaclient.shared.screenshots.GoldenImageManager; /** * * @author Dmitry Zinkevich @@ -121,17 +120,17 @@ System.out.println(ex.getMessage()); Image imgTranslucent = testedControl.getScreenImage(); - String screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_img_opacity40", nodeName)); + String screenshotPath = GoldenImageManager.getScreenshotPath(String.format("%s_img_opacity40", nodeName)); imgOpacity40.save(screenshotPath); - screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_control_opacity40", nodeName)); + screenshotPath = GoldenImageManager.getScreenshotPath(String.format("%s_control_opacity40", nodeName)); imgTranslucent.save(screenshotPath); Image diff = Environment.getEnvironment().getProperty(ImageComparator.class).compare(imgOpacity40, imgTranslucent); Assert.assertTrue(diff != null); - screenshotPath = ImagesManager.getInstance().getScreenshotPath(String.format("%s_diff", nodeName)); + screenshotPath = GoldenImageManager.getScreenshotPath(String.format("%s_diff", nodeName)); diff.save(screenshotPath); } setPropertyBySlider(settingType, TestedProperties.opacity, 1); diff --git a/functional/ControlsTests/test/javafx/scene/control/test/dialog/DialogTest.java b/functional/ControlsTests/test/javafx/scene/control/test/dialog/DialogTest.java --- a/functional/ControlsTests/test/javafx/scene/control/test/dialog/DialogTest.java +++ b/functional/ControlsTests/test/javafx/scene/control/test/dialog/DialogTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -59,8 +59,7 @@ import org.junit.runner.RunWith; import test.javaclient.shared.FilteredTestRunner; import test.javaclient.shared.TestUtil; -import test.javaclient.shared.screenshots.ImagesManager; - +import test.javaclient.shared.screenshots.GoldenImageManager; /** * * @author Alexander Vorobyev @@ -593,7 +592,7 @@ protected void saveSceneImage(String fileName) { Image sceneImage; sceneImage = scene.getScreenImage(); - sceneImage.save(ImagesManager.getInstance().getScreenshotPath(fileName)); + sceneImage.save(GoldenImageManager.getScreenshotPath(fileName)); } protected boolean requestFocusByKeyboard(final Wrap wrap) { diff --git a/functional/ControlsTests/test/javafx/scene/control/test/richtexteditor/RichTextEditorTest.java b/functional/ControlsTests/test/javafx/scene/control/test/richtexteditor/RichTextEditorTest.java --- a/functional/ControlsTests/test/javafx/scene/control/test/richtexteditor/RichTextEditorTest.java +++ b/functional/ControlsTests/test/javafx/scene/control/test/richtexteditor/RichTextEditorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ package javafx.scene.control.test.richtexteditor; @@ -46,7 +46,7 @@ import test.javaclient.shared.FilteredTestRunner; import test.javaclient.shared.JemmyUtils; import test.javaclient.shared.Utils; -import test.javaclient.shared.screenshots.ImagesManager; +import test.javaclient.shared.screenshots.GoldenImageManager; import test.javaclient.shared.screenshots.ScreenshotUtils; @RunWith(FilteredTestRunner.class) @@ -876,7 +876,7 @@ } }, readResource(getHTMLName(html) + ".html")); } finally { - Writer out = new OutputStreamWriter(new FileOutputStream(ImagesManager.getInstance().getHTMLPath(html))); + Writer out = new OutputStreamWriter(new FileOutputStream(GoldenImageManager.getGoldenPath(html, ".html"))); try { out.write("Found : " + htmlEditor.getControl().getHtmlText() + "\n"); out.write("Expected : " + readResource(getHTMLName(html) + ".html") + "\n"); @@ -895,7 +895,7 @@ } }, readResource(getHTMLName(html) + ".html")); } finally { - Writer out = new OutputStreamWriter(new FileOutputStream(ImagesManager.getInstance().getHTMLPath(html))); + Writer out = new OutputStreamWriter(new FileOutputStream(GoldenImageManager.getGoldenPath(html, ".html"))); try { out.write(getClipboardHTML()); } finally { diff --git a/functional/FXCssTests/test/test/css/controls/style/size/SizeStyleBaseTest.java b/functional/FXCssTests/test/test/css/controls/style/size/SizeStyleBaseTest.java --- a/functional/FXCssTests/test/test/css/controls/style/size/SizeStyleBaseTest.java +++ b/functional/FXCssTests/test/test/css/controls/style/size/SizeStyleBaseTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -41,8 +41,7 @@ import org.junit.Test; import test.css.controls.api.SizeStyleApp; import test.javaclient.shared.TestBase; -import test.javaclient.shared.screenshots.ImagesManager; -import test.javaclient.shared.screenshots.ScreenshotUtils; +import test.javaclient.shared.screenshots.GoldenImageManager; /** * @@ -187,9 +186,9 @@ return true; } else { - String cssStyleImage = ImagesManager.getInstance().getScreenshotPath(getPageName() + testName + "_css"); - String apiImage = ImagesManager.getInstance().getScreenshotPath(getPageName() + testName + "_api"); - String diffPath = ImagesManager.getInstance().getScreenshotPath(getPageName() + testName + "_diff"); + String cssStyleImage = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_css"); + String apiImage = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_api"); + String diffPath = GoldenImageManager.getScreenshotPath(getPageName() + testName + "_diff"); testedImage.save(cssStyleImage); goldenImage.save(apiImage); testedImage.compareTo(goldenImage).save(diffPath); diff --git a/tools/FxTestRunner/src/client/test/runner/CommonTestRunnerWorker.java b/tools/FxTestRunner/src/client/test/runner/CommonTestRunnerWorker.java --- a/tools/FxTestRunner/src/client/test/runner/CommonTestRunnerWorker.java +++ b/tools/FxTestRunner/src/client/test/runner/CommonTestRunnerWorker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2016 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,7 +37,7 @@ import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; -import test.javaclient.shared.screenshots.ImagesManager; +import test.javaclient.shared.screenshots.GoldenImageManager; /** * @@ -202,12 +202,16 @@ testClassName = trc.param; break; case SET_BASEDIR: - //TODO: this design is a bit flawed as we introducing backward - //dependency on SharedTestUtils here. - ImagesManager.getInstance().setBaseDir(trc.param); + //Looks like method is required for some remote invocation and could be + //used later if it will be decided to run jnlp-mode again. + //So commenting instead of removing. + //GoldenImageManager.setBaseDir(trc.param); break; case SET_ABSOLUTE_DIR: - ImagesManager.getInstance().setAbsoluteDir(trc.param); + //Looks like method is required for some remote invocation and could be + //used later if it will be decided to run jnlp-mode again. + //So commenting instead of removing. + //GoldenImageManager.setAbsoluteDir(trc.param); break; case SET_TEST_NAME: testName = trc.param; diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestBase.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestBase.java --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestBase.java +++ b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,15 +30,12 @@ import java.util.concurrent.Semaphore; import javafx.application.Platform; import javafx.scene.Node; -import javafx.scene.Scene; import javafx.scene.SnapshotParameters; import javafx.scene.image.ImageView; -import javafx.stage.Stage; import org.jemmy.Point; import org.jemmy.action.GetAction; import org.jemmy.control.Wrap; import org.jemmy.env.Environment; -import org.jemmy.fx.ByWindowType; import org.jemmy.fx.Lookups; import org.jemmy.fx.NodeDock; import org.jemmy.fx.Root; @@ -48,9 +45,9 @@ import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; -import test.javaclient.shared.screenshots.ImagesManager; import test.javaclient.shared.screenshots.ScreenshotUtils; import static test.javaclient.shared.TestUtil.isEmbedded; +import test.javaclient.shared.screenshots.GoldenImageManager; /** * Base class for scenegraph tests. Provides jemmy initialization, scene finding @@ -266,7 +263,7 @@ if (shoots) { //verify screenshot String testName = new StringBuilder(getName()).append("-").append(toplevel_name).append(innerlevel_name).append("-").append("additional").toString(); - String pathToGoldenScreenshot = ImagesManager.getInstance().lookupGoldenScreenshot(testName); + String pathToGoldenScreenshot = GoldenImageManager.getScreenshotPath(testName); if (!(new File(pathToGoldenScreenshot)).exists()) { testName = new StringBuilder(getName()).append("-").append(toplevel_name).append(innerlevel_name).toString(); } @@ -290,8 +287,8 @@ String filename = prefixForFilename + getNameForScreenShot(toplevel_name, innerlevel_name); org.jemmy.image.Image imageJemmy = (null == noda) ? getScene().getScreenImage() : noda.getScreenImage(); - String resName = ImagesManager.getInstance().getScreenshotPath(filename); - String res2Name = ImagesManager.getInstance().getScreenshotPath(filename + "-2"); + String resName = GoldenImageManager.getScreenshotPath(filename); + String res2Name = GoldenImageManager.getScreenshotPath(filename + "-2"); imageJemmy.save(resName); @@ -301,7 +298,7 @@ //org.jemmy.image.Image sceneImageJemmy2 = scene.getScreenImage(); //sceneImageJemmy2.save(ImagesManager.getInstance().getScreenshotPath(filename + "2")); - String diffName = ImagesManager.getInstance().getScreenshotPath(filename + "-diff"); + String diffName = GoldenImageManager.getScreenshotPath(filename + "-diff"); boolean scenesEqual = true; try { if (null == noda) { diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestUtil.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestUtil.java --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestUtil.java +++ b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/TestUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,14 +32,11 @@ import javafx.stage.Window; import org.jemmy.TimeoutExpiredException; import org.jemmy.control.Wrap; -import org.jemmy.env.Timeout; import org.jemmy.fx.ByWindowType; import org.jemmy.fx.Root; -import org.jemmy.image.Image; import org.jemmy.timing.State; -import org.jemmy.timing.Waiter; import test.javaclient.shared.description.TreeNode; -import test.javaclient.shared.screenshots.ImagesManager; +import test.javaclient.shared.screenshots.GoldenImageManager; /** * Utility methods @@ -127,9 +124,9 @@ * @param oneToWaitForWrap wrap for object can be not yet placed on UI */ public static void compareScreenshots(String testName, final Wrap existingWrap, final Wrap oneToWaitForWrap) { - String existingName = ImagesManager.getInstance().getScreenshotPath(testName + "-existing"); //= RESULT_PATH + testName + "-diff.png"; - String waitingForName = ImagesManager.getInstance().getScreenshotPath(testName + "-waitingFor"); - String diffName = ImagesManager.getInstance().getScreenshotPath(testName + "-diff"); //= RESULT_PATH + testName + IMAGE_POSTFIX; + String existingName = GoldenImageManager.getScreenshotPath(testName + "-existing"); //= RESULT_PATH + testName + "-diff.png"; + String waitingForName = GoldenImageManager.getScreenshotPath(testName + "-waitingFor"); + String diffName = GoldenImageManager.getScreenshotPath(testName + "-diff"); //= RESULT_PATH + testName + IMAGE_POSTFIX; String OUTPUT = oneToWaitForWrap.getClass().getName() + ".OUTPUT"; try { oneToWaitForWrap.waitState(new State() { diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImageManager.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImageManager.java new file mode 100644 --- /dev/null +++ b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImageManager.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + */ +package test.javaclient.shared.screenshots; + +import com.sun.prism.GraphicsPipeline; +import java.io.File; +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * + * @author andrey.rusakov@oracle.com + */ +public class GoldenImageManager { + + private static final String IMG_VERSION = "-\\d+"; + private static final String IMG_FOLDER = "images-svn"; + + private static File getImagesRoot() throws IOException { + return new File("../", IMG_FOLDER).getCanonicalFile(); + } + + private static String getSuiteName() throws IOException { + return new File(".").getCanonicalFile().getName(); + } + + //Requires -XaddExports:javafx.graphics/com.sun.prism=ALL-UNNAMED to work in JDK9 + private static String getRenderingMode() { + GraphicsPipeline pipeline = com.sun.prism.GraphicsPipeline.getPipeline(); + return pipeline.getClass().getName().endsWith("J2DPipeline") + || pipeline.getClass().getName().endsWith("SWPipeline") ? "j2d" : "prism"; + } + + private static String getPlatformAlias() { + String os = System.getProperty("os.name").toLowerCase(); + String version = System.getProperty("os.version").toLowerCase(); + if (os.contains("win")) { + if (version.startsWith("5.")) { + return "xp"; + } else if (version.startsWith("6.2") + || version.startsWith("6.3")) { + return "eight"; + } else if (version.startsWith("10.")) { + return "ten"; + } + + } else if (os.contains("mac")) { + return "mac"; + } else if (os.contains("linux") || os.contains("ubuntu")) { + return "linux"; + } + return ""; + } + + public static String getDiffPath(String goldenPath) { + String relativeGoldenPath = goldenPath.substring(goldenPath.indexOf(IMG_FOLDER) + + IMG_FOLDER.length()); + String goldenName = relativeGoldenPath.replace(File.pathSeparator, "_"); + String diffName = goldenName.substring(0, goldenName.lastIndexOf(".") - 1) + "-diff"; + return getScreenshotPath(diffName); + } + + public static String getScreenshotPath(String testName) { + return getGoldenPath(testName, "png"); + } + + public static String getGoldenPath(String testName, String extension) { + String imagesOutputPath = System.getProperty("imageutils.outputpath"); + if (imagesOutputPath == null) { + imagesOutputPath = Paths.get(".", "build").toString(); + } + try { + return new File(imagesOutputPath, testName + "." + extension).getCanonicalFile().toString(); + } catch (IOException ex) { + System.err.println("Failed to get screenshot path: " + ex.getMessage()); + } + return null; + } + + public static List getTestImages(String testName) { + return getTestImages(testName, ".png"); + } + + /** + * Retrieves images from reference image folder according to suffix (usually test name) and + * postfix (usually file extension). + * Files should have name that matches "suffix[-number-]postfix", e.g: "SomeTestName.png", + * "SomeTestName-1.png"... "SomeTestName-NUMBER.png". Method searches for all matching files + * in "TestsuiteName/RenderingMode/Platform", "TestsuiteName/RenderingMode/" and + * "TestsuiteName/". + * + * @param suffix filename suffix, usually test name. + * @param postfix filename prefix, usually file extension. + * @return Full paths to all matching files or empty list if no files matched. + */ + public static List getTestImages(String suffix, String postfix) { + File imageRoot; + String suiteName; + try { + imageRoot = getImagesRoot(); + System.out.println(imageRoot.getCanonicalPath()); + } catch (IOException ex) { + System.err.printf("Failed to get golden images folder path: %s\n", ex.getMessage()); + return Collections.emptyList(); + } + try { + suiteName = getSuiteName(); + } catch (IOException ex) { + System.err.printf("Failed to get testsuite name: %s\n", ex.getMessage()); + return Collections.emptyList(); + } + File currentDirectory = new File(imageRoot, + Paths.get(suiteName, getRenderingMode(), getPlatformAlias()).toString()); + List goldenVariants = new ArrayList<>(); + while (!currentDirectory.equals(imageRoot)) { + try (DirectoryStream stream = Files.newDirectoryStream(currentDirectory.toPath())) { + stream.forEach(path -> { + String name = path.getFileName().toString(); + if (name.startsWith(suffix) && name.endsWith(postfix)) { + String imageVersion = name.substring(suffix.length(), name.lastIndexOf(postfix)); + if (imageVersion.isEmpty() || imageVersion.matches(IMG_VERSION)) { + goldenVariants.add(path.toAbsolutePath().toString()); + } + } + }); + } catch (IOException ex) { + System.err.printf("Failed to enumerate golden images at %s: %s\n", + currentDirectory, ex); + } + currentDirectory = currentDirectory.getParentFile(); + } + System.out.printf("Found %d acceptable golden image(s)\n", goldenVariants.size()); + goldenVariants.forEach(System.out::println); + return goldenVariants; + } +} diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImagePath.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImagePath.java deleted file mode 100644 --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/GoldenImagePath.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2010-2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - */ -package test.javaclient.shared.screenshots; - -import java.io.File; -import java.io.IOException; -import test.javaclient.shared.TestUtil; -import test.javaclient.shared.Utils; - -/** - * - * @author shubov - */ - -public class GoldenImagePath { - - private static final String PRISM_ORDER_J2D_FOLDER_NAME = "j2d"; - private static final String DEFAULT_PRISM_ORDER_FOLDER_NAME = "prism"; - private static final String EMBEDDED_DFB_PLATFORM_NAME = "dfb"; - private static final String EMBEDDED_EGLFB_PLATFORM_NAME = "eglfb"; - private static final String DEFAULT_OS_FOLDER_NAME = "seven"; - static final String WIN_8_FOLDER = "eight"; - static final String WIN_8_OS_VERSION = "6.2"; - static final String WIN_81_OS_VERSION = "6.3"; - - - - - private static String getPrismOrderFolderName() { - if (Utils.isJ2D()) { - return PRISM_ORDER_J2D_FOLDER_NAME; - } else { - return DEFAULT_PRISM_ORDER_FOLDER_NAME; - } - } - - private static String getProjectName(final String _baseDir) { - String projectName = null; - try { - projectName = new File(new File(_baseDir).getCanonicalPath()).getName(); - } catch (IOException ex) { - ex.printStackTrace(); - } - return projectName; - } - - public static String getGoldenImagePath(final String _baseDir, - final String _ROOT_IMAGES_DIRECTORY) { - final boolean inUpLevel = true; - return getGoldenImagePathCommon(_baseDir, _ROOT_IMAGES_DIRECTORY , inUpLevel); - } - public static String getGoldenImagePath2(final String _baseDir, - final String _ROOT_IMAGES_DIRECTORY) { - final boolean inUpLevel = false; - return getGoldenImagePathCommon(_baseDir, _ROOT_IMAGES_DIRECTORY , inUpLevel); - } - - private static String getGoldenImagePathCommon(final String _baseDir, - final String _ROOT_IMAGES_DIRECTORY, - final boolean inUpLevel - ) { - - final String oldLookAndFeelName = "caspian"; - final String lfProp = System.getProperty("javafx.userAgentStylesheetUrl"); - - String result = _baseDir; - if (inUpLevel) { - result = result + File.separator + ".."; - } - result = result + File.separator + _ROOT_IMAGES_DIRECTORY + File.separator; - - if (Utils.isCaspian()) { - result += oldLookAndFeelName + File.separator; - } - - result += - getProjectName(_baseDir) + File.separator - + getPrismOrderFolderName() + File.separator - + osFolder(); - - return result; - } - - private static String osFolder() { - //For embedded mode, to use special folder - if (TestUtil.isEmbedded()) { - //dfb or eglfb - String embPlatform = TestUtil.getEmbeddedFxPlatform(); - //returns dfb or eglfb - return (embPlatform != null && embPlatform.equals(EMBEDDED_DFB_PLATFORM_NAME) ? EMBEDDED_DFB_PLATFORM_NAME : EMBEDDED_EGLFB_PLATFORM_NAME); - } - - String os = System.getProperty("os.name").toLowerCase(); - String version = System.getProperty("os.version").toLowerCase(); - if (os.indexOf("win") >= 0) { - if (version.startsWith("5.")) { - return "xp"; - } else if (version.startsWith(WIN_8_OS_VERSION) - || version.startsWith(WIN_81_OS_VERSION)) { - return WIN_8_FOLDER; - } else if (version.startsWith("6.")) { - return "seven"; - } - } else if (os.indexOf("mac") >= 0) { - return "mac"; - } else if ((os.indexOf("linux") >= 0) || (os.indexOf("ubuntu") >= 0)) { - return "linux"; - } - return DEFAULT_OS_FOLDER_NAME; - } - - - - -} diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ImagesManager.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ImagesManager.java deleted file mode 100755 --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ImagesManager.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (c) 2010-2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - */ -package test.javaclient.shared.screenshots; - -import java.io.File; -import java.io.IOException; -import test.javaclient.shared.TestUtil; -import test.javaclient.shared.Utils; - -/** - * This singleton manager handles all work with golden screenshots and tests' - * resulting screenshots - * - * @author Sergey - */ -public class ImagesManager { - - /** - * property for disabling search up root directory of images. - */ - private static final boolean IS_NOT_LOOK_UP_IMAGES_MODE = Boolean.getBoolean("test.javaclient.notlookupmode"); - private static final String PREDEFINED_RESULT_PATH = "build" + File.separator; - private static final String IMAGES_OUTPUTPATH_PROP = "imageutils.outputpath"; - private static final String IMAGE_POSTFIX = ".png"; - private static final String HTML_POSTFIX = ".html"; - private static final String DESCRIPTION_POSTFIX = ".dsc"; - // private static final String PRISM_ORDER_PROPERTY = "prism.order"; - private static final String ROOT_IMAGES_DIRECTORY = "images-svn"; - private final static ImagesManager INSTANCE = new ImagesManager(); - private File goldenImagesPath; - private File imagesOutputPath; - private String baseDir = "."; - private String absoluteDir = null; - private String projectName; - private boolean needUpdate = true; - - public ImagesManager() { - needUpdate = true; - } - - public static ImagesManager getInstance() { - return INSTANCE; - } - - /** - * Set's the base directory to for screenshots work. Shouldn't be called for - * desktop run. - * - * @param baseDir base directory, root of the test suite folder - */ - public void setBaseDir(String baseDir) { - System.err.println("Setting baseDir to " + baseDir); - this.baseDir = baseDir; - needUpdate = true; - } - - /** - * Set's the absolute directory to for screenshots work. Shouldn't be called for - * desktop run. - * - * @param absoluteDir absolute directory, where whill be result screenshots - */ - public void setAbsoluteDir(String absoluteDir) { - System.err.println("Setting absoluteDir to " + absoluteDir); - this.absoluteDir = absoluteDir; - needUpdate = true; - } - - /** - * Finds out golden screenshot location based on name and current settings. - * Automatically takes in account current prism.order, operating system and - * suite name. - * - * @param name golden screenshot name without extension - * @return absolute path to searched screenshot or null if nothing found - */ - public String lookupGoldenScreenshot(String name) { - update(); - return handleFileNames(goldenImagesPath, name, IMAGE_POSTFIX, true); - } - - public String lookupGoldenDescription(String name) { - update(); - return handleFileNames(goldenImagesPath, name, DESCRIPTION_POSTFIX, true); - } - - /** - * Return path to image where screenshot should be stored - * - * @param name screenshot name without extension - * @return absolute path to target file - */ - public String getScreenshotPath(String name) { - update(); - return handleFileNames(imagesOutputPath, name, IMAGE_POSTFIX, false); - } - - public String getHTMLPath(String name) { - update(); - return handleFileNames(imagesOutputPath, name, HTML_POSTFIX, false); - } - - public String getDescriptionPath(String name) { - update(); - return handleFileNames(imagesOutputPath, name, DESCRIPTION_POSTFIX, false); - } - - private void update() { - if (needUpdate) { - - // attempt 1 - goldenImagesPath = new File(GoldenImagePath.getGoldenImagePath(baseDir, ROOT_IMAGES_DIRECTORY)); - try { - goldenImagesPath = goldenImagesPath.getCanonicalFile(); - } catch (IOException ex) { - ex.printStackTrace(System.err); - reportWrongPath("golden images", goldenImagesPath); - } - - if (isValidPath(goldenImagesPath)) { - System.out.println("Golden images will be loaded from " + goldenImagesPath.getAbsolutePath()); - } else { - // attempt 2 - goldenImagesPath = new File(GoldenImagePath.getGoldenImagePath2(baseDir, ROOT_IMAGES_DIRECTORY)); - try { - goldenImagesPath = goldenImagesPath.getCanonicalFile(); - } catch (IOException ex) { - ex.printStackTrace(System.err); - reportWrongPath("golden images", goldenImagesPath); - } - if (isValidPath(goldenImagesPath)){ - System.out.println("Golden images will be loaded from " + goldenImagesPath.getAbsolutePath()); - } else { - reportWrongPath("golden images", goldenImagesPath); - } - } - - if (absoluteDir == null) { - - String outImagesPath = System.getProperty(IMAGES_OUTPUTPATH_PROP); //getenv - - imagesOutputPath = outImagesPath == null - ? new File(baseDir + File.separator + PREDEFINED_RESULT_PATH + File.separator) - : new File(outImagesPath + File.separator); - } else { - imagesOutputPath = new File(absoluteDir); - } - try { - imagesOutputPath = imagesOutputPath.getCanonicalFile(); - } catch (IOException ex) { - ex.printStackTrace(System.err); - reportWrongPath("output images", imagesOutputPath); - } - - if (isValidPath(imagesOutputPath)) { - System.out.println("Result images will be saved to " + imagesOutputPath.getAbsolutePath()); - } else { - reportWrongPath("output images", imagesOutputPath); - } - needUpdate = false; - } - - } - - private String handleFileNames(File filePath, String suffix, String image_postfix, boolean doSearch) { - if (null == filePath) { - reportWrongPath("screenshot", filePath); - return null; - } - if (IS_NOT_LOOK_UP_IMAGES_MODE || !doSearch) { - return ( new File(filePath, suffix + image_postfix).getAbsolutePath()); - } - return searchImage(suffix, image_postfix, filePath); - } - - private static String searchImage(String suffix, String image_postfix, File filePath) { - final String imageName = suffix + image_postfix; - File imageFile; - File currentDirectory = filePath; - do { - System.out.println("Go_to " + currentDirectory.getAbsolutePath() + "/ currDir=" + currentDirectory); - imageFile = new File(currentDirectory, imageName); - currentDirectory = currentDirectory.getParentFile(); - } while (!imageFile.exists() && currentDirectory != null && !currentDirectory.getName().equals(ROOT_IMAGES_DIRECTORY)); - return imageFile.getAbsolutePath(); - } - - private static boolean isValidPath(File filePath) { - return filePath != null && filePath.exists() && filePath.isDirectory(); - } - - private static void reportWrongPath(String msg, File filePath) { - System.err.println("Error: Invalid " + msg + " path [" + (filePath == null ? "null" : filePath.getAbsolutePath()) - + "] Please, refer to README_dev.html for information about images location."); - } - -} diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ScreenshotUtils.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ScreenshotUtils.java --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ScreenshotUtils.java +++ b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/ScreenshotUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,7 @@ */ package test.javaclient.shared.screenshots; -import java.io.File; -import java.util.ArrayList; +import java.nio.file.Paths; import java.util.LinkedList; import java.util.List; import javafx.scene.Node; @@ -33,32 +32,27 @@ import javafx.stage.Stage; import org.jemmy.Rectangle; import org.jemmy.TimeoutExpiredException; -import org.jemmy.action.GetAction; import org.jemmy.control.Wrap; -import org.jemmy.env.Environment; import org.jemmy.fx.ByWindowType; import org.jemmy.fx.NodeDock; import org.jemmy.fx.Root; import org.jemmy.image.Image; +import org.jemmy.image.ImageLoader; import org.jemmy.interfaces.Parent; -import org.jemmy.timing.State; import org.junit.Assert; import test.javaclient.shared.AbstractTestableApplication; import test.javaclient.shared.JemmyUtils; -import test.javaclient.shared.TestUtil; -import test.javaclient.shared.description.FXSceneTreeBuilder; -import test.javaclient.shared.description.FXSimpleNodeDescription; -import test.javaclient.shared.description.TreeNode; /** - * @author Andrey Glushchenko + * @author Andrey Glushchenko, andrey.rusakov@oracle.com */ public class ScreenshotUtils { - //private static Wrap scene = null; + + private static final List SCREENSHOT_ERRORS = new LinkedList<>(); private static AbstractTestableApplication application; -/** - * Verify or generate golden screenshot for a test depending on - * IS_GOLDEN_MODE status. + + /** + * Verify or generate golden screenshot for a test. * * @param testClass * @param testName test name for a test @@ -67,9 +61,9 @@ public static void checkScreenshot(Class testClass, String testName, Wrap node) { checkScreenshot(testClass.getName() + "." + testName, node); } + /** - * Verify or generate golden screenshot for a test depending on - * IS_GOLDEN_MODE status. + * Verify or generate golden screenshot for a test. * * @param testName test name for a test * @param scene scene to take a screenshot of @@ -83,116 +77,69 @@ } public static void checkScreenshot(String testName, final Wrap node, Rectangle rect) { - Image sceneImage; - if (rect != null) { - sceneImage = node.getScreenImage(rect); - } else { - sceneImage = node.getScreenImage(); + ImageLoader imgLoader = node.getEnvironment().getImageLoader(); + List goldenImages = GoldenImageManager.getTestImages(testName, ".png"); + String resultPath = GoldenImageManager.getScreenshotPath(testName); + if (goldenImages.isEmpty()) { + Image sceneImage = (rect == null) ? node.getScreenImage() : node.getScreenImage(rect); + sceneImage.save(resultPath); + throw new RuntimeException("No golden images found for " + testName + + ", actual image saved to " + resultPath); } - String name = ImagesManager.getInstance().lookupGoldenScreenshot(testName); - String descrName = ImagesManager.getInstance().getDescriptionPath(testName); - Assert.assertNotNull("Invalid images path", name); - if (TestUtil.IS_GOLDEN_MODE) { - //TODO: specified directory - sceneImage.save(ImagesManager.getInstance().getScreenshotPath(testName)); - } else { - String diffName = ImagesManager.getInstance().getScreenshotPath(testName + "-diff"); - String resName = ImagesManager.getInstance().getScreenshotPath(testName); - if (TestUtil.IS_DESCRIPTION_MODE) { - String descr_name = ImagesManager.getInstance().lookupGoldenDescription(testName); - if (new File(descr_name).exists()) { - try { - List> list = (List>) TestUtil.read(descr_name); - node.waitState(new State>>() { - public List> reached() { - return FXSceneTreeBuilder.build(node); - } - - @Override - public String toString() { - return "Control having expected image description"; - } - }, list); - } catch (TimeoutExpiredException e) { - if (diffName != null) { - node.getEnvironment().getOutput(Wrap.OUTPUT).println("Saving difference to " + diffName); - final Image diffImage = node.getScreenImage().compareTo(node.getEnvironment().getImageLoader().load(name)); - if (diffImage != null) { - diffImage.save(diffName); - } - } - throw e; - } finally { - if (resName != null) { - node.getEnvironment().getOutput(Wrap.OUTPUT).println("Saving result to " + resName); - node.getScreenImage().save(resName); - } - } + boolean nothingMatched = true; + for (int i = 0; i < goldenImages.size(); i++) { + String goldenPath = goldenImages.get(i); + String goldenName = Paths.get(goldenPath).getFileName().toString() + .replaceFirst("\\.[^\\.]*$", String.format("-%02d", i)); + String diffPath = GoldenImageManager.getScreenshotPath(goldenName + "-diff"); + Image image = imgLoader.load(goldenPath); + try { + if (rect != null) { + node.waitImage(image, rect, resultPath, diffPath); } else { - Assert.fail("Golden image description (" + descr_name + ") not found. Actual image is saved as " + descrName); - ArrayList> description = FXSceneTreeBuilder.build(node); - TestUtil.write(description, descrName); + node.waitImage(image, resultPath, diffPath); } - } else { - if (new File(name).exists()) { - try { - if (rect != null) { - node.waitImage(node.getEnvironment().getImageLoader().load(name), rect, resName, diffName); - } else { - node.waitImage(node.getEnvironment().getImageLoader().load(name), resName, diffName); - } - } catch (TimeoutExpiredException imagesAreDifferentException) { - //If images are really different, then not only diff and - //original images are to put in the results folder, but also golden image. - try { - String expectedName = ImagesManager.getInstance().getScreenshotPath(testName + "-expected"); - node.getEnvironment().getImageLoader().load(name).save(expectedName); - } catch (Throwable ex) { - ex.printStackTrace(); - } - throw imagesAreDifferentException; - } - if (TestUtil.IS_DESCRIBING_MODE) { - ArrayList> description = FXSceneTreeBuilder.build(node); - TestUtil.write(description, descrName); - } - } else { - sceneImage.save(resName); - Assert.fail("Golden image (" + name + ") not found. Actual image is saved as " + resName); + nothingMatched = false; + break; + } catch (TimeoutExpiredException imagesAreDifferentException) { + try { + String expectedName = GoldenImageManager.getScreenshotPath(goldenName + "-expected"); + imgLoader.load(goldenPath).save(expectedName); + } catch (Throwable ex) { + System.err.println(ex); } } } + if (nothingMatched) { + throw new TimeoutExpiredException("Control having expected image has not been reached"); + } } - private static List screenshotErrors = new LinkedList(); public static void setApplication(AbstractTestableApplication application) { ScreenshotUtils.application = application; } + public static void setScene(Wrap scene) { - // ScreenshotUtils.scene = scene; + // ScreenshotUtils.scene = scene; } - - private static void error(Throwable ex){ - screenshotErrors.add(ex); + + private static void error(Throwable ex) { + SCREENSHOT_ERRORS.add(ex); ex.printStackTrace(); } - /** - * Set image comparator distance. Should be called from constructor or {} - * class initialization block. - * - * @param comparatorDistance distance - */ + public static void setComparatorDistance(float comparatorDistance) { JemmyUtils.comparatorDistance = comparatorDistance; } - public static void throwScreenshotErrors(){ + + public static void throwScreenshotErrors() { StringBuilder sb = new StringBuilder(); - for(Throwable ex: screenshotErrors){ + for (Throwable ex : SCREENSHOT_ERRORS) { sb.append(ex.toString()).append("\n"); } - screenshotErrors.clear(); + SCREENSHOT_ERRORS.clear(); String msg = sb.toString(); - if(!"".equals(msg) && msg!=null){ + if (!"".equals(msg) && msg != null) { Assert.fail(msg); } } @@ -203,30 +150,30 @@ public static void checkPageContentScreenshot(String name, boolean valuable_rect) { try { - if(application.getNodeForScreenshot()==null){ - final Wrap scene = Root.ROOT.lookup(new ByWindowType(Stage.class)).lookup(Scene.class).wrap(0); + if (application.getNodeForScreenshot() == null) { + final Wrap scene = Root.ROOT.lookup(new ByWindowType(Stage.class)).lookup(Scene.class).wrap(0); - checkScreenshot(name, scene); - }else{ - if (valuable_rect) { + checkScreenshot(name, scene); + } else if (valuable_rect) { checkScreenshot(name, getPageContent(), getPageContentSize()); } else { checkScreenshot(name, getPageContent()); } - } } catch (Throwable th) { error(th); } } - public static Wrap getPageContent() { + + public static Wrap getPageContent() { Node n = application.getNodeForScreenshot(); final Wrap scene = Root.ROOT.lookup(new ByWindowType(Stage.class)).lookup(Scene.class).wrap(0); return new NodeDock(scene.as(Parent.class, Node.class), n.getId()).wrap(); } + /** * - * @return rectangle contains all children of page content. This rectangle - * may have different size then size of Content. + * @return rectangle contains all children of page content. This rectangle may have different + * size then size of Content. */ public static Rectangle getPageContentSize() { Node node = getPageContent().getControl(); @@ -241,5 +188,5 @@ } return rect; } - + } diff --git a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/SerializableBufferedImage.java b/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/SerializableBufferedImage.java deleted file mode 100755 --- a/tools/SharedTestUtilsOpen/src/test/javaclient/shared/screenshots/SerializableBufferedImage.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - */ -package test.javaclient.shared.screenshots; - -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.Serializable; -import javax.imageio.ImageIO; - -public class SerializableBufferedImage implements Serializable { - - private byte[] byteImage = null; - - public SerializableBufferedImage(BufferedImage bufferedImage) { - this.byteImage = toByteArray(bufferedImage); - } - - public BufferedImage getBufferedImage() { - return fromByteArray(byteImage); - } - - private BufferedImage fromByteArray(byte[] bytes) { - try { - if (bytes != null && (bytes.length > 0)) { - return ImageIO.read(new ByteArrayInputStream(bytes)); - } - return null; - } catch (IOException e) { - throw new IllegalArgumentException(e.toString()); - } - } - - private byte[] toByteArray(BufferedImage image) { - if (image != null) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - ImageIO.write(image, "png", baos); - } catch (IOException e) { - throw new IllegalStateException(e.toString()); - } - byte[] b = baos.toByteArray(); - return b; - } - return new byte[0]; - } -} \ No newline at end of file