ThemeUtil { public static Drawable getDrawable(Context context, final TypedArray typedArray, final int index) { final TypedValue value = typedArray.peekValue(index); if (value == null) { return null; } if (value.type == TypedValue.TYPE_REFERENCE || value.type == TypedValue.TYPE_STRING) { return typedArray.getDrawable(index); } else if (value.type == TypedValue.TYPE_INT_COLOR_ARGB8 || value.type == TypedValue.TYPE_INT_COLOR_ARGB4 || value.type == TypedValue.TYPE_INT_COLOR_RGB8 || value.type == TypedValue.TYPE_INT_COLOR_RGB4) { return new ColorDrawable(typedArray.getColor(index, 0xFF000000)); } else { throw new IllegalArgumentException( context.getString(R.string.msdkui_exception_attribute_invalid, index)); } } private  ThemeUtil(); static Drawable getDrawable(Context context, final TypedArray typedArray, final int index); static String getString(final TypedArray typedArray, final int index); @ColorInt static int getColor(Context context, int colorAttribute); static int getStyleId(Context context, int themeAttribute);  }
@Test public void testGetDrawableWhenTypeValueIsNull() { TypedArray typedArray = mock(TypedArray.class); when(typedArray.peekValue(anyInt())).thenReturn(null); assertNull(ThemeUtil.getDrawable(getApplicationContext(), typedArray, anyInt())); }
@Test public void testGetDrawableWhenTypeIsReference() { TypedArray typedArray = mock(TypedArray.class); TypedValue typedValue = spy(new TypedValue()); typedValue.type = TypedValue.TYPE_REFERENCE; when(typedArray.peekValue(anyInt())).thenReturn(typedValue); ThemeUtil.getDrawable(getApplicationContext(), typedArray, anyInt()); verify(typedArray).getDrawable(anyInt()); }
@Test(expected = IllegalArgumentException.class) public void testGetDrawableWhenTypeIsSomethingElse() { TypedArray typedArray = mock(TypedArray.class); TypedValue typedValue = spy(new TypedValue()); typedValue.type = TypedValue.TYPE_FLOAT; when(typedArray.peekValue(anyInt())).thenReturn(typedValue); ThemeUtil.getDrawable(getApplicationContext(), typedArray, anyInt()); }