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.awt; 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.image.awt.AWTImage; 38 import org.jemmy.image.awt.AWTRobotCapturer; 39 import org.jemmy.image.awt.FilesystemImageLoader; 40 import org.jemmy.operators.awt.AWTScreen; 41 import org.jemmy.operators.Screen; 42 import org.testng.annotations.AfterClass; 43 import org.testng.annotations.AfterMethod; 44 import org.testng.annotations.BeforeClass; 45 import org.testng.annotations.BeforeMethod; 46 import org.testng.annotations.Test; 47 48 import static org.testng.Assert.assertTrue; 49 import static org.testng.Assert.fail; 50 import static org.testng.AssertJUnit.assertEquals; 51 52 /** 53 * 54 * @author shura 55 */ 56 public class ScreenImageTest { 57 public static final String GOLDEN = "golden.png"; 58 59 public ScreenImageTest() { 60 } 61 62 static FilesystemImageLoader loader; 63 64 @BeforeClass 65 public static void setUpClass() throws Exception { 66 Environment.getEnvironment().setImageCapturer(new AWTRobotCapturer()); 67 File workdir = new File(System.getProperty("user.dir") + File.separator + 68 "build" + File.separator + 69 "test" + File.separator + 70 "results"); 71 workdir.mkdirs(); 72 loader = new FilesystemImageLoader(); 73 loader.setImageRoot(workdir); 74 AWTImage.setImageRoot(workdir); 75 Screen.setSCREEN(new AWTScreen()); 76 Screen.SCREEN.getEnvironment().setTimeout(new Timeout(Wrap.WAIT_STATE_TIMEOUT.getName(), 10000)); 77 System.out.println("Saving data to " + AWTImage.getImageRoot().getAbsolutePath()); 78 } 79 80 @AfterClass 81 public static void tearDownClass() throws Exception { 82 } 83 84 @BeforeMethod 85 public void setUp() throws IOException { 86 Screen.SCREEN.getScreenImage().save(GOLDEN); 87 } 88 89 @AfterMethod 90 public void tearDown() { 91 } 92 93 /** 94 * This test usually fails because it compares the whole screen where 95 * changes usually happen between screenshots. 96 */ 97 @Test 98 public void compareFull() { 99 try { 100 Screen.SCREEN.waitImage(loader.load(GOLDEN), "positive.png", "positive_diff.png"); 101 } catch(TimeoutExpiredException e) { 102 e.printStackTrace(); 103 fail("compareFull failed, see positive.png and positive_diff.png for details"); 104 } 105 } 106 107 @Test 108 public void compareNegative() throws InterruptedException, InvocationTargetException { 109 final Frame frm = new Frame("some frame"); 110 EventQueue.invokeAndWait(new Runnable() { 111 112 public void run() { 113 frm.setSize(100, 100); 114 frm.setVisible(true); 115 } 116 }); 117 //note - if you will be running test from netbeans you would also get a difference 118 //from JUnit execution progress. Which is fine :) 119 try { 120 Screen.SCREEN.waitImage(loader.load(GOLDEN), "negative.png", "negative_diff.png"); 121 fail("compareFull failed, see negative.png and negative_diff.png for details"); 122 } catch(TimeoutExpiredException e) { 123 } 124 frm.setVisible(false); 125 assertTrue(new File(AWTImage.getImageRoot(), "negative_diff.png").exists()); 126 AWTImage res = (AWTImage) loader.load("negative.png"); 127 AWTImage diff = (AWTImage) loader.load("negative_diff.png"); 128 assertEquals(res.getTheImage().getWidth(), diff.getTheImage().getWidth()); 129 assertEquals(res.getTheImage().getHeight(), diff.getTheImage().getHeight()); 130 } 131 132 }