001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.testing;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.OperationCanceledException;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.jface.dialogs.ErrorDialog;
016: import org.eclipse.jface.util.SafeRunnable;
017: import org.eclipse.swt.widgets.Display;
018: import org.eclipse.ui.IWorkbench;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.internal.Workbench;
021: import org.eclipse.ui.testing.TestableObject;
022:
023: /**
024: * The Workbench's testable object facade to a test harness.
025: *
026: * @since 3.0
027: */
028: public class WorkbenchTestable extends TestableObject {
029:
030: private Display display;
031:
032: private IWorkbench workbench;
033:
034: private boolean oldAutomatedMode;
035:
036: private boolean oldIgnoreErrors;
037:
038: /**
039: * Constructs a new workbench testable object.
040: */
041: public WorkbenchTestable() {
042: // do nothing
043: }
044:
045: /**
046: * Initializes the workbench testable with the display and workbench,
047: * and notifies all listeners that the tests can be run.
048: *
049: * @param display the display
050: * @param workbench the workbench
051: */
052: public void init(Display display, IWorkbench workbench) {
053: Assert.isNotNull(display);
054: Assert.isNotNull(workbench);
055: this .display = display;
056: this .workbench = workbench;
057: if (getTestHarness() != null) {
058: // don't use a job, since tests often wait for all jobs to complete before proceeding
059: Runnable runnable = new Runnable() {
060: public void run() {
061: // Some tests (notably the startup performance tests) do not want to wait for early startup.
062: // Allow this to be disabled by specifying the system property: org.eclipse.ui.testsWaitForEarlyStartup=false
063: // For details, see bug 94129 [Workbench] Performance test regression caused by workbench harness change
064: if (!"false".equalsIgnoreCase(System.getProperty(PlatformUI.PLUGIN_ID + ".testsWaitForEarlyStartup"))) { //$NON-NLS-1$ //$NON-NLS-2$
065: waitForEarlyStartup();
066: }
067: getTestHarness().runTests();
068: }
069: };
070: new Thread(runnable, "WorkbenchTestable").start(); //$NON-NLS-1$
071: }
072: }
073:
074: /**
075: * Waits for the early startup job to complete.
076: */
077: private void waitForEarlyStartup() {
078: try {
079: Platform.getJobManager().join(
080: Workbench.EARLY_STARTUP_FAMILY, null);
081: } catch (OperationCanceledException e) {
082: // ignore
083: } catch (InterruptedException e) {
084: // ignore
085: }
086: }
087:
088: /**
089: * The <code>WorkbenchTestable</code> implementation of this
090: * <code>TestableObject</code> method ensures that the workbench
091: * has been set.
092: */
093: public void testingStarting() {
094: Assert.isNotNull(workbench);
095: oldAutomatedMode = ErrorDialog.AUTOMATED_MODE;
096: ErrorDialog.AUTOMATED_MODE = true;
097: oldIgnoreErrors = SafeRunnable.getIgnoreErrors();
098: SafeRunnable.setIgnoreErrors(true);
099: }
100:
101: /**
102: * The <code>WorkbenchTestable</code> implementation of this
103: * <code>TestableObject</code> method flushes the event queue,
104: * runs the test in a <code>syncExec</code>, then flushes the
105: * event queue again.
106: */
107: public void runTest(Runnable testRunnable) {
108: Assert.isNotNull(workbench);
109: display.syncExec(testRunnable);
110: }
111:
112: /**
113: * The <code>WorkbenchTestable</code> implementation of this
114: * <code>TestableObject</code> method flushes the event queue,
115: * then closes the workbench.
116: */
117: public void testingFinished() {
118: // force events to be processed, and ensure the close is done in the UI thread
119: display.syncExec(new Runnable() {
120: public void run() {
121: Assert.isTrue(workbench.close());
122: }
123: });
124: ErrorDialog.AUTOMATED_MODE = oldAutomatedMode;
125: SafeRunnable.setIgnoreErrors(oldIgnoreErrors);
126: }
127: }
|