summaryrefslogtreecommitdiff
path: root/android/support/transition/FadeTest.java
blob: 3b171e2cd14af369a3af7e33b5033cd1ff9e1237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.transition;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.filters.MediumTest;
import android.view.View;
import android.view.ViewGroup;

import org.junit.Before;
import org.junit.Test;

@MediumTest
public class FadeTest extends BaseTest {

    private View mView;
    private ViewGroup mRoot;

    @UiThreadTest
    @Before
    public void setUp() {
        mRoot = rule.getActivity().getRoot();
        mView = new View(rule.getActivity());
        mRoot.addView(mView, new ViewGroup.LayoutParams(100, 100));
    }

    @Test
    public void testMode() {
        assertThat(Fade.IN, is(Visibility.MODE_IN));
        assertThat(Fade.OUT, is(Visibility.MODE_OUT));
        final Fade fade = new Fade();
        assertThat(fade.getMode(), is(Visibility.MODE_IN | Visibility.MODE_OUT));
        fade.setMode(Visibility.MODE_IN);
        assertThat(fade.getMode(), is(Visibility.MODE_IN));
    }

    @Test
    @UiThreadTest
    public void testDisappear() {
        final Fade fade = new Fade();
        final TransitionValues startValues = new TransitionValues();
        startValues.view = mView;
        fade.captureStartValues(startValues);
        mView.setVisibility(View.INVISIBLE);
        final TransitionValues endValues = new TransitionValues();
        endValues.view = mView;
        fade.captureEndValues(endValues);
        Animator animator = fade.createAnimator(mRoot, startValues, endValues);
        assertThat(animator, is(notNullValue()));
    }

    @Test
    @UiThreadTest
    public void testAppear() {
        mView.setVisibility(View.INVISIBLE);
        final Fade fade = new Fade();
        final TransitionValues startValues = new TransitionValues();
        startValues.view = mView;
        fade.captureStartValues(startValues);
        mView.setVisibility(View.VISIBLE);
        final TransitionValues endValues = new TransitionValues();
        endValues.view = mView;
        fade.captureEndValues(endValues);
        Animator animator = fade.createAnimator(mRoot, startValues, endValues);
        assertThat(animator, is(notNullValue()));
    }

    @Test
    @UiThreadTest
    public void testNoChange() {
        final Fade fade = new Fade();
        final TransitionValues startValues = new TransitionValues();
        startValues.view = mView;
        fade.captureStartValues(startValues);
        final TransitionValues endValues = new TransitionValues();
        endValues.view = mView;
        fade.captureEndValues(endValues);
        Animator animator = fade.createAnimator(mRoot, startValues, endValues);
        // No visibility change; no animation should happen
        assertThat(animator, is(nullValue()));
    }

    @Test
    public void testFadeOutThenIn() throws Throwable {
        // Fade out
        final Runnable interrupt = mock(Runnable.class);
        float[] valuesOut = new float[2];
        final InterruptibleFade fadeOut = new InterruptibleFade(Fade.MODE_OUT, interrupt,
                valuesOut);
        final Transition.TransitionListener listenerOut = mock(Transition.TransitionListener.class);
        fadeOut.addListener(listenerOut);
        changeVisibility(fadeOut, mRoot, mView, View.INVISIBLE);
        verify(listenerOut, timeout(3000)).onTransitionStart(any(Transition.class));

        // The view is in the middle of fading out
        verify(interrupt, timeout(3000)).run();

        // Fade in
        float[] valuesIn = new float[2];
        final InterruptibleFade fadeIn = new InterruptibleFade(Fade.MODE_IN, null, valuesIn);
        final Transition.TransitionListener listenerIn = mock(Transition.TransitionListener.class);
        fadeIn.addListener(listenerIn);
        changeVisibility(fadeIn, mRoot, mView, View.VISIBLE);
        verify(listenerOut, timeout(3000)).onTransitionPause(any(Transition.class));
        verify(listenerIn, timeout(3000)).onTransitionStart(any(Transition.class));
        assertThat(valuesOut[1], allOf(greaterThan(0f), lessThan(1f)));
        if (Build.VERSION.SDK_INT >= 19) {
            // These won't match on API levels 18 and below due to lack of Animator pause.
            assertEquals(valuesOut[1], valuesIn[0], 0.01f);
        }

        verify(listenerIn, timeout(3000)).onTransitionEnd(any(Transition.class));
        assertThat(mView.getVisibility(), is(View.VISIBLE));
        assertEquals(valuesIn[1], 1.f, 0.01f);
    }

