Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotShell
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotShell
100%
13/13
N/A
1.133
SWTBotShell$1
80%
4/5
N/A
1.133
SWTBotShell$1$1
100%
5/5
N/A
1.133
SWTBotShell$2
100%
5/5
50%
1/2
1.133
SWTBotShell$3
75%
3/4
50%
1/2
1.133
SWTBotShell$4
100%
3/3
25%
1/4
1.133
SWTBotShell$5
100%
3/3
50%
1/2
1.133
 
 1  7
 /*******************************************************************************
 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.Shell;
 15  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 16  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 17  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 18  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 19  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 20  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 21  
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 22  
 import org.hamcrest.SelfDescribing;
 23  
 
 24  
 /**
 25  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 26  
  * @version $Id$
 27  
  */
 28  
 @SWTBotWidget(clasz = Shell.class, preferredName = "shell")
 29  
 public class SWTBotShell extends AbstractSWTBotControl<Shell> {
 30  
 
 31  
         /**
 32  
          * Constructs an instance of this with the given shell.
 33  
          *
 34  
          * @param shell the widget.
 35  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 36  
          */
 37  
         public SWTBotShell(Shell shell) throws WidgetNotFoundException {
 38  50
                 this(shell, null);
 39  50
         }
 40  
 
 41  
         /**
 42  
          * Constructs an instance of this with the given shell.
 43  
          *
 44  
          * @param shell the widget.
 45  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 46  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 47  
          */
 48  
         public SWTBotShell(Shell shell, SelfDescribing description) throws WidgetNotFoundException {
 49  50
                 super(shell, description);
 50  50
         }
 51  
 
 52  
         // @Override
 53  
         // protected Widget findWidget(int index) throws WidgetNotFoundException {
 54  
         // // could have used a matcher, but that would just slow down things
 55  
         // Shell[] shells = finder.getShells();
 56  
         // for (int i = 0; i < shells.length; i++) {
 57  
         // Shell shell = shells[i];
 58  
         // if (new SWTBotShell(shell).getText().equals(text))
 59  
         // return shell;
 60  
         // }
 61  
         // throw new WidgetNotFoundException("Cound not find shell matching text:" + text);
 62  
         // }
 63  
 
 64  
         /**
 65  
          * Activates the shell.
 66  
          * @return itself.
 67  
          *
 68  
          * @throws TimeoutException if the shell could not be activated
 69  
          */
 70  
         public SWTBotShell activate() throws TimeoutException {
 71  84
                 new SWTBot().waitUntil(new DefaultCondition() {
 72  
                         public String getFailureMessage() {
 73  0
                                 return "Timed out waiting for " + SWTUtils.toString(widget) + " to get activated"; //$NON-NLS-1$ //$NON-NLS-2$
 74  
                         }
 75  
 
 76  
                         public boolean test() throws Exception {
 77  28
                                 syncExec(new VoidResult() {
 78  
                                         public void run() {
 79  28
                                                 widget.forceActive();
 80  28
                                                 widget.forceFocus();
 81  28
                                         }
 82  
                                 });
 83  28
                                 return isActive();
 84  
                         }
 85  
                 });
 86  28
                 notify(SWT.Activate);
 87  28
                 return this;
 88  
         }
 89  
 
 90  
         /**
 91  
          * Closes the shell
 92  
          *
 93  
          * @throws TimeoutException if the shell does not close.
 94  
          */
 95  
         public void close() throws TimeoutException {
 96  7
                 notify(SWT.Close);
 97  7
                 asyncExec(new VoidResult() {
 98  
                         public void run() {
 99  
                                 // TODO investigate bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=259895
 100  7
                                 if (!widget.isDisposed())
 101  7
                                         widget.close();
 102  7
                         }
 103  
                 });
 104  7
                 new SWTBot().waitUntil(new DefaultCondition() {
 105  
                         public boolean test() throws Exception {
 106  7
                                 return !isOpen();
 107  
                         }
 108  
 
 109  
                         public String getFailureMessage() {
 110  0
                                 return "Timed out waiting for " + SWTUtils.toString(widget) + " to close."; //$NON-NLS-1$ //$NON-NLS-2$
 111  
                         }
 112  
                 });
 113  7
         }
 114  
 
 115  
         /**
 116  
          * Checks if the shell is open.
 117  
          *
 118  
          * @return <code>true</code> if the shell is visible, <code>false</code> otherwise.
 119  
          */
 120  
         public boolean isOpen() {
 121  7
                 return syncExec(new BoolResult() {
 122  
                         public Boolean run() {
 123  7
                                 return !widget.isDisposed() && widget.isVisible();
 124  
                         }
 125  
                 });
 126  
         }
 127  
 
 128  
         /**
 129  
          * Checks if the shell is active.
 130  
          *
 131  
          * @return <code>true</code> if the shell is active, <code>false</code> otherwise.
 132  
          */
 133  
         public boolean isActive() {
 134  28
                 return syncExec(new BoolResult() {
 135  
                         public Boolean run() {
 136  28
                                 return display.getActiveShell() == widget;
 137  
                         }
 138  
                 });
 139  
         }
 140  
 
 141  
         /**
 142  
          * Returns a SWTBot instance that matches the contents of this shell.
 143  
          * 
 144  
          * @return SWTBot
 145  
          */
 146  
         public SWTBot bot() {
 147  
                 return new SWTBot(widget);
 148  
         }
 149  
 
 150  
 }