Home | History | Annotate | Download | only in test
      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 com.android.setupwizardlib.test;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.content.Context;
     23 import android.support.annotation.NonNull;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.view.animation.Animation;
     28 import android.view.animation.AnimationSet;
     29 import android.view.animation.ScaleAnimation;
     30 
     31 import com.android.setupwizardlib.items.ReflectionInflater;
     32 
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 import java.util.List;
     37 
     38 @SmallTest
     39 @RunWith(AndroidJUnit4.class)
     40 public class ReflectionInflaterTest {
     41 
     42     @Test
     43     public void testInflateXml() {
     44         final Context context = InstrumentationRegistry.getContext();
     45         TestInflater inflater = new TestInflater(context);
     46         final Animation result = inflater.inflate(R.xml.reflection_inflater_test);
     47 
     48         assertTrue(result instanceof AnimationSet);
     49         final AnimationSet set = (AnimationSet) result;
     50         final List<Animation> animations = set.getAnimations();
     51         assertEquals(1, animations.size());
     52         assertTrue(animations.get(0) instanceof ScaleAnimation);
     53     }
     54 
     55     @Test
     56     public void testDefaultPackage() {
     57         final Context context = InstrumentationRegistry.getContext();
     58         TestInflater inflater = new TestInflater(context);
     59         inflater.setDefaultPackage("android.view.animation.");
     60         final Animation result =
     61                 inflater.inflate(R.xml.reflection_inflater_test_with_default_package);
     62 
     63         assertTrue(result instanceof AnimationSet);
     64         final AnimationSet set = (AnimationSet) result;
     65         final List<Animation> animations = set.getAnimations();
     66         assertEquals(1, animations.size());
     67         assertTrue(animations.get(0) instanceof ScaleAnimation);
     68     }
     69 
     70     private static class TestInflater extends ReflectionInflater<Animation> {
     71 
     72         protected TestInflater(@NonNull Context context) {
     73             super(context);
     74         }
     75 
     76         @Override
     77         protected void onAddChildItem(Animation parent, Animation child) {
     78             final AnimationSet group = (AnimationSet) parent;
     79             group.addAnimation(child);
     80         }
     81     }
     82 }
     83