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