1 /* 2 * Copyright (c) 2010-2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 */ 5 package test.javaclient.shared.screenshots; 6 7 import java.io.File; 8 import java.io.IOException; 9 import test.javaclient.shared.TestUtil; 10 import test.javaclient.shared.Utils; 11 12 /** 13 * This singleton manager handles all work with golden screenshots and tests' 14 * resulting screenshots 15 * 16 * @author Sergey 17 */ 18 public class ImagesManager { 19 20 /** 21 * property for disabling search up root directory of images. 22 */ 23 private static final boolean IS_NOT_LOOK_UP_IMAGES_MODE = Boolean.getBoolean("test.javaclient.notlookupmode"); 24 private static final String PREDEFINED_RESULT_PATH = "build" + File.separator; 25 private static final String IMAGES_OUTPUTPATH_PROP = "imageutils.outputpath"; 26 private static final String IMAGE_POSTFIX = ".png"; 27 private static final String HTML_POSTFIX = ".html"; 28 private static final String DESCRIPTION_POSTFIX = ".dsc"; 29 // private static final String PRISM_ORDER_PROPERTY = "prism.order"; 30 private static final String ROOT_IMAGES_DIRECTORY = "images-svn"; 31 private final static ImagesManager INSTANCE = new ImagesManager(); 32 private File goldenImagesPath; 33 private File imagesOutputPath; 34 private String baseDir = "."; 35 private String absoluteDir = null; 36 private String projectName; 37 private boolean needUpdate = true; 38 39 public ImagesManager() { 40 needUpdate = true; 41 } 42 43 public static ImagesManager getInstance() { 44 return INSTANCE; 45 } 46 47 /** 48 * Set's the base directory to for screenshots work. Shouldn't be called for 49 * desktop run. 50 * 51 * @param baseDir base directory, root of the test suite folder 52 */ 53 public void setBaseDir(String baseDir) { 54 System.err.println("Setting baseDir to " + baseDir); 55 this.baseDir = baseDir; 56 needUpdate = true; 57 } 58 59 /** 60 * Set's the absolute directory to for screenshots work. Shouldn't be called for 61 * desktop run. 62 * 63 * @param absoluteDir absolute directory, where whill be result screenshots 64 */ 65 public void setAbsoluteDir(String absoluteDir) { 66 System.err.println("Setting absoluteDir to " + absoluteDir); 67 this.absoluteDir = absoluteDir; 68 needUpdate = true; 69 } 70 71 /** 72 * Finds out golden screenshot location based on name and current settings. 73 * Automatically takes in account current prism.order, operating system and 74 * suite name. 75 * 76 * @param name golden screenshot name without extension 77 * @return absolute path to searched screenshot or null if nothing found 78 */ 79 public String lookupGoldenScreenshot(String name) { 80 update(); 81 return handleFileNames(goldenImagesPath, name, IMAGE_POSTFIX, true); 82 } 83 84 public String lookupGoldenDescription(String name) { 85 update(); 86 return handleFileNames(goldenImagesPath, name, DESCRIPTION_POSTFIX, true); 87 } 88 89 /** 90 * Return path to image where screenshot should be stored 91 * 92 * @param name screenshot name without extension 93 * @return absolute path to target file 94 */ 95 public String getScreenshotPath(String name) { 96 update(); 97 return handleFileNames(imagesOutputPath, name, IMAGE_POSTFIX, false); 98 } 99 100 public String getHTMLPath(String name) { 101 update(); 102 return handleFileNames(imagesOutputPath, name, HTML_POSTFIX, false); 103 } 104 105 public String getDescriptionPath(String name) { 106 update(); 107 return handleFileNames(imagesOutputPath, name, DESCRIPTION_POSTFIX, false); 108 } 109 110 private void update() { 111 if (needUpdate) { 112 113 // attempt 1 114 goldenImagesPath = new File(GoldenImagePath.getGoldenImagePath(baseDir, ROOT_IMAGES_DIRECTORY)); 115 try { 116 goldenImagesPath = goldenImagesPath.getCanonicalFile(); 117 } catch (IOException ex) { 118 ex.printStackTrace(System.err); 119 reportWrongPath("golden images", goldenImagesPath); 120 } 121 122 if (isValidPath(goldenImagesPath)) { 123 System.out.println("Golden images will be loaded from " + goldenImagesPath.getAbsolutePath()); 124 } else { 125 // attempt 2 126 goldenImagesPath = new File(GoldenImagePath.getGoldenImagePath2(baseDir, ROOT_IMAGES_DIRECTORY)); 127 try { 128 goldenImagesPath = goldenImagesPath.getCanonicalFile(); 129 } catch (IOException ex) { 130 ex.printStackTrace(System.err); 131 reportWrongPath("golden images", goldenImagesPath); 132 } 133 if (isValidPath(goldenImagesPath)){ 134 System.out.println("Golden images will be loaded from " + goldenImagesPath.getAbsolutePath()); 135 } else { 136 reportWrongPath("golden images", goldenImagesPath); 137 } 138 } 139 140 if (absoluteDir == null) { 141 142 String outImagesPath = System.getProperty(IMAGES_OUTPUTPATH_PROP); //getenv 143 144 imagesOutputPath = outImagesPath == null 145 ? new File(baseDir + File.separator + PREDEFINED_RESULT_PATH + File.separator) 146 : new File(outImagesPath + File.separator); 147 } else { 148 imagesOutputPath = new File(absoluteDir); 149 } 150 try { 151 imagesOutputPath = imagesOutputPath.getCanonicalFile(); 152 } catch (IOException ex) { 153 ex.printStackTrace(System.err); 154 reportWrongPath("output images", imagesOutputPath); 155 } 156 157 if (isValidPath(imagesOutputPath)) { 158 System.out.println("Result images will be saved to " + imagesOutputPath.getAbsolutePath()); 159 } else { 160 reportWrongPath("output images", imagesOutputPath); 161 } 162 needUpdate = false; 163 } 164 165 } 166 167 private String handleFileNames(File filePath, String suffix, String image_postfix, boolean doSearch) { 168 if (null == filePath) { 169 reportWrongPath("screenshot", filePath); 170 return null; 171 } 172 if (IS_NOT_LOOK_UP_IMAGES_MODE || !doSearch) { 173 return ( new File(filePath, suffix + image_postfix).getAbsolutePath()); 174 } 175 return searchImage(suffix, image_postfix, filePath); 176 } 177 178 private static String searchImage(String suffix, String image_postfix, File filePath) { 179 final String imageName = suffix + image_postfix; 180 File imageFile; 181 File currentDirectory = filePath; 182 do { 183 System.out.println("Go_to " + currentDirectory.getAbsolutePath() + "/ currDir=" + currentDirectory); 184 imageFile = new File(currentDirectory, imageName); 185 currentDirectory = currentDirectory.getParentFile(); 186 } while (!imageFile.exists() && currentDirectory != null && !currentDirectory.getName().equals(ROOT_IMAGES_DIRECTORY)); 187 return imageFile.getAbsolutePath(); 188 } 189 190 private static boolean isValidPath(File filePath) { 191 return filePath != null && filePath.exists() && filePath.isDirectory(); 192 } 193 194 private static void reportWrongPath(String msg, File filePath) { 195 System.err.println("Error: Invalid " + msg + " path [" + (filePath == null ? "null" : filePath.getAbsolutePath()) 196 + "] Please, refer to README_dev.html for information about images location."); 197 } 198 199 }