2015-01-06 14 views
5

を読まれることはありませんRobotoAttributeSetが作成され、カスタムビューに渡すように見える読まないか、正しく構築されません。ここでRobolectricのRoboAttributeSetは

は私のテストである:ここでは

ArrayList<Attribute> attributes = new ArrayList<>(); 
    attributes.add(
     new Attribute("com.package.name:attr/CustomButton_inputType", 
      String.valueOf(2), "com.package.name")); // no matter what value I use (2) 

    AttributeSet attrs = 
     new RoboAttributeSet(attributes, Robolectric.application.getResources(), CustomButton.class); 

    CustomButton button = new CustomButton(Robolectric.application, attrs); 

は私attr.xml次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CustomButton"> 
    <attr name="inputType" format="enum"> 
     <enum name="text" value="0"/> 
     <enum name="textEmailAddress" value="1"/> 
     <enum name="password" value="2"/> 
    </attr> 
    </declare-styleable> 
</resources> 

CustomButtonの一部:だからどんなに属性を準備するとき、私が設定したもの値

private void applyAttributes(Context context, AttributeSet attrs) { 
    TypedArray typedArray = context.getTheme() 
     .obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0); 

    try { 
     int typeValue = // is always 0 
      typedArray.getInt(R.styleable.CustomButton_inputType, 0); 
     switch (typeValue) { 
     case 0: 
// do something 
      break; 
     case 1: 
// do something 
      break; 
     case 2: 
      // do something 
      break; 
     default: 
      // just do nothing 
      break; 
     } 
    } finally { 
     typedArray.recycle(); 
    } 
    } 

(これは一例で2です)、私は常に取得の場合は3210です。

何か間違っていますか?どうもありがとう!

+1

を試してみて、私は私のセットアップに類似したしようとしたともstyledArrayが空であることを確認しました。しかし、私はそれがアクティビティとXMLリソースからビューを膨張しようとすると空ではないことがわかります。このテスト方法について検討しますか?それは常にゼロのいずれかの方法だ - –

+1

私は、いや、フィールドの名前を渡しても問題が解決しないチケットhttps://github.com/robolectric/robolectric/issues/1478 –

答えて

3

問題は、テスト、属性セットに渡され、特に値から来ています。 は確かに、代わりに列挙型の値フィールドを渡すのは、列挙型の名前フィールドを渡す必要がありますので、あなたは、あなたのビューに後者、関連する値フィールドを取得することができます。

attributes.add(
    new Attribute("com.package.name:attr/CustomButton_inputType", 
     "textEmailAddress", "com.package.name")); 

これはまた:) を助けることができる希望は、行くとmy post dealing with custom attributesを見てすることを躊躇しないでください。

+0

Turhanオズを作成しました。 – Eugene

+0

奇妙な。どのバージョンのRobolectricを使用していますか?私が使用している2.2(私がチェックしているに対する1) –

+1

で試してみてください「:robolectric:org.robolectric 2.4」 – Eugene

0

attributes.add(
     new Attribute("com.package.name:attr/inputType", 
      String.valueOf(2), "com.package.name")); 
関連する問題