2011-07-10 22 views
0

SWTBotを使用して、クイックヘルプメニューからローカル変数をインライン展開したいと思います。私のSWTBotテストでは、クイックアシストメニューがポップアップしますが、提案項目を選択できません。この問題を示すa minimal projectGitHubに作成し、問題を詳しく説明したan issueを開いた。SWTBotを使用してEclipseのクイックアシスト機能を実行するにはどうすればよいですか?

答えて

1

私はオートコンプリートメニューで同じ問題を抱えていました。私が見つけた唯一の回避策は、自分のautoCompleteメソッドを実装することでした。迅速なアシストのために、同様のことを簡単に行うことができます。私のコードはWindowsとUnixシステムでテストされています:

package aaa.bbb.ccc; 

import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType; 

import java.util.List; 

import org.eclipse.jface.bindings.keys.KeyStroke; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Table; 
import org.eclipse.swtbot.swt.finder.SWTWorkbenchBot; 
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; 
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; 
import org.eclipse.swtbot.swt.finder.results.VoidResult; 
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; 
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; 
import org.junit.Assert; 

public abstract class AutoCompletionHelper { 

public static void autoCompleteWithFirstMatchingProposal(SWTWorkbenchBot bot) { 
    SWTBotTable proposalsTable = showCompletionProposalsTable(bot); 

    Assert.assertTrue("No completion proposals found", proposalsTable.rowCount() > 0); 

    selectProposal(proposalsTable, 0); 
} 

public static void autoCompleteWithProposal(SWTWorkbenchBot bot, String completionProposal) { 
    SWTBotTable proposalsTable = showCompletionProposalsTable(bot); 
    int rowCount = proposalsTable.rowCount(); 

    int index = -1; 
    int matchingProposalsCount = 0; 

    for (int i = 0; i < rowCount; i++) { 
     if (proposalsTable.cell(i, 0).startsWith(completionProposal)) { 
      index = i; 
      matchingProposalsCount++; 
     } 
    } 

    Assert.assertFalse("No completion proposals matching '" + completionProposal + "'", matchingProposalsCount == 0); 
    Assert.assertFalse("Multiple completion proposals matching '" + completionProposal + "'", matchingProposalsCount > 1); 

    selectProposal(proposalsTable, index); 
} 

private static SWTBotTable showCompletionProposalsTable(EclipseBot bot) { 
    bot.pressShortcut(KeyStroke.getInstance(SWT.CTRL, 0), KeyStroke.getInstance(0, SWT.SPACE)); 

    bot.sleep(100); //Wait for auto-completion shell to be displayed 
    List<Shell> shells = bot.shells(""); 
    Table proposalsTable = null; 

    long timeout = SWTBotPreferences.TIMEOUT; 
    SWTBotPreferences.TIMEOUT = 200; 
    boolean findInvisibleControls = bot.getFinder().shouldFindInvisibleControls(); 
    bot.getFinder().setShouldFindInvisibleControls(true); 

    try { 
     for (Shell shell : shells) { 
      try { 
       proposalsTable = bot.widget(widgetOfType(Table.class), shell); 
      } catch (WidgetNotFoundException ex) { 
       continue; 
      } 
      break; 
     } 
    } finally { 
     bot.getFinder().setShouldFindInvisibleControls(findInvisibleControls); 
     SWTBotPreferences.TIMEOUT = timeout; 
    } 

    if (proposalsTable == null) { 
     throw new RuntimeException("Did not find any completion proposals table ..."); 
    } 
    return new SWTBotTable(proposalsTable); 
} 

private static void selectProposal(final SWTBotTable proposalsTable, final int proposalIndex) { 
    UIThreadRunnable.asyncExec(new VoidResult() { 

     @Override 
     public void run() { 
      Table table = proposalsTable.widget; 
      table.setSelection(proposalIndex); 
      Event event = new Event(); 
      event.type = SWT.Selection; 
      event.widget = table; 
      event.item = table.getItem(proposalIndex); 
      table.notifyListeners(SWT.Selection, event); 
      table.notifyListeners(SWT.DefaultSelection, event); 
     } 
    }); 
} 
} 
関連する問題