|
/*
* (c) Copyright IBM Corp. 2000, 2006. All Rights Reserved.
* Licensed Materials - Use restricted, please refer to the "Samples Gallery" terms and conditions in the IBM International Program License Agreement.
*/
package superscript;
import testobject.eclipse.WorkbenchTestObject;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.AmbiguousRecognitionException;
import com.rational.test.ft.ObjectNotFoundException;
import com.rational.test.ft.RationalTestException;
/**
* This class provides some methods that may be useful when testing
* plugins running inside the eclipse shell (see http://www.eclipse.org/).
* Note that this code makes use of internal eclipse classes, and consequently
* may break with future versions of eclipse.
*
* This class illustrates invoking static methods in the SUT and using custom
* TestObjects.
* There is also sample code illustrating the use of find(), where a TestObject can be found
* based on a given set of properties.
*/
public abstract class EclipseScript extends SwtScript
{
/**
* Get a workbench object from any object from the workbench UI
* Note that the returned TestObject is registered, and should be unregistered
* by the caller.
*/
public WorkbenchTestObject getWorkbench(TestObject object)
{
DomainTestObject domain = object.getDomain();
if (domain == null)
throw new SwtScriptException("can't get domain from object: "+object.getClass().getName());
// TestObject workspace = (TestObject)domain.invokeStaticMethod( "org.eclipse.ui.internal.WorkbenchPlugin", "getPluginWorkspace");
TestObject workbenchPlugin = (TestObject)domain.invokeStaticMethod( "org.eclipse.ui.internal.WorkbenchPlugin", "getDefault");
return new WorkbenchTestObject((TestObject)workbenchPlugin.getProperty("workbench"));
}
/**
* General-purpose exception for eclipse scripts
*/
public static class SwtScriptException extends RuntimeException
{
SwtScriptException(String s) { super(s); }
}
/**************************************************************************************
* The following are methods used for interacting with the Workspace Launcher window,
* which is not testable (using recording) by default.
* Use of find() using TestObject properties is shown.
* Note that this is a Windows only solution.
**************************************************************************************/
/**
* Call this method to select a particular workspace from the Workspace
* Launcher drop-down menu.
* @param workspace The name of the workspace to connect to.
* It should be a choice available in the Launcher drop-down.
* @return <code>true</code> if a workspace was selected.
*/
public boolean selectWorkspace(String workspace)
{
boolean selected = false;
TopLevelSubitemTestObject window = getWorkspaceWindow();
if (window != null)
{
try
{
TestObject[] found = window.find(atList(
atDescendant(".class", "SWT_Window0"),
atChild(".class", "ComboBox", ".text", "Workspace:")));
checkFindResults(found, "Workspace Selection ComboBox");
try
{
((TextSelectGuiSubitemTestObject)found[0]).click(ARROW);
((TextSelectGuiSubitemTestObject)found[0]).click(atText(workspace));
selected = true;
}
finally
{
found[0].unregister();
}
}
finally
{
window.unregister();
}
}
return selected;
}
/**
* Call this method to click the "OK" button in the Workspace Launcher.
* @return <code>true</code> if the OK Button was clicked.
*/
public boolean clickOK()
{
boolean clicked = false;
TopLevelSubitemTestObject window = getWorkspaceWindow();
if (window != null)
{
try
{
TestObject[] found = window.find(
atDescendant(".class", ".Pushbutton", ".name", "OK"));
checkFindResults(found, "OK Button");
try
{
((GuiTestObject)found[0]).click();
}
finally
{
found[0].unregister();
}
clicked = true;
}
finally
{
window.unregister();
}
}
return clicked;
}
/**
* Call this method to obtain a TestObject for the Workspace Launcher window.
* Please refer to "clickOK" and "selectWorkspace" for the most common
* functionality needed, pre-canned.
* @return a TopLevelSubitemTestObject representing the Workspace
* Launcher window.
* The TestObject must be unregistered.
*/
public TopLevelSubitemTestObject getWorkspaceWindow()
{
TopLevelSubitemTestObject topWindow = null;
RootTestObject rootTO = getRootTestObject();
IWindow[] topWindows = rootTO.getTopWindows();
for (int i=0; i<topWindows.length; ++i)
{
String title = topWindows.getText();
if (title != null && title.equals("Workspace Launcher"))
{
Long hWnd = new Long(topWindows.getHandle());
// By specifying domain "Win" and giving a window handle,
// we will enable the window for testing.
TestObject[] found = rootTO.find(
atChild(".domain", "Win", ".hwnd", hWnd));
checkFindResults(found, "Workspace Launcher window");
topWindow = (TopLevelSubitemTestObject)found[0];
}
}
if (topWindow == null)
{
throw new ObjectNotFoundException("Unable to locate: Workspace Launcher window");
}
return topWindow;
}
// The name of the eclipse application, as defined in the Application Configuration tool.
public String ECLIPSE_APP_NAME = "eclipse";
// The maximum amount of time to wait for an eclipse application to start.
public double MAX_STARTUP_WAIT = 30.0;
/**
* Call this method to start an eclipse application, select the default workspace
* (if applicable), and wait for the workspace to appear.
* @return <code>true</code> if the eclipse application starts and the workspace appears.
*/
public boolean startEclipse()
{
boolean started = false;
startApp(ECLIPSE_APP_NAME);
RootTestObject root = getRootTestObject();
Timer timer = new Timer();
boolean clickedOK = false;
do
{
TestObject[] found = root.find(
atChild(".domain", "Java", "class", "org.eclipse.swt.widgets.Shell"));
if (found.length == 0)
{
try
{
if (!clickedOK)
{
clickedOK = clickOK();
if (clickedOK)
{
sleep(.5);
}
}
}
catch (RationalTestException e)
{
}
}
else
{
started = true;
unregister(found);
}
sleep(.5);
} while (!started && !timer.isTimeUp(MAX_STARTUP_WAIT));
return started;
}
/**
* Utility method to make sure we find one and only one TestObject for a given search.
*/
private void checkFindResults(TestObject[] found, String objectDescription)
{
if (found.length == 0)
{
throw new ObjectNotFoundException("Unable to locate: " + objectDescription);
}
else if (found.length > 1)
{
unregister(found);
throw new AmbiguousRecognitionException("Found " + found.length + " matches for: " + objectDescription);
}
}
} |
|