1 11 package org.eclipse.test; 12 13 import java.io.IOException ; 14 15 import junit.framework.Assert; 16 17 import org.eclipse.core.runtime.IPlatformRunnable; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IExtension; 21 import org.eclipse.core.runtime.Platform; 22 23 import org.eclipse.ui.IWindowListener; 24 import org.eclipse.ui.IWorkbench; 25 import org.eclipse.ui.IWorkbenchWindow; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.testing.ITestHarness; 28 import org.eclipse.ui.testing.TestableObject; 29 30 34 public class UITestApplication implements IPlatformRunnable, ITestHarness { 35 36 private static final String DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; private static final String DEFAULT_APP_PRE_3_0 = "org.eclipse.ui.workbench"; 39 private boolean fInDeprecatedMode = false; 40 private TestableObject fTestableObject; 41 private int fTestRunnerResult = -1; 42 43 44 47 public Object run(final Object args) throws Exception { 48 IPlatformRunnable application = getApplication((String [])args); 50 51 Assert.assertNotNull(application); 52 53 Object result; 54 if (fInDeprecatedMode) { 55 result = runDeprecatedApplication(application, args); 56 } 57 else { 58 result = runApplication(application, args); 59 } 60 if (!IPlatformRunnable.EXIT_OK.equals(result)) { 61 System.err.println("UITestRunner: Unexpected result from running application " + application + ": " + result); 62 } 63 return new Integer (fTestRunnerResult); 64 } 65 66 67 71 private IPlatformRunnable getApplication(String [] args) throws CoreException { 72 IExtension extension = 77 Platform.getExtensionRegistry().getExtension( 78 Platform.PI_RUNTIME, 79 Platform.PT_APPLICATIONS, 80 getApplicationToRun(args)); 81 82 if (extension == null) { 86 extension = Platform.getExtensionRegistry().getExtension( 87 Platform.PI_RUNTIME, 88 Platform.PT_APPLICATIONS, 89 DEFAULT_APP_PRE_3_0); 90 fInDeprecatedMode = true; 91 } 92 93 Assert.assertNotNull(extension); 94 95 IConfigurationElement[] elements = extension.getConfigurationElements(); 98 if (elements.length > 0) { 99 IConfigurationElement[] runs = elements[0].getChildren("run"); if (runs.length > 0) { 101 Object runnable = runs[0].createExecutableExtension("class"); if (runnable instanceof IPlatformRunnable) 103 return (IPlatformRunnable) runnable; 104 } 105 } 106 return null; 107 } 108 109 116 private String getApplicationToRun(String [] args) { 117 for (int i = 0; i < args.length; i++) { 118 if (args[i].equals("-testApplication") && i < args.length -1) return args[i+1]; 120 } 121 return DEFAULT_APP_3_0; 122 } 123 124 128 private Object runApplication(IPlatformRunnable application, Object args) throws Exception { 129 fTestableObject = PlatformUI.getTestableObject(); 130 fTestableObject.setTestHarness(this); 131 return application.run(args); 132 133 } 134 135 142 private Object runDeprecatedApplication( 143 IPlatformRunnable object, 144 final Object args) 145 throws Exception { 146 147 Assert.assertTrue(object instanceof IWorkbench); 148 149 final IWorkbench workbench = (IWorkbench) object; 150 final boolean[] started = { false }; 154 workbench.addWindowListener(new IWindowListener() { 155 public void windowOpened(IWorkbenchWindow w) { 156 if (started[0]) 157 return; 158 w.getShell().getDisplay().asyncExec(new Runnable () { 159 public void run() { 160 started[0] = true; 161 try { 162 fTestRunnerResult = EclipseTestRunner.run((String []) args); 163 } catch (IOException e) { 164 e.printStackTrace(); 165 } 166 workbench.close(); 167 } 168 }); 169 } 170 public void windowActivated(IWorkbenchWindow window) { 171 } 172 public void windowDeactivated(IWorkbenchWindow window) { 173 } 174 public void windowClosed(IWorkbenchWindow window) { 175 } 176 }); 177 return ((IPlatformRunnable) workbench).run(args); 178 } 179 180 183 public void runTests() { 184 fTestableObject.testingStarting(); 185 fTestableObject.runTest(new Runnable () { 186 public void run() { 187 try { 188 fTestRunnerResult = EclipseTestRunner.run(Platform.getCommandLineArgs()); 189 } catch (IOException e) { 190 e.printStackTrace(); 191 } 192 } 193 }); 194 fTestableObject.testingFinished(); 195 } 196 197 } 198 199 | Popular Tags |