Select 单元测试中的 Eclipse 4 RCP 部分。
Select an Eclipse 4 RCP Part in Unit Test.
我目前正在为我的 Eclipse 4 RCP 应用程序编写测试。
对于一个测试,有必要在测试期间 select 部分(第 2 部分)。我可以动态创建它并调用 Post 构造方法,但零件本身不是 selected.
我试图为 PartService 添加一个注入,但无论我是在 TestCase 还是在 Part 本身中尝试这样做,PartService 都是空的。
我也考虑过使用 SWTBot,但我想 select 那里的部件是不可能的,因为部件本身不是 SWT Widget。
关于如何以编程方式确保测试期间部件的 selection 有什么想法吗?
在下面的代码中,打开(并选择)了两个视图部分:MemoryView
和 DisplayView
。方法 org.eclipse.ui.IWorkbenchPage.showView(String)
获取视图部件 ID 作为参数,并在未打开视图时打开视图。在此方法调用中始终选择该部分。
public class PluginTestCase {
@Test
public void showView() {
Display display = PlatformUI.getWorkbench().getDisplay();
Shell shell = display.getActiveShell();
// Create a new thread
Thread thread = new Thread(() -> {
runUICode(display);
});
thread.start();
// Enter the UI message loop
while (!shell.isDisposed() && thread.isAlive()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Runs the code in a separate thread.
*
* @param display
* the display which handles UI modifications
*/
private void runUICode(Display display) {
closeIntro(display);
showView(display, "org.eclipse.jdt.debug.ui.DisplayView");
showView(display, "org.eclipse.debug.ui.MemoryView");
}
/**
* Closes the introduction part.
*/
private void closeIntro(Display display) {
display.syncExec(new Runnable() {
@Override
public void run() {
IIntroPart introPart = PlatformUI.getWorkbench().getIntroManager().getIntro();
PlatformUI.getWorkbench().getIntroManager().closeIntro(introPart);
}
});
}
/**
* Shows view of the given ID in the current perspective. The view gets
* focus (is selected).
*
* @param display
* the display object
* @param viewId
* the ID of the view to be shown
*/
private void showView(Display display, String viewId) {
display.syncExec(new Runnable() {
@Override
public void run() {
try {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
// Show and select the view
activePage.showView(viewId);
} catch (WorkbenchException e) {
throw new IllegalStateException(e);
}
}
});
}
}
该代码不利用 SWTBot,而是利用自定义消息循环。
请务必运行 将测试作为 JUnit 插件测试。
我目前正在为我的 Eclipse 4 RCP 应用程序编写测试。
对于一个测试,有必要在测试期间 select 部分(第 2 部分)。我可以动态创建它并调用 Post 构造方法,但零件本身不是 selected.
我试图为 PartService 添加一个注入,但无论我是在 TestCase 还是在 Part 本身中尝试这样做,PartService 都是空的。
我也考虑过使用 SWTBot,但我想 select 那里的部件是不可能的,因为部件本身不是 SWT Widget。
关于如何以编程方式确保测试期间部件的 selection 有什么想法吗?
在下面的代码中,打开(并选择)了两个视图部分:MemoryView
和 DisplayView
。方法 org.eclipse.ui.IWorkbenchPage.showView(String)
获取视图部件 ID 作为参数,并在未打开视图时打开视图。在此方法调用中始终选择该部分。
public class PluginTestCase {
@Test
public void showView() {
Display display = PlatformUI.getWorkbench().getDisplay();
Shell shell = display.getActiveShell();
// Create a new thread
Thread thread = new Thread(() -> {
runUICode(display);
});
thread.start();
// Enter the UI message loop
while (!shell.isDisposed() && thread.isAlive()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Runs the code in a separate thread.
*
* @param display
* the display which handles UI modifications
*/
private void runUICode(Display display) {
closeIntro(display);
showView(display, "org.eclipse.jdt.debug.ui.DisplayView");
showView(display, "org.eclipse.debug.ui.MemoryView");
}
/**
* Closes the introduction part.
*/
private void closeIntro(Display display) {
display.syncExec(new Runnable() {
@Override
public void run() {
IIntroPart introPart = PlatformUI.getWorkbench().getIntroManager().getIntro();
PlatformUI.getWorkbench().getIntroManager().closeIntro(introPart);
}
});
}
/**
* Shows view of the given ID in the current perspective. The view gets
* focus (is selected).
*
* @param display
* the display object
* @param viewId
* the ID of the view to be shown
*/
private void showView(Display display, String viewId) {
display.syncExec(new Runnable() {
@Override
public void run() {
try {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
// Show and select the view
activePage.showView(viewId);
} catch (WorkbenchException e) {
throw new IllegalStateException(e);
}
}
});
}
}
该代码不利用 SWTBot,而是利用自定义消息循环。
请务必运行 将测试作为 JUnit 插件测试。