如何使用 SWTBot 来使用 Eclipse 的快速辅助功能?

发布于 2024-11-19 14:40:25 字数 316 浏览 7 评论 0 原文

我想使用 SWTBot 通过快速辅助菜单内联局部变量。我的SWTBot测试弹出快速辅助菜单,但无法选择提案项。我已经在 一个最小项目 ="nofollow">GitHub 演示了这个问题,并打开了一个问题详细描述了问题。

I'd like to use SWTBot to inline a local variable via the quick assist menu. My SWTBot test pops up the quick assist menu, but it fails to select the proposal item. I've created a minimal project on GitHub that demonstrates this problem and opened an issue that describes the problem in detail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-11-26 14:40:26

我对自动完成菜单也有同样的问题,我发现的唯一解决方法是实现我自己的自动完成方法。您可以轻松执行类似的操作以获得快速帮助。我的代码在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);
        }
    });
}
}

I had the same problem with the auto-completion menu and the only workaround I found was to implement my own autoComplete methods. You can easily do something similar for quick assist. My code has been tested under windows and unix systems:

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);
        }
    });
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文