Android JUnit 首选项测试
一个相当正常的场景:Android 应用程序有一个首选项活动,从 ListPreference 中选择一个选项会触发代码来更改该 ListPreference 的摘要文本。即:从颜色 ListPreference 中选择“绿色”将通过 onPreferenceChange
回调将 ListPreference 的摘要文本更改为“绿色”。
我希望能够使用 Android JUnit 测试来确认这些摘要更改均已正确执行。然而,关于如何做到这一点的信息似乎很少。
我尝试过在测试线程中和通过 runOnUiThread()
在 ListPreference 上使用 setValue()
的变体,但没有成功 - 这不会触发对onPreferenceChange()
。我还在调用 setValue()
后尝试过 getInstrumentation().waitForIdleSync()
,但这也没有成功。
所以,我的问题是:这是如何完成的?
谢谢!
A fairly normal scenario: an Android application has a preferences activity, and selecting an option from a ListPreference triggers code to change that ListPreference's summary text. ie: Selecting "Green" from a color ListPreference would change the ListPreference's summary text to "Green" through a onPreferenceChange
callback.
I'd like to be able to use the Android JUnit testing to confirm that these summary changes are all being performed correctly. However, there seems to be very little information out there on how to do this.
I've tried variations of using setValue()
on ListPreference, both in the test thread and through runOnUiThread()
, with no success - this doesn't trigger the call to onPreferenceChange()
. I've also tried getInstrumentation().waitForIdleSync()
after calling setValue()
, but that's also with no success.
So, my question is: how is this done?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个小时的工作产生了这个工作解决方案,但我很好奇是否其他人有更好的解决方案。此代码的灵感来自 这个解决方案针对类似的问题,但这种情况有两点不同:
runOnUiThread()
单击。此方法将接受特定 ListPreference 项目的键,以及列表中要单击的项目的位置。然后它将执行该列表项单击,其他代码将执行我正在寻找的检查。
请注意,这需要在
setUp()
方法中调用getActivity()
之前设置setActivityInitialTouchMode(true);
。A few more hours of work produced this working solution, but I'm curious if anyone else has a better solution. This code was inspired by this solution to a similar question, but this case is different in two ways:
runOnUiThread()
.This method will accept the key for a particular ListPreference item, along with the position of the item in the list to be clicked. It will then perform that list item click, and other code would do the checking I'm looking for.
Note that this requires
setActivityInitialTouchMode(true);
to be set before thegetActivity()
call in thesetUp()
method.