1 /*
   2  * Copyright (c) 2007, 2018, 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.swt.lookup;
  26 
  27 import java.lang.reflect.InvocationTargetException;
  28 import java.lang.reflect.Method;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import org.eclipse.swt.widgets.Item;
  32 import org.jemmy.resources.StringComparePolicy;
  33 
  34 /**
  35  *
  36  * @author erikgreijus
  37  * @param <T>
  38  */
  39 public class ByItemStringsLookup<T extends Item> extends QueueLookup<T> {
  40 
  41     private final StringComparePolicy policy;
  42     private final String text;
  43 
  44     /**
  45      *
  46      * @param text The text to use for matching
  47      */
  48     public ByItemStringsLookup(String text) {
  49         this(text, StringComparePolicy.SUBSTRING);
  50     }
  51 
  52     /**
  53      *
  54      * @param text The text to use for matching
  55      * @param policy The policy to use when matching the text
  56      */
  57     public ByItemStringsLookup(String text, StringComparePolicy policy) {
  58         this.policy = policy;
  59         this.text = text;
  60     }
  61 
  62     public static <V extends Item> List<String> getTexts(V item) {
  63         List<String> result = new ArrayList<>();
  64         result.add(item.getText());
  65         try {
  66             Method getText = item.getClass().getMethod("getText", int.class);
  67             Method getParent = item.getClass().getMethod("getParent");
  68             Object parent = getParent.invoke(item);
  69             Method getColumnCount = parent.getClass().getMethod("getColumnCount");
  70             int columnCount = (int) getColumnCount.invoke(parent);
  71             for (int i = 0; i < columnCount; i++) {
  72                 result.add((String) getText.invoke(item, i));
  73             }
  74         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  75             System.err.println("Exception when using reflection to get additional text elements: " + ex.getMessage());
  76             ex.printStackTrace();
  77         }
  78         return result;
  79     }
  80 
  81     @Override
  82     public boolean doCheck(T control) {
  83         // this lookup only supports objects with a getText(int) method
  84         try {
  85             control.getClass().getMethod("getText", int.class);
  86             return getTexts(control).stream().map((textElement) -> policy.compare(text, textElement)).anyMatch((matches) -> (matches));
  87         } catch (NoSuchMethodException e) {
  88             System.err.println("Class " + control.getClass() + " isn't supported by " + this.getClass().getSimpleName() + " (i.e has no getText(int) method");
  89             return false;
  90         }
  91     }
  92 
  93     @Override
  94     public String toString() {
  95         return "Text = " + text;
  96     }
  97 }