Coverage Report - org.eclipse.swtbot.swt.finder.matchers.InGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
InGroup
77%
14/18
100%
6/6
1.667
 
 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 org.eclipse.swt.widgets.Group;
 15  
 import org.eclipse.swt.widgets.Widget;
 16  
 import org.eclipse.swtbot.swt.finder.finders.PathGenerator;
 17  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 18  
 import org.eclipse.swtbot.swt.finder.utils.TreePath;
 19  
 import org.hamcrest.Description;
 20  
 import org.hamcrest.Factory;
 21  
 import org.hamcrest.Matcher;
 22  
 
 23  
 /**
 24  
  * Tells if a particular widget is within a {@link Group} with the specified text.
 25  
  * 
 26  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 27  
  * @version $Id$
 28  
  * @since 2.0
 29  
  */
 30  
 public class InGroup<T extends Widget> extends AbstractMatcher<T> {
 31  
 
 32  
         /**
 33  
          * The mnemonic text matcher instance to use.
 34  
          */
 35  
         private final Matcher<?>        matcher;
 36  
 
 37  
         /**
 38  
          * Matches a widget that has the specified Label.
 39  
          * 
 40  
          * @param labelText the label.
 41  
          */
 42  341
         InGroup(String labelText) {
 43  341
                 matcher = new WithMnemonic<Widget>(labelText);
 44  341
         }
 45  
 
 46  
         /**
 47  
          * Matches a widget in a group, if the matcher evaluates to true for the group.
 48  
          * 
 49  
          * @param matcher the matcher.
 50  
          */
 51  0
         InGroup(Matcher matcher) {
 52  0
                 this.matcher = matcher;
 53  0
         }
 54  
 
 55  
         protected boolean doMatch(Object obj) {
 56  666
                 Widget previousWidget = SWTUtils.previousWidget((Widget) obj);
 57  666
                 TreePath path = new PathGenerator().getPath((Widget) obj);
 58  666
                 int segmentCount = path.getSegmentCount();
 59  2317
                 for (int i = 1; i < segmentCount; i++) {
 60  2016
                         previousWidget = (Widget) path.getSegment(segmentCount - i - 1);
 61  2016
                         if ((previousWidget instanceof Group) && matcher.matches(previousWidget))
 62  365
                                 return true;
 63  
                 }
 64  301
                 return false;
 65  
         }
 66  
 
 67  
         public void describeTo(Description description) {
 68  431
                 description.appendText("in group (").appendDescriptionOf(matcher).appendText(")"); //$NON-NLS-1$ //$NON-NLS-2$
 69  431
         }
 70  
 
 71  
         /**
 72  
          * Matches a widget that belongs to the specified group
 73  
          * 
 74  
          * @param labelText the label.
 75  
          * @return a matcher.
 76  
          * @since 2.0
 77  
          */
 78  
         @Factory
 79  
         public static <T extends Widget> Matcher<T> inGroup(String labelText) {
 80  341
                 return new InGroup<T>(labelText);
 81  
         }
 82  
 
 83  
         /**
 84  
          * Matches a widget in a group, if the matcher evaluates to true for the group.
 85  
          * 
 86  
          * @param matcher the matcher.
 87  
          * @return a matcher.
 88  
          * @since 2.0
 89  
          */
 90  
         @Factory
 91  
         public static <T extends Widget> Matcher<T> inGroup(Matcher<?> matcher) {
 92  0
                 return new InGroup<T>(matcher);
 93  
         }
 94  
 
 95  
 }