Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2017 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 androidx.vectordrawable.graphics.drawable.tests;
     18 
     19 import static androidx.vectordrawable.graphics.drawable.tests.DrawableUtils
     20         .saveVectorDrawableIntoPNG;
     21 
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import android.content.Context;
     25 import android.content.res.Resources;
     26 import android.graphics.Bitmap;
     27 import android.graphics.Canvas;
     28 import android.graphics.drawable.Drawable;
     29 import android.support.test.InstrumentationRegistry;
     30 import android.support.test.filters.MediumTest;
     31 import android.support.test.rule.ActivityTestRule;
     32 
     33 import androidx.vectordrawable.graphics.drawable.Animatable2Compat.AnimationCallback;
     34 import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
     35 import androidx.vectordrawable.test.R;
     36 
     37 import org.junit.Before;
     38 import org.junit.Rule;
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 import org.junit.runners.Parameterized;
     42 
     43 import java.util.Arrays;
     44 import java.util.Collection;
     45 
     46 @MediumTest
     47 @RunWith(Parameterized.class)
     48 public class AnimatedVectorDrawableParameterizedTest {
     49     @Rule public final ActivityTestRule<DrawableStubActivity> mActivityTestRule =
     50             new ActivityTestRule<>(DrawableStubActivity.class);;
     51 
     52     private static final int IMAGE_WIDTH = 64;
     53     private static final int IMAGE_HEIGHT = 64;
     54     private static final boolean DBG_DUMP_PNG = false;
     55 
     56     private Context mContext;
     57     private Resources mResources;
     58     private int mResId;
     59     private int mStartExpected;
     60     private int mEndExpected;
     61 
     62     @Parameterized.Parameters
     63     public static Collection<Object[]> data() {
     64         return Arrays.asList(new Object[][]{
     65                 { R.drawable.animation_path_morphing_rect, 0xffff0000, 0x0},
     66                 { R.drawable.animation_path_motion_rect, 0xffff0000, 0x0},
     67         });
     68     }
     69 
     70     public AnimatedVectorDrawableParameterizedTest(final int resId, int startExpected,
     71             int endExpected) throws Throwable {
     72         mResId = resId;
     73         mStartExpected = startExpected;
     74         mEndExpected = endExpected;
     75     }
     76 
     77     /**
     78      * Render AVD with path morphing, make sure the bitmap is different when it render at the start
     79      * and the end.
     80      *
     81      * @throws Exception for time out or I/O problem while dumping debug images.
     82      */
     83     @Test
     84     public void testPathMorphing() throws Exception {
     85         final Object lock = new Object();
     86         final Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_WIDTH,
     87                 Bitmap.Config.ARGB_8888);
     88         final Canvas c = new Canvas(bitmap);
     89 
     90         final AnimatedVectorDrawableCompat avd = AnimatedVectorDrawableCompat.create(mContext,
     91                 mResId);
     92         avd.setBounds(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
     93 
     94         bitmap.eraseColor(0);
     95         avd.draw(c);
     96         int centerColor = bitmap.getPixel(IMAGE_WIDTH / 2 , IMAGE_WIDTH / 2);
     97         assertTrue(centerColor == mStartExpected);
     98 
     99         if (DBG_DUMP_PNG) {
    100             saveVectorDrawableIntoPNG(mResources, bitmap, -1, "start");
    101         }
    102 
    103         avd.registerAnimationCallback(new AnimationCallback() {
    104             @Override
    105             public void onAnimationStart(Drawable drawable) {
    106                 // Nothing to do.
    107             }
    108 
    109             @Override
    110             public void onAnimationEnd(Drawable drawable) {
    111                 bitmap.eraseColor(0);
    112                 drawable.draw(c);
    113                 int centerColor = bitmap.getPixel(IMAGE_WIDTH / 2 , IMAGE_WIDTH / 2);
    114                 assertTrue(centerColor == mEndExpected);
    115 
    116                 synchronized (lock) {
    117                     lock.notify();
    118                 }
    119             }
    120         });
    121 
    122         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    123             @Override
    124             public void run() {
    125                 avd.start();
    126             }
    127         });
    128 
    129         synchronized (lock) {
    130             lock.wait(1000);
    131         }
    132 
    133         if (DBG_DUMP_PNG) {
    134             saveVectorDrawableIntoPNG(mResources, bitmap, -1, "ended");
    135         }
    136     }
    137 
    138     @Before
    139     public void setup() throws Exception {
    140         mContext = mActivityTestRule.getActivity();
    141         mResources = mContext.getResources();
    142     }
    143 }
    144