Search code examples
javaeclipseunit-testingeclipse-rcpe4

Select an Eclipse 4 RCP Part in Unit Test.


I´m currently writing Tests for my Eclipse 4 RCP Application.
For one Test it is necessary to select the Part (Part2) during the Test. I can dynamically create it and invoke the Post Construct Method, but the Part itself is not selected.
I tried to include an Injection for a PartService but regardless if I try to do it in the TestCase or the Part itself, the PartService is null.
I also thought about using SWTBot, but I guess it´s not possible to select Parts there because the Part itself is no SWT Widget.
Any ideas how to programatically ensure the selection of the Part during the Test?


Solution

  • In below code, two view parts are opened (and selected): MemoryView and then DisplayView. Method org.eclipse.ui.IWorkbenchPage.showView(String) gets a view part ID as the argument and opens the view when it is not opened. The part is always selected in this method call.

    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);
                    }
                }
    
            });
        }
    }
    

    The code doesn't leverage SWTBot but custom message loop.

    Be sure you run the test as JUnit Plug-in Test.