Pop Up Menu Item Enablement

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

Pop Up Menu Item Enablement

Postby Otisler » Wed Dec 05, 2007 6:16 am

Hi, I was wondering if there was a way to check whether or not a right-click menu item is enabled/disabled in the test.

My RCP has the following pop-up ObjectContribution Action set up:

<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="false"
id="ZbiIde.setDebugPopup"
objectClass="zbiide.views.IDebuggablePrinter">
<action
class="zbiide.actions.SetDebugPrinterAction"
icon="icons/debug_view.gif"
id="popup.action.SetDebugPrinterAction"
label="Create Debug Connection"
menubarPath="additions"/>
<visibility>
<not>
<objectState
name="createDebug"
value="enable"/>
</not>
</visibility>
</objectContribution>
</extension>

Now depending on some stuff in my RCP app, this action is either enabled or disabled.

In my test, I can say:
ui.contextClick(new TreeItemLocator("Virtual Printers/TestPrinter", new ViewLocator("ZbiIde.SearchView")), "Create Debug Connection");

This line will find the Item in the tree (Virtual Printers/TestPrinter) and right click on it, then in the right-click menu list find the proper action. (Create Debug Connection)

Of course, the problem is, that menu-item might be disabled.
I know the WindowTester API has the isEnabled Condition. But while looking at it, I couldn't find a way to find a menu-item, only Widget items, like Buttons, Text fields, etc.

So, is there a way to simply right click on an object, find the list of menu-items under the object which was clicked on, then check whether or not each is enabled?

Thanks
Otisler
 
Posts: 28
Joined: Tue Mar 27, 2007 1:24 pm
Location: Vernon Hills IL

Postby Phil Quitslund » Wed Dec 05, 2007 11:57 am

There is not currently a way to do this but it's a great use case for an assertion. If you're interested in tracking this, please send a feature-request to:

wintest-support@instantiations.com

Thanks!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Postby Mac » Fri Dec 28, 2007 5:14 am

Hi,

we solved the problem like this:
-------------------------------------------------------------
Code: Select all
/**
     * Click an item in a menu.
     * @param ui The <code>IUIContext</code> to perform ui actions.
     * @param pathToMenu
     * @param menuItemName
     * @param mustBeEnabled
     * @throws WidgetSearchException
     */
    public static void clickMenuItem(final IUIContext ui, final String pathToMenu, final String menuItemName,
            final boolean mustBeEnabled) throws WidgetSearchException {
        MenuItemTester menuItemTester = new MenuItemTester();
        MenuItem menuItem = (MenuItem) getWidget(ui, new SWTWidgetLocator(MenuItem.class,
                pathToMenu));
        Menu menu = menuItemTester.getMenu(menuItem);

        String pathToMenuItem = pathToMenu + "/" + menuItemName;
        MenuTester menuTester = new MenuTester();
        MenuItem[] items = menuTester.getItems(menu);
        for (int i = 0; i < items.length; ++i) {
            MenuItem item = items[i];
            if (menuItemTester.getText(item).equals(menuItemName)) {
                boolean isEnabled = menuItemTester.getEnabled(item);
                if (mustBeEnabled) {
                    if (!isEnabled) {
                       fail("Menu item '" + pathToMenuItem + "' is not enabled");
                    }
                }
                if (isEnabled) {
                    MenuItemLocator menuLocator = new MenuItemLocator(pathToMenuItem);
                    ui.click(menuLocator);
                }
                break;
            }
        }

    }





/**
     * Returns the widget for the corresponding locator.
     * @param locator
     * @throws WidgetSearchException
     */
    public static Widget getWidget(final IUIContext ui, IWidgetLocator locator) throws WidgetSearchException {
        IWidgetLocator loc = ui.find(locator);
        if (loc instanceof WidgetReference) {
            WidgetReference reference = (WidgetReference) loc;
            final Object widget = reference.getWidget();
            if (widget instanceof Widget) {
                return (Widget) widget;
            }
        }
        return null;
    }

Mac
 
Posts: 14
Joined: Thu Apr 05, 2007 4:19 am

Postby Phil Quitslund » Fri Dec 28, 2007 8:59 am

Cool!

Ultimately, we'd like to roll similar logic into a condition to make this a first-class "assertable" but this is really great in the interim. Thanks for sharing your solution!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Pop Up Menu Item Enablement

Postby Bosrup » Wed Apr 09, 2008 6:09 am

Hi!

Has there been any progress in making a condition out of this? I'm curious, becuase I'm faced with the same problem as the original poster.

Best regards,
Joel
Bosrup
 
Posts: 2
Joined: Wed Apr 09, 2008 6:06 am

Re: Pop Up Menu Item Enablement

Postby Phil Quitslund » Fri Apr 11, 2008 12:24 pm

Have you tried Mac's example code? Did that work for you?
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Pop Up Menu Item Enablement

Postby Bosrup » Tue Aug 19, 2008 9:44 pm

Hi!

Realize it's been a while since I posted here, but I thought I'd share my solution...

I found out that the keystroke combination SHIFT + F10 in Windows brings out the context menu for the current mouse position, regardless of what application you're running. So I simply send a SHIFT + F10 using WT and look for the expected menu item to check if it's enabled or not. Works? Yes. Ugly? Oh yeah! :-)

Code: Select all

public void checkRightClickMenuItemStatus(
         final String menuItemName,
         final boolean expStatus)

   {
      // SHIFT + F10 to bring out the context menu
      ui.keyClick(WT.SHIFT | WT.F10);
      Display.getDefault().syncExec(new Runnable()
      {
         public void run()
         {
            MenuItem aMenuItem = null;
            try
            {
               aMenuItem = (MenuItem) ((WidgetReference) ui
                     .find(new MenuItemLocator(menuItemName))).getWidget();
            }
            catch (WidgetSearchException e)
            {
               fail("Context menu not found");
            }
            final boolean realStatus;
            realStatus = aMenuItem.getEnabled();
            // Send ESC to remove the context menu, otherwise WT will be confused
            ui.keyClick(WT.ESC);
            assertEquals(
                  "Menu item not enabled/disabled as it should",
                  expStatus,
                  realStatus);
         }
      });



/Joel
Bosrup
 
Posts: 2
Joined: Wed Apr 09, 2008 6:06 am

Re: Pop Up Menu Item Enablement

Postby Phil Quitslund » Wed Aug 20, 2008 8:32 am

Woah! Now that's handy. (Ugly perhaps but handy.) Thanks for the tip!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest