Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotTabItem
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotTabItem
100%
18/18
N/A
1
SWTBotTabItem$1
100%
3/3
N/A
1
SWTBotTabItem$2
100%
5/5
N/A
1
SWTBotTabItem$3
75%
3/4
N/A
1
SWTBotTabItem$4
100%
3/3
50%
1/2
1
SWTBotTabItem$5
100%
3/3
N/A
1
 
 1  486
 /*******************************************************************************
 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  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import org.eclipse.swt.SWT;
 14  
 import org.eclipse.swt.widgets.Event;
 15  
 import org.eclipse.swt.widgets.TabFolder;
 16  
 import org.eclipse.swt.widgets.TabItem;
 17  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 18  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 19  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 20  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 21  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 22  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 23  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 24  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 25  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 26  
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 27  
 import org.hamcrest.SelfDescribing;
 28  
 
 29  
 /**
 30  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 31  
  * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 32  
  * @version $Id$
 33  
  */
 34  
 @SWTBotWidget(clasz = TabItem.class, preferredName = "tabItem", referenceBy = { ReferenceBy.MNEMONIC })
 35  
 public class SWTBotTabItem extends AbstractSWTBot<TabItem> {
 36  
 
 37  486
         private TabFolder        parent;
 38  
 
 39  
         /**
 40  
          * Constructs a new instance of this object.
 41  
          * 
 42  
          * @param w the widget.
 43  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 44  
          */
 45  
         public SWTBotTabItem(TabItem w) throws WidgetNotFoundException {
 46  1
                 this(w, null);
 47  1
         }
 48  
 
 49  
         /**
 50  
          * Constructs a new instance of this object.
 51  
          * 
 52  
          * @param w the widget.
 53  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 54  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 55  
          */
 56  
         public SWTBotTabItem(TabItem w, SelfDescribing description) throws WidgetNotFoundException {
 57  245
                 super(w, description);
 58  245
                 this.parent = syncExec(new WidgetResult<TabFolder>() {
 59  
                         public TabFolder run() {
 60  245
                                 return widget.getParent();
 61  
                         }
 62  
                 });
 63  245
         }
 64  
 
 65  
         /**
 66  
          * Activates the tabItem.
 67  
          * 
 68  
          * @return itself.
 69  
          * @throws TimeoutException if the tab does not activate
 70  
          */
 71  
         public SWTBotTabItem activate() throws TimeoutException {
 72  243
                 log.trace(MessageFormat.format("Activating {0}", this)); //$NON-NLS-1$
 73  243
                 waitForEnabled();
 74  
                 // this runs in sync because tabFolder.setSelection() does not send a notification, and so should not block.
 75  243
                 asyncExec(new VoidResult() {
 76  
                         public void run() {
 77  243
                                 widget.getParent().setSelection(widget);
 78  243
                                 log.debug(MessageFormat.format("Activated {0}", this)); //$NON-NLS-1$
 79  243
                         }
 80  
                 });
 81  
 
 82  243
                 notify(SWT.Selection, createEvent(), parent);
 83  
 
 84  243
                 new SWTBot().waitUntil(new DefaultCondition() {
 85  
                         public boolean test() throws Exception {
 86  243
                                 return isActive();
 87  
                         }
 88  
 
 89  
                         public String getFailureMessage() {
 90  0
                                 return "Timed out waiting for " + SWTUtils.toString(widget) + " to activate"; //$NON-NLS-1$ //$NON-NLS-2$
 91  
                         }
 92  
                 });
 93  243
                 return this;
 94  
         }
 95  
 
 96  
         protected Event createEvent() {
 97  243
                 Event event = super.createEvent();
 98  243
                 event.widget = parent;
 99  243
                 event.item = widget;
 100  243
                 return event;
 101  
         }
 102  
 
 103  
         public boolean isActive() {
 104  243
                 return syncExec(new BoolResult() {
 105  
                         public Boolean run() {
 106  243
                                 return parent.getSelection()[0] == widget;
 107  
                         }
 108  
                 });
 109  
         }
 110  
 
 111  
         public boolean isEnabled() {
 112  243
                 return syncExec(new BoolResult() {
 113  
                         public Boolean run() {
 114  243
                                 return parent.isEnabled();
 115  
                         }
 116  
                 });
 117  
         }
 118  
 
 119  
 }