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.dock;
  24 
  25 import org.jemmy.Rectangle;
  26 import org.jemmy.action.GetAction;
  27 import org.jemmy.control.Wrap;
  28 import org.jemmy.env.Environment;
  29 import org.jemmy.interfaces.Drag;
  30 import org.jemmy.interfaces.Keyboard;
  31 import org.jemmy.interfaces.Mouse;
  32 import org.jemmy.interfaces.Parent;
  33 import org.jemmy.lookup.Lookup;
  34 import org.jemmy.lookup.LookupCriteria;
  35 
  36 /**
  37  * Superclass for all "docks" - classes which simple provide API for lookup, interfaces
  38  * and properties.
  39  * @author shura
  40  */
  41 public class Dock {
  42 
  43     /**
  44      * Default suffix to construct result image name.
  45      */
  46     public static final String DEFAULT_RESULT_IMAGE_SUFFIX = "default.result.image.suffix";
  47     /**
  48      * Default suffix to construct diff image name.
  49      */
  50     public static final String DEFAULT_DIFF_IMAGE_SUFFIX = "default.diff.image.suffix";
  51     
  52     static {
  53         Environment.getEnvironment().setPropertyIfNotSet(DEFAULT_DIFF_IMAGE_SUFFIX, "-diff");
  54         Environment.getEnvironment().setPropertyIfNotSet(DEFAULT_RESULT_IMAGE_SUFFIX, "-result");
  55     }
  56     
  57     private Wrap<?> wrap;
  58 
  59     protected Dock(Wrap<?> wrap) {
  60         this.wrap = wrap;
  61     }
  62 
  63     /**
  64      * Method which at the end actually get called from all dock lookup
  65      * constructors.
  66      * @param <T>
  67      * @param parent
  68      * @param controlType
  69      * @param index
  70      * @param criteria
  71      * @return 
  72      */
  73     protected static <T> Wrap<? extends T> lookup(Parent<? super T> parent, Class<T> controlType, int index, LookupCriteria<T>... criteria) {
  74         Lookup<T> lookup;
  75         if (criteria.length > 0) {
  76             lookup = parent.lookup(controlType, criteria[0]);
  77             for (int i = 1; i < criteria.length; i++) {
  78                 lookup = lookup.lookup(controlType, criteria[i]);
  79             }
  80         } else {
  81             lookup = parent.lookup(controlType);
  82         }
  83         return lookup.wrap(index);
  84     }
  85 
  86     /**
  87      * 
  88      * @return Wrap instance obtainer through lookup 
  89      */
  90     public Wrap<?> wrap() {
  91         return wrap;
  92     }
  93     
  94     /**
  95      * 
  96      * @return Wrap instance obtainer through lookup 
  97      */
  98     public Object control() {
  99         return wrap.getControl();
 100     }
 101     
 102     /**
 103      * Shortcut to <code>wrap().mouse()</code> 
 104      */
 105     public Mouse mouse() {
 106         return wrap.mouse();
 107     }
 108 
 109     /**
 110      * Shortcut to <code>wrap().keyboard()</code> 
 111      */
 112     public Keyboard keyboard() {
 113         return wrap.keyboard();
 114     }
 115 
 116     /**
 117      * Shortcut to <code>wrap().drag()</code> 
 118      */
 119     public Drag drag() {
 120         return wrap.drag();
 121     }
 122 
 123     /**
 124      * Shortcut to <code>wrap().getScreenBounds()</code> 
 125      */
 126     public Rectangle bounds() {
 127         return wrap.getScreenBounds();
 128     }
 129     
 130     protected <P> P getProperty(GetAction<P> action) {
 131         action.execute();
 132         return action.getResult();
 133     }
 134     
 135     /**
 136      * 
 137      * @return <code>wrap().getEnvironment()</code>. 
 138      */
 139     public Environment environment() {
 140         return wrap.getEnvironment();
 141     }
 142     
 143     /**
 144      * Loads image with <code>goldenId</code> id waits for the control to match it.
 145      * @see Wrap#waitImage(org.jemmy.image.Image, org.jemmy.Rectangle, java.lang.String, java.lang.String) 
 146      */
 147     public void waitImage(String goldenId, Rectangle rect, String resID, String diffID) {
 148         wrap.waitImage(environment().getImageLoader().load(goldenId), rect, resID, diffID);
 149     }
 150 
 151     /**
 152      * Constructs names for diff and result images and waits for the control to match it.
 153      * Diff and result names 
 154      * constructed by adding suffixes. Suffixes are obtained from environment with
 155      * default values being &quot;-diff&quot; and &quot;-result&quot;
 156      * @see #waitImage(java.lang.String, org.jemmy.Rectangle, java.lang.String, java.lang.String) 
 157      * @see #DEFAULT_DIFF_IMAGE_SUFFIX
 158      * @see #DEFAULT_RESULT_IMAGE_SUFFIX
 159      */
 160     public void waitImage(String goldenId, Rectangle rect) {
 161         waitImage(goldenId,
 162                 rect, 
 163                 goldenId + environment().getProperty(DEFAULT_RESULT_IMAGE_SUFFIX),
 164                 goldenId + environment().getProperty(DEFAULT_DIFF_IMAGE_SUFFIX));
 165     }
 166     
 167     /**
 168      * Loads image with <code>goldenId</code> id waits for the control to match it.
 169      * @see Wrap#waitImage(org.jemmy.image.Image, java.lang.String, java.lang.String) 
 170      */
 171     public void waitImage(String goldenId, String resID, String diffID) {
 172         wrap.waitImage(environment().getImageLoader().load(goldenId), resID, diffID);
 173     }
 174 
 175     /**
 176      * Constructs names for diff and result images and waits for the control to match it.
 177      * Diff and result names 
 178      * constructed by adding suffixes. Suffixes are obtained from environment with
 179      * default values being &quot;-diff&quot; and &quot;-result&quot;
 180      * @see #waitImage(java.lang.String, java.lang.String, java.lang.String) 
 181      * @see #DEFAULT_DIFF_IMAGE_SUFFIX
 182      * @see #DEFAULT_RESULT_IMAGE_SUFFIX
 183      */
 184     public void waitImage(String goldenId) {
 185         waitImage(goldenId,
 186                 goldenId + environment().getProperty(DEFAULT_RESULT_IMAGE_SUFFIX),
 187                 goldenId + environment().getProperty(DEFAULT_DIFF_IMAGE_SUFFIX));
 188     }
 189  }