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
17package androidx.transition;
18
19import static org.hamcrest.CoreMatchers.is;
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.sameInstance;
22
23import android.support.test.annotation.UiThreadTest;
24import android.support.test.filters.MediumTest;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.FrameLayout;
28
29import androidx.transition.test.R;
30
31import org.junit.Test;
32
33@MediumTest
34public class SceneTest extends BaseTest {
35
36    @Test
37    public void testGetSceneRoot() {
38        TransitionActivity activity = rule.getActivity();
39        ViewGroup root = activity.getRoot();
40        Scene scene = new Scene(root);
41        assertThat(scene.getSceneRoot(), is(sameInstance(root)));
42    }
43
44    @Test
45    @UiThreadTest
46    public void testSceneWithViewGroup() {
47        TransitionActivity activity = rule.getActivity();
48        ViewGroup root = activity.getRoot();
49        FrameLayout layout = new FrameLayout(activity);
50        Scene scene = new Scene(root, layout);
51        CheckCalledRunnable enterAction = new CheckCalledRunnable();
52        CheckCalledRunnable exitAction = new CheckCalledRunnable();
53        scene.setEnterAction(enterAction);
54        scene.setExitAction(exitAction);
55        scene.enter();
56        assertThat(enterAction.wasCalled(), is(true));
57        assertThat(exitAction.wasCalled(), is(false));
58        assertThat(root.getChildCount(), is(1));
59        assertThat(root.getChildAt(0), is((View) layout));
60        scene.exit();
61        assertThat(exitAction.wasCalled(), is(true));
62    }
63
64    @Test
65    @UiThreadTest
66    public void testSceneWithView() {
67        TransitionActivity activity = rule.getActivity();
68        ViewGroup root = activity.getRoot();
69        View view = new View(activity);
70        Scene scene = new Scene(root, view);
71        CheckCalledRunnable enterAction = new CheckCalledRunnable();
72        CheckCalledRunnable exitAction = new CheckCalledRunnable();
73        scene.setEnterAction(enterAction);
74        scene.setExitAction(exitAction);
75        scene.enter();
76        assertThat(enterAction.wasCalled(), is(true));
77        assertThat(exitAction.wasCalled(), is(false));
78        assertThat(root.getChildCount(), is(1));
79        assertThat(root.getChildAt(0), is(view));
80        scene.exit();
81        assertThat(exitAction.wasCalled(), is(true));
82    }
83
84    @Test
85    public void testEnterAction() {
86        TransitionActivity activity = rule.getActivity();
87        ViewGroup root = activity.getRoot();
88        Scene scene = new Scene(root);
89        CheckCalledRunnable runnable = new CheckCalledRunnable();
90        scene.setEnterAction(runnable);
91        scene.enter();
92        assertThat(runnable.wasCalled(), is(true));
93    }
94
95    @Test
96    public void testExitAction() {
97        TransitionActivity activity = rule.getActivity();
98        ViewGroup root = activity.getRoot();
99        Scene scene = new Scene(root);
100        scene.enter();
101        CheckCalledRunnable runnable = new CheckCalledRunnable();
102        scene.setExitAction(runnable);
103        scene.exit();
104        assertThat(runnable.wasCalled(), is(true));
105    }
106
107    @Test
108    public void testExitAction_withoutEnter() {
109        TransitionActivity activity = rule.getActivity();
110        ViewGroup root = activity.getRoot();
111        Scene scene = new Scene(root);
112        CheckCalledRunnable runnable = new CheckCalledRunnable();
113        scene.setExitAction(runnable);
114        scene.exit();
115        assertThat(runnable.wasCalled(), is(false));
116    }
117
118    @Test
119    public void testGetSceneForLayout_cache() {
120        TransitionActivity activity = rule.getActivity();
121        ViewGroup root = activity.getRoot();
122        Scene scene = Scene.getSceneForLayout(root, R.layout.support_scene0, activity);
123        assertThat("getSceneForLayout should return the same instance for subsequent calls",
124                Scene.getSceneForLayout(root, R.layout.support_scene0, activity),
125                is(sameInstance(scene)));
126    }
127
128}
129