    @Test
    public void testFadeInThenOut() throws Throwable {
        changeVisibility(null, mRoot, mView, View.INVISIBLE);
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();

        // Fade in
        final Runnable interrupt = mock(Runnable.class);
        float[] valuesIn = new float[2];
        final InterruptibleFade fadeIn = new InterruptibleFade(Fade.MODE_IN, interrupt, valuesIn);
        final Transition.TransitionListener listenerIn = mock(Transition.TransitionListener.class);
        fadeIn.addListener(listenerIn);
        changeVisibility(fadeIn, mRoot, mView, View.VISIBLE);
        verify(listenerIn, timeout(3000)).onTransitionStart(any(Transition.class));

        // The view is in the middle of fading in
        verify(interrupt, timeout(3000)).run();

        // Fade out
        float[] valuesOut = new float[2];
        final InterruptibleFade fadeOut = new InterruptibleFade(Fade.MODE_OUT, null, valuesOut);
        final Transition.TransitionListener listenerOut = mock(Transition.TransitionListener.class);
        fadeOut.addListener(listenerOut);
        changeVisibility(fadeOut, mRoot, mView, View.INVISIBLE);
        verify(listenerIn, timeout(3000)).onTransitionPause(any(Transition.class));
        verify(listenerOut, timeout(3000)).onTransitionStart(any(Transition.class));
        assertThat(valuesIn[1], allOf(greaterThan(0f), lessThan(1f)));
        if (Build.VERSION.SDK_INT >= 19) {
            // These won't match on API levels 18 and below due to lack of Animator pause.
            assertEquals(valuesIn[1], valuesOut[0], 0.01f);
        }

        verify(listenerOut, timeout(3000)).onTransitionEnd(any(Transition.class));
        assertThat(mView.getVisibility(), is(View.INVISIBLE));
    }

    @Test
    public void testFadeWithAlpha() throws Throwable {
        // Set the view alpha to 0.5
        rule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mView.setAlpha(0.5f);
            }
        });
        // Fade out
        final Fade fadeOut = new Fade(Fade.OUT);
        final Transition.TransitionListener listenerOut = mock(Transition.TransitionListener.class);
        fadeOut.addListener(listenerOut);
        changeVisibility(fadeOut, mRoot, mView, View.INVISIBLE);
        verify(listenerOut, timeout(3000)).onTransitionStart(any(Transition.class));
        verify(listenerOut, timeout(3000)).onTransitionEnd(any(Transition.class));
        // Fade in
        final Fade fadeIn = new Fade(Fade.IN);
        final Transition.TransitionListener listenerIn = mock(Transition.TransitionListener.class);
        fadeIn.addListener(listenerIn);
        changeVisibility(fadeIn, mRoot, mView, View.VISIBLE);
        verify(listenerIn, timeout(3000)).onTransitionStart(any(Transition.class));
        verify(listenerIn, timeout(3000)).onTransitionEnd(any(Transition.class));
        // Confirm that the view still has the original alpha value
        assertThat(mView.getVisibility(), is(View.VISIBLE));
        assertEquals(0.5f, mView.getAlpha(), 0.01f);
    }

    private void changeVisibility(final Fade fade, final ViewGroup container, final View target,
            final int visibility) throws Throwable {
        rule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (fade != null) {
                    TransitionManager.beginDelayedTransition(container, fade);
                }
                target.setVisibility(visibility);
            }
        });
    }

    /**
     * A special version of {@link Fade} that runs a specified {@link Runnable} soon after the
     * target starts fading in or out.
     */
    private static class InterruptibleFade extends Fade {

        static final float ALPHA_THRESHOLD = 0.2f;

        float mInitialAlpha = -1;
        Runnable mMiddle;
        final float[] mAlphaValues;

        InterruptibleFade(int mode, Runnable middle, float[] alphaValues) {
            super(mode);
            mMiddle = middle;
            mAlphaValues = alphaValues;
        }

        @Nullable
        @Override
        public Animator createAnimator(@NonNull ViewGroup sceneRoot,
                @Nullable final TransitionValues startValues,
                @Nullable final TransitionValues endValues) {
            final Animator animator = super.createAnimator(sceneRoot, startValues, endValues);
            if (animator instanceof ObjectAnimator) {
                ((ObjectAnimator) animator).addUpdateListener(
                        new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
                                final float alpha = (float) animation.getAnimatedValue();
                                mAlphaValues[1] = alpha;
                                if (mInitialAlpha < 0) {
                                    mInitialAlpha = alpha;
                                    mAlphaValues[0] = mInitialAlpha;
                                } else if (Math.abs(alpha - mInitialAlpha) > ALPHA_THRESHOLD) {
                                    if (mMiddle != null) {
                                        mMiddle.run();
                                        mMiddle = null;
                                    }
                                }
                            }
                        });
            }
            return animator;
        }

    }

}