SWTBot中 dialogs的处理

              官方问题文档地址:

                             http://wiki.eclipse.org/SWTBot/FAQ

              官方文档描述如下:

                          Why do tests run on a non-UI thread?

A lot of events that SWTBot sends to the UI are blocking. SWT dialogs are one of them. This means that functions opening dialogs, will block until the dialog closes. Since we do not want tests to block when a dialog open up, SWTBot runs in a non-UI thread, and posts events to the UI thread.

There are two solutions to this:

  1. Make the dialog non-modal, by invoking Dialog#setBlockOnOpen(false)
  2. Open the dialog in a non-ui thread

SWTBot chooses the later approach, since the first approach is not always practical.

翻译如下:

       SWTBot发送给SWT UI的一些事件为阻塞事件。SWT dialog就是这种情况。那将以为当对话框打开时候,将阻塞UI主线程,直到dialog关闭。所以dialog打开的时候,我们不能测试。因为SWTBot运行在一个非UI的线程中,发送事件到UI线程。

这种情况的解决有两种情况:

1.将dialog设置为非模式对话框,通过设置dialog的setBlockOnOpen(false);

2.在非UI线程下打开dialog。

How do I execute parts of tests that need UI thread?

Since SWTBot runs on a non-UI thread, all your code using PlatformUI.getWorkbench() will generally result with a NullPointerException, since PlatformUI.getWorkbench() returns null in non-UI threads.

If you really need to invoke or execute code that needs UI-thread (such as opening an editor without simulating click on New and so on), you can useDisplay.getDefault().syncExec() to wrap the piece of code that requires UI-thread in your tests:

翻译如下:

       SWTBot运行在一个非UI线程下,因为所有PlatformUI.getWorkbench() 在非线程下返回为空,所以使用PlatformUI.getWorkbench() 测试获取的结果为为空异常。

       如果你需要执行UI线程下面的代码,你可以使用Display.getDefault().syncExec() 去封装测试中UI线程的相关的代码。

 

@Test
public void testDiagram() throws ExecutionException {
	// part of test that requires UI-thread
	Display.getDefault().syncExec(new Runnable() {
		public void run() {
			try {
				new NewProcessCommandHandler().execute(null); // requires UI-thread since it is gonna invoke PlatformUI.getWorkbench()
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	});
 
	// Normal SWTBot execution
	SWTBotEditor botEditor = bot.activeEditor();
	SWTBotGefEditor gmfEditor = bot.gefEditor(botEditor.getTitle());
	gmfEditor.activateTool("Step");
	gmfEditor.mouseMoveLeftClick(200, 200);
}









Why do I have to run tests as SWTBot tests, instead of PDE-JUnit tests?

PDE-Junit tests run on the UI thread. SWTBot needs that tests run on a non-UI thread, hence a new run configuration. See Why do tests run on a non-UI thread?for more info. You could also run SWTBot test as plain JUnit/PDE tests, just ensure that the "Run in UI Thread" checkbox is not ticked.

        

猜你喜欢

转载自topmanopensource.iteye.com/blog/1972427