Coverage Report - org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarDropDownButton
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTBotToolbarDropDownButton
18%
8/44
0%
0/4
1.5
 
 1  0
 /*******************************************************************************
 2  
  * Copyright (c) 2008, 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  
  *     Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=88
 11  
  *******************************************************************************/
 12  
 package org.eclipse.swtbot.swt.finder.widgets;
 13  
 
 14  
 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;
 15  
 
 16  
 import java.util.ArrayList;
 17  
 import java.util.List;
 18  
 
 19  
 import org.eclipse.swt.SWT;
 20  
 import org.eclipse.swt.widgets.Event;
 21  
 import org.eclipse.swt.widgets.MenuItem;
 22  
 import org.eclipse.swt.widgets.ToolItem;
 23  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 24  
 import org.eclipse.swtbot.swt.finder.SWTBot;
 25  
 import org.eclipse.swtbot.swt.finder.SWTBotWidget;
 26  
 import org.eclipse.swtbot.swt.finder.Style;
 27  
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
 28  
 import org.eclipse.swtbot.swt.finder.finders.EventContextMenuFinder;
 29  
 import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 30  
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 31  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 32  
 import org.hamcrest.Matcher;
 33  
 import org.hamcrest.SelfDescribing;
 34  
 
 35  
 /**
 36  
  * This represents a toolbar item that is a drop down button.
 37  
  *
 38  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 39  
  * @version $Id$
 40  
  * @since 1.2
 41  
  */
 42  
 @SWTBotWidget(clasz = ToolItem.class, preferredName = "toolbarDropDownButton", style = @Style(name = "SWT.DROP_DOWN", value = SWT.DROP_DOWN), referenceBy = {
 43  
                 ReferenceBy.MNEMONIC, ReferenceBy.TOOLTIP}, returnType = SWTBotToolbarDropDownButton.class )
 44  
 public class SWTBotToolbarDropDownButton extends SWTBotToolbarButton {
 45  
 
 46  
         /**
 47  
          * Constructs an new instance of this item.
 48  
          *
 49  
          * @param w the tool item.
 50  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 51  
          */
 52  
         public SWTBotToolbarDropDownButton(ToolItem w) throws WidgetNotFoundException {
 53  0
                 this(w, null);
 54  0
         }
 55  
 
 56  
         /**
 57  
          * Constructs an new instance of this item.
 58  
          * 
 59  
          * @param w the tool item.
 60  
          * @param description the description of the widget, this will be reported by {@link #toString()}
 61  
          * @throws WidgetNotFoundException if the widget is <code>null</code> or widget has been disposed.
 62  
          */
 63  
         public SWTBotToolbarDropDownButton(ToolItem w, SelfDescribing description) throws WidgetNotFoundException {
 64  6
                 super(w, description);
 65  6
                 Assert.isTrue(SWTUtils.hasStyle(w, SWT.DROP_DOWN), "Expecting a drop down button."); //$NON-NLS-1$
 66  
 
 67  6
         }
 68  
 
 69  
         /**
 70  
          * Finds the submenu inside this menu item.
 71  
          * <p>
 72  
          * <b>NOTE:</b>Invoking this keeps the menu open until you click on it.
 73  
          * </p>
 74  
          * 
 75  
          * @param menuItem the submenu to search
 76  
          * @return the menu item with the specified text
 77  
          * @since 1.0
 78  
          */
 79  
         public SWTBotMenu menuItem(String menuItem) {
 80  0
                 Matcher<MenuItem> withMnemonic = withMnemonic(menuItem);
 81  0
                 return menuItem(withMnemonic);
 82  
         }
 83  
 
 84  
         /**
 85  
          * Finds all the submenu inside this menu item that match the given matcher.
 86  
          * <p>
 87  
          * <b>NOTE:</b>Invoking this keeps the menu open until you click on it.
 88  
          * </p>
 89  
          * 
 90  
          * @param matcher the matcher
 91  
          * @return the menu item with the specified text
 92  
          */
 93  
         public SWTBotMenu menuItem(Matcher<MenuItem> matcher) {
 94  0
                 return menuItems(matcher).get(0);
 95  
         }
 96  
 
 97  
         /**
 98  
          * Finds all the submenus inside this menu item that match the given matcher.
 99  
          * <p>
 100  
          * <b>NOTE:</b>Invoking this keeps the menu open until you click on it.
 101  
          * </p>
 102  
          * 
 103  
          * @param matcher the matcher
 104  
          * @return the menu items matching the matcher.
 105  
          * @throws WidgetNotFoundException if the menuItem could not be found
 106  
          */
 107  
         public List<? extends SWTBotMenu> menuItems(Matcher<MenuItem> matcher) {
 108  0
                 EventContextMenuFinder menuFinder = new EventContextMenuFinder();
 109  
                 try {
 110  0
                         menuFinder.register();
 111  0
                         log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 112  0
                         waitForEnabled();
 113  0
                         notify(SWT.MouseEnter);
 114  0
                         notify(SWT.MouseMove);
 115  0
                         notify(SWT.Activate);
 116  0
                         notify(SWT.FocusIn);
 117  0
                         notify(SWT.MouseDown);
 118  0
                         notify(SWT.MouseUp);
 119  0
                         notify(SWT.Selection, arrowEvent());
 120  0
                         notify(SWT.MouseHover);
 121  0
                         notify(SWT.MouseMove);
 122  0
                         notify(SWT.MouseExit);
 123  0
                         notify(SWT.Deactivate);
 124  0
                         notify(SWT.FocusOut);
 125  0
                         log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 126  
                         List<MenuItem> findMenus = menuFinder.findMenus(new SWTBot().activeShell().widget, matcher, true);
 127  0
                         return toSWTBotMenuItems(matcher, findMenus);
 128  0
                 } finally {
 129  0
                         menuFinder.unregister();
 130  0
                 }
 131  
         }
 132  
 
 133  
         /**
 134  
          * Click on the tool item.
 135  
          *
 136  
          * @since 1.0
 137  
          */
 138  
         public SWTBotToolbarDropDownButton click() {
 139  2
                 log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
 140  2
                 waitForEnabled();
 141  2
                 sendNotifications();
 142  2
                 log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
 143  2
                 return this;
 144  
         }
 145  
 
 146  
         private ArrayList<SWTBotMenu> toSWTBotMenuItems(Matcher<?> matcher, List<MenuItem> findMenus) {
 147  0
                 ArrayList<SWTBotMenu> result = new ArrayList<SWTBotMenu>(findMenus.size());
 148  0
                 for (MenuItem menuItem : findMenus) {
 149  0
                         result.add(new SWTBotMenu(menuItem, matcher));
 150  
                 }
 151  
 
 152  0
                 if (result.isEmpty())
 153  0
                         throw new WidgetNotFoundException("Could not find a menu item"); //$NON-NLS-1$
 154  0
                 return result;
 155  
         }
 156  
 
 157  
         /**
 158  
          * Gets the arrow event.
 159  
          * 
 160  
          * @return The event.
 161  
          */
 162  
         private Event arrowEvent() {
 163  0
                 Event event = createEvent();
 164  0
                 event.detail = SWT.ARROW;
 165  0
                 return event;
 166  
         }
 167  
 }