Coverage Report - org.eclipse.swtbot.swt.finder.matchers.WithTooltip
 
Classes in this File Line Coverage Branch Coverage Complexity
WithTooltip
80%
17/21
50%
1/2
1.429
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008 Ketan Padegaonkar and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *     Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=126
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.swt.finder.matchers;
 13  
 
 14  
 import java.lang.reflect.InvocationTargetException;
 15  
 import java.lang.reflect.Method;
 16  
 
 17  
 import org.eclipse.swt.widgets.Text;
 18  
 import org.eclipse.swt.widgets.Widget;
 19  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 20  
 import org.hamcrest.Description;
 21  
 import org.hamcrest.Factory;
 22  
 import org.hamcrest.Matcher;
 23  
 
 24  
 /**
 25  
  * Matches widgets if the getToolTipText() method of the widget matches the specified text.
 26  
  * 
 27  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 28  
  * @version $Id$
 29  
  * @since 2.0
 30  
  */
 31  
 public class WithTooltip<T extends Widget> extends AbstractMatcher<T> {
 32  
 
 33  
         /** The text */
 34  
         protected String        text;
 35  
 
 36  
         /**
 37  
          * A flag to denote if this should ignore case.
 38  
          * 
 39  
          * @since 1.2
 40  
          */
 41  21
         protected boolean        ignoreCase        = false;
 42  
 
 43  
         /**
 44  
          * Constructs this matcher with the given text.
 45  
          * 
 46  
          * @param text the text to match on the {@link org.eclipse.swt.widgets.Widget}
 47  
          */
 48  
         WithTooltip(String text) {
 49  21
                 this(text, false);
 50  21
         }
 51  
 
 52  
         /**
 53  
          * Constructs this matcher with the given text.
 54  
          * 
 55  
          * @param text the text to match on the {@link org.eclipse.swt.widgets.Widget}
 56  
          * @param ignoreCase Determines if this should ignore case during the comparison.
 57  
          * @since 1.2
 58  
          */
 59  21
         WithTooltip(String text, boolean ignoreCase) {
 60  21
                 text = text.replaceAll("\\r\\n", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 61  21
                 text = text.replaceAll("\\r", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 62  21
                 this.text = text;
 63  21
                 this.ignoreCase = ignoreCase;
 64  21
         }
 65  
 
 66  
         // FIXME: optimize the if() code block, use strategy or something else.
 67  
         protected boolean doMatch(Object obj) {
 68  
                 try {
 69  495
                         boolean result = false;
 70  495
                         if (ignoreCase)
 71  0
                                 result = getToolTip(obj).equalsIgnoreCase(text);
 72  
                         else
 73  495
                                 result = getToolTip(obj).equals(text);
 74  495
                         return result;
 75  0
                 } catch (Exception e) {
 76  
                         // do nothing
 77  
                 }
 78  0
                 return false;
 79  
         }
 80  
 
 81  
         /**
 82  
          * Gets the text of the object using the getText method. If the object doesn't contain a get text method an
 83  
          * exception is thrown.
 84  
          * 
 85  
          * @param obj any object to get the text from.
 86  
          * @return the return value of obj#getText()
 87  
          * @throws NoSuchMethodException if the method "getToolTipText" does not exist on the object.
 88  
          * @throws IllegalAccessException if the java access control does not allow invocation.
 89  
          * @throws InvocationTargetException if the method "getText" throws an exception.
 90  
          * @see Method#invoke(Object, Object[])
 91  
          */
 92  
         private static String getToolTip(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
 93  495
                 return ((String) SWTUtils.invokeMethod(obj, "getToolTipText")).replaceAll(Text.DELIMITER, "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 94  
         }
 95  
 
 96  
         public void describeTo(Description description) {
 97  11
                 description.appendText("with tooltip '").appendText(text).appendText("'"); //$NON-NLS-1$ //$NON-NLS-2$
 98  11
         }
 99  
 
 100  
         /**
 101  
          * Matches a widget that has the specified exact tooltip.
 102  
          * 
 103  
          * @param text the label.
 104  
          * @return a matcher.
 105  
          * @since 2.0
 106  
          */
 107  
         @Factory
 108  
         public static <T extends Widget> Matcher<T> withTooltip(String text) {
 109  21
                 return new WithTooltip<T>(text);
 110  
         }
 111  
 
 112  
         /**
 113  
          * Matches a widget that has the specified tooltip, ignoring case considerations.
 114  
          * 
 115  
          * @param text the label.
 116  
          * @return a matcher.
 117  
          * @since 2.0
 118  
          */
 119  
         @Factory
 120  
         public static <T extends Widget> Matcher<T> withTooltipIgoringCase(String text) {
 121  0
                 return new WithTooltip<T>(text, true);
 122  
         }
 123  
 
 124  
 }