Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotRadio
96%
28/29
100%
8/8
2.444
SWTBotRadio$1
100%
4/4
N/A
2.444
SWTBotRadio$2
100%
4/4
N/A
2.444
SWTBotRadio$3
90%
9/10
70%
7/10
2.444
SWTBotRadio$4
100%
3/3
N/A
2.444
 
 1  66
 /*******************************************************************************
 2  
  * Copyright (c) 2008,2010 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.Button;
 15  
 import org.eclipse.swt.widgets.Widget;
 16  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 17  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 18  
 import org.eclipse.swtbot.swt.finder.Style;
 19  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 20  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 21  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 22  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 23  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 24  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 25  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 26  
 import org.hamcrest.SelfDescribing;
 27  
 
 28  
 /**
 29  
  * Represents a radio {@link Button} of type {@link SWT#RADIO}.
 30  
  *
 31  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 32  
  * @version $Id$
 33  
  * @see SWTBotButton
 34  
  * @see SWTBotCheckBox
 35  
  * @see SWTBotToggleButton
 36  
  */
 37  
 @SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.RADIO", value = SWT.RADIO), preferredName = "radio", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP })//$NON-NLS-1$
 38  
 public class SWTBotRadio extends AbstractSWTBotControl<Button> {
 39  
 
 40  
         /**
 41  
          * Constructs an instance of this with the given widget.
 42  
          *
 43  
          * @param w the widget.
 44  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 45  
          */
 46  
         public SWTBotRadio(Button w) throws WidgetNotFoundException {
 47  31
                 this(w, null);
 48  31
         }
 49  
 
 50  
         /**
 51  
          * Constructs an instance of this with the given widget.
 52  
          *
 53  
          * @param w the widget.
 54  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 55  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 56  
          */
 57  
         public SWTBotRadio(Button w, SelfDescribing description) throws WidgetNotFoundException {
 58  96
                 super(w, description);
 59  96
                 Assert.isTrue(SWTUtils.hasStyle(w, SWT.RADIO), "Expecting a radio."); //$NON-NLS-1$
 60  96
         }
 61  
 
 62  
         /**
 63  
          * Selects the radio button.
 64  
          */
 65  
         public SWTBotRadio click() {
 66  55
                 if (isSelected()) {
 67  22
                         log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); //$NON-NLS-1$
 68  22
                         return this;
 69  
                 }
 70  33
                 waitForEnabled();
 71  
 
 72  33
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 73  
 
 74  33
                 final SWTBotRadio otherSelectedButton = otherSelectedButton();
 75  
 
 76  33
                 if (otherSelectedButton != null) {
 77  31
                         otherSelectedButton.notify(SWT.Deactivate);
 78  31
                         asyncExec(new VoidResult() {
 79  
                                 public void run() {
 80  31
                                         otherSelectedButton.widget.setSelection(false);
 81  31
                                 }
 82  
                         });
 83  
                 }
 84  
 
 85  33
                 notify(SWT.Activate);
 86  33
                 notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
 87  33
                 notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));
 88  33
                 asyncExec(new VoidResult() {
 89  
                         public void run() {
 90  33
                                 widget.setSelection(true);
 91  33
                         }
 92  
                 });
 93  33
                 notify(SWT.Selection);
 94  33
                 if (otherSelectedButton != null) {
 95  31
                         otherSelectedButton.notify(SWT.Selection);
 96  
                 }
 97  33
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 98  33
                 return this;
 99  
         }
 100  
 
 101  
         private SWTBotRadio otherSelectedButton() {
 102  33
                 Button button = syncExec(new WidgetResult<Button>() {
 103  
                         public Button run() {
 104  33
                                 if (hasStyle(widget.getParent(), SWT.NO_RADIO_GROUP))
 105  0
                                         return null;
 106  33
                                 Widget[] siblings = SWTUtils.siblings(widget);
 107  48
                                 for (Widget widget : siblings) {
 108  46
                                         if ((widget instanceof Button) && hasStyle(widget, SWT.RADIO))
 109  46
                                                 if (((Button) widget).getSelection())
 110  31
                                                         return (Button) widget;
 111  
                                 }
 112  2
                                 return null;
 113  
                         }
 114  
                 });
 115  
 
 116  33
                 if (button != null)
 117  31
                         return new SWTBotRadio(button);
 118  2
                 return null;
 119  
         }
 120  
 
 121  
         /**
 122  
          * Checks if the item is selected.
 123  
          *
 124  
          * @return <code>true</code> if the radio button is selected. Otherwise <code>false</code>.
 125  
          */
 126  
         public boolean isSelected() {
 127  61
                 return syncExec(new BoolResult() {
 128  
                         public Boolean run() {
 129  61
                                 return widget.getSelection();
 130  
                         }
 131  
                 });
 132  
         }
 133  
 }