diff options
Diffstat (limited to 'org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java')
-rw-r--r-- | org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java index fda837ec..c740ffff 100644 --- a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java +++ b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotView.java @@ -12,12 +12,23 @@ package org.eclipse.swtbot.eclipse.finder.widgets; import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.equalTo; + +import java.util.List; import javax.swing.text.View; import org.eclipse.swt.widgets.Control; import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; +import org.eclipse.swtbot.eclipse.finder.finders.CommandFinder; +import org.eclipse.swtbot.eclipse.finder.finders.ViewMenuFinder; +import org.eclipse.swtbot.eclipse.finder.widgets.utils.PartLabelDescription; +import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; +import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; import org.eclipse.swtbot.swt.finder.results.VoidResult; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; import org.eclipse.ui.IViewReference; import org.hamcrest.SelfDescribing; @@ -30,6 +41,7 @@ import org.hamcrest.SelfDescribing; */ public class SWTBotView extends SWTBotWorkbenchPart<IViewReference> { + private final ViewMenuFinder menuFinder; /** * Creates an instance of a view part. * @@ -38,7 +50,7 @@ public class SWTBotView extends SWTBotWorkbenchPart<IViewReference> { * @since 2.0 */ public SWTBotView(IViewReference partReference, SWTWorkbenchBot bot) { - super(partReference, bot); + this(partReference, bot, new PartLabelDescription<IViewReference>(partReference)); } /** @@ -50,6 +62,7 @@ public class SWTBotView extends SWTBotWorkbenchPart<IViewReference> { */ public SWTBotView(IViewReference partReference, SWTWorkbenchBot bot, SelfDescribing description) { super(partReference, bot, description); + this.menuFinder = new ViewMenuFinder(); } public void setFocus() { @@ -71,4 +84,58 @@ public class SWTBotView extends SWTBotWorkbenchPart<IViewReference> { return partReference.getPage().getActivePartReference() == partReference; } + /** + * Close the partReference. + */ + public void close() { + UIThreadRunnable.syncExec(new VoidResult() { + public void run() { + partReference.getPage().hideView(partReference); + } + }); + } + + /** + * Gets a list of all menus within the partReference. This will also include sub menus. + * + * @return The list of menus + */ + public List<SWTBotViewMenu> menus() { + return menuFinder.findMenus(partReference, anything(), true); + } + + /** + * Gets a menu item matching the give label within the partReference menu if one exists. + * + * @param label The label matching name in the menu. + * @return The {@link SWTBotMenu} item. + * @throws WidgetNotFoundException Thrown if the menu can not be found or if the partReference does not contain a + * menu. + */ + public SWTBotViewMenu menu(String label) throws WidgetNotFoundException { + return menu(label, 0); + } + + /** + * Gets a menu item matching the give label within the partReference menu if one exists. + * + * @param label The label matching name in the menu. + * @param index The index of the menu to choose. + * @return The {@link SWTBotMenu} item. + * @throws WidgetNotFoundException Thrown if the menu can not be found or if the partReference does not contain a + * menu. + */ + public SWTBotViewMenu menu(String label, int index) throws WidgetNotFoundException { + try { + List<SWTBotViewMenu> menuItems = menuFinder.findMenus(partReference, withMnemonic(label), true); + if ((menuItems == null) || (menuItems.size() < 1)) { + CommandFinder finder = new CommandFinder(); + List<SWTBotCommand> command = finder.findCommand(equalTo(label)); + return command.get(index); + } + return menuItems.get(index); + } catch (Exception e) { + throw new WidgetNotFoundException("Could not find view menu with label " + label + " at index " + index, e); //$NON-NLS-1$ //$NON-NLS-2$ + } + } } |