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