Wiki

Clone wiki

main / Testing_Framework

Testing Framework

Due to the high number of wizards and dialogs that are used in Qualyzer we have developed a strategy for more easily testing the handlers which create the wizards and dialogs and then act on the data that was collected.

Handlers

Apply these changes to any command handler that needs to be tested.

  • The handler must implement ITestableHandler
  • Add the following fields to the handler:
private boolean fTesting = false;
private boolean fTester = new NullTester();
  • Get the selection from the project explorer, and the shell from PlatformUI:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
ISelection selection = view.getCommonViewer().getSelection();
	
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
  • If using a Wizard use QualyzerWizardDialog instead of WizardDialog.
  • Create any dialogs as follows:
Dialog dialog = new Dialog(shell, ...);
dialog.setBlockOnOpen(!fTesting);
dialog.open();
fTester.execute(dialog);
if(dialog.getReturnCode() == Window.OK)
{
	...
}
  • Any calls to MessageDialog.openConfirm(shell, ...) should be changed as follows:
boolean confirm = fTesting || MessageDialog.openConfirm(shell, ...);

Dialogs and Wizards

  • Override the okPressed() method and make it public.
  • Add accessors for any widgets whos values you want to manipulate.
  • Convert any KeyListeners and KeyAdapters into ModifyListeners.

Handler Tests

The test itself should follow this template:

@Test
public void testHandler()
{
	TestUtil.setProjectExplorerSelection( /* Object To Select */);
	
	Handler handler = new Handler();
	handler.setTesting(true);
	handler.setTester(new IDialogTester(){
		public void execute(Dialog dialog)
		{
			/* Cast to specific dialog */
		
			/* Access and set the values of any widgets */
			
			dialog.okPressed();
			/* Or if a QualyzerWizardDialog then use
			dialog.finishPressed();
			*/
		}
	});
	
	try
	{
		handler.execute(null);
	}
	catch(ExecutionException e)
	{
		fail();
	}
	
	/* Assert the various values */
}

Updated