Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotMenu
88%
16/18
N/A
1.273
SWTBotMenu$1
80%
4/5
25%
1/4
1.273
SWTBotMenu$2
87%
7/8
50%
1/2
1.273
SWTBotMenu$3
100%
3/3
N/A
1.273
SWTBotMenu$4
0%
0/3
N/A
1.273
 
 1  8
 /*******************************************************************************
 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 static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
 14  
 
 15  
 import java.util.List;
 16  
 
 17  
 import org.eclipse.swt.SWT;
 18  
 import org.eclipse.swt.widgets.Menu;
 19  
 import org.eclipse.swt.widgets.MenuItem;
 20  
 import org.eclipse.swt.widgets.Widget;
 21  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 22  
 import org.eclipse.swtbot.swt.finder.finders.MenuFinder;
 23  
 import org.eclipse.swtbot.swt.finder.results.BoolResult;
 24  
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 25  
 import org.eclipse.swtbot.swt.finder.results.WidgetResult;
 26  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 27  
 import org.hamcrest.Matcher;
 28  
 import org.hamcrest.SelfDescribing;
 29  
 
 30  
 /**
 31  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 32  
  * @version $Id$
 33  
  */
 34  
 public class SWTBotMenu extends AbstractSWTBot<MenuItem> {
 35  
 
 36  
         /**
 37  
          * @param w the widget.
 38  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 39  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 40  
          */
 41  
         public SWTBotMenu(MenuItem w, SelfDescribing description) throws WidgetNotFoundException {
 42  17
                 super(w, description);
 43  17
         }
 44  
 
 45  
         /**
 46  
          * @param w the widget.
 47  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 48  
          */
 49  
         public SWTBotMenu(MenuItem w) throws WidgetNotFoundException {
 50  1
                 this(w, null);
 51  1
         }
 52  
 
 53  
         /**
 54  
          * Clicks on the menu item
 55  
          */
 56  
         public SWTBotMenu click() {
 57  5
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 58  5
                 waitForEnabled();
 59  5
                 toggleSelection();
 60  5
                 notify(SWT.Selection);
 61  5
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 62  5
                 return this;
 63  
         }
 64  
 
 65  
         /**
 66  
          * Toggle the selection of the checkbox if applicable.
 67  
          */
 68  
         private void toggleSelection() {
 69  5
                 syncExec(new VoidResult() {
 70  
                         public void run() {
 71  5
                                 if (hasStyle(widget, SWT.CHECK) | hasStyle(widget, SWT.RADIO))
 72  0
                                         widget.setSelection(!widget.getSelection());
 73  5
                         }
 74  
                 });
 75  5
         }
 76  
 
 77  
         /**
 78  
          * Gets the menu matching the given name.
 79  
          *
 80  
          * @param menuName the name of the menu item that is to be found
 81  
          * @return the first menu that matches the menuName
 82  
          * @throws WidgetNotFoundException if the widget is not found.
 83  
          */
 84  
         public SWTBotMenu menu(final String menuName) throws WidgetNotFoundException {
 85  4
                 final Matcher<? extends Widget> matcher = withMnemonic(menuName);
 86  4
                 MenuItem menuItem = syncExec(new WidgetResult<MenuItem>() {
 87  
                         public MenuItem run() {
 88  4
                                 Menu bar = widget.getMenu();
 89  4
                                 Matcher<MenuItem> withMnemonic = withMnemonic(menuName);
 90  4
                                 List<MenuItem> menus = new MenuFinder().findMenus(bar, withMnemonic, true);
 91  4
                                 if (!menus.isEmpty())
 92  4
                                         return menus.get(0);
 93  0
                                 return null;
 94  
                         }
 95  
                 });
 96  4
                 return new SWTBotMenu(menuItem, matcher);
 97  
         }
 98  
 
 99  
         @Override
 100  
         public boolean isEnabled() {
 101  5
                 return syncExec(new BoolResult() {
 102  
                         public Boolean run() {
 103  5
                                 return widget.isEnabled();
 104  
                         }
 105  
                 });
 106  
         }
 107  
 
 108  
         /**
 109  
          * Gets if this menu item is checked.
 110  
          *
 111  
          * @return <code>true</code> if the menu is checked, <code>false</code> otherwise.
 112  
          * @see MenuItem#getSelection()
 113  
          * @since 1.2
 114  
          */
 115  
         public boolean isChecked() {
 116  0
                 return syncExec(new BoolResult() {
 117  
                         public Boolean run() {
 118  0
                                 return widget.getSelection();
 119  
                         }
 120  
                 });
 121  
         }
 122  
 }