Coverage Report - org.eclipse.swtbot.swt.finder.matchers.WithLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
WithLabel
100%
19/19
90%
9/10
1.833
 
 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.util.List;
 15  
 import java.util.ListIterator;
 16  
 
 17  
 import org.eclipse.swt.custom.CLabel;
 18  
 import org.eclipse.swt.widgets.Label;
 19  
 import org.eclipse.swt.widgets.Widget;
 20  
 import org.eclipse.swtbot.swt.finder.finders.ControlFinder;
 21  
 import org.eclipse.swtbot.swt.finder.finders.Finder;
 22  
 import org.eclipse.swtbot.swt.finder.finders.MenuFinder;
 23  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 24  
 import org.hamcrest.Description;
 25  
 import org.hamcrest.Factory;
 26  
 import org.hamcrest.Matcher;
 27  
 import org.hamcrest.Matchers;
 28  
 
 29  
 /**
 30  
  * Tells if a particular widget has a label with the specified text.
 31  
  * <p>
 32  
  * <b>NOTE:</b> This will <em>drill upwards</em> in the widget hierarchy in an attempt to find the label for a widget.
 33  
  * </p>
 34  
  * 
 35  
  * @see WithMnemonic
 36  
  * @author Ketan Padegaonkar &lt;KetanPadegaonkar [at] gmail [dot] com&gt;
 37  
  * @version $Id$
 38  
  * @since 2.0
 39  
  */
 40  
 public class WithLabel<T extends Widget> extends AbstractMatcher<T> {
 41  
 
 42  
         /**
 43  
          * The mnemonic text matcher instance to use.
 44  
          */
 45  
         private final WithMnemonic<Widget>        mnemonicTextMatcher;
 46  
         private final Finder                        finder;
 47  
 
 48  
         /**
 49  
          * Matches a widget that has the specified Label.
 50  
          * 
 51  
          * @param labelText the label.
 52  
          * @param finder finder for locating widgets
 53  
          */
 54  10
         WithLabel(String labelText, Finder finder) {
 55  10
                 Assert.isNotNull(labelText, "The parameter labelText was null.");
 56  10
                 Assert.isNotNull(finder, "The parameter finder was null.");
 57  10
                 mnemonicTextMatcher = new WithMnemonic<Widget>(labelText);
 58  10
                 this.finder = finder;
 59  10
         }
 60  
 
 61  
         protected boolean doMatch(Object obj) {
 62  295
                 List<? extends Widget> allWidgets = finder.findControls(Matchers.<Widget> anything());
 63  
 
 64  295
                 int widgetIndex = allWidgets.indexOf(obj);
 65  
 
 66  295
                 ListIterator<? extends Widget> listIterator = allWidgets.listIterator(widgetIndex);
 67  
 
 68  5608
                 while (listIterator.hasPrevious()) {
 69  5169
                         Widget previousWidget = listIterator.previous();
 70  5169
                         if ((isLabel(previousWidget)) && mnemonicTextMatcher.matches(previousWidget))
 71  151
                                 return true;
 72  
                 }
 73  
 
 74  144
                 return false;
 75  
         }
 76  
 
 77  
         private boolean isLabel(Widget widget) {
 78  5169
                 return widget instanceof Label || widget instanceof CLabel;
 79  
         }
 80  
 
 81  
         public void describeTo(Description description) {
 82  73
                 description.appendText("with label (").appendDescriptionOf(mnemonicTextMatcher).appendText(")"); //$NON-NLS-1$ //$NON-NLS-2$
 83  73
         }
 84  
 
 85  
         /**
 86  
          * Matches a widget that has the specified labelText.
 87  
          * 
 88  
          * @param labelText the label.
 89  
          * @return a matcher.
 90  
          * @since 2.0
 91  
          */
 92  
         @Factory
 93  
         public static <T extends Widget> Matcher<T> withLabel(String labelText) {
 94  1
                 return new WithLabel<T>(labelText, new Finder(new ControlFinder(), new MenuFinder()));
 95  
         }
 96  
 
 97  
         /**
 98  
          * Matches a widget that has the specified labelText within the given parent.
 99  
          * 
 100  
          * @param labelText the label.
 101  
          * @param finder finder for locating widgets
 102  
          * @return a matcher.
 103  
          * @since 2.0
 104  
          */
 105  
         @Factory
 106  
         public static <T extends Widget> Matcher<T> withLabel(String labelText, Finder finder) {
 107  9
                 return new WithLabel<T>(labelText, finder);
 108  
         }
 109  
 
 110  
 }