AttrsDecorator implements Decorator { @Override public void apply(View view, View parent, String name, Context context, AttributeSet attributeSet) { if (!clazz().isAssignableFrom(view.getClass())) { return; } values = obtainAttributes(context, attributeSet); if(values == null || values.length() == 0) return; try { for (int i = 0; i < values.length(); i++) { TypedValue buf = new TypedValue(); if (values.hasValue(i) && values.getValue(i, buf)) { apply((T) view, values); break; } } } finally { values.recycle(); } }  @Override void apply(View view, View parent, String name, Context context, AttributeSet attributeSet);  }
@Test public void decorNotAppliedOnWidgetOfAnotherType() throws Exception { String name = "android.widget.ImageView"; ImageView imageView = mock(ImageView.class); attrsDecorator.apply(imageView, parent, name, context, attributeSet); assertThat(attrsDecorator.values).isNull(); }
@Test public void decorNotAppliedOnWidgetButWithoutAttr() throws Exception { String name = "android.widget.TextView"; spyWithTypedArray(null); attrsDecorator.apply(textView, parent, name, context, attributeSet); assertThat(attrsDecorator.values).isNull(); }
@Test public void decorNotAppliedOnWidgetWithAttrWithoutValue() throws Exception { TypedArray typedArray = mockTypedArray(1, false); spyWithTypedArray(typedArray); String name = "android.widget.TextView"; attrsDecorator.apply(textView, parent, name, context, attributeSet); assertThat(attrsDecorator.values).isNotNull(); verify(typedArray).recycle(); assertThat(attrsDecorator.getDecorStrValue()).isNull(); }
@Test public void decorAppliedWithAttrValue() throws Exception { TypedArray typedArray = mockTypedArray(1, true); when(typedArray.getString(0)).thenReturn("test"); spyWithTypedArray(typedArray); String name = "android.widget.TextView"; attrsDecorator.apply(textView, parent, name, context, attributeSet); assertThat(attrsDecorator.values).isNotNull(); assertThat(attrsDecorator.values.length()).isGreaterThan(0); verify(typedArray).recycle(); assertThat(attrsDecorator.getDecorStrValue()).isEqualTo("test"); }