Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.assertFalse;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertNull;
     23 import static org.junit.Assert.assertSame;
     24 import static org.junit.Assert.assertTrue;
     25 import static org.mockito.Matchers.any;
     26 import static org.mockito.Matchers.anyLong;
     27 import static org.mockito.Mockito.mock;
     28 import static org.mockito.Mockito.never;
     29 import static org.mockito.Mockito.reset;
     30 import static org.mockito.Mockito.times;
     31 import static org.mockito.Mockito.verify;
     32 
     33 import android.content.res.Configuration;
     34 import android.content.res.Resources;
     35 import android.graphics.Canvas;
     36 import android.graphics.Color;
     37 import android.graphics.ColorFilter;
     38 import android.graphics.PixelFormat;
     39 import android.graphics.Rect;
     40 import android.graphics.cts.R;
     41 import android.graphics.drawable.BitmapDrawable;
     42 import android.graphics.drawable.ColorDrawable;
     43 import android.graphics.drawable.Drawable;
     44 import android.graphics.drawable.Drawable.ConstantState;
     45 import android.graphics.drawable.RotateDrawable;
     46 import android.util.AttributeSet;
     47 import android.util.Xml;
     48 
     49 import androidx.test.InstrumentationRegistry;
     50 import androidx.test.filters.SmallTest;
     51 import androidx.test.runner.AndroidJUnit4;
     52 
     53 import org.junit.Before;
     54 import org.junit.Test;
     55 import org.junit.runner.RunWith;
     56 import org.xmlpull.v1.XmlPullParser;
     57 import org.xmlpull.v1.XmlPullParserException;
     58 
     59 import java.io.IOException;
     60 
     61 @SmallTest
     62 @RunWith(AndroidJUnit4.class)
     63 public class RotateDrawableTest {
     64     private Resources mResources;
     65     private RotateDrawable mRotateDrawable;
     66 
     67     @Before
     68     public void setup() {
     69         mResources = InstrumentationRegistry.getTargetContext().getResources();
     70         mRotateDrawable = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable);
     71     }
     72 
     73     @Test
     74     public void testConstructor() {
     75         new RotateDrawable();
     76     }
     77 
     78     @Test
     79     public void testDraw() {
     80         Canvas canvas = new Canvas();
     81         mRotateDrawable.draw(canvas);
     82     }
     83 
     84     @Test
     85     public void testInflate() {
     86         RotateDrawable d;
     87 
     88         d = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable_rel);
     89         assertEquals(0.1f, d.getPivotX(), 0.01f);
     90         assertEquals(0.2f, d.getPivotY(), 0.01f);
     91         assertEquals(360.0f, d.getFromDegrees(), 0.01f);
     92         assertEquals(360.0f, d.getToDegrees(), 0.01f);
     93         assertEquals(true, d.isPivotXRelative());
     94         assertEquals(true, d.isPivotYRelative());
     95 
     96         d = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable_abs);
     97         assertEquals(0.3f, d.getPivotX(), 0.01f);
     98         assertEquals(0.3f, d.getPivotY(), 0.01f);
     99         assertEquals(180.0f, d.getFromDegrees(), 0.01f);
    100         assertEquals(-180.0f, d.getToDegrees(), 0.01f);
    101         assertEquals(false, d.isPivotXRelative());
    102         assertEquals(false, d.isPivotYRelative());
    103     }
    104 
    105     @Test
    106     public void testSetPivot() {
    107         RotateDrawable d = new RotateDrawable();
    108         assertEquals(0.5f, d.getPivotX(), 0.01f);
    109         assertEquals(0.5f, d.getPivotY(), 0.01f);
    110         assertEquals(true, d.isPivotXRelative());
    111         assertEquals(true, d.isPivotYRelative());
    112 
    113         d.setPivotX(10.0f);
    114         assertEquals(10.0f, d.getPivotX(), 0.01f);
    115 
    116         d.setPivotY(10.0f);
    117         assertEquals(10.0f, d.getPivotY(), 0.01f);
    118 
    119         d.setPivotXRelative(false);
    120         assertEquals(false, d.isPivotXRelative());
    121 
    122         d.setPivotYRelative(false);
    123         assertEquals(false, d.isPivotYRelative());
    124     }
    125 
    126     @Test
    127     public void testSetDegrees() {
    128         RotateDrawable d = new RotateDrawable();
    129         assertEquals(0.0f, d.getFromDegrees(), 0.01f);
    130         assertEquals(360.0f, d.getToDegrees(), 0.01f);
    131 
    132         d.setFromDegrees(-10.0f);
    133         assertEquals(-10.0f, d.getFromDegrees(), 0.01f);
    134         assertEquals(360.0f, d.getToDegrees(), 0.01f);
    135 
    136         d.setToDegrees(10.0f);
    137         assertEquals(10.0f, d.getToDegrees(), 0.01f);
    138         assertEquals(-10.0f, d.getFromDegrees(), 0.01f);
    139     }
    140 
    141     @Test
    142     public void testGetChangingConfigurations() {
    143         assertEquals(0, mRotateDrawable.getChangingConfigurations());
    144 
    145         mRotateDrawable.setChangingConfigurations(Configuration.KEYBOARD_NOKEYS);
    146         assertEquals(Configuration.KEYBOARD_NOKEYS, mRotateDrawable.getChangingConfigurations());
    147 
    148         mRotateDrawable.setChangingConfigurations(Configuration.KEYBOARD_12KEY);
    149         assertEquals(Configuration.KEYBOARD_12KEY, mRotateDrawable.getChangingConfigurations());
    150     }
    151 
    152     @Test
    153     public void testSetAlpha() {
    154         mRotateDrawable.setAlpha(100);
    155         assertEquals(100, ((BitmapDrawable) mRotateDrawable.getDrawable()).getPaint().getAlpha());
    156 
    157         mRotateDrawable.setAlpha(255);
    158         assertEquals(255, ((BitmapDrawable) mRotateDrawable.getDrawable()).getPaint().getAlpha());
    159     }
    160 
    161     @Test
    162     public void testSetColorFilter() {
    163         ColorFilter filter = new ColorFilter();
    164         mRotateDrawable.setColorFilter(filter);
    165         assertSame(filter,
    166                 ((BitmapDrawable) mRotateDrawable.getDrawable()).getPaint().getColorFilter());
    167 
    168         mRotateDrawable.setColorFilter(null);
    169         assertNull(((BitmapDrawable) mRotateDrawable.getDrawable()).getPaint().getColorFilter());
    170     }
    171 
    172     @Test
    173     public void testGetOpacity() {
    174         assertEquals(PixelFormat.OPAQUE, mRotateDrawable.getOpacity());
    175     }
    176 
    177     @Test
    178     public void testInvalidateDrawable() {
    179         Drawable drawable = mResources.getDrawable(R.drawable.pass);
    180         Drawable.Callback callback = mock(Drawable.Callback.class);
    181 
    182         mRotateDrawable.setCallback(callback);
    183         mRotateDrawable.invalidateDrawable(null);
    184         verify(callback, times(1)).invalidateDrawable(any());
    185 
    186         reset(callback);
    187         mRotateDrawable.invalidateDrawable(drawable);
    188         verify(callback, times(1)).invalidateDrawable(any());
    189 
    190         reset(callback);
    191         mRotateDrawable.setCallback(null);
    192         mRotateDrawable.invalidateDrawable(drawable);
    193         verify(callback, never()).invalidateDrawable(any());
    194     }
    195 
    196     @Test
    197     public void testScheduleDrawable() {
    198         Drawable.Callback callback = mock(Drawable.Callback.class);
    199 
    200         mRotateDrawable.setCallback(callback);
    201         mRotateDrawable.scheduleDrawable(null, null, 0);
    202         verify(callback, times(1)).scheduleDrawable(any(), any(), anyLong());
    203 
    204         reset(callback);
    205         mRotateDrawable.scheduleDrawable(new ColorDrawable(Color.RED), () -> {}, 1000L);
    206         verify(callback, times(1)).scheduleDrawable(any(), any(), anyLong());
    207 
    208         reset(callback);
    209         mRotateDrawable.setCallback(null);
    210         mRotateDrawable.scheduleDrawable(null, null, 0);
    211         verify(callback, never()).scheduleDrawable(any(), any(), anyLong());
    212     }
    213 
    214     @Test
    215     public void testUnscheduleDrawable() {
    216         Drawable.Callback callback = mock(Drawable.Callback.class);
    217 
    218         mRotateDrawable.setCallback(callback);
    219         mRotateDrawable.unscheduleDrawable(null, null);
    220         verify(callback, times(1)).unscheduleDrawable(any(), any());
    221 
    222         reset(callback);
    223         mRotateDrawable.unscheduleDrawable(new ColorDrawable(Color.RED), () -> {});
    224         verify(callback, times(1)).unscheduleDrawable(any(), any());
    225 
    226         reset(callback);
    227         mRotateDrawable.setCallback(null);
    228         mRotateDrawable.unscheduleDrawable(null, null);
    229         verify(callback, never()).unscheduleDrawable(any(), any());
    230     }
    231 
    232     @Test
    233     public void testGetPadding() {
    234         Rect rect = new Rect();
    235         assertFalse(mRotateDrawable.getPadding(rect));
    236         assertEquals(0, rect.left);
    237         assertEquals(0, rect.top);
    238         assertEquals(0, rect.right);
    239         assertEquals(0, rect.bottom);
    240     }
    241 
    242     @Test
    243     public void testSetVisible() {
    244         assertTrue(mRotateDrawable.isVisible());
    245 
    246         assertTrue(mRotateDrawable.setVisible(false, false));
    247         assertFalse(mRotateDrawable.isVisible());
    248 
    249         assertFalse(mRotateDrawable.setVisible(false, true));
    250         assertFalse(mRotateDrawable.isVisible());
    251 
    252         assertTrue(mRotateDrawable.setVisible(true, false));
    253         assertTrue(mRotateDrawable.isVisible());
    254     }
    255 
    256     @Test
    257     public void testIsStateful() {
    258         assertFalse(mRotateDrawable.isStateful());
    259     }
    260 
    261     @Test
    262     public void testGetIntrinsicWidthAndHeight() throws XmlPullParserException, IOException {
    263         // testimage is set in res/drawable/rotatedrawable.xml
    264         Drawable drawable = mResources.getDrawable(R.drawable.testimage);
    265         assertEquals(drawable.getIntrinsicWidth(), mRotateDrawable.getIntrinsicWidth());
    266         assertEquals(drawable.getIntrinsicHeight(), mRotateDrawable.getIntrinsicHeight());
    267 
    268         RotateDrawable rotateDrawable = new RotateDrawable();
    269         XmlPullParser parser = mResources.getXml(R.drawable.rotatedrawable);
    270         while (parser.next() != XmlPullParser.START_TAG) {
    271             // ignore event, just seek to first tag
    272         }
    273         AttributeSet attrs = Xml.asAttributeSet(parser);
    274         rotateDrawable.inflate(mResources, parser, attrs);
    275         assertEquals(drawable.getIntrinsicWidth(), rotateDrawable.getIntrinsicWidth());
    276         assertEquals(drawable.getIntrinsicHeight(), rotateDrawable.getIntrinsicHeight());
    277     }
    278 
    279     @Test(expected=NullPointerException.class)
    280     public void testInflateNull() throws XmlPullParserException, IOException {
    281         mRotateDrawable.inflate(null, null, null);
    282     }
    283 
    284     @Test
    285     public void testGetConstantState() {
    286         ConstantState state = mRotateDrawable.getConstantState();
    287         assertNotNull(state);
    288     }
    289 
    290     @Test
    291     public void testMutate() {
    292         RotateDrawable d1 = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable);
    293         RotateDrawable d2 = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable);
    294         RotateDrawable d3 = (RotateDrawable) mResources.getDrawable(R.drawable.rotatedrawable);
    295 
    296         int restoreAlpha = d1.getAlpha();
    297 
    298         try {
    299             // verify bad behavior - modify before mutate pollutes other drawables
    300             d1.setAlpha(100);
    301             assertEquals(100, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
    302             assertEquals(100, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
    303             assertEquals(100, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
    304 
    305             d1.mutate();
    306             d1.setAlpha(200);
    307             assertEquals(200, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
    308             assertEquals(100, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
    309             assertEquals(100, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
    310 
    311             d2.setAlpha(50);
    312             assertEquals(200, ((BitmapDrawable) d1.getDrawable()).getPaint().getAlpha());
    313             assertEquals(50, ((BitmapDrawable) d2.getDrawable()).getPaint().getAlpha());
    314             assertEquals(50, ((BitmapDrawable) d3.getDrawable()).getPaint().getAlpha());
    315         } finally {
    316             // restore drawable state
    317             mResources.getDrawable(R.drawable.rotatedrawable).setAlpha(restoreAlpha);
    318         }
    319     }
    320 }
    321