VisibilityTest.java revision a6f2ebe33d03c42114b0082720cf9c42f7dad5a3
148087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos/*
248087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * Copyright (C) 2016 The Android Open Source Project
348087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos *
448087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * Licensed under the Apache License, Version 2.0 (the "License");
548087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * you may not use this file except in compliance with the License.
648087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * You may obtain a copy of the License at
748087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos *
848087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos *      http://www.apache.org/licenses/LICENSE-2.0
948087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos *
1048087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * Unless required by applicable law or agreed to in writing, software
1148087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * distributed under the License is distributed on an "AS IS" BASIS,
1248087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * See the License for the specific language governing permissions and
1448087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos * limitations under the License.
1548087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos */
1648087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos
1748087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulospackage android.support.transition;
1848087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos
1948087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport static org.hamcrest.core.Is.is;
2084322c9402f810da3cd80b52e9f9ef72150a9004Alexander Galazinimport static org.hamcrest.core.IsEqual.equalTo;
2148087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport static org.junit.Assert.assertNotNull;
2248087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport static org.junit.Assert.assertThat;
2384322c9402f810da3cd80b52e9f9ef72150a9004Alexander Galazin
2448087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.animation.Animator;
2548087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.animation.ObjectAnimator;
2648087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.support.annotation.NonNull;
2748087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.support.annotation.Nullable;
2848087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.support.test.annotation.UiThreadTest;
2948087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.support.test.filters.MediumTest;
3048087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.view.View;
3148087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport android.view.ViewGroup;
3248087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos
3348087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport org.junit.Before;
3448087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport org.junit.Test;
3548087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos
3648087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulosimport java.util.Arrays;
3748087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos
3848087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulos@MediumTest
3948087f5f0eb08759ee763f98daf3b34becb74559Pyry Haulospublic class VisibilityTest extends BaseTest {
40
41    private View mView;
42    private ViewGroup mRoot;
43
44    @UiThreadTest
45    @Before
46    public void setUp() {
47        mRoot = rule.getActivity().getRoot();
48        mView = new View(rule.getActivity());
49        mRoot.addView(mView, new ViewGroup.LayoutParams(100, 100));
50    }
51
52    @Test
53    public void testMode() {
54        final CustomVisibility visibility = new CustomVisibility();
55        assertThat(visibility.getMode(), is(Visibility.MODE_IN | Visibility.MODE_OUT));
56        visibility.setMode(Visibility.MODE_IN);
57        assertThat(visibility.getMode(), is(Visibility.MODE_IN));
58    }
59
60    @Test
61    @UiThreadTest
62    public void testCustomVisibility() {
63        final CustomVisibility visibility = new CustomVisibility();
64        assertThat(visibility.getName(), is(equalTo(CustomVisibility.class.getName())));
65        assertNotNull(visibility.getTransitionProperties());
66
67        // Capture start values
68        mView.setScaleX(0.5f);
69        final TransitionValues startValues = new TransitionValues();
70        startValues.view = mView;
71        visibility.captureStartValues(startValues);
72        assertThat((float) startValues.values.get(CustomVisibility.PROPNAME_SCALE_X), is(0.5f));
73
74        // Hide the view and capture end values
75        mView.setVisibility(View.GONE);
76        final TransitionValues endValues = new TransitionValues();
77        endValues.view = mView;
78        visibility.captureEndValues(endValues);
79
80        // This should invoke onDisappear, not onAppear
81        ObjectAnimator animator = (ObjectAnimator) visibility
82                .createAnimator(mRoot, startValues, endValues);
83        assertNotNull(animator);
84        assertThat(animator.getPropertyName(), is(equalTo("scaleX")));
85
86        // Jump to the end of the animation
87        animator.end();
88
89        // This value confirms that onDisappear, not onAppear, was called
90        assertThat((float) animator.getAnimatedValue(), is(0.25f));
91    }
92
93    @Test
94    @UiThreadTest
95    public void testCustomVisibility2() {
96        final CustomVisibility2 visibility = new CustomVisibility2();
97        final TransitionValues startValues = new TransitionValues();
98        startValues.view = mView;
99        visibility.captureStartValues(startValues);
100        mView.setVisibility(View.GONE);
101        final TransitionValues endValues = new TransitionValues();
102        endValues.view = mView;
103        visibility.captureEndValues(endValues);
104        ObjectAnimator animator = (ObjectAnimator) visibility
105                .createAnimator(mRoot, startValues, endValues);
106        assertNotNull(animator);
107
108        // Jump to the end of the animation
109        animator.end();
110
111        // This value confirms that onDisappear, not onAppear, was called
112        assertThat((float) animator.getAnimatedValue(), is(0.25f));
113    }
114
115    /**
116     * A custom {@link Visibility} with 5-arg onAppear/Disappear
117     */
118    public static class CustomVisibility extends Visibility {
119
120        static final String PROPNAME_SCALE_X = "customVisibility:scaleX";
121
122        private static String[] sTransitionProperties;
123
124        @Nullable
125        @Override
126        public String[] getTransitionProperties() {
127            if (sTransitionProperties == null) {
128                String[] properties = super.getTransitionProperties();
129                if (properties != null) {
130                    sTransitionProperties = Arrays.copyOf(properties, properties.length + 1);
131                } else {
132                    sTransitionProperties = new String[1];
133                }
134                sTransitionProperties[sTransitionProperties.length - 1] = PROPNAME_SCALE_X;
135            }
136            return sTransitionProperties;
137        }
138
139        @Override
140        public void captureStartValues(@NonNull TransitionValues transitionValues) {
141            super.captureStartValues(transitionValues);
142            transitionValues.values.put(PROPNAME_SCALE_X, transitionValues.view.getScaleX());
143        }
144
145        @Override
146        public Animator onAppear(ViewGroup sceneRoot, TransitionValues startValues,
147                int startVisibility, TransitionValues endValues, int endVisibility) {
148            if (startValues == null) {
149                return null;
150            }
151            float startScaleX = (float) startValues.values.get(PROPNAME_SCALE_X);
152            return ObjectAnimator.ofFloat(startValues.view, "scaleX", startScaleX, 0.75f);
153        }
154
155        @Override
156        public Animator onDisappear(ViewGroup sceneRoot, TransitionValues startValues,
157                int startVisibility, TransitionValues endValues, int endVisibility) {
158            if (startValues == null) {
159                return null;
160            }
161            float startScaleX = (float) startValues.values.get(PROPNAME_SCALE_X);
162            return ObjectAnimator.ofFloat(startValues.view, "scaleX", startScaleX, 0.25f);
163        }
164
165    }
166
167    /**
168     * A custom {@link Visibility} with 4-arg onAppear/Disappear
169     */
170    public static class CustomVisibility2 extends Visibility {
171
172        static final String PROPNAME_SCALE_X = "customVisibility:scaleX";
173
174        @Override
175        public void captureStartValues(@NonNull TransitionValues transitionValues) {
176            super.captureStartValues(transitionValues);
177            transitionValues.values.put(PROPNAME_SCALE_X, transitionValues.view.getScaleX());
178        }
179
180        @Override
181        public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues,
182                TransitionValues endValues) {
183            float startScaleX = startValues == null ? 0.25f :
184                    (float) startValues.values.get(PROPNAME_SCALE_X);
185            return ObjectAnimator.ofFloat(view, "scaleX", startScaleX, 0.75f);
186        }
187
188        @Override
189        public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues,
190                TransitionValues endValues) {
191            if (startValues == null) {
192                return null;
193            }
194            float startScaleX = (float) startValues.values.get(PROPNAME_SCALE_X);
195            return ObjectAnimator.ofFloat(view, "scaleX", startScaleX, 0.25f);
196        }
197
198    }
199
200}
201