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