在仪表测试发送大写字母一个文本编辑 [英] Sending upper case letters to a TextEdit during instrumented tests

查看:260
本文介绍了在仪表测试发送大写字母一个文本编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个JUnit测试案例我的Andr​​oid应用程序。测试类扩展 ActivityInstrumentationTestCase2 键,通话的SendKeys()来模拟对文本编辑小部件。然而,所有的字母键codeS(如 KeyEvent.KEY code_G )的只发送小写字母的文本编辑。我试着发 KeyEvent.KEY code_SHIFT_LEFT 发送字母键code,但似乎并没有工作之前。那么,如何模拟用户输入一个大写字母?

I am writing a JUnit test case for my Android app. The test class extends ActivityInstrumentationTestCase2 and calls sendKeys() to emulate user input for TextEdit widgets. However, all of the alphabetic keycodes (e.g. KeyEvent.KEYCODE_G) only send lower case letters to the TextEdit. I tried sending KeyEvent.KEYCODE_SHIFT_LEFT before sending an alphabetic keycode, but that didn't seem to work. So how do I simulate the user typing an upper-case letter?

编辑:

我可以手动输入大写字母。实际上,的EditText被定义为

I can enter upper case letters manually. In fact, the EditText is defined as

    <EditText android:id="@id/brand_text"
              android:singleLine="true"
              android:capitalize="words"
              android:hint="@string/brand_hint"
    />

的android:大写=字属性将强制屏幕键盘到模拟器大写模式。 (我假设它会做同样的设备上,但没有一个测试它。)由于其自带的SDK模拟器并不模拟硬件键盘,我一直无法测试我的UI是如何工作的使用硬键。

The android:capitalize="words" attribute forces the onscreen keyboard into uppercase mode in the emulator. (I assume it will do the same on a device but don't have one to test it on.) Since the emulator which comes with the SDK doesn't emulate the hardware keyboard, I have been unable to test how my UI works using hard keys.

我也试过

EditText brandText = this.activity.findViewById(R.id.brand_text);
brandText.setText(someString);

不过,我这样做时,测试失败。我砍掉了所有的code,所以我不此刻在这里有细节。我会尝试重新创建它,并与细节编辑这个问题。

However, the test failed when I did this. I axed all that code, so I don't have the details here at the moment. I will try to recreate it and edit this question with those details.

推荐答案

我没有在我的OP,我在写一个方法来发送一个字符串中的字符提到一个文本框。我试着用的setText()作为@PaulHarris建议,但不能让它开始工作;我的测试中还是失败了一些断言。

I didn't mention in my OP that I was writing a method to send the characters of a String to a text box. I tried using setText() as @PaulHarris suggested but couldn't get it to work; my tests still failed some assertions.

在一些挖,终于找到<一href=\"http://developer.android.com/reference/android/app/Instrumentation.html#sendStringSync%28java.lang.String%29\"相对=nofollow> Instrumentation.sendStringSync() 它适合我的目的。 (您可以通过调用<一个获得仪表对象href=\"http://developer.android.com/reference/android/test/InstrumentationTestCase.html#getInstrumentation%28%29\"相对=nofollow> getInstrumentation()在您的测试类。)

After some digging, finally found Instrumentation.sendStringSync() which works for my purposes. (You can get an Instrumentation object by calling getInstrumentation() in your test class.)

这篇关于在仪表测试发送大写字母一个文本编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