Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotExpandBar
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotExpandBar
76%
16/21
0%
0/2
1.533
SWTBotExpandBar$1
100%
3/3
N/A
1.533
SWTBotExpandBar$2
100%
7/7
100%
4/4
1.533
SWTBotExpandBar$3
75%
6/8
100%
2/2
1.533
 
 1  12
 /*******************************************************************************
 2  
  * Copyright (c) 2011 SWTBot Committers 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  
  *     Toby Weston - initial API and implementation (Bug 259799)
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import static java.util.Collections.emptyList;
 14  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
 15  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
 16  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withText;
 17  
 import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForWidget;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.eclipse.swt.widgets.ExpandBar;
 23  
 import org.eclipse.swt.widgets.ExpandItem;
 24  
 import org.eclipse.swt.widgets.Widget;
 25  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 26  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 27  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 28  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 29  
 import org.eclipse.swtbot.swt.finder.results.IntResult;
 30  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 31  
 import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition;
 32  
 import org.hamcrest.Matcher;
 33  
 import org.hamcrest.SelfDescribing;
 34  
 
 35  
 /**
 36  
  * Represents an {@link ExpandBar}.
 37  
  * 
 38  
  * @author Toby Weston
 39  
  */
 40  
 @SWTBotWidget(clasz = ExpandBar.class, preferredName = "expandBar", referenceBy = { ReferenceBy.LABEL })
 41  
 public class SWTBotExpandBar extends AbstractSWTBot<ExpandBar> {
 42  
 
 43  
         /**
 44  
          * Constructs a new instance with the given widget.
 45  
          * 
 46  
          * @param w the widget.
 47  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 48  
          */
 49  
         public SWTBotExpandBar(ExpandBar w) {
 50  0
                 super(w);
 51  0
         }
 52  
 
 53  
         /**
 54  
          * Constructs a new instance with the given widget.
 55  
          * 
 56  
          * @param w the widget.
 57  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 58  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 59  
          */
 60  
         public SWTBotExpandBar(ExpandBar w, SelfDescribing description) {
 61  6
                 super(w, description);
 62  6
         }
 63  
 
 64  
         /**
 65  
          * @return the number of {@link ExpandItem}s in the widget.
 66  
          */
 67  
         public int itemCount() {
 68  4
                 return syncExec(new IntResult() {
 69  
                         public Integer run() {
 70  4
                                 return widget.getItemCount();
 71  
                         }
 72  
                 });
 73  
         }
 74  
 
 75  
         /**
 76  
          * @return the number of items that are expanded.
 77  
          * @see #itemCount()
 78  
          * @see #collapsedItemCount()
 79  
          */
 80  
         public int expandedItemCount() {
 81  6
                 return syncExec(new IntResult() {
 82  
                         public Integer run() {
 83  6
                                 int count = 0;
 84  18
                                 for (ExpandItem item : widget.getItems()) {
 85  12
                                         if (item.getExpanded())
 86  6
                                                 count++;
 87  
                                 }
 88  6
                                 return count;
 89  
                         }
 90  
                 });
 91  
         }
 92  
 
 93  
         /**
 94  
          * @return the number of items that are collapsed.
 95  
          * @see #itemCount()
 96  
          * @see #expandedItemCount()
 97  
          */
 98  
         public int collapsedItemCount() {
 99  3
                 return itemCount() - expandedItemCount();
 100  
         }
 101  
 
 102  
         /**
 103  
          * Expands the {@link ExpandItem} and returns it.
 104  
          * 
 105  
          * @param itemText the text on the item.
 106  
          * @return the {@link SWTBotExpandItem} with the specified text.
 107  
          */
 108  
         @SuppressWarnings("unchecked")
 109  
         public SWTBotExpandItem expandItem(final String itemText) {
 110  2
                 return expandItem(withText(itemText));
 111  
         }
 112  
 
 113  
         /**
 114  
          * Expands the {@link ExpandItem} and returns it.
 115  
          * 
 116  
          * @param matcher the matcher.
 117  
          * @return the {@link SWTBotExpandItem} that matches the matcher
 118  
          */
 119  
         public SWTBotExpandItem expandItem(Matcher<Widget> matcher) {
 120  2
                 waitForEnabled();
 121  2
                 return getExpandItem(matcher).expand();
 122  
         }
 123  
 
 124  
         /**
 125  
          * Collapses the {@link ExpandItem} and returns it.
 126  
          * 
 127  
          * @param itemText the text on the item.
 128  
          * @return the {@link SWTBotExpandItem} with the specified text.
 129  
          */
 130  
         @SuppressWarnings("unchecked")
 131  
         public SWTBotExpandItem collapseItem(final String itemText) {
 132  2
                 return collapseItem(withText(itemText));
 133  
         }
 134  
 
 135  
         /**
 136  
          * Collapses the {@link ExpandItem} and returns it.
 137  
          * 
 138  
          * @param matcher the matcher.
 139  
          * @return the {@link SWTBotExpandItem} that matches the matcher.
 140  
          */
 141  
         public SWTBotExpandItem collapseItem(Matcher<Widget> matcher) {
 142  2
                 waitForEnabled();
 143  2
                 return getExpandItem(matcher).collapse();
 144  
         }
 145  
 
 146  
         /**
 147  
          * Return the {@link ExpandItem} that matches the specified matcher.
 148  
          * 
 149  
          * @param matcher the matcher.
 150  
          * @return the {@link SWTBotExpandItem} with the specified text.
 151  
          */
 152  
         public SWTBotExpandItem getExpandItem(Matcher<Widget> matcher) {
 153  
                 try {
 154  5
                         matcher = allOf(widgetOfType(ExpandItem.class), matcher);
 155  5
                         WaitForObjectCondition<? extends Widget> waitForWidget = waitForWidget(matcher, widget);
 156  5
                         new SWTBot().waitUntilWidgetAppears(waitForWidget);
 157  4
                         return new SWTBotExpandItem((ExpandItem) waitForWidget.get(0), matcher);
 158  0
                 } catch (TimeoutException e) {
 159  0
                         throw new WidgetNotFoundException("Timed out waiting for expandBar item " + matcher, e);
 160  
                 }
 161  
         }
 162  
 
 163  
         /**
 164  
          * @return a list of {@link SWTBotExpandItem} or the empty list if there was a problem with any of the items.
 165  
          */
 166  
         public List<SWTBotExpandItem> getAllItems() {
 167  1
                 return syncExec(new ListResult<SWTBotExpandItem>() {
 168  
                         public List<SWTBotExpandItem> run() {
 169  1
                                 List<SWTBotExpandItem> result = new ArrayList<SWTBotExpandItem>();
 170  3
                                 for (ExpandItem item : widget.getItems()) {
 171  
                                         try {
 172  2
                                                 result.add(new SWTBotExpandItem(item));
 173  0
                                         } catch (WidgetNotFoundException e) {
 174  0
                                                 return emptyList();
 175  
                                         }
 176  
                                 }
 177  1
                                 return result;
 178  
                         }
 179  
                 });
 180  
         }
 181  
 
 182  
         /**
 183  
          * @return <code>true</code> if the expandBar has any items, <code>false</code> otherwise.
 184  
          */
 185  
         public boolean hasItems() {
 186  0
                 return itemCount() > 0;
 187  
         }
 188  
 }