Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » How to test context or popup Menus for a Composite.
How to test context or popup Menus for a Composite. [message #1700733] Mon, 06 July 2015 17:28
Jordan Deyton is currently offline Jordan DeytonFriend
Messages: 10
Registered: July 2013
Junior Member
Hello SWTBot gurus and fellow users,

Question: I'm wondering if it is possible and, if so, how to test a context (popup, right-click) Menu that is set for a Composite.

So far, I've only seen examples where the context Menu is for an item in a Tree. I've also tried using the built-in context Menu finders with no luck.

I have some sample code below for a simple Composite with a context Menu and SWTBot test code.

Thanks in advance for any pointers, clues, etc.

Jordan

Example: Here are a couple of test classes to demonstrate the problem.

The Composite being tested. A simple Composite with a context Menu set for it. The Menu is built using a JFace MenuManager.
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

public class TestComposite extends Composite {

	public boolean menuItemClicked = false;

	public TestComposite(Composite parent) {
		super(parent, SWT.NONE);

		setLayout(new FillLayout());

		final MenuManager menuManager = new MenuManager();
		menuManager.add(new Action("bleh") {
			@Override
			public void run() {
				menuItemClicked = true;
				System.out.println("bleh clicked...");
			}
		});

		setMenu(menuManager.createContextMenu(this));

		return;
	}

}


The SWTBot test code.
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBotTestCase;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(SWTBotJunit4ClassRunner.class)
public class TestCompositeTester extends SWTBotTestCase {

	private Shell s;
	private TestComposite c;

	@Test
	public void checkContextMenuItem() {
		final Display display = Display.getDefault();
		display.syncExec(new Runnable() {
			@Override
			public void run() {
				s = new Shell(display);
				c = new TestComposite(s);
			}
		});

		SWTBotShell botShell = new SWTBotShell(s);
		botShell.contextMenu("bleh").click();
		assertTrue(c.menuItemClicked);
	}

}


The failure trace when running the SWTBot test code.
org.eclipse.swtbot.swt.finder.widgets.TimeoutException: Timeout after: 5000 ms.: Could not find context menu with text: bleh
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:437)
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:411)
	at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:399)
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(AbstractSWTBot.java:442)
	at org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot.contextMenu(AbstractSWTBot.java:423)
	at sandbox.plugin.TestCompositeTester.checkContextMenuItem(TestCompositeTester.java:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner.run(SWTBotJunit4ClassRunner.java:54)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.swtbot.eclipse.core.RemotePluginTestRunner.main(RemotePluginTestRunner.java:64)
	at org.eclipse.swtbot.eclipse.core.UITestApplication.runTests(UITestApplication.java:117)
	at org.eclipse.e4.ui.internal.workbench.swt.E4Testable$1.run(E4Testable.java:73)
	at java.lang.Thread.run(Unknown Source)
Previous Topic:RCP Testing -- Contribution Identities
Next Topic:Problem with Mars on Kali
Goto Forum:
  


Current Time: Fri Apr 26 05:40:29 GMT 2024

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

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

Back to the top