Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics.drawable.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.content.res.Resources;
     23 import android.content.res.Resources.Theme;
     24 import android.content.res.TypedArray;
     25 import android.graphics.Canvas;
     26 import android.graphics.Color;
     27 import android.graphics.ColorFilter;
     28 import android.graphics.PixelFormat;
     29 import android.graphics.cts.R;
     30 import android.graphics.drawable.Drawable;
     31 import android.support.test.InstrumentationRegistry;
     32 import android.support.test.filters.SmallTest;
     33 import android.support.test.runner.AndroidJUnit4;
     34 import android.util.AttributeSet;
     35 
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.xmlpull.v1.XmlPullParser;
     39 import org.xmlpull.v1.XmlPullParserException;
     40 
     41 import java.io.IOException;
     42 
     43 @SmallTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class CustomDrawableTest {
     46     @Test
     47     public void testInflation() {
     48         Drawable dr = InstrumentationRegistry.getTargetContext().getDrawable(
     49                 R.drawable.custom_drawable);
     50         assertTrue(dr instanceof CustomDrawable);
     51         assertEquals(Color.RED, ((CustomDrawable) dr).getColor());
     52     }
     53 
     54     public static class CustomDrawable extends Drawable {
     55         private static final int[] ATTRS = new int[] { android.R.attr.color };
     56 
     57         private int mColor;
     58 
     59         @Override
     60         public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)
     61                 throws XmlPullParserException, IOException {
     62             super.inflate(r, parser, attrs, theme);
     63 
     64             final TypedArray ta;
     65             if (theme != null) {
     66                 ta = theme.obtainStyledAttributes(attrs, ATTRS, 0, 0);
     67             } else {
     68                 ta = r.obtainAttributes(attrs, ATTRS);
     69             }
     70 
     71             mColor = ta.getColor(0, Color.BLACK);
     72         }
     73 
     74         public int getColor() {
     75             return mColor;
     76         }
     77 
     78         @Override
     79         public void draw(Canvas canvas) {
     80 
     81         }
     82 
     83         @Override
     84         public void setAlpha(int alpha) {
     85 
     86         }
     87 
     88         @Override
     89         public void setColorFilter(ColorFilter colorFilter) {
     90 
     91         }
     92 
     93         @Override
     94         public int getOpacity() {
     95             return PixelFormat.OPAQUE;
     96         }
     97     }
     98 }
     99