gpt4 book ai didi

java - 如何测试 InputMethodService

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:11 32 4
gpt4 key购买 nike

我有一个 Android 的基本实现 InputMethodService我正在尝试为其编写单元测试。我的应用程序没有任何 Activity ,只有 InputMethodService 的实现。

到目前为止,我已经有了一个运行良好的 ServiceTestCase 的基本实现:

SoftKeyboardTest.java

    public class SoftKeyboardTest extends ServiceTestCase<SoftKeyboard> {

@Override
protected void setUp() throws Exception {
super.setUp();
bindService(new Intent(this.getContext(), SoftKeyboard.class));
}

public void testShowKeyboard() {
this.getService().ShowKeyboard();
assertTrue(this.getService().GetKeyboardIsVisible());
}

public void testInsertText() {
String text = "Hello, world";
this.getService().InsertText(text);
assertEquals(this.getService().ReadText(text.length()), text);
}
}

但是,我想测试一些使用 getCurrentInputConnection() 将文本插入当前聚焦的 EditText 的功能。 :

SoftKeyboard.java

public void InsertText(String sentence) {
getCurrentInputConnection().commitText(sentence, 1);
}

public void ReadText(int chars) {
getCurrentInputConnection().getTextBeforeCursor(chars, 0);
}

显然,在这种情况下,由于实际上没有任何聚焦的 EditText,我得到了一个 NullPointerException。

如何让我的测试应用程序启动我的服务,以某种方式关注 EditText,然后启动我的测试用例以便我可以正确地测试我的服务方法?

最佳答案

如果您正在对输入法进行单元测试,我会在足够低的级别对其进行测试,它不需要有 InputConnection。如果您正在进行集成/验收级别测试,我会编写一个带有编辑文本字段的应用程序,并通过那里驱动输入/输出。

但实际上,已经做了 2 年 - 那个级别的测试几乎毫无用处。您的问题不会来自默认的编辑文本实现 - 它将来自数千个子类化 EditText 或创建自己的 EditText 类的应用程序,这些类的行为略有不同。您无法对此进行自动化测试,并且您将花费数年时间来修复它们的错误。

关于java - 如何测试 InputMethodService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17278241/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com