Bug 439017 - Replace TestHelper with TestUtil

Change-Id: I8de74690df87054e73e6ee8111a0f88bb6aea87f
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java
index 1453b64..fba4831 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java
@@ -73,16 +73,35 @@
 	}
 
 	/**
-	 * Utility for waiting until the execution of jobs of any family has finished or timeout is reached. If no jobs are running, the method waits
-	 * given minimum wait time. While this method is waiting for jobs, UI events are processed.
+	 * Process all queued UI events. If called from background thread, just
+	 * waits
+	 */
+	public static void processUIEvents(final long millis) throws Exception {
+		long start = System.currentTimeMillis();
+		while (System.currentTimeMillis() - start < millis) {
+			Display display = Display.getCurrent();
+			if (display != null && !display.isDisposed()) {
+				while (display.readAndDispatch()) {
+					// loop until the queue is empty
+				}
+			} else {
+				Thread.sleep(10);
+			}
+		}
+	}
+
+	/**
+	 * Utility for waiting until the execution of jobs of any family has
+	 * finished or timeout is reached. If no jobs are running, the method waits
+	 * given minimum wait time. While this method is waiting for jobs, UI events
+	 * are processed.
 	 *
-	 * @param owner
-	 *            name of the caller which will be logged as prefix if the wait times out
-	 * @param minTimeMs
-	 *            minimum wait time in milliseconds
-	 * @param maxTimeMs
-	 *            maximum wait time in milliseconds
-	 * @return true if the method timed out, false if all the jobs terminated before the timeout
+	 * @param owner name of the caller which will be logged as prefix if the
+	 *            wait times out
+	 * @param minTimeMs minimum wait time in milliseconds
+	 * @param maxTimeMs maximum wait time in milliseconds
+	 * @return true if the method timed out, false if all the jobs terminated
+	 *         before the timeout
 	 */
 	public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs) {
 		return waitForJobs(owner, minTimeMs, maxTimeMs, (Object[]) null);
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java
index 277ad97..5ae877f 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java
@@ -17,6 +17,7 @@
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.eclipse.debug.tests.AbstractDebugTest;
+import org.eclipse.debug.tests.TestUtil;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.swt.SWT;
@@ -25,7 +26,6 @@
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IViewReference;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.console.ConsolePlugin;
@@ -40,7 +40,6 @@
  */
 public class ConsoleManagerTests extends AbstractDebugTest {
 
-	private static final String INTROVIEW_ID = "org.eclipse.ui.internal.introview"; //$NON-NLS-1$
 	private ExecutorService executorService;
 	private IConsoleManager manager;
 	private int count;
@@ -58,8 +57,7 @@
 		executorService = Executors.newFixedThreadPool(count);
 		manager = ConsolePlugin.getDefault().getConsoleManager();
 		IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-		hideWelcomePage(activePage);
-		TestHelper.processUIEvents(100);
+		TestUtil.processUIEvents(100);
 		consoles = new ConsoleMock[count];
 		for (int i = 0; i < count; i++) {
 			final ConsoleMock console = new ConsoleMock(i + 1);
@@ -70,7 +68,7 @@
 
 		IViewPart consoleView = activePage.showView("org.eclipse.ui.console.ConsoleView"); //$NON-NLS-1$
 		activePage.activate(consoleView);
-		TestHelper.processUIEvents(100);
+		TestUtil.processUIEvents(100);
 
 		// The test is unstable ("show" event on the the first console seem to
 		// be not always sent), so make sure console view has shown at least
@@ -78,8 +76,8 @@
 		firstConsole = new ConsoleMock(0);
 		manager.addConsoles(new ConsoleMock[] { firstConsole });
 		manager.showConsoleView(firstConsole);
-		TestHelper.waitForJobs();
-		TestHelper.processUIEvents(100);
+		TestUtil.waitForJobs(getName(), 200, 5000);
+		TestUtil.processUIEvents(100);
 		ConsoleMock.allShownConsoles.set(0);
 	}
 
@@ -88,24 +86,10 @@
 		executorService.shutdownNow();
 		manager.removeConsoles(consoles);
 		manager.removeConsoles(new ConsoleMock[] { firstConsole });
-		TestHelper.processUIEvents(100);
+		TestUtil.processUIEvents(100);
 		super.tearDown();
 	}
 
-	private void hideWelcomePage(IWorkbenchPage activePage) {
-		IViewReference[] refs = activePage.getViewReferences();
-		IViewPart intro = null;
-		for (IViewReference ref : refs) {
-			if (INTROVIEW_ID.equals(ref.getId())) {
-				intro = ref.getView(false);
-			}
-		}
-		if (intro != null) {
-			activePage.hideView(intro);
-			TestHelper.processUIEvents(100);
-		}
-	}
-
 	/**
 	 * The test triggers {@link #count} simultaneous calls to the
 	 * {@link IConsoleManager#showConsoleView(IConsole)} and checks if all of
@@ -122,15 +106,15 @@
 			showConsole(console);
 		}
 		System.out.println("All tasks scheduled, processing UI events now..."); //$NON-NLS-1$
-		TestHelper.processUIEvents(1000);
+		TestUtil.processUIEvents(1000);
 
 		// Console manager starts a job with delay, let wait for him a bit
 		System.out.println("Waiting on jobs now..."); //$NON-NLS-1$
-		TestHelper.waitForJobs();
+		TestUtil.waitForJobs(getName(), 200, 5000);
 
 		// Give UI a chance to proceed pending console manager jobs
 		System.out.println("Done with jobs, processing UI events again..."); //$NON-NLS-1$
-		TestHelper.processUIEvents(3000);
+		TestUtil.processUIEvents(3000);
 
 		executorService.shutdown();
 
@@ -138,7 +122,7 @@
 		boolean OK = waitForExecutorService();
 		if (!OK) {
 			System.out.println("Timed out..."); //$NON-NLS-1$
-			TestHelper.processUIEvents(10000);
+			TestUtil.processUIEvents(10000);
 
 			// timeout?
 			assertTrue("Timeout occurred while waiting on console to be shown", //$NON-NLS-1$
@@ -150,12 +134,12 @@
 		assertEquals("Only " + shown + " consoles were shown from " + count, count, shown); //$NON-NLS-1$ //$NON-NLS-2$
 	}
 
-	private boolean waitForExecutorService() throws InterruptedException {
+	private boolean waitForExecutorService() throws Exception {
 		for (int i = 0; i < 60; i++) {
 			if (executorService.awaitTermination(1, TimeUnit.SECONDS)) {
 				return true;
 			}
-			TestHelper.processUIEvents(100);
+			TestUtil.processUIEvents(100);
 		}
 		return false;
 	}
@@ -171,7 +155,7 @@
 					latch.await(1, TimeUnit.MINUTES);
 					System.out.println("Requesting to show: " + console); //$NON-NLS-1$
 					manager.showConsoleView(console);
-					TestHelper.waitForJobs();
+					TestUtil.waitForJobs(getName(), 200, 5000);
 				} catch (InterruptedException e) {
 					e.printStackTrace();
 					Thread.interrupted();
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java
index f59f0c7..d0704a8 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java
@@ -15,6 +15,7 @@
 import java.nio.charset.StandardCharsets;
 
 import org.eclipse.debug.tests.AbstractDebugTest;
+import org.eclipse.debug.tests.TestUtil;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.ui.console.IConsoleConstants;
 import org.eclipse.ui.console.IOConsoleOutputStream;
@@ -42,21 +43,21 @@
 		MessageConsole console = new MessageConsole("Test Console", //$NON-NLS-1$
 				IConsoleConstants.MESSAGE_CONSOLE_TYPE, null, StandardCharsets.UTF_8.name(), true);
 		IDocument document = console.getDocument();
-		TestHelper.waitForJobs();
+		TestUtil.waitForJobs(getName(), 200, 5000);
 		TestCase.assertEquals("Document should be empty", "", document.get()); //$NON-NLS-1$ //$NON-NLS-2$
 		try (IOConsoleOutputStream outStream = console.newOutputStream()) {
 			outStream.write(testStringBuffer, 0, 6);
 			// half of ö (\u00f6) is written so we don't expect this char in
 			// output but all previous chars can be decoded
-			TestHelper.waitForJobs();
+			TestUtil.waitForJobs(getName(), 200, 5000);
 			TestCase.assertEquals("First 4 chars should be written", testString.substring(0, 4), document.get()); //$NON-NLS-1$
 			outStream.write(testStringBuffer, 6, 6);
 			// all remaining bytes are written so we expect the whole string
 			// including the ö (\u00f6) which was at buffer boundary
-			TestHelper.waitForJobs();
+			TestUtil.waitForJobs(getName(), 200, 5000);
 			TestCase.assertEquals("whole test string should be written", testString, document.get()); //$NON-NLS-1$
 		}
-		TestHelper.waitForJobs();
+		TestUtil.waitForJobs(getName(), 200, 5000);
 		// after closing the stream, the document content should still be the
 		// same
 		TestCase.assertEquals("closing the stream should not alter the document", testString, document.get()); //$NON-NLS-1$
@@ -69,15 +70,15 @@
 		MessageConsole console = new MessageConsole("Test Console 2", //$NON-NLS-1$
 				IConsoleConstants.MESSAGE_CONSOLE_TYPE, null, StandardCharsets.UTF_8.name(), true);
 		IDocument document = console.getDocument();
-		TestHelper.waitForJobs();
+		TestUtil.waitForJobs(getName(), 200, 5000);
 		TestCase.assertEquals("Document should be empty", "", document.get()); //$NON-NLS-1$ //$NON-NLS-2$
 		try (IOConsoleOutputStream outStream = console.newOutputStream()) {
 			outStream.write(testStringBuffer);
 			// everything but pending \r should be written
-			TestHelper.waitForJobs();
+			TestUtil.waitForJobs(getName(), 200, 5000);
 			TestCase.assertEquals("First char should be written", testString.substring(0, 1), document.get()); //$NON-NLS-1$
 		}
-		TestHelper.waitForJobs();
+		TestUtil.waitForJobs(getName(), 200, 5000);
 		// after closing the stream, the document content should still be the
 		// same
 		TestCase.assertEquals("closing the stream should write the pending \\r", testString, document.get()); //$NON-NLS-1$
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java
deleted file mode 100644
index 981459a..0000000
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2017 Andreas Loth and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     Andreas Loth - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.debug.tests.console;
-
-import org.eclipse.core.runtime.jobs.Job;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.PlatformUI;
-
-
-public final class TestHelper {
-
-	private TestHelper() {
-		throw new AssertionError("No instances of this utility class!"); //$NON-NLS-1$
-	}
-
-	public static void processUIEvents(final long millis) {
-		long start = System.currentTimeMillis();
-		while (System.currentTimeMillis() - start < millis) {
-			while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) {
-				// loop untile the queue is empty
-			}
-		}
-	}
-
-	public static void waitForJobs() throws InterruptedException {
-		if (Display.getCurrent() == null) {
-			Thread.sleep(200);
-		} else {
-			processUIEvents(200);
-		}
-		while (!Job.getJobManager().isIdle()) {
-			if (Display.getCurrent() == null) {
-				Thread.sleep(200);
-			} else {
-				processUIEvents(200);
-			}
-		}
-	}
-
-}
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
index a15ddbd..c17d130 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
@@ -20,6 +20,7 @@
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
@@ -35,6 +36,7 @@
 import org.eclipse.debug.internal.core.groups.GroupLaunchElement;
 import org.eclipse.debug.internal.core.groups.GroupLaunchElement.GroupElementPostLaunchAction;
 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
+import org.eclipse.debug.tests.TestUtil;
 import org.eclipse.debug.ui.IDebugUIConstants;
 
 public class LaunchGroupTests extends AbstractLaunchTest {
@@ -77,7 +79,20 @@
 	protected void tearDown() throws Exception {
 		// make sure listener is removed
 		getLaunchManager().removeLaunchListener(lcListener);
-
+		ILaunch[] launches = getLaunchManager().getLaunches();
+		for (ILaunch launch : launches) {
+			try {
+				if (!launch.isTerminated()) {
+					IProcess[] processes = launch.getProcesses();
+					for (IProcess process : processes) {
+						process.terminate();
+					}
+					launch.terminate();
+				}
+			} catch (Exception e) {
+				TestUtil.log(IStatus.ERROR, getName(), "Error terminating launch: " + launch, e);
+			}
+		}
 		super.tearDown();
 	}
 
@@ -170,8 +185,7 @@
 						}
 					}
 				} catch (Exception e) {
-					// uh oh
-					e.printStackTrace();
+					TestUtil.log(IStatus.ERROR, getName(), e.getMessage(), e);
 				}
 			}
 
@@ -193,7 +207,6 @@
 		assertTrue("history[2] should be Test1", history[2].contentsEqual(t1)); //$NON-NLS-1$
 	}
 
-
 	public void testAdopt() throws Exception {
 		final ILaunchConfiguration t1 = getLaunchConfiguration("Test1"); //$NON-NLS-1$
 		final ILaunchConfiguration grp = createLaunchGroup(DEF_GRP_NAME, createLaunchGroupElement(t1, GroupElementPostLaunchAction.NONE, null, false), createLaunchGroupElement(t1, GroupElementPostLaunchAction.NONE, null, true));