preferences Android的JUnit进行测试 [英] Android JUnit testing of Preferences

查看:120
本文介绍了preferences Android的JUnit进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个相当正常的情况:一个是Android应用程序有一个preferences活动,并从列表中选择preference选项触发code更改列表preference的摘要文本。即:从颜色列表preference选择绿色将在preferenceChange 回调通过更改List preference的摘要文本绿色。

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.

我希望能够使用Android的JUnit测试,以确认都被正确地执行这些汇总的变化。然而,似乎很少的信息在那里就如何做到这一点。

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.

我试着使用变化的setValue()项列表preference,无论是在测试线程,并通过 runOnUiThread(),但没有成功 - 这不会触发preferenceChange调用()。我也试过 getInstrumentation()。waitForIdleSync()呼叫后的setValue(),但也没有成功

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?

谢谢!

推荐答案

几个小时的工作产生这种工作的解决方案,但我很好奇,如果其他人有更好的解决方案。这code是由<一个启发href=\"http://stackoverflow.com/questions/4805896/how-to-open-or-simulate-a-click-on-a-android-$p$pference-which-was-created-with-x/4869034#4869034\">this解决方案,以一个类似的问题,但这种情况下,有两种不同的方式:

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:


  1. 它适用于由Android JUnit的,这意味着它需要调用通过 runOnUiThread列表preference UI点击()使用。

  2. 据预计,那里是使用preference类,其中足够复杂找准位置(相对于整个preferences名单)点击。上述解决方案仅适用于例无preference类别。

被点击此方法将接受对特定列表preference项的键时,与该列表中的项的位置沿。然后,将执行列表项的点击,和其他code会做我要找的检查。

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.

请注意,这需要 setActivityInitialTouchMode(真); getActivity()呼叫前要设置的设置()方法。

Note that this requires setActivityInitialTouchMode(true); to be set before the getActivity() call in the setUp() method.

private void clickListPreference(String _listPreferenceKey, int _listItemPos){
    final String listPreferenceKey = _listPreferenceKey;
    final int listItemPos = _listItemPos;

    mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    // get a handle to the particular ListPreference
                    ListPreference listPreference= (ListPreference) mActivity.findPreference(listPreferenceKey);

                    // bring up the dialog box  
                    mActivity.getPreferenceScreen().onItemClick( null, null, getPreferencePosition(), 0 ); 

                    // click the requested item
                    AlertDialog listDialog = (AlertDialog) listPreference.getDialog();
                    ListView listView = listDialog.getListView();
                    listView.performItemClick(listView, listItemPos, 0);
                }

                /***
                 * Finding a ListPreference is difficult when Preference Categories are involved,
                 * as the category header itself counts as a position in the preferences screen
                 * list.
                 * 
                 * This method iterates over the preference items inside preference categories
                 * to find the ListPreference that is wanted.
                 * 
                 * @return The position of the ListPreference relative to the entire preferences screen list
                 */
                private int getPreferencePosition(){
                    int counter = 0;
                    PreferenceScreen screen = mActivity.getPreferenceScreen();

                     // loop over categories
                    for (int i = 0; i < screen.getPreferenceCount(); i++){
                        PreferenceCategory cat = (PreferenceCategory) screen.getPreference(i);
                        counter++;

                        // loop over category items
                        for (int j = 0; j < cat.getPreferenceCount(); j++){ 
                            if (cat.getPreference(j).getKey().contentEquals(listPreferenceKey)){
                                return counter;
                            }
                            counter++;
                        }
                    }
                    return 0; // did not match
                }
            }
    );

    getInstrumentation().waitForIdleSync();
}

这篇关于preferences Android的JUnit进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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