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
17package android.support.transition;
18
19import static org.mockito.Matchers.any;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.timeout;
22import static org.mockito.Mockito.verify;
23
24import android.animation.Animator;
25import android.animation.ObjectAnimator;
26import android.support.test.InstrumentationRegistry;
27import android.support.transition.test.R;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.LinearLayout;
31
32import org.junit.Before;
33
34import java.util.ArrayList;
35
36public abstract class BaseTransitionTest extends BaseTest {
37
38    ArrayList<View> mTransitionTargets = new ArrayList<>();
39    LinearLayout mRoot;
40    Transition mTransition;
41    Transition.TransitionListener mListener;
42    float mAnimatedValue;
43
44    @Before
45    public void setUp() {
46        InstrumentationRegistry.getInstrumentation().setInTouchMode(false);
47        mRoot = (LinearLayout) rule.getActivity().findViewById(R.id.root);
48        mTransitionTargets.clear();
49        mTransition = createTransition();
50        mListener = mock(Transition.TransitionListener.class);
51        mTransition.addListener(mListener);
52    }
53
54    Transition createTransition() {
55        return new TestTransition();
56    }
57
58    void waitForStart() {
59        verify(mListener, timeout(3000)).onTransitionStart(any(Transition.class));
60    }
61
62    void waitForEnd() {
63        verify(mListener, timeout(3000)).onTransitionEnd(any(Transition.class));
64    }
65
66    Scene loadScene(final int layoutId) throws Throwable {
67        final Scene[] scene = new Scene[1];
68        rule.runOnUiThread(new Runnable() {
69            @Override
70            public void run() {
71                scene[0] = Scene.getSceneForLayout(mRoot, layoutId, rule.getActivity());
72            }
73        });
74        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
75        return scene[0];
76    }
77
78    void startTransition(final int layoutId) throws Throwable {
79        startTransition(loadScene(layoutId));
80    }
81
82    private void startTransition(final Scene scene) throws Throwable {
83        rule.runOnUiThread(new Runnable() {
84            @Override
85            public void run() {
86                TransitionManager.go(scene, mTransition);
87            }
88        });
89        waitForStart();
90    }
91
92    void enterScene(final int layoutId) throws Throwable {
93        enterScene(loadScene(layoutId));
94    }
95
96    void enterScene(final Scene scene) throws Throwable {
97        rule.runOnUiThread(new Runnable() {
98            @Override
99            public void run() {
100                scene.enter();
101            }
102        });
103        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
104    }
105
106    void resetListener() {
107        mTransition.removeListener(mListener);
108        mListener = mock(Transition.TransitionListener.class);
109        mTransition.addListener(mListener);
110    }
111
112    void setAnimatedValue(float animatedValue) {
113        mAnimatedValue = animatedValue;
114    }
115
116    public class TestTransition extends Visibility {
117
118        @Override
119        public Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues,
120                TransitionValues endValues) {
121            mTransitionTargets.add(endValues.view);
122            return ObjectAnimator.ofFloat(BaseTransitionTest.this, "animatedValue", 0, 1);
123        }
124
125        @Override
126        public Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues,
127                TransitionValues endValues) {
128            mTransitionTargets.add(startValues.view);
129            return ObjectAnimator.ofFloat(BaseTransitionTest.this, "animatedValue", 1, 0);
130        }
131
132    }
133
134}
135