我有一個新創建的android-18應用程序,其中PreferenceScreen
太明顯了,&很簡單。ActivityUnitTestCase vs PreferenceScreen
而且我還有一個自動化測試[固定將問題轉換爲答案]。
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()中的新代碼是否更新來自通用語言的摘要以反映首選項的當前值。
哪個斷言「返回」「字符串,就好像首選項是空的」? 'test_full_userNames_reflect_into_the_summary()'中有四個,其中兩個被註釋掉。所以,例如,你是說'userName.getKey()'返回一個空字符串? – CommonsWare
我對'userName.getEditText()。getText()'返回''BookerT'''生根。如果(工作原理)EditText對象尚未填充,因爲它沒有顯示,一般的問題是如何編寫測試,從PreferenceScreen中提取行爲細節 - 最好是_without_在問題中拋出Robotium ... – Phlip