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> todo document
  69      * @param parent todo document
  70      * @param controlType todo document
  71      * @param index todo document
  72      * @param criteria todo document
  73      * @return todo document
  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      * @return Wrap instance obtainer through lookup
  98      */
  99     public Object control() {
 100         return wrap.getControl();
 101     }
 102 
 103     /**
 104      * @return Shortcut to <code>wrap().mouse()</code>
 105      */
 106     public Mouse mouse() {
 107         return wrap.mouse();
 108     }
 109 
 110     /**
 111      * @return Shortcut to <code>wrap().keyboard()</code>
 112      */
 113     public Keyboard keyboard() {
 114         return wrap.keyboard();
 115     }
 116 
 117     /**
 118      * @return Shortcut to <code>wrap().drag()</code>
 119      */
 120     public Drag drag() {
 121         return wrap.drag();
 122     }
 123 
 124     /**
 125      * @return Shortcut to <code>wrap().getScreenBounds()</code>
 126      */
 127     public Rectangle bounds() {
 128         return wrap.getScreenBounds();
 129     }
 130 
 131     protected <P> P getProperty(GetAction<P> action) {
 132         action.execute();
 133         return action.getResult();
 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      * @param goldenId todo document
 147      * @param rect todo document
 148      * @param resID todo document
 149      * @param diffID todo document
 150      */
 151     public void waitImage(String goldenId, Rectangle rect, String resID, String diffID) {
 152         wrap.waitImage(environment().getImageLoader().load(goldenId), rect, resID, diffID);
 153     }
 154 
 155     /**
 156      * Constructs names for diff and result images and waits for the control to match it.
 157      * Diff and result names
 158      * constructed by adding suffixes. Suffixes are obtained from environment with
 159      * default values being &quot;-diff&quot; and &quot;-result&quot;
 160      * @see #waitImage(java.lang.String, org.jemmy.Rectangle, java.lang.String, java.lang.String)
 161      * @see #DEFAULT_DIFF_IMAGE_SUFFIX
 162      * @see #DEFAULT_RESULT_IMAGE_SUFFIX
 163      * @param goldenId todo document
 164      * @param rect todo document
 165      */
 166     public void waitImage(String goldenId, Rectangle rect) {
 167         waitImage(goldenId,
 168                 rect,
 169                 goldenId + environment().getProperty(DEFAULT_RESULT_IMAGE_SUFFIX),
 170                 goldenId + environment().getProperty(DEFAULT_DIFF_IMAGE_SUFFIX));
 171     }
 172 
 173     /**
 174      * Loads image with <code>goldenId</code> id waits for the control to match it.
 175      * @see Wrap#waitImage(org.jemmy.image.Image, java.lang.String, java.lang.String)
 176      * @param goldenId todo document
 177      * @param resID todo document
 178      * @param diffID todo document
 179      */
 180     public void waitImage(String goldenId, String resID, String diffID) {
 181         wrap.waitImage(environment().getImageLoader().load(goldenId), resID, diffID);
 182     }
 183 
 184     /**
 185      * Constructs names for diff and result images and waits for the control to match it.
 186      * Diff and result names
 187      * constructed by adding suffixes. Suffixes are obtained from environment with
 188      * default values being &quot;-diff&quot; and &quot;-result&quot;
 189      * @see #waitImage(java.lang.String, java.lang.String, java.lang.String)
 190      * @see #DEFAULT_DIFF_IMAGE_SUFFIX
 191      * @see #DEFAULT_RESULT_IMAGE_SUFFIX
 192      * @param goldenId todo document
 193      */
 194     public void waitImage(String goldenId) {
 195         waitImage(goldenId,
 196                 goldenId + environment().getProperty(DEFAULT_RESULT_IMAGE_SUFFIX),
 197                 goldenId + environment().getProperty(DEFAULT_DIFF_IMAGE_SUFFIX));
 198     }
 199  }