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