1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.jemmy.image; 26 27 28 import java.awt.EventQueue; 29 import java.awt.Frame; 30 import java.io.File; 31 import java.io.IOException; 32 import java.lang.reflect.InvocationTargetException; 33 import org.jemmy.TimeoutExpiredException; 34 import org.jemmy.control.Wrap; 35 import org.jemmy.env.Environment; 36 import org.jemmy.env.Timeout; 37 import org.jemmy.operators.AWTScreen; 38 import org.jemmy.operators.Screen; 39 import org.testng.annotations.AfterClass; 40 import org.testng.annotations.AfterMethod; 41 import org.testng.annotations.BeforeClass; 42 import org.testng.annotations.BeforeMethod; 43 import org.testng.annotations.Test; 44 45 import static org.testng.Assert.assertTrue; 46 import static org.testng.Assert.fail; 47 import static org.testng.AssertJUnit.assertEquals; 48 49 /** 50 * 51 * @author shura 52 */ 53 public class ScreenImageTest { 54 public static final String GOLDEN = "golden.png"; 55 56 public ScreenImageTest() { 57 } 58 59 static FilesystemImageLoader loader; 60 61 @BeforeClass 62 public static void setUpClass() throws Exception { 63 Environment.getEnvironment().setImageCapturer(new AWTRobotCapturer()); 64 File workdir = new File(System.getProperty("user.dir") + File.separator + 65 "build" + File.separator + 66 "test" + File.separator + 67 "results"); 68 workdir.mkdirs(); 69 loader = new FilesystemImageLoader(); 70 loader.setImageRoot(workdir); 71 AWTImage.setImageRoot(workdir); 72 Screen.setSCREEN(new AWTScreen()); 73 Screen.SCREEN.getEnvironment().setTimeout(new Timeout(Wrap.WAIT_STATE_TIMEOUT.getName(), 10000)); 74 System.out.println("Saving data to " + AWTImage.getImageRoot().getAbsolutePath()); 75 } 76 77 @AfterClass 78 public static void tearDownClass() throws Exception { 79 } 80 81 @BeforeMethod 82 public void setUp() throws IOException { 83 Screen.SCREEN.getScreenImage().save(GOLDEN); 84 } 85 86 @AfterMethod 87 public void tearDown() { 88 } 89 90 /** 91 * This test usually fails because it compares the whole screen where 92 * changes usually happen between screenshots. 93 */ 94 @Test 95 public void compareFull() { 96 try { 97 Screen.SCREEN.waitImage(loader.load(GOLDEN), "positive.png", "positive_diff.png"); 98 } catch(TimeoutExpiredException e) { 99 e.printStackTrace(); 100 fail("compareFull failed, see positive.png and positive_diff.png for details"); 101 } 102 } 103 104 @Test 105 public void compareNegative() throws InterruptedException, InvocationTargetException { 106 final Frame frm = new Frame("some frame"); 107 EventQueue.invokeAndWait(new Runnable() { 108 109 public void run() { 110 frm.setSize(100, 100); 111 frm.setVisible(true); 112 } 113 }); 114 //note - if you will be running test from netbeans you would also get a difference 115 //from JUnit execution progress. Which is fine :) 116 try { 117 Screen.SCREEN.waitImage(loader.load(GOLDEN), "negative.png", "negative_diff.png"); 118 fail("compareFull failed, see negative.png and negative_diff.png for details"); 119 } catch(TimeoutExpiredException e) { 120 } 121 frm.setVisible(false); 122 assertTrue(new File(AWTImage.getImageRoot(), "negative_diff.png").exists()); 123 AWTImage res = (AWTImage) loader.load("negative.png"); 124 AWTImage diff = (AWTImage) loader.load("negative_diff.png"); 125 assertEquals(res.getTheImage().getWidth(), diff.getTheImage().getWidth()); 126 assertEquals(res.getTheImage().getHeight(), diff.getTheImage().getHeight()); 127 } 128 129 }