Home | History | Annotate | Download | only in transition
      1 /*
      2  * Copyright (C) 2016 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.transition;
     18 
     19 import android.animation.AnimatorSetActivity;
     20 import android.app.Activity;
     21 import android.graphics.Rect;
     22 import android.test.ActivityInstrumentationTestCase2;
     23 import android.transition.Transition.EpicenterCallback;
     24 import android.util.ArrayMap;
     25 import android.view.View;
     26 import android.view.animation.AccelerateInterpolator;
     27 import android.widget.TextView;
     28 
     29 import com.android.frameworks.coretests.R;
     30 
     31 import java.lang.reflect.Field;
     32 
     33 public class TransitionTest extends ActivityInstrumentationTestCase2<AnimatorSetActivity> {
     34     Activity mActivity;
     35     public TransitionTest() {
     36         super(AnimatorSetActivity.class);
     37     }
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         mActivity = getActivity();
     42     }
     43 
     44     public void testClone() throws Throwable {
     45         View square1 = mActivity.findViewById(R.id.square1);
     46         View square2 = mActivity.findViewById(R.id.square2);
     47         View square3 = mActivity.findViewById(R.id.square3);
     48         Fade fade = new Fade();
     49         fade.setStartDelay(1000);
     50         fade.setDuration(1001);
     51 
     52         fade.addTarget(square1);
     53         fade.excludeTarget(square2, true);
     54         fade.excludeChildren(square3, true);
     55 
     56         fade.addTarget(R.id.square4);
     57         fade.excludeTarget(R.id.square3, true);
     58         fade.excludeChildren(R.id.square2, true);
     59 
     60         fade.addTarget("hello");
     61         fade.excludeTarget("world", true);
     62 
     63         fade.addTarget(View.class);
     64         fade.excludeTarget(TextView.class, true);
     65 
     66         fade.setMatchOrder(Transition.MATCH_ID);
     67         fade.setPropagation(new CircularPropagation());
     68         fade.setPathMotion(new ArcMotion());
     69         fade.setInterpolator(new AccelerateInterpolator());
     70         fade.setNameOverrides(new ArrayMap<>());
     71 
     72         EpicenterCallback epicenterCallback = new EpicenterCallback() {
     73             @Override
     74             public Rect onGetEpicenter(Transition transition) {
     75                 return null;
     76             }
     77         };
     78 
     79         fade.setEpicenterCallback(epicenterCallback);
     80 
     81         Fade clone = (Fade) fade.clone();
     82         assertFieldEquals(fade, clone, "mStartDelay");
     83         assertFieldEquals(fade, clone, "mDuration");
     84         assertFieldEquals(fade, clone, "mInterpolator");
     85         assertFieldEquals(fade, clone, "mPropagation");
     86         assertEquals(fade.getPathMotion(), clone.getPathMotion());
     87         assertEquals(fade.getEpicenterCallback(), clone.getEpicenterCallback());
     88         assertFieldEquals(fade, clone, "mNameOverrides");
     89         assertFieldEquals(fade, clone, "mMatchOrder");
     90 
     91         assertFieldEquals(fade, clone, "mTargets");
     92         assertFieldEquals(fade, clone, "mTargetExcludes");
     93         assertFieldEquals(fade, clone, "mTargetChildExcludes");
     94 
     95         assertFieldEquals(fade, clone, "mTargetIds");
     96         assertFieldEquals(fade, clone, "mTargetIdExcludes");
     97         assertFieldEquals(fade, clone, "mTargetIdChildExcludes");
     98 
     99         assertFieldEquals(fade, clone, "mTargetNames");
    100         assertFieldEquals(fade, clone, "mTargetNameExcludes");
    101 
    102         assertFieldEquals(fade, clone, "mTargetTypes");
    103         assertFieldEquals(fade, clone, "mTargetTypeExcludes");
    104     }
    105 
    106     private static void assertFieldEquals(Fade fade1, Fade fade2, String fieldName)
    107             throws NoSuchFieldException, IllegalAccessException {
    108         Field field = findField(Fade.class, fieldName);
    109         field.setAccessible(true);
    110         assertEquals("Field '" + fieldName + "' value mismatch", field.get(fade1),
    111                 field.get(fade2));
    112     }
    113 
    114     private static Field findField(Class<?> type, String fieldName) throws NoSuchFieldException {
    115         while (type != null) {
    116             try {
    117                 return type.getDeclaredField(fieldName);
    118             } catch (NoSuchFieldException e) {
    119                 // try the parent
    120                 type = type.getSuperclass();
    121             }
    122         }
    123         throw new NoSuchFieldException(fieldName);
    124     }
    125 }
    126