Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotCTabItem
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotCTabItem
73%
31/42
N/A
1.105
SWTBotCTabItem$1
100%
3/3
N/A
1.105
SWTBotCTabItem$2
0%
0/4
N/A
1.105
SWTBotCTabItem$3
0%
0/5
N/A
1.105
SWTBotCTabItem$4
0%
0/4
N/A
1.105
SWTBotCTabItem$5
0%
0/3
0%
0/2
1.105
SWTBotCTabItem$6
100%
3/3
N/A
1.105
SWTBotCTabItem$7
71%
5/7
N/A
1.105
 
 1  0
 /*******************************************************************************
 2  
  * Copyright (c) 2009 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  
  *     Ketan Patel - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import java.lang.reflect.Field;
 14  
 
 15  
 import org.eclipse.swt.SWT;
 16  
 import org.eclipse.swt.custom.CTabFolder;
 17  
 import org.eclipse.swt.custom.CTabItem;
 18  
 import org.eclipse.swt.graphics.Rectangle;
 19  
 import org.eclipse.swt.widgets.Event;
 20  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 21  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 22  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 23  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 24  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 25  
 import org.eclipse.swtbot.swt.finder.results.Result;
 26  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 27  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 28  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 29  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 30  
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 31  
 import org.hamcrest.SelfDescribing;
 32  
 
 33  
 /**
 34  
  * @author Ketan Patel
 35  
  * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 36  
  */
 37  
 @SWTBotWidget(clasz = CTabItem.class, preferredName = "cTabItem", referenceBy = { ReferenceBy.MNEMONIC })
 38  
 public class SWTBotCTabItem extends AbstractSWTBot<CTabItem> {
 39  
 
 40  0
         private CTabFolder        parent;
 41  
 
 42  
         /**
 43  
          * Constructs a new instance of this object.
 44  
          * 
 45  
          * @param w the widget.
 46  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 47  
          */
 48  
         public SWTBotCTabItem(CTabItem w) throws WidgetNotFoundException {
 49  0
                 this(w, null);
 50  0
         }
 51  
 
 52  
         /**
 53  
          * Constructs a new instance of this object.
 54  
          * 
 55  
          * @param w the widget.
 56  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 57  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 58  
          */
 59  
         public SWTBotCTabItem(CTabItem w, SelfDescribing description) throws WidgetNotFoundException {
 60  1
                 super(w, description);
 61  1
                 this.parent = syncExec(new WidgetResult<CTabFolder>() {
 62  
                         public CTabFolder run() {
 63  1
                                 return widget.getParent();
 64  
                         }
 65  
                 });
 66  1
         }
 67  
 
 68  
 
 69  
         /**
 70  
          * Shows the item. If the item is already showing in the receiver, this method simply returns. Otherwise, the items
 71  
          * are scrolled until the item is visible.
 72  
          * 
 73  
          * @return This {@link SWTBotCTabItem}.
 74  
          */
 75  
         public SWTBotCTabItem show() {
 76  0
                 syncExec(new VoidResult() {
 77  
                         public void run() {
 78  0
                                 parent.showItem(widget);
 79  0
                         }
 80  
                 });
 81  0
                 return this;
 82  
         }
 83  
 
 84  
         /**
 85  
          * Activates the tabItem.
 86  
          * 
 87  
          * @return itself.
 88  
          * @throws TimeoutException if the tab does not activate
 89  
          */
 90  
         public SWTBotCTabItem activate() throws TimeoutException {
 91  0
                 log.trace(MessageFormat.format("Activating {0}", this)); //$NON-NLS-1$
 92  0
                 waitForEnabled();
 93  
                 // this runs in sync because tabFolder.setSelection() does not send a notification, and so should not block.
 94  0
                 asyncExec(new VoidResult() {
 95  
                         public void run() {
 96  0
                                 widget.getParent().setSelection(widget);
 97  0
                                 log.debug(MessageFormat.format("Activated {0}", this)); //$NON-NLS-1$
 98  0
                         }
 99  
                 });
 100  
 
 101  0
                 notify(SWT.Selection, createEvent(), parent);
 102  
 
 103  0
                 new SWTBot().waitUntil(new DefaultCondition() {
 104  
                         public boolean test() throws Exception {
 105  0
                                 return isActive();
 106  
                         }
 107  
 
 108  
                         public String getFailureMessage() {
 109  0
                                 return "Timed out waiting for " + SWTUtils.toString(widget) + " to activate"; //$NON-NLS-1$ //$NON-NLS-2$
 110  
                         }
 111  
                 });
 112  
 
 113  0
                 return this;
 114  
         }
 115  
 
 116  
         protected Event createEvent() {
 117  10
                 Event event = super.createEvent();
 118  10
                 event.widget = parent;
 119  10
                 event.item = widget;
 120  10
                 return event;
 121  
         }
 122  
 
 123  
         public boolean isActive() {
 124  0
                 return syncExec(new BoolResult() {
 125  
                         public Boolean run() {
 126  0
                                 return parent.getSelection() == widget;
 127  
                         }
 128  
                 });
 129  
         }
 130  
 
 131  
         public boolean isEnabled() {
 132  1
                 return syncExec(new BoolResult() {
 133  
                         public Boolean run() {
 134  1
                                 return widget.getParent().isEnabled();
 135  
                         }
 136  
                 });
 137  
         }
 138  
 
 139  
         /**
 140  
          * Closes the CTabItem.
 141  
          *
 142  
          * @return this CTabItem.
 143  
          */
 144  
         public SWTBotCTabItem close() {
 145  1
                 waitForEnabled();
 146  1
                 Rectangle rectangleCloseBox = syncExec(new Result<Rectangle>() {
 147  
                         public Rectangle run() {
 148  
                                 try {
 149  1
                                         Field field = CTabItem.class.getDeclaredField("closeRect");
 150  1
                                         field.setAccessible(true);
 151  1
                                         return  (Rectangle) field.get(widget);
 152  
                                 }
 153  0
                                 catch (Exception e) {
 154  
 
 155  
                                 }
 156  0
                                 return null;
 157  
                         }
 158  
                 });
 159  1
                 clickCloseButton(rectangleCloseBox.x + (rectangleCloseBox.width / 2), rectangleCloseBox.y + (rectangleCloseBox.height / 2));
 160  1
                 return this;
 161  
         }
 162  
 
 163  
         private void notifyParent(final int eventType, final Event createEvent) {
 164  2
                 notify(eventType, createEvent, widget.getParent());
 165  2
         }
 166  
 
 167  
         private void notifyParent(int event) {
 168  10
                 notify(event, createEvent(), widget.getParent());
 169  10
         }
 170  
 
 171  
 
 172  
         private void clickCloseButton(int x, int y) {
 173  1
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 174  1
                 notifyParent(SWT.MouseEnter);
 175  1
                 notifyParent(SWT.MouseMove);
 176  1
                 notifyParent(SWT.Activate);
 177  1
                 notifyParent(SWT.FocusIn);
 178  1
                 notifyParent(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.NONE, 1));
 179  
                 // this event being button 1 is what allows CTabItem to close
 180  1
                 notifyParent(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.BUTTON1, 1));
 181  1
                 notifyParent(SWT.Selection);
 182  1
                 notifyParent(SWT.MouseHover);
 183  1
                 notifyParent(SWT.MouseMove);
 184  1
                 notifyParent(SWT.MouseExit);
 185  1
                 notifyParent(SWT.Deactivate);
 186  1
                 notifyParent(SWT.FocusOut);
 187  1
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 188  1
         }
 189  
 
 190  
 }