Coverage Report - org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBotControl
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSWTBotControl
44%
8/18
25%
1/4
1.25
AbstractSWTBotControl$1
100%
3/3
N/A
1.25
AbstractSWTBotControl$2
0%
0/5
N/A
1.25
 
 1  0
 /*******************************************************************************
 2  
  * Copyright (c) 2009 IBM Corporation 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  
  *     IBM Corporation - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.widgets;
 12  
 
 13  
 import org.eclipse.swt.graphics.Rectangle;
 14  
 import org.eclipse.swt.widgets.Control;
 15  
 import org.eclipse.swt.widgets.Display;
 16  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 17  
 import org.eclipse.swtbot.swt.finder.results.Result;
 18  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 19  
 import org.hamcrest.SelfDescribing;
 20  
 
 21  
 /**
 22  
  * Helper to find SWT {@link Control}s and perform operations on them.
 23  
  * 
 24  
  * @author Joshua Gosse <jlgosse [at] ca [dot] ibm [dot] com>
 25  
  * @version $Id$
 26  
  */
 27  
 public class AbstractSWTBotControl<T extends Control> extends AbstractSWTBot<T> {
 28  
 
 29  
         /**
 30  
          * Constructs a new instance with the given widget.
 31  
          * 
 32  
          * @param w the widget.
 33  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 34  
          */
 35  
         public AbstractSWTBotControl(T w) throws WidgetNotFoundException {
 36  0
                 super(w);
 37  0
         }
 38  
 
 39  
         /**
 40  
          * Constructs a new instance with the given widget.
 41  
          * 
 42  
          * @param w the widget.
 43  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 44  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 45  
          */
 46  
         public AbstractSWTBotControl(T w, SelfDescribing description) throws WidgetNotFoundException {
 47  795
                 super(w, description);
 48  795
         }
 49  
 
 50  
         /**
 51  
          * Get the bounds of the Widget in relation to Display
 52  
          * 
 53  
          * @return the bounds of the Widget in relation to Display
 54  
          */
 55  
         protected Rectangle absoluteLocation() {
 56  1
                 return syncExec(new Result<Rectangle>() {
 57  
                         public Rectangle run() {
 58  1
                                 return display.map(widget.getParent(), null, widget.getBounds());
 59  
                         }
 60  
                 });
 61  
         }
 62  
 
 63  
         /**
 64  
          * Click on the center of the widget.
 65  
          * 
 66  
          * @param post Whether or not {@link Display#post} should be used
 67  
          * @return itself.
 68  
          */
 69  
         protected AbstractSWTBotControl<T> click(final boolean post) {
 70  1
                 setFocus();
 71  1
                 if (post) {
 72  1
                         Rectangle location = absoluteLocation();
 73  1
                         click(location.x + location.width / 2, location.y + location.height / 2, true);
 74  
                 } else
 75  0
                         click();
 76  1
                 return this;
 77  
         }
 78  
 
 79  
         /**
 80  
          * Right click on the center of the widget.
 81  
          * 
 82  
          * @param post Whether or not {@link Display#post} should be used
 83  
          * @return itself
 84  
          */
 85  
         protected AbstractSWTBotControl<T> rightClick(final boolean post) {
 86  0
                 if (post) {
 87  0
                         Rectangle location = absoluteLocation();
 88  0
                         rightClick(location.x + location.width / 2, location.y + location.height / 2, true);
 89  
                 } else
 90  0
                         rightClick();
 91  0
                 return this;
 92  
         }
 93  
 
 94  
         /**
 95  
          * Moves the cursor to the center of the widget
 96  
          * 
 97  
          * @return itself
 98  
          */
 99  
         protected AbstractSWTBotControl<T> moveMouseToWidget() {
 100  0
                 syncExec(new VoidResult() {
 101  
                         public void run() {
 102  0
                                 Rectangle location = absoluteLocation();
 103  0
                                 moveMouse(location.x + location.width / 2, location.y + location.height / 2);
 104  0
                         }
 105  
                 });
 106  0
                 return this;
 107  
         }
 108  
 }