Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotCheckBox
93%
41/44
100%
4/4
1.417
SWTBotCheckBox$1
100%
5/5
N/A
1.417
SWTBotCheckBox$2
100%
5/5
N/A
1.417
SWTBotCheckBox$3
100%
6/6
100%
4/4
1.417
SWTBotCheckBox$4
100%
3/3
N/A
1.417
 
 1  10
 /*******************************************************************************
 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.Button;
 15  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 16  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 17  
 import org.eclipse.swtbot.swt.finder.Style;
 18  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 19  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 20  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 21  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 22  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 23  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 24  
 import org.hamcrest.SelfDescribing;
 25  
 
 26  
 /**
 27  
  * Represents a checkbox {@link Button} of type {@link SWT#CHECK}.
 28  
  *
 29  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 30  
  * @version $Id$
 31  
  * @see SWTBotButton
 32  
  * @see SWTBotRadio
 33  
  * @see SWTBotToggleButton
 34  
  */
 35  
 @SWTBotWidget(clasz = Button.class, style = @Style(name = "SWT.CHECK", value = SWT.CHECK), preferredName = "checkBox", referenceBy = { ReferenceBy.LABEL, ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP })//$NON-NLS-1$
 36  
 public class SWTBotCheckBox extends AbstractSWTBotControl<Button> {
 37  
 
 38  
         /**
 39  
          * Constructs an instance of this object with the given button (Checkbox)
 40  
          *
 41  
          * @param w the widget.
 42  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 43  
          * @since 1.0
 44  
          */
 45  
         public SWTBotCheckBox(Button w) throws WidgetNotFoundException {
 46  0
                 this(w, null);
 47  0
         }
 48  
 
 49  
         /**
 50  
          * Constructs an instance of this object with the given button (Checkbox)
 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  
          * @since 1.0
 56  
          */
 57  
         public SWTBotCheckBox(Button w, SelfDescribing description) throws WidgetNotFoundException {
 58  725
                 super(w, description);
 59  725
                 Assert.isTrue(SWTUtils.hasStyle(w, SWT.CHECK), "Expecting a checkbox."); //$NON-NLS-1$
 60  725
         }
 61  
 
 62  
         /**
 63  
          * Click on the checkbox, toggle it.
 64  
          */
 65  
         public SWTBotCheckBox click() {
 66  10
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 67  10
                 toggle();
 68  10
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 69  10
                 return this;
 70  
         }
 71  
 
 72  
         /**
 73  
          * Deselect the checkbox.
 74  
          */
 75  
         public void deselect() {
 76  239
                 log.debug(MessageFormat.format("Deselecting {0}", this)); //$NON-NLS-1$
 77  239
                 waitForEnabled();
 78  239
                 if (!isChecked()) {
 79  81
                         log.debug(MessageFormat.format("Widget {0} already deselected, not deselecting again.", this)); //$NON-NLS-1$
 80  81
                         return;
 81  
                 }
 82  158
                 asyncExec(new VoidResult() {
 83  
                         public void run() {
 84  158
                                 log.debug(MessageFormat.format("Deselecting {0}", this)); //$NON-NLS-1$
 85  158
                                 widget.setSelection(false);
 86  158
                         }
 87  
                 });
 88  158
                 notifyListeners();
 89  158
         }
 90  
 
 91  
         /**
 92  
          * Select the checkbox.
 93  
          */
 94  
         public void select() {
 95  471
                 log.debug(MessageFormat.format("Selecting {0}", this)); //$NON-NLS-1$
 96  471
                 waitForEnabled();
 97  471
                 if (isChecked()) {
 98  300
                         log.debug(MessageFormat.format("Widget {0} already selected, not selecting again.", this)); //$NON-NLS-1$
 99  300
                         return;
 100  
                 }
 101  171
                 asyncExec(new VoidResult() {
 102  
                         public void run() {
 103  171
                                 log.debug(MessageFormat.format("Selecting {0}", this)); //$NON-NLS-1$
 104  171
                                 widget.setSelection(true);
 105  171
                         }
 106  
                 });
 107  171
                 notifyListeners();
 108  171
         }
 109  
 
 110  
         /**
 111  
          * Toggle the checkbox.
 112  
          */
 113  
         protected void toggle() {
 114  10
                 waitForEnabled();
 115  10
                 asyncExec(new VoidResult() {
 116  
                         public void run() {
 117  20
                                 log.debug(MessageFormat.format("Toggling state on {0}. Setting state to {1}", widget, (!widget.getSelection() ? "selected" //$NON-NLS-1$ //$NON-NLS-2$
 118  5
                                                 : "unselected"))); //$NON-NLS-1$
 119  10
                                 widget.setSelection(!widget.getSelection());
 120  10
                         }
 121  
                 });
 122  10
                 notifyListeners();
 123  10
         }
 124  
 
 125  
         /**
 126  
          * notify listeners about checkbox state change.
 127  
          */
 128  
         protected void notifyListeners() {
 129  339
                 notify(SWT.MouseEnter);
 130  339
                 notify(SWT.MouseMove);
 131  339
                 notify(SWT.Activate);
 132  339
                 notify(SWT.FocusIn);
 133  339
                 notify(SWT.MouseDown);
 134  339
                 notify(SWT.MouseUp);
 135  339
                 notify(SWT.Selection);
 136  339
                 notify(SWT.MouseHover);
 137  339
                 notify(SWT.MouseMove);
 138  339
                 notify(SWT.MouseExit);
 139  339
                 notify(SWT.Deactivate);
 140  339
                 notify(SWT.FocusOut);
 141  339
         }
 142  
 
 143  
         /**
 144  
          * Gets if the checkbox button is checked.
 145  
          *
 146  
          * @return <code>true</code> if the checkbox is checked. Otherwise <code>false</code>.
 147  
          */
 148  
         public boolean isChecked() {
 149  713
                 return syncExec(new BoolResult() {
 150  
                         public Boolean run() {
 151  713
                                 return widget.getSelection();
 152  
                         }
 153  
                 });
 154  
         }
 155  
 
 156  
 }