首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActivityUnitTestCase对PreferenceScreen

ActivityUnitTestCase对PreferenceScreen
EN

Stack Overflow用户
提问于 2013-10-07 18:58:17
回答 1查看 158关注 0票数 0

我有一个新开发的android-18应用程序,它的PreferenceScreen太明显了,太简单了,无法列出。

我还有一个自动测试,可以把问题转换成答案。

代码语言:javascript
复制
public class MyPreferenceTest extends ActivityUnitTestCase<MyPreference> {  
    Intent intent;
    MyPreference activity;

    public MyPreferenceTest() {
        super(MyPreference.class);
    }

    @Override
    protected void setUp() throws Exception {
        Context targetContext = getInstrumentation().getTargetContext();
        intent = new Intent(targetContext, MyPreference.class);
        super.setUp();
    }

    private void assembleActivity() {
        startActivity(intent, null, null);
        activity = getActivity();
    }

    private void assemblePreferences(String device, String userName, String password) {
        Context targetContext = getInstrumentation().getTargetContext();        
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(targetContext);
        SharedPreferences.Editor editor = sharedPref.edit();

        editor.putString("smart_phin_bluetooth_devices", device);
        editor.putString("username", userName);  
        editor.putString("password", password);  
        editor.commit();
    }

    public void test_empty_userNames_dont_reflect_into_the_summary() {
        assemblePreferences("", "", ""); 
        assembleActivity();
        activity.onResume();
        EditTextPreference userName = (EditTextPreference) activity.findPreference("username");  // FIXME  userName
        assertEquals("", userName.getEditText().getText().toString());
        assertEquals("", userName.getText());
        assertEquals("Enter your user name", userName.getSummary().toString());
    }

    public void test_full_userNames_reflect_into_the_summary() {
        assemblePreferences("", "BookerT", "");
        assembleActivity();

        activity.onResume();  //  We must call this bc Android does but the test rig does not...
        EditTextPreference userName = (EditTextPreference) activity.findPreference("username");
        assertEquals("username", userName.getKey());

        assertEquals("BookerT", String.valueOf(userName.getText().toString());
//        assertEquals("BookerT", userName.getEditText().getText().toString());
        assertEquals("BookerT", userName.getSummary().toString());
    }

}  // end of class MyPreferenceTest

注释掉的断言将失败,因为还没有调用该EditText视图。

测试现在检查onResume()中的新代码是否从泛型语句中更新摘要,以反映首选项的当前值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-07 21:01:07

你的测试中有个错误。

代码语言:javascript
复制
private void assemblePreferences(String device, String userName, String password)

注意,这里的值顺序是deviceuserNamepassword

代码语言:javascript
复制
assemblePreferences("BookerT", "", "");

因此,deviceBookerT,而不是userName

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19232475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档