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 import java.awt.EventQueue;
  26 import java.awt.Frame;
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.lang.reflect.InvocationTargetException;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 import org.jemmy.Rectangle;
  33 import org.jemmy.TimeoutExpiredException;
  34 import org.jemmy.control.Wrap;
  35 import org.jemmy.env.Environment;
  36 import org.jemmy.operators.AWTScreen;
  37 import org.jemmy.operators.Screen;
  38 import org.testng.annotations.AfterClass;
  39 import org.testng.annotations.AfterMethod;
  40 import org.testng.annotations.BeforeClass;
  41 import org.testng.annotations.BeforeMethod;
  42 import org.testng.annotations.Test;
  43 
  44 import static org.testng.Assert.fail;
  45 
  46 /**
  47  *
  48  * @author shura
  49  */
  50 public class ScreenAreaImageTest {
  51 
  52     public ScreenAreaImageTest() {
  53     }
  54 
  55     static ImageLoader loader;
  56 
  57     @BeforeClass
  58     public static void setUpClass() throws Exception {
  59         Screen.setSCREEN(new AWTScreen());
  60     }
  61 
  62     @AfterClass
  63     public static void tearDownClass() throws Exception {
  64     }
  65     File tmpDump = null;
  66     File tmpDumpPart = null;
  67     Wrap<Object> area;
  68     Frame frm;
  69     Rectangle part = new Rectangle(10, 10, 80, 80);
  70 
  71     @BeforeMethod
  72     public void setUp() throws IOException, InterruptedException {
  73         Environment.getEnvironment().setImageCapturer(new AWTRobotCapturer());
  74         loader = new FilesystemImageLoader();
  75         frm = new Frame("some frame");
  76         frm.setVisible(true);
  77         frm.setSize(100, 100);
  78         frm.setLocation(100, 100);
  79         area = new Wrap<Object>(Environment.getEnvironment(), "screen area") {
  80 
  81             @Override
  82             public Rectangle getScreenBounds() {
  83                 return new Rectangle(100, 100, 100, 100);
  84             }
  85         };
  86         // Added timeout to prevent failures due to Windows Vista Visual Effects
  87         Thread.sleep(1000);
  88         tmpDump = File.createTempFile("screen", ".png");
  89         tmpDumpPart = File.createTempFile("screenPart", ".png");
  90         area.getScreenImage().save(tmpDump.getAbsolutePath());
  91         area.getScreenImage(part).save(tmpDumpPart.getAbsolutePath());
  92         try {
  93             Thread.sleep(2000);
  94         } catch (InterruptedException ex) {
  95             Logger.getLogger(ScreenImageTest.class.getName()).log(Level.SEVERE, null, ex);
  96         }
  97     }
  98 
  99     @AfterMethod
 100     public void tearDown() {
 101         frm.setVisible(false);
 102     }
 103 
 104     @Test
 105     public void compareFull() {
 106         try {
 107             area.waitImage(loader.load(tmpDump.getAbsolutePath()), null, null);
 108         } catch(TimeoutExpiredException e) {
 109             fail(e.getLocalizedMessage());
 110             throw e;
 111         }
 112     }
 113 
 114     @Test
 115     public void comparePart() {
 116         try {
 117             area.waitImage(loader.load(tmpDumpPart.getAbsolutePath()), part, null, null);
 118         } catch(TimeoutExpiredException e) {
 119             fail(e.getLocalizedMessage());
 120             throw e;
 121         }
 122     }
 123 
 124     @Test
 125     public void compareNegative() throws InterruptedException, InvocationTargetException {
 126         EventQueue.invokeAndWait(new Runnable() {
 127 
 128             public void run() {
 129                 frm.setVisible(false);
 130             }
 131         });
 132         Thread.sleep(100);
 133         try {
 134             area.waitImage(loader.load(tmpDump.getAbsolutePath()), null, null);
 135             // TODO: Test unstable. Sometimes passes
 136             fail();
 137         } catch(TimeoutExpiredException e) {
 138         }
 139     }
 140 }