Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarToggleButton
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotToolbarToggleButton
86%
19/22
50%
2/4
1.2
SWTBotToolbarToggleButton$1
100%
4/4
100%
2/2
1.2
SWTBotToolbarToggleButton$2
100%
3/3
N/A
1.2
 
 1  6
 /*******************************************************************************
 2  
  * Copyright (c) 2009 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.ToolItem;
 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 tool item of type checkbox
 28  
  *
 29  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 30  
  * @version $Id$
 31  
  */
 32  
 @SWTBotWidget(clasz = ToolItem.class, preferredName = "toolbarToggleButton", style = @Style(name = "SWT.CHECK", value = SWT.CHECK), referenceBy = {
 33  
                 ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP }, returnType = SWTBotToolbarToggleButton.class)
 34  
 public class SWTBotToolbarToggleButton extends SWTBotToolbarButton {
 35  
 
 36  
         /**
 37  
          * Constructs an new instance of this item.
 38  
          *
 39  
          * @param w the tool item.
 40  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 41  
          */
 42  
         public SWTBotToolbarToggleButton(ToolItem w) throws WidgetNotFoundException {
 43  0
                 this(w, null);
 44  0
         }
 45  
 
 46  
         /**
 47  
          * Constructs an new instance of this item.
 48  
          *
 49  
          * @param w the tool item.
 50  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 51  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 52  
          */
 53  
         public SWTBotToolbarToggleButton(ToolItem w, SelfDescribing description) throws WidgetNotFoundException {
 54  7
                 super(w, description);
 55  7
                 Assert.isTrue(SWTUtils.hasStyle(w, SWT.CHECK), "Expecting a checkbox."); //$NON-NLS-1$
 56  7
         }
 57  
 
 58  
         /**
 59  
          * Click on the tool item. This will toggle the tool item.
 60  
          *
 61  
          * @return itself
 62  
          */
 63  
         public SWTBotToolbarToggleButton toggle() {
 64  6
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 65  6
                 waitForEnabled();
 66  6
                 internalToggle();
 67  6
                 sendNotifications();
 68  6
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 69  6
                 return this;
 70  
         }
 71  
 
 72  
         public SWTBotToolbarToggleButton click() {
 73  2
                 return toggle();
 74  
         }
 75  
 
 76  
         private void internalToggle() {
 77  6
                 syncExec(new VoidResult() {
 78  
                         public void run() {
 79  6
                                 widget.setSelection(!widget.getSelection());
 80  6
                         }
 81  
                 });
 82  6
         }
 83  
 
 84  
         /**
 85  
          * Selects the checkbox button.
 86  
          */
 87  
         public void select() {
 88  2
                 if (!isChecked())
 89  2
                         toggle();
 90  2
         }
 91  
 
 92  
         /**
 93  
          * Deselects the checkbox button.
 94  
          */
 95  
         public void deselect() {
 96  1
                 if (isChecked())
 97  1
                         toggle();
 98  1
         }
 99  
 
 100  
         /**
 101  
          * @return <code>true</code> if the button is checked, <code>false</code> otherwise.
 102  
          */
 103  
         public boolean isChecked() {
 104  11
                 return syncExec(new BoolResult() {
 105  
                         public Boolean run() {
 106  11
                                 return widget.getSelection();
 107  
                         }
 108  
                 });
 109  
         }
 110  
 }