Make waitForJobs available to non-SWTBot test plugins
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui.tests / src / org / eclipse / tracecompass / lttng2 / control / ui / tests / model / component / TraceControlTestFacility.java
index f14a4a3dc63f8adbc37384c13088f5e3a9fbe16b..7b6e5ebeaaec13eefb6ad4b02f384db1df89de42 100644 (file)
 
 package org.eclipse.tracecompass.lttng2.control.ui.tests.model.component;
 
+import static org.junit.Assert.assertNotNull;
+
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.NotEnabledException;
 import org.eclipse.core.commands.NotHandledException;
 import org.eclipse.core.commands.common.NotDefinedException;
-import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TargetNodeState;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.JobUtils;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
@@ -53,7 +55,7 @@ public class TraceControlTestFacility {
     // ------------------------------------------------------------------------
     private static TraceControlTestFacility fInstance = null;
     private ControlView fControlView = null;
-    private boolean fIsInitialized = false;
+    private volatile boolean fIsInitialized = false;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -77,46 +79,41 @@ public class TraceControlTestFacility {
     public void init() {
 
         if (!fIsInitialized) {
-
             IViewPart view;
             try {
-
-                view = PlatformUI.getWorkbench()
-                        .getActiveWorkbenchWindow()
-                        .getActivePage()
-                        .findView("org.eclipse.ui.internal.introview");
-
-                if (view != null) {
-                    PlatformUI.getWorkbench()
-                    .getActiveWorkbenchWindow()
-                    .getActivePage().hideView(view);
-                }
-
-                view = PlatformUI.getWorkbench()
-                                 .getActiveWorkbenchWindow()
-                                 .getActivePage()
-                                 .showView(ControlView.ID);
-
+                hideView("org.eclipse.ui.internal.introview");
+                view = showView(ControlView.ID);
             } catch (PartInitException e) {
                 throw new RuntimeException(e);
             }
-
             fControlView = (ControlView) view;
 
-            delay(3000);
+            /*
+             * It is possible that the connections are saved due to the
+             * auto-save feature of the workbench which calls
+             * ControlView.saveState(IMemento). This can happen at any
+             * time (e.g. when calling delay()).
+             *
+             * When showing the view above ControlView.init(IMemento) is
+             * called which restores saved connections.
+             *
+             * The tests require that the ControlView is empty. So
+             * we remove all the connection nodes from the root.
+             */
+            fControlView.getTraceControlRoot().removeAllChildren();
+
             fIsInitialized = true;
         }
     }
 
-
     /**
      * Disposes the facility (and GUI)
      */
     public void dispose() {
         if (fIsInitialized) {
             waitForJobs();
-
-            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(fControlView);
+            hideView(ControlView.ID);
+            delay(200);
             fIsInitialized = false;
         }
     }
@@ -163,13 +160,60 @@ public class TraceControlTestFacility {
         }
     }
 
+    /**
+     * Waits for a view to be closed
+     */
+    public void waitForViewClosed(String viewId) {
+        for (int i = 1; i < 5000 && (getViewPart(viewId) != null); i *= 2) {
+            delay(i);
+        }
+    }
+
+    /**
+     * Waits for a view to be closed
+     */
+    public void waitForViewOpend(String viewId) {
+        for (int i = 1; i < 5000 && (getViewPart(viewId) == null); i *= 2) {
+            delay(i);
+        }
+    }
+
     /**
      * Waits for all Eclipse jobs to finish
      */
     public void waitForJobs() {
-        while (!Job.getJobManager().isIdle()) {
-            delay(WAIT_FOR_JOBS_DELAY);
+        JobUtils.waitForJobs();
+    }
+
+    private IViewPart showView(String viewId) throws PartInitException {
+        IViewPart view = getViewPart(viewId);
+
+        if (view == null) {
+            view = PlatformUI.getWorkbench()
+            .getActiveWorkbenchWindow()
+            .getActivePage().showView(viewId);
+
+            waitForViewOpend(viewId);
         }
+        assertNotNull(view);
+        return view;
+    }
+
+    private void hideView(String viewId) {
+        IViewPart view = getViewPart(viewId);
+        if (view != null) {
+            PlatformUI.getWorkbench()
+            .getActiveWorkbenchWindow()
+            .getActivePage().hideView(view);
+        }
+        waitForViewClosed(viewId);
+    }
+
+    private static IViewPart getViewPart(String viewId) {
+        return PlatformUI.getWorkbench()
+        .getActiveWorkbenchWindow()
+        .getActivePage()
+        .findView(viewId);
     }
 
     /**
This page took 0.026012 seconds and 5 git commands to generate.