Ignore:
Timestamp:
01/30/15 18:09:47 (9 years ago)
Author:
funger
Message:
  • clean up
  • comments improved
  • add name, implement getSimilartiy, correct equals and hashCode in ANDROIDGUIElementSpec
  • add get and set methods to ANDROIDGUIElement
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-android/src/main/java/de/ugoe/cs/autoquest/plugin/android/guimodel/ANDROIDGUIElementSpec.java

    r1819 r1869  
    3131    /** 
    3232     * <p> 
    33      * default serial version UID 
     33     * Default serial version UID 
    3434     * </p> 
    3535     */ 
     
    3737 
    3838    /* 
    39      * (non-Javadoc) see de.ugoe.cs.autoquest.androidmonitor.AndroidmonitorLogFile#logComponent() 
     39     * (non-Javadoc) 
     40     *  
     41     * @see de.ugoe.cs.autoquest.androidmonitor.AndroidmonitorLogFile#logComponent() 
    4042     */ 
    4143    /** 
     
    4648     * </p> 
    4749     */ 
    48     private int hashCode; 
     50    private int elementHash; 
    4951 
    5052    /** 
     
    5658    private String path; 
    5759 
    58     /** 
    59      * <p> 
    60      * id of the object as it is returned by view.getId() 
     60    /* 
     61     * (non-Javadoc) 
     62     *  
     63     * @see http://developer.android.com/reference/android/view/View.html#getId() 
     64     */ 
     65    /** 
     66     * <p> 
     67     * Id of the object as it is returned by view.getId(). 
    6168     * </p> 
    6269     */ 
     
    6572    /** 
    6673     * <p> 
    67      * the type of GUI element represented by this specification, which is usually the java class of 
    68      * the android GUI element 
     74     * Current name of the GUI element 
     75     * </p> 
     76     */ 
     77    private String name; 
     78 
     79    /** 
     80     * <p> 
     81     * The type of GUI element, i.e., the class of the android GUI element. 
    6982     * </p> 
    7083     */ 
     
    7386    /** 
    7487     * <p> 
    75      * Type hierarchy of the class itself 
     88     * Type hierarchy of the class itself. 
    7689     * </p> 
    7790     */ 
    7891    private List<String> typeHierarchy = null; 
     92     
     93    /* 
     94     * (non-Javadoc) 
     95     *  
     96     * @see 
     97     * de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#getSecificationSimilarity(IGUIElementSpec 
     98     * ) 
     99     */ 
     100    @Override 
     101    public boolean getSimilarity(IGUIElementSpec other) { 
     102        if (this == other) { 
     103            return true; 
     104        } 
     105 
     106        if (!(other instanceof ANDROIDGUIElementSpec)) { 
     107            return false; 
     108        } 
     109 
     110        ANDROIDGUIElementSpec otherSpec = (ANDROIDGUIElementSpec) other; 
     111 
     112        if (type == null ? otherSpec.type != null : !type.equals(otherSpec.type)) { 
     113            return false; 
     114        } 
     115 
     116        /* 
     117         * Up to now, we compared, if the basics match. Due to testing with different virtual 
     118         * devices it seems to be the case that the id of a view (named index here) keeps the same 
     119         * even on different devices even if the hashCode changes. Some of the GUI elements does not 
     120         * have an id (id is -1). 
     121         */ 
     122 
     123        if (otherSpec.index > 1 && index == otherSpec.index) { 
     124            return true; 
     125        } 
     126 
     127        /* 
     128         * Two elements could also be similar if the path of the elements is equal and the name is 
     129         * set, equal and not equal to "image:" even if index <= 1. This comparison is not 
     130         * implemented up to know due to the reason that all recorded elements up to 2015/01 either 
     131         * have an index > 1 or no name to be compared. 
     132         *  
     133         * In all other cases up to know it is not clear if two elements are similar. 
     134         *  
     135         * Not working: 
     136         *  
     137         * - Position of the elements: Due to the reason that there are a lot of different displays 
     138         * on the android market and the in the most cases the layout depends on the display size 
     139         * (different resolutions) similar elements have different positions. 
     140         */ 
     141 
     142        return false; 
     143    } 
     144     
    79145 
    80146    /* 
     
    102168            return typeHierarchy.toArray(new String[typeHierarchy.size()]); 
    103169    } 
    104  
    105     @Override 
    106     public boolean getSimilarity(IGUIElementSpec other) { 
     170     
     171    /** 
     172     * <p> 
     173     * Returns the object hash of the specified GUI element. 
     174     * </p> 
     175     *  
     176     * @return the elementHash 
     177     */ 
     178    public int getElementHash() { 
     179        return elementHash; 
     180    } 
     181 
     182    /** 
     183     * <p> 
     184     * Returns the path associated with the specified GUI element. 
     185     * </p> 
     186     *  
     187     * @return the path to an element 
     188     */ 
     189    public String getPath() { 
     190        return path; 
     191    } 
     192 
     193    /** 
     194     * <p> 
     195     * Returns the GUI element identifier. 
     196     * </p> 
     197     *  
     198     * @return identifier of the GUI element 
     199     */ 
     200    public int getIndex() { 
     201        return index; 
     202    } 
     203 
     204    /** 
     205     * <p> 
     206     * Returns the name of the specified GUI element. Displayed text in the application or image 
     207     * name. 
     208     * </p> 
     209     *  
     210     * @return text or image of the GUI element. 
     211     */ 
     212    public String getName() {         
     213        if (name == null || name.trim().length() == 0) { 
     214            return "NOT_SET"; 
     215        } 
     216        return name; 
     217    } 
     218 
     219    /** 
     220     * <p> 
     221     * Sets the GUI element identifier. 
     222     * </p> 
     223     *  
     224     * @param indentifier 
     225     */ 
     226    public void setIndex(int index) { 
     227        this.index = index; 
     228    } 
     229 
     230    /** 
     231     * Set the hash code associated with the GUI element. 
     232     *  
     233     * @param hash 
     234     *            the hash of an element object 
     235     */ 
     236    public void setElementHash(int hash) { 
     237        this.elementHash = hash; 
     238    } 
     239 
     240    /** 
     241     * Set the path associated with the specified GUI element. 
     242     *  
     243     * @param path 
     244     *            the path to an element 
     245     */ 
     246    public void setPath(String path) { 
     247        this.path = path; 
     248    } 
     249 
     250    /** 
     251     * <p> 
     252     * Sets the type of the specified GUI element. 
     253     * </p> 
     254     *  
     255     * @param type 
     256     *            the type 
     257     */ 
     258    public void setType(String type) { 
     259        this.type = type; 
     260    } 
     261 
     262    /** 
     263     * <p> 
     264     * Sets the name of the specified GUI element. Displayed text in the application or image name. 
     265     * </p> 
     266     *  
     267     * @param name 
     268     *            the name 
     269     */ 
     270    public void setName(String name) { 
     271        this.name = name; 
     272    } 
     273 
     274    /** 
     275     * <p> 
     276     * Sets the type hierarchy of the specified GUI element. 
     277     *  
     278     * @param typeHierarchy 
     279     *            </p> 
     280     */ 
     281    public void setTypeHierarchy(List<String> typeHierarchy) { 
     282        this.typeHierarchy = typeHierarchy; 
     283    } 
     284     
     285    /* 
     286     * (non-Javadoc) 
     287     *  
     288     * @see de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElementSpec#equals(IGUIElementSpec) 
     289     */ 
     290    @Override 
     291    public boolean equals(Object other) { 
    107292        if (this == other) { 
    108293            return true; 
     
    113298        } 
    114299 
    115         // Check wheter view.id() keeps the same even if something in the 
    116         // structure changes. The hash in the JFCMonitor seems to be unique at 
    117         // all. In the Androidmonitore the hash of an element changes even from 
    118         // one start of the activity to another. 
     300        ANDROIDGUIElementSpec otherSpec = (ANDROIDGUIElementSpec) other; 
     301 
     302        return (elementHash == otherSpec.elementHash) && 
     303            (path == null ? otherSpec.path == null : path.equals(otherSpec.path)) && 
     304            (index == otherSpec.index) && 
     305            (name == null ? otherSpec.name == null : name.equals(otherSpec.name)) && 
     306            (type == null ? otherSpec.type == null : type.equals(otherSpec.type)); 
     307    } 
     308     
     309    /* 
     310     * (non-Javadoc) 
     311     *  
     312     * @see java.lang.Object#hashCode() 
     313     */ 
     314    @Override 
     315    public int hashCode() { 
     316     // 17 due to the reason that this is a prime number. 
     317        int result = 17; 
    119318        /* 
    120          * Maybe some other comparisons will be necessary in the future. 
     319         * 31 due to the reason that a lot of VM's could optimize this multiplication by a shift. 
     320         * Source: Effective Java, Joshua Bloch, 2008, p.48 
    121321         */ 
    122  
    123         return false; 
    124     } 
    125  
    126     /* 
    127      * (non-Javadoc) 
    128      *  
    129      * @see java.lang.Object#hashCode() 
    130      */ 
    131     @Override 
    132     public int hashCode() { 
    133         return hashCode; 
    134     } 
    135  
    136     /** 
    137      * <p> 
    138      * Returns the path associated with the specified GUI element. 
    139      * </p> 
    140      *  
    141      * @return the path to an element 
    142      */ 
    143     public String getPath() { 
    144         return path; 
    145     } 
    146  
    147     public int getIndex() { 
    148         return index; 
    149     } 
    150  
    151     public void setIndex(int index) { 
    152         this.index = index; 
    153     } 
    154  
    155     /** 
    156      * Set the hash code associated with the GUI element. 
    157      *  
    158      * @param hash 
    159      *            the hash of an element object 
    160      */ 
    161     public void setHashCode(int hash) { 
    162         this.hashCode = hash; 
    163     } 
    164  
    165     /** 
    166      * Set the path associated with the specified GUI element. 
    167      *  
    168      * @param path 
    169      *            the path to an element 
    170      */ 
    171     public void setPath(String path) { 
    172         this.path = path; 
    173     } 
    174  
    175     /** 
    176      * <p> 
    177      * Sets the type of the specified GUI element. 
    178      * </p> 
    179      *  
    180      * @param type 
    181      *            the type 
    182      */ 
    183     public void setType(String type) { 
    184         this.type = type; 
    185     } 
    186  
    187     /** 
    188      * <p> 
    189      * Sets the type hierarchy of the specified GUI element. 
    190      *  
    191      * @param typeHierarchy 
    192      *            </p> 
    193      */ 
    194     public void setTypeHierarchy(List<String> typeHierarchy) { 
    195         this.typeHierarchy = typeHierarchy; 
    196     } 
     322        result = 31 * result + elementHash; 
     323        result = 31 * result + path.hashCode(); 
     324        result = 31 * result + index; 
     325        result = 31 * result + getName().hashCode(); 
     326        result = 31 * result + type.hashCode();      
     327        return result; 
     328    } 
     329     
    197330 
    198331} 
Note: See TracChangeset for help on using the changeset viewer.