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