2013年11月26日火曜日

Espresso で EditTextPreference に文字を入力する方法

Espresso で EditText に日本語を入力する方法」と「Espresso で Preference をクリックさせる」の応用です。

EditTextPreference はダイアログ内の EditText をコードから生成し、Id として com.android.internal.R.id.edit をセットしています。

http://tools.oesf.biz/android-4.4.0_r1.0/xref/frameworks/base/core/java/android/preference/EditTextPreference.java#53
  1. 45 public class EditTextPreference extends DialogPreference {  
  2. 46     /** 
  3. 47      * The edit text shown in the dialog. 
  4. 48      */  
  5. 49     private EditText mEditText;  
  6. 50   
  7. 51     private String mText;  
  8. 52   
  9. 53     public EditTextPreference(Context context, AttributeSet attrs, int defStyle) {  
  10. 54         super(context, attrs, defStyle);  
  11. 55   
  12. 56         mEditText = new EditText(context, attrs);  
  13. 57   
  14. 58         // Give it an ID so it can be saved/restored  
  15. 59         mEditText.setId(com.android.internal.R.id.edit);  
  16. 60   
  17. 61         /* 
  18. 62          * The preference framework and view framework both have an 'enabled' 
  19. 63          * attribute. Most likely, the 'enabled' specified in this XML is for 
  20. 64          * the preference framework, but it was also given to the view framework. 
  21. 65          * We reset the enabled state. 
  22. 66          */  
  23. 67         mEditText.setEnabled(true);  
  24. 68     }  
internal なので Id を指定する方法は使えません。そこで、isAssignableFrom()を使います。(withClassName()でも実現できます)
  1. public void testEditTextPreference() {  
  2.     // EditTextPreference をクリック  
  3.     onData(withPreferenceKey("edit_text_pref1")).perform(click());  
  4.     onView(isAssignableFrom(EditText.class)).perform(clearText(), new InputTextAction("エスプレッソよりカフェラテ派"));  
  5.     onView(withText("OK")).perform(click());  
  6.   
  7.     // 例えば入力値が summary に表示されるような実装になっているなら、それをチェックできる  
  8.     onData(withPreferenceKey("edit_text_pref1"))  
  9.         .onChildView(withId(android.R.id.summary))  
  10.         .check(matches(withText("エスプレッソよりカフェラテ派")));  
  11. }  



0 件のコメント:

コメントを投稿