Skip to main content



      Home
Home » Eclipse Projects » SWTBot » Problem clicking popup menu (stays visible)
Problem clicking popup menu (stays visible) [message #551300] Thu, 05 August 2010 16:15
Eclipse UserFriend
Hallo,

I am running into trouble when trying to click a popup menu within an SWTBot test. I am able to find the popup menu via EventContextMenuFinder and I am also able to click the menu (i.e. the SWT.Selection event arrives at the menu). However, the menu is not hidden (and thus the thread showing the popup remains in its readAndDispatch loop). I have created a test case to reproduce my problem (see below).

An additional note: on MacOSX I was able to let the popup disappear by sending a keyboard event to the shell after having done the click (new SWTWorkbenchBot().activeShell().pressShortcut(Keystrokes.ESC )Wink, on Linux GTK not even this worked. Any ideas?

Cheers,
Alexander

package org.mechatronicmodeler.ui.editors.tests.utils;

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory. withMnemonic;

import java.util.List;

import junit.framework.TestCase;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundExcep tion;
import org.eclipse.swtbot.swt.finder.finders.EventContextMenuFinder ;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
import org.hamcrest.Matcher;

public class PopupTestCase extends TestCase {

public class PopupHolder {

public void open() {
final Menu menu = new Menu(new Shell(SWTUtils.display()), SWT.POP_UP);

final MenuItem menuItem1 = new MenuItem(menu, SWT.PUSH);
menuItem1.setText("1");
menuItem1.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event e) {
System.out.println("1");
}
});

final MenuItem menuItem2 = new MenuItem(menu, SWT.PUSH);
menuItem2.setText("1");
menuItem2.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event e) {
System.out.println("2");
}
});

final MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
menuItem.setText("3");
final Menu subMenu = new Menu(menu.getShell(), SWT.DROP_DOWN);
for (int i = 0; i < 5; i++) {
final MenuItem subMenuItem = new MenuItem(subMenu, SWT.CASCADE);
final String menuText = "3." + (i + 1);
subMenuItem.setText(menuText);
subMenuItem.addListener(SWT.Selection, new Listener() {

public void handleEvent(final Event e) {
System.out.println(menuText);
}
});
menuItem.setMenu(subMenu);
}

menu.setVisible(true);

final Display display = menu.getDisplay();
while (!menu.isDisposed() && menu.isVisible()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}

public void testClickPopup(){
MenuItem menuItem = null;
EventContextMenuFinder finder = new EventContextMenuFinder();
try {
finder.register();

// show popup
UIThreadRunnable.asyncExec(new VoidResult() {
public void run() {
new PopupHolder().open();
}
});

waitForSync();

Matcher<MenuItem> withMnemonic = withMnemonic("1");
List<MenuItem> menus = finder.findMenus(withMnemonic);
if (menus.isEmpty())
throw new WidgetNotFoundException("Could not find a menu item");
menuItem = menus.get(0);

} finally {
finder.unregister();
}

waitForSync();

new SWTBotMenu(menuItem).click();

// wait here just to observe that menu is not closed
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

protected static void waitForSync() {
Thread.yield();
UIThreadRunnable.syncExec(new VoidResult() {
public void run() {
// wait for sync
}
});
}
}
Previous Topic:How to check if a Text() is read only
Next Topic:Delete a particular line or section of code in editor
Goto Forum:
  


Current Time: Fri Jul 04 17:30:03 EDT 2025

Powered by FUDForum. Page generated in 0.03867 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top