KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > testing > WorkbenchTestable


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.testing;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.OperationCanceledException;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.jface.util.SafeRunnable;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.internal.Workbench;
22 import org.eclipse.ui.testing.TestableObject;
23
24 /**
25  * The Workbench's testable object facade to a test harness.
26  *
27  * @since 3.0
28  */

29 public class WorkbenchTestable extends TestableObject {
30
31     private Display display;
32
33     private IWorkbench workbench;
34
35     private boolean oldAutomatedMode;
36
37     private boolean oldIgnoreErrors;
38
39     /**
40      * Constructs a new workbench testable object.
41      */

42     public WorkbenchTestable() {
43         // do nothing
44
}
45
46     /**
47      * Initializes the workbench testable with the display and workbench,
48      * and notifies all listeners that the tests can be run.
49      *
50      * @param display the display
51      * @param workbench the workbench
52      */

53     public void init(Display display, IWorkbench workbench) {
54         Assert.isNotNull(display);
55         Assert.isNotNull(workbench);
56         this.display = display;
57         this.workbench = workbench;
58         if (getTestHarness() != null) {
59             // don't use a job, since tests often wait for all jobs to complete before proceeding
60
Runnable JavaDoc runnable = new Runnable JavaDoc() {
61                 public void run() {
62                     // Some tests (notably the startup performance tests) do not want to wait for early startup.
63
// Allow this to be disabled by specifying the system property: org.eclipse.ui.testsWaitForEarlyStartup=false
64
// For details, see bug 94129 [Workbench] Performance test regression caused by workbench harness change
65
if (!"false".equalsIgnoreCase(System.getProperty(PlatformUI.PLUGIN_ID + ".testsWaitForEarlyStartup"))) { //$NON-NLS-1$ //$NON-NLS-2$
66
waitForEarlyStartup();
67                     }
68                     getTestHarness().runTests();
69                 }
70             };
71             new Thread JavaDoc(runnable, "WorkbenchTestable").start(); //$NON-NLS-1$
72
}
73     }
74
75     /**
76      * Waits for the early startup job to complete.
77      */

78     private void waitForEarlyStartup() {
79         try {
80             Platform.getJobManager().join(Workbench.EARLY_STARTUP_FAMILY, null);
81         } catch (OperationCanceledException e) {
82             // ignore
83
} catch (InterruptedException JavaDoc e) {
84             // ignore
85
}
86     }
87     
88     /**
89      * The <code>WorkbenchTestable</code> implementation of this
90      * <code>TestableObject</code> method ensures that the workbench
91      * has been set.
92      */

93     public void testingStarting() {
94         Assert.isNotNull(workbench);
95         oldAutomatedMode = ErrorDialog.AUTOMATED_MODE;
96         ErrorDialog.AUTOMATED_MODE = true;
97         oldIgnoreErrors = SafeRunnable.getIgnoreErrors();
98         SafeRunnable.setIgnoreErrors(true);
99     }
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 JavaDoc 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 JavaDoc() {
120             public void run() {
121                 Assert.isTrue(workbench.close());
122             }
123         });
124         ErrorDialog.AUTOMATED_MODE = oldAutomatedMode;
125         SafeRunnable.setIgnoreErrors(oldIgnoreErrors);
126     }
127 }
128
Popular Tags