Coverage Report - org.eclipse.swtbot.eclipse.finder.widgets.SWTBotWorkbenchPart
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotWorkbenchPart
68%
39/57
50%
8/16
2.455
SWTBotWorkbenchPart$1
71%
5/7
N/A
2.455
SWTBotWorkbenchPart$2
78%
18/23
71%
10/14
2.455
 
 1  12
 /*******************************************************************************
 2  
  * Copyright (c) 2008-2009 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  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.eclipse.finder.widgets;
 12  
 
 13  
 import static org.hamcrest.Matchers.any;
 14  
 
 15  
 import java.util.ArrayList;
 16  
 import java.util.List;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.eclipse.swt.SWT;
 20  
 import org.eclipse.swt.widgets.Control;
 21  
 import org.eclipse.swt.widgets.ToolBar;
 22  
 import org.eclipse.swt.widgets.ToolItem;
 23  
 import org.eclipse.swt.widgets.Widget;
 24  
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
 25  
 import org.eclipse.swtbot.eclipse.finder.widgets.utils.PartLabelDescription;
 26  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 27  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 28  
 import org.eclipse.swtbot.swt.finder.finders.Finder;
 29  
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 30  
 import org.eclipse.swtbot.swt.finder.results.ListResult;
 31  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 32  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 33  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 34  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 35  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarButton;
 36  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarDropDownButton;
 37  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarPushButton;
 38  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarRadioButton;
 39  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarSeparatorButton;
 40  
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarToggleButton;
 41  
 import org.eclipse.ui.IWorkbenchPartReference;
 42  
 import org.eclipse.ui.PartInitException;
 43  
 import org.eclipse.ui.internal.PartPane;
 44  
 import org.eclipse.ui.internal.WorkbenchPartReference;
 45  
 import org.hamcrest.Matcher;
 46  
 import org.hamcrest.SelfDescribing;
 47  
 
 48  
 /**
 49  
  * This represents the eclipse {@link IWorkbenchPartReference} item, subclasses must extend this to implement support
 50  
  * for various {@link IWorkbenchPartReference}s.
 51  
  * 
 52  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 53  
  * @author Ralf Ebert www.ralfebert.de (bug 271630)
 54  
  * @version $Id$
 55  
  * @since 2.0
 56  
  */
 57  
 public abstract class SWTBotWorkbenchPart<T extends IWorkbenchPartReference> {
 58  
 
 59  
         /** The IWorkbenchPartReference reference that this part encapsulates. */
 60  
         protected final T                                partReference;
 61  
         /** The logger. */
 62  
         protected final Logger                        log;
 63  
         /** A helper swtbot instance. */
 64  
         protected final SWTWorkbenchBot        bot;
 65  
         private final SelfDescribing        description;
 66  
         private Widget widget;
 67  
 
 68  
         /**
 69  
          * Creates an instance of a workbench part.
 70  
          * 
 71  
          * @param partReference the part reference.
 72  
          * @param bot the helper bot.
 73  
          */
 74  
         public SWTBotWorkbenchPart(T partReference, SWTWorkbenchBot bot) {
 75  42
                 this(partReference, bot, new PartLabelDescription<T>(partReference));
 76  42
         }
 77  
 
 78  
         /**
 79  
          * Creates an instance of a workbench part.
 80  
          * 
 81  
          * @param partReference the part reference.
 82  
          * @param bot the helper bot.
 83  
          * @param description the description of the workbench part.
 84  
          */
 85  60
         public SWTBotWorkbenchPart(T partReference, SWTWorkbenchBot bot, SelfDescribing description) {
 86  60
                 this.bot = bot;
 87  60
                 if (description == null )
 88  0
                         description = new PartLabelDescription<T>(partReference);
 89  60
                 this.description = description;
 90  60
                 Assert.isNotNull(partReference, "The part reference cannot be null"); //$NON-NLS-1$
 91  60
                 this.partReference = partReference;
 92  60
                 log = Logger.getLogger(getClass());
 93  60
         }
 94  
 
 95  
         /**
 96  
          * @return the reference for this part.
 97  
          */
 98  
         public T getReference() {
 99  7
                 return partReference;
 100  
         }
 101  
 
 102  
         /**
 103  
          * Close the partReference.
 104  
          */
 105  
         public abstract void close();
 106  
 
 107  
         /**
 108  
          * Shows the part if it is visible.
 109  
          */
 110  
         public void show() {
 111  2
                 UIThreadRunnable.syncExec(new VoidResult() {
 112  
                         public void run() {
 113  
                                 try {
 114  2
                                         partReference.getPage().activate(partReference.getPart(true));
 115  2
                                         partReference.getPage().showView(partReference.getId());
 116  0
                                 } catch (PartInitException e) {
 117  0
                                         throw new RuntimeException("Could not show partReference - " + partReference.getPartName(), e); //$NON-NLS-1$
 118  
                                 }
 119  2
                         }
 120  
                 });
 121  2
         }
 122  
 
 123  
         /**
 124  
          * Gets the title of the partReference.
 125  
          * 
 126  
          * @return the title of the part as visible in the tab
 127  
          */
 128  
         public String getTitle() {
 129  1
                 return partReference.getPartName();
 130  
         }
 131  
 
 132  
         /**
 133  
          * Gets the toolbar buttons currently visible.
 134  
          * 
 135  
          * @return The set of toolbar buttons.
 136  
          */
 137  
         public List<SWTBotToolbarButton> getToolbarButtons() {
 138  6
                 return UIThreadRunnable.syncExec(new ListResult<SWTBotToolbarButton>() {
 139  
 
 140  
                         public List<SWTBotToolbarButton> run() {
 141  6
                                 PartPane obj = ((WorkbenchPartReference) partReference).getPane();
 142  6
                                 ToolBar toolbar = (ToolBar) obj.getToolBar();
 143  6
                                 final List<SWTBotToolbarButton> l = new ArrayList<SWTBotToolbarButton>();
 144  
 
 145  6
                                 if (toolbar == null)
 146  0
                                         return l;
 147  
 
 148  6
                                 ToolItem[] items = toolbar.getItems();
 149  6
                                 log.debug("number of items : " + items.length);
 150  30
                                 for (int i = 0; i < items.length; i++) {
 151  
                                         try {
 152  24
                                                 if (SWTUtils.hasStyle(items[i], SWT.PUSH))
 153  6
                                                         l.add(new SWTBotToolbarPushButton(items[i]));
 154  18
                                                 else if(SWTUtils.hasStyle(items[i], SWT.CHECK))
 155  6
                                                         l.add(new SWTBotToolbarToggleButton(items[i]));
 156  12
                                                 else if(SWTUtils.hasStyle(items[i], SWT.RADIO))
 157  6
                                                         l.add(new SWTBotToolbarRadioButton(items[i]));
 158  6
                                                 else if(SWTUtils.hasStyle(items[i], SWT.DROP_DOWN))
 159  6
                                                         l.add(new SWTBotToolbarDropDownButton(items[i]));
 160  0
                                                 else if(SWTUtils.hasStyle(items[i], SWT.SEPARATOR))
 161  0
                                                         l.add(new SWTBotToolbarSeparatorButton(items[i]));
 162  0
                                         } catch (WidgetNotFoundException e) {
 163  0
                                                 log.warn("Failed to find widget " + items[i].getText(), e); //$NON-NLS-1$
 164  
                                         }
 165  
                                 }
 166  
 
 167  6
                                 return l;
 168  
 
 169  
                         }
 170  
                 });
 171  
         }
 172  
 
 173  
         /**
 174  
          * Gets the toolbar drop down button matching the given toolbar button.
 175  
          *
 176  
          * @param tooltip The tooltip to use to find the button to return.
 177  
          * @return The toolbar button.
 178  
          * @throws WidgetNotFoundException Thrown if the widget was not found matching the given tooltip.
 179  
          */
 180  
         public SWTBotToolbarDropDownButton toolbarDropDownButton(String tooltip) throws WidgetNotFoundException {
 181  1
                 SWTBotToolbarButton abstractButton = toolbarButton(tooltip);
 182  1
                 if (abstractButton instanceof SWTBotToolbarDropDownButton)
 183  1
                         return (SWTBotToolbarDropDownButton) abstractButton;
 184  
 
 185  0
                 throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); //$NON-NLS-1$ //$NON-NLS-2$
 186  
         }
 187  
 
 188  
         /**
 189  
          * Gets the toolbar radio button matching the given toolbar button.
 190  
          *
 191  
          * @param tooltip The tooltip to use to find the button to return.
 192  
          * @return The toolbar button.
 193  
          * @throws WidgetNotFoundException Thrown if the widget was not found matching the given tooltip.
 194  
          */
 195  
         public SWTBotToolbarRadioButton toolbarRadioButton(String tooltip) throws WidgetNotFoundException {
 196  1
                 SWTBotToolbarButton abstractButton = toolbarButton(tooltip);
 197  1
                 if (abstractButton instanceof SWTBotToolbarRadioButton)
 198  1
                         return (SWTBotToolbarRadioButton) abstractButton;
 199  
 
 200  0
                 throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); //$NON-NLS-1$ //$NON-NLS-2$
 201  
         }
 202  
 
 203  
         /**
 204  
          * Gets the toolbar push button matching the given toolbar button.
 205  
          *
 206  
          * @param tooltip The tooltip to use to find the button to return.
 207  
          * @return The toolbar button.
 208  
          * @throws WidgetNotFoundException Thrown if the widget was not found matching the given tooltip.
 209  
          */
 210  
         public SWTBotToolbarPushButton toolbarPushButton(String tooltip) throws WidgetNotFoundException {
 211  0
                 SWTBotToolbarButton abstractButton = toolbarButton(tooltip);
 212  0
                 if (abstractButton instanceof SWTBotToolbarPushButton)
 213  0
                         return (SWTBotToolbarPushButton) abstractButton;
 214  
 
 215  0
                 throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); //$NON-NLS-1$ //$NON-NLS-2$
 216  
         }
 217  
 
 218  
         /**
 219  
          * Gets the toggle toolbar button matching the given toolbar button.
 220  
          *
 221  
          * @param tooltip The tooltip to use to find the button to return.
 222  
          * @return The toolbar button.
 223  
          * @throws WidgetNotFoundException Thrown if the widget was not found matching the given tooltip.
 224  
          */
 225  
         public SWTBotToolbarToggleButton toolbarToggleButton(String tooltip) throws WidgetNotFoundException {
 226  1
                 SWTBotToolbarButton abstractButton = toolbarButton(tooltip);
 227  1
                 if (abstractButton instanceof SWTBotToolbarToggleButton)
 228  1
                         return (SWTBotToolbarToggleButton) abstractButton;
 229  
 
 230  0
                 throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); //$NON-NLS-1$ //$NON-NLS-2$
 231  
         }
 232  
 
 233  
         /**
 234  
          * Gets the toolbar button matching the given toolbar button.
 235  
          * 
 236  
          * @param tooltip The tooltip to use to find the button to return.
 237  
          * @return The toolbar button.
 238  
          * @throws WidgetNotFoundException Thrown if the widget was not found matching the given tooltip.
 239  
          */
 240  
         public SWTBotToolbarButton toolbarButton(String tooltip) throws WidgetNotFoundException {
 241  5
                 List<SWTBotToolbarButton> l = getToolbarButtons();
 242  
 
 243  15
                 for (int i = 0; i < l.size(); i++) {
 244  14
                         SWTBotToolbarButton item = l.get(i);
 245  14
                         if (item.getToolTipText().equals(tooltip)) {
 246  4
                                 return item;
 247  
                         }
 248  
                 }
 249  
 
 250  1
                 throw new WidgetNotFoundException("Unable to find toolitem with the given tooltip '" + tooltip + "'"); //$NON-NLS-1$ //$NON-NLS-2$
 251  
         }
 252  
 
 253  
         /**
 254  
          * @param matcher a matcher.
 255  
          * @return a widget within the parent widget that matches the specified matcher.
 256  
          */
 257  
         protected <S extends Widget> S findWidget(Matcher<S> matcher) {
 258  16
                 return findWidgets(matcher).get(0);
 259  
         }
 260  
 
 261  
         /**
 262  
          * @param matcher a matcher.
 263  
          * @return a widget within the parent widget that matches the specified matcher.
 264  
          */
 265  
         protected <S extends Widget> List<? extends S> findWidgets(Matcher<S> matcher) {
 266  19
                 Finder finder = bot.getFinder();
 267  19
                 Control control = getControl();
 268  19
                 boolean shouldFindInvisibleControls = finder.shouldFindInvisibleControls();
 269  19
                 finder.setShouldFindInvisibleControls(true);
 270  
                 try {
 271  38
                         return bot.widgets(matcher, control);
 272  0
                 } catch (Exception e) {
 273  0
                         throw new WidgetNotFoundException("Could not find any control inside the view " + partReference.getPartName(), e); //$NON-NLS-1$
 274  0
                 } finally {
 275  19
                         finder.setShouldFindInvisibleControls(shouldFindInvisibleControls);
 276  0
                 }
 277  
         }
 278  
 
 279  
         /**
 280  
          * Returns the workbench pane control.
 281  
          * 
 282  
          * @return returns the workbench pane control.
 283  
          */
 284  
         private Control getControl() {
 285  26
                 return ((WorkbenchPartReference) partReference).getPane().getControl();
 286  
         }
 287  
 
 288  
         /**
 289  
          * Returns a SWTBot instance that matches the contents of this workbench part.
 290  
          * 
 291  
          * @return SWTBot
 292  
          */
 293  
         public SWTBot bot() {
 294  7
                 return new SWTBot(getControl());
 295  
         }
 296  
 
 297  
         /**
 298  
          * Asserts that the viewpart is active.
 299  
          * 
 300  
          */
 301  
         protected void assertActive() {
 302  0
                 Assert.isLegal(isActive(), MessageFormat.format("The workbench part {0}is not active", description));
 303  0
         }
 304  
 
 305  
         /**
 306  
          * @return <code>true</code> if the part is currently active.
 307  
          */
 308  
         public abstract boolean isActive();
 309  
 
 310  
         /**
 311  
          * Sets focus on the current part.
 312  
          */
 313  
         public abstract void setFocus();
 314  
 
 315  
         /**
 316  
          * The parent widget inside the partReference. If you want to look for a particular widget within the part, this is
 317  
          * a good place to start searching for the widget.
 318  
          * <p>
 319  
          * <b>NOTE:</b> Clients must ensure that the part is active at the time of making this call. If the part is not
 320  
          * active, then this method will throw a {@link WidgetNotFoundException}.
 321  
          * </p>
 322  
          * 
 323  
          * @return the parent widget in the part.
 324  
          * @see #findWidget(org.hamcrest.Matcher)
 325  
          * @see #assertActive()
 326  
          * @see #show()
 327  
          */
 328  
         public Widget getWidget() {
 329  0
                 show();
 330  0
                 if (widget == null)
 331  0
                         widget = findWidget(any(Widget.class));
 332  0
                 return widget;
 333  
         }
 334  
         
 335  
 }