This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
/
UITestApplication.java
120 lines (106 loc) · 4.51 KB
/
UITestApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*******************************************************************************
* Copyright (c) 2003, 2008 IBM Corporation 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:
* IBM Corporation - initial API and implementation
* Ketan Padegaonkar - modification to run tests on non-ui thread
*******************************************************************************/
package org.eclipse.swtbot.eclipse.core;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.testing.ITestHarness;
import org.eclipse.ui.testing.TestableObject;
/**
* Just a copy of {@link org.eclipse.pde.internal.junit.runtime.UITestApplication} with {@link #runTests()} overridden
* so that the tests run in a non-UI thread.
*
* @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
* @version $Id$
*/
public class UITestApplication implements IApplication, ITestHarness {
private static final String DEFAULT_APP_3_0 = "org.eclipse.ui.ide.workbench"; //$NON-NLS-1$
private TestableObject fTestableObject;
private IApplication fApplication;
/*
* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
*/
public Object start(IApplicationContext context) throws Exception {
String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
Object app = getApplication(args);
Assert.isNotNull(app, "The application " + getApplicationToRun(args) + " could not be found. Please see the SWTBot troubleshooting guide: http://wiki.eclipse.org/SWTBot/Troubleshooting");
fTestableObject = PlatformUI.getTestableObject();
fTestableObject.setTestHarness(this);
if (app instanceof IApplication) {
fApplication = (IApplication) app;
return fApplication.start(context);
}
throw new IllegalArgumentException("Could not execute application " + getApplicationToRun(args));
}
/*
* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#stop()
*/
public void stop() {
if (fApplication != null)
fApplication.stop();
}
/*
* return the application to run, or null if not even the default application is found.
*/
private Object getApplication(String[] args) throws CoreException {
// Find the name of the application as specified by the PDE JUnit launcher.
// If no application is specified, the 3.0 default workbench application
// is returned.
String applicationToRun = getApplicationToRun(args);
IExtension extension = Platform.getExtensionRegistry().getExtension(Platform.PI_RUNTIME, Platform.PT_APPLICATIONS,
applicationToRun);
Assert.isNotNull(extension, "Could not find IExtension for application: " + applicationToRun);
// If the extension does not have the correct grammar, return null.
// Otherwise, return the application object.
IConfigurationElement[] elements = extension.getConfigurationElements();
if (elements.length > 0) {
IConfigurationElement[] runs = elements[0].getChildren("run"); //$NON-NLS-1$
if (runs.length > 0) {
Object runnable = runs[0].createExecutableExtension("class"); //$NON-NLS-1$
if (runnable instanceof IApplication)
return runnable;
}
}
return null;
}
/*
* The -testApplication argument specifies the application to be run. If the PDE JUnit launcher did not set this
* argument, then return the name of the default application. In 3.0, the default is the
* "org.eclipse.ui.ide.worbench" application.
*/
private String getApplicationToRun(String[] args) {
IProduct product = Platform.getProduct();
if (product != null)
return product.getApplication();
for (int i = 0; i < args.length; i++)
if (args[i].equals("-testApplication") && (i < args.length - 1)) //$NON-NLS-1$
return args[i + 1];
return DEFAULT_APP_3_0;
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.testing.ITestHarness#runTests()
*/
public void runTests() {
fTestableObject.testingStarting();
RemotePluginTestRunner.main(Platform.getCommandLineArgs());
fTestableObject.testingFinished();
}
}