【问题标题】:Select an Eclipse 4 RCP Part in Unit Test.在单元测试中选择一个 Eclipse 4 RCP 部件。
【发布时间】:2016-05-20 17:10:01
【问题描述】:

我目前正在为我的 Eclipse 4 RCP 应用程序编写测试。
对于一项测试,有必要在测试期间选择部件 (Part2)。我可以动态创建它并调用 Post Construct 方法,但没有选择 Part 本身。
我尝试为 PartService 包含注入,但无论我尝试在 TestCase 还是 Part 本身中执行它,PartService 都是空的。
我也考虑过使用 SWTBot,但我想在那里选择 Parts 是不可能的,因为 Part 本身不是 SWT Widget。
任何想法如何以编程方式确保在测试期间选择零件?

【问题讨论】:

    标签: java eclipse unit-testing eclipse-rcp e4


    【解决方案1】:

    在下面的代码中,打开(并选择了)两个视图部分: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 插件测试运行。

    【讨论】:

    • 分叉一个后台线程然后在显示线程上运行 UI 代码是不必要的。如果您确保启动配置已启用 在 UI 线程中运行(这是默认设置),则在当前 (UI) 线程上调用 activePage.showView() 等就足够了。
    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多