2016-04-28 56 views
2

我知道我有過最具異國情調的用例,但我非常喜歡測試,所以我想測試一下IHandler是否按我的設想進行測試。問題是我無法弄清楚如何以編程方式調用它:從集成測試中調用處理程序

IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 
IWorkbenchWindow site = workbenchPage.getWorkbenchWindow(); 
Command command = ((ICommandService) site.getService(ICommandService.class)).getCommand(commandId); 

final IHandlerService service = (IHandlerService) site.getService(IHandlerService.class); 
final Event trigger = new Event(); // or trigger = null 
ExecutionEvent executionEvent = service.createExecutionEvent(command, trigger); 
command.executeWithChecks(executionEvent); 

處理程序被調用,但ExecutionEvent沒有正確填寫(例如返回空,即使我只是打開一個編輯器)。

如何以正確的方式以編程方式調用IHandler

回答

0

我也喜歡測試很多:-)但是,我通常手動裝備評估上下文與處理程序執行所需的變量。

例如:

EvaluationContext context = new EvaluationContext(null, new Object()) 
context.addVariable(ISources.ACTIVE_PART_NAME, activePart); 
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection); 
// add whatever is used by the handler... 

Map<String, String> parameters = new HashMap<>(); 
ExecutionEvent event = new ExecutionEvent(command, parameters, null, context); 

command.executeWithChecks(event); 

讓我知道如果這個方法是可行的爲您服務。否則,我可能能夠挖掘出如何獲得我的測試助手代碼的評估上下文的真實。

+0

你可以很明顯地從IHandlerService.getCurrentState()得到'IEvaluationContext' ...或者至少現在我的測試工作。 –

相關問題