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.server.am;
18
19import static android.server.am.Components.BOTTOM_ACTIVITY;
20import static android.server.am.Components.BottomActivity.EXTRA_BOTTOM_WALLPAPER;
21import static android.server.am.Components.BottomActivity.EXTRA_STOP_DELAY;
22import static android.server.am.Components.TOP_ACTIVITY;
23import static android.server.am.Components.TRANSLUCENT_TOP_ACTIVITY;
24import static android.server.am.Components.TopActivity.EXTRA_FINISH_DELAY;
25import static android.server.am.Components.TopActivity.EXTRA_TOP_WALLPAPER;
26import static android.server.am.WindowManagerState.TRANSIT_ACTIVITY_CLOSE;
27import static android.server.am.WindowManagerState.TRANSIT_ACTIVITY_OPEN;
28import static android.server.am.WindowManagerState.TRANSIT_TASK_CLOSE;
29import static android.server.am.WindowManagerState.TRANSIT_TASK_OPEN;
30import static android.server.am.WindowManagerState.TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE;
31import static android.server.am.WindowManagerState.TRANSIT_WALLPAPER_CLOSE;
32import static android.server.am.WindowManagerState.TRANSIT_WALLPAPER_INTRA_CLOSE;
33import static android.server.am.WindowManagerState.TRANSIT_WALLPAPER_INTRA_OPEN;
34import static android.server.am.WindowManagerState.TRANSIT_WALLPAPER_OPEN;
35
36import static org.junit.Assert.assertEquals;
37
38import android.content.ComponentName;
39import android.os.SystemClock;
40import android.platform.test.annotations.Presubmit;
41import android.support.test.filters.FlakyTest;
42
43import org.junit.Assume;
44import org.junit.Before;
45import org.junit.Test;
46
47/**
48 * This test tests the transition type selection logic in ActivityManager/WindowManager.
49 * BottomActivity is started first, then TopActivity, and we check the transition type that the
50 * system selects when TopActivity enters or exits under various setups.
51 *
52 * Note that we only require the correct transition type to be reported (eg. TRANSIT_ACTIVITY_OPEN,
53 * TRANSIT_TASK_CLOSE, TRANSIT_WALLPAPER_OPEN, etc.). The exact animation is unspecified and can be
54 * overridden.
55 *
56 * <p>Build/Install/Run:
57 *     atest CtsActivityManagerDeviceTestCases:ActivityManagerTransitionSelectionTests
58 */
59@Presubmit
60@FlakyTest(bugId = 71792333)
61public class ActivityManagerTransitionSelectionTests extends ActivityManagerTestBase {
62
63    @Override
64    @Before
65    public void setUp() throws Exception {
66        super.setUp();
67
68        // Transition selection tests are currently disabled on Wear because
69        // config_windowSwipeToDismiss is set to true, which breaks all kinds of assumptions in the
70        // transition selection logic.
71        Assume.assumeTrue(!isWatch());
72    }
73
74    // Test activity open/close under normal timing
75    @Test
76    public void testOpenActivity_NeitherWallpaper() {
77        testOpenActivity(false /*bottomWallpaper*/, false /*topWallpaper*/,
78                false /*slowStop*/, TRANSIT_ACTIVITY_OPEN);
79    }
80
81    @Test
82    public void testCloseActivity_NeitherWallpaper() {
83        testCloseActivity(false /*bottomWallpaper*/, false /*topWallpaper*/,
84                false /*slowStop*/, TRANSIT_ACTIVITY_CLOSE);
85    }
86
87    @Test
88    public void testOpenActivity_BottomWallpaper() {
89        testOpenActivity(true /*bottomWallpaper*/, false /*topWallpaper*/,
90                false /*slowStop*/, TRANSIT_WALLPAPER_CLOSE);
91    }
92
93    @Test
94    public void testCloseActivity_BottomWallpaper() {
95        testCloseActivity(true /*bottomWallpaper*/, false /*topWallpaper*/,
96                false /*slowStop*/, TRANSIT_WALLPAPER_OPEN);
97    }
98
99    @Test
100    public void testOpenActivity_BothWallpaper() {
101        testOpenActivity(true /*bottomWallpaper*/, true /*topWallpaper*/,
102                false /*slowStop*/, TRANSIT_WALLPAPER_INTRA_OPEN);
103    }
104
105    @Test
106    public void testCloseActivity_BothWallpaper() {
107        testCloseActivity(true /*bottomWallpaper*/, true /*topWallpaper*/,
108                false /*slowStop*/, TRANSIT_WALLPAPER_INTRA_CLOSE);
109    }
110
111    //------------------------------------------------------------------------//
112
113    // Test task open/close under normal timing
114    @Test
115    public void testOpenTask_NeitherWallpaper() {
116        testOpenTask(false /*bottomWallpaper*/, false /*topWallpaper*/,
117                false /*slowStop*/, TRANSIT_TASK_OPEN);
118    }
119
120    @FlakyTest(bugId = 71792333)
121    @Test
122    public void testCloseTask_NeitherWallpaper() {
123        testCloseTask(false /*bottomWallpaper*/, false /*topWallpaper*/,
124                false /*slowStop*/, TRANSIT_TASK_CLOSE);
125    }
126
127    @Test
128    public void testOpenTask_BottomWallpaper() {
129        testOpenTask(true /*bottomWallpaper*/, false /*topWallpaper*/,
130                false /*slowStop*/, TRANSIT_WALLPAPER_CLOSE);
131    }
132
133    @Test
134    public void testCloseTask_BottomWallpaper() {
135        testCloseTask(true /*bottomWallpaper*/, false /*topWallpaper*/,
136                false /*slowStop*/, TRANSIT_WALLPAPER_OPEN);
137    }
138
139    @Test
140    public void testOpenTask_BothWallpaper() {
141        testOpenTask(true /*bottomWallpaper*/, true /*topWallpaper*/,
142                false /*slowStop*/, TRANSIT_WALLPAPER_INTRA_OPEN);
143    }
144
145    @Test
146    public void testCloseTask_BothWallpaper() {
147        testCloseTask(true /*bottomWallpaper*/, true /*topWallpaper*/,
148                false /*slowStop*/, TRANSIT_WALLPAPER_INTRA_CLOSE);
149    }
150
151    //------------------------------------------------------------------------//
152
153    // Test activity close -- bottom activity slow in stopping
154    // These simulate the case where the bottom activity is resumed
155    // before AM receives its activitiyStopped
156    @Test
157    public void testCloseActivity_NeitherWallpaper_SlowStop() {
158        testCloseActivity(false /*bottomWallpaper*/, false /*topWallpaper*/,
159                true /*slowStop*/, TRANSIT_ACTIVITY_CLOSE);
160    }
161
162    @Test
163    public void testCloseActivity_BottomWallpaper_SlowStop() {
164        testCloseActivity(true /*bottomWallpaper*/, false /*topWallpaper*/,
165                true /*slowStop*/, TRANSIT_WALLPAPER_OPEN);
166    }
167
168    @Test
169    public void testCloseActivity_BothWallpaper_SlowStop() {
170        testCloseActivity(true /*bottomWallpaper*/, true /*topWallpaper*/,
171                true /*slowStop*/, TRANSIT_WALLPAPER_INTRA_CLOSE);
172    }
173
174    //------------------------------------------------------------------------//
175
176    // Test task close -- bottom task top activity slow in stopping
177    // These simulate the case where the bottom activity is resumed
178    // before AM receives its activitiyStopped
179    @FlakyTest(bugId = 71792333)
180    @Test
181    public void testCloseTask_NeitherWallpaper_SlowStop() {
182        testCloseTask(false /*bottomWallpaper*/, false /*topWallpaper*/,
183                true /*slowStop*/, TRANSIT_TASK_CLOSE);
184    }
185
186    @Test
187    public void testCloseTask_BottomWallpaper_SlowStop() {
188        testCloseTask(true /*bottomWallpaper*/, false /*topWallpaper*/,
189                true /*slowStop*/, TRANSIT_WALLPAPER_OPEN);
190    }
191
192    @Test
193    public void testCloseTask_BothWallpaper_SlowStop() {
194        testCloseTask(true /*bottomWallpaper*/, true /*topWallpaper*/,
195                true /*slowStop*/, TRANSIT_WALLPAPER_INTRA_CLOSE);
196    }
197
198    //------------------------------------------------------------------------//
199
200    /// Test closing of translucent activity/task
201    @Test
202    public void testCloseActivity_NeitherWallpaper_Translucent() {
203        testCloseActivityTranslucent(false /*bottomWallpaper*/, false /*topWallpaper*/,
204                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
205    }
206
207    @Test
208    public void testCloseActivity_BottomWallpaper_Translucent() {
209        testCloseActivityTranslucent(true /*bottomWallpaper*/, false /*topWallpaper*/,
210                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
211    }
212
213    @Test
214    public void testCloseActivity_BothWallpaper_Translucent() {
215        testCloseActivityTranslucent(true /*bottomWallpaper*/, true /*topWallpaper*/,
216                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
217    }
218
219    @Test
220    public void testCloseTask_NeitherWallpaper_Translucent() {
221        testCloseTaskTranslucent(false /*bottomWallpaper*/, false /*topWallpaper*/,
222                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
223    }
224
225    @FlakyTest(bugId = 71792333)
226    @Test
227    public void testCloseTask_BottomWallpaper_Translucent() {
228        testCloseTaskTranslucent(true /*bottomWallpaper*/, false /*topWallpaper*/,
229                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
230    }
231
232    @Test
233    public void testCloseTask_BothWallpaper_Translucent() {
234        testCloseTaskTranslucent(true /*bottomWallpaper*/, true /*topWallpaper*/,
235                TRANSIT_TRANSLUCENT_ACTIVITY_CLOSE);
236    }
237
238    //------------------------------------------------------------------------//
239
240    private void testOpenActivity(boolean bottomWallpaper,
241            boolean topWallpaper, boolean slowStop, String expectedTransit) {
242        testTransitionSelection(true /*testOpen*/, false /*testNewTask*/,
243                bottomWallpaper, topWallpaper, false /*topTranslucent*/, slowStop, expectedTransit);
244    }
245
246    private void testCloseActivity(boolean bottomWallpaper,
247            boolean topWallpaper, boolean slowStop, String expectedTransit) {
248        testTransitionSelection(false /*testOpen*/, false /*testNewTask*/,
249                bottomWallpaper, topWallpaper, false /*topTranslucent*/, slowStop, expectedTransit);
250    }
251
252    private void testOpenTask(boolean bottomWallpaper,
253            boolean topWallpaper, boolean slowStop, String expectedTransit) {
254        testTransitionSelection(true /*testOpen*/, true /*testNewTask*/,
255                bottomWallpaper, topWallpaper, false /*topTranslucent*/, slowStop, expectedTransit);
256    }
257
258    private void testCloseTask(boolean bottomWallpaper,
259            boolean topWallpaper, boolean slowStop, String expectedTransit) {
260        testTransitionSelection(false /*testOpen*/, true /*testNewTask*/,
261                bottomWallpaper, topWallpaper, false /*topTranslucent*/, slowStop, expectedTransit);
262    }
263
264    private void testCloseActivityTranslucent(boolean bottomWallpaper,
265            boolean topWallpaper, String expectedTransit) {
266        testTransitionSelection(false /*testOpen*/, false /*testNewTask*/,
267                bottomWallpaper, topWallpaper, true /*topTranslucent*/,
268                false /*slowStop*/, expectedTransit);
269    }
270
271    private void testCloseTaskTranslucent(boolean bottomWallpaper,
272            boolean topWallpaper, String expectedTransit) {
273        testTransitionSelection(false /*testOpen*/, true /*testNewTask*/,
274                bottomWallpaper, topWallpaper, true /*topTranslucent*/,
275                false /*slowStop*/, expectedTransit);
276    }
277
278    //------------------------------------------------------------------------//
279
280    private void testTransitionSelection(
281            boolean testOpen, boolean testNewTask,
282            boolean bottomWallpaper, boolean topWallpaper, boolean topTranslucent,
283            boolean testSlowStop, String expectedTransit) {
284        String bottomStartCmd = getAmStartCmd(BOTTOM_ACTIVITY);
285        if (bottomWallpaper) {
286            bottomStartCmd += " --ez " + EXTRA_BOTTOM_WALLPAPER + " true";
287        }
288        if (testSlowStop) {
289            bottomStartCmd += " --ei " + EXTRA_STOP_DELAY + " 3000";
290        }
291        executeShellCommand(bottomStartCmd);
292
293        mAmWmState.computeState(BOTTOM_ACTIVITY);
294
295        final ComponentName topActivity = topTranslucent ? TRANSLUCENT_TOP_ACTIVITY : TOP_ACTIVITY;
296        String topStartCmd = getAmStartCmd(topActivity);
297        if (testNewTask) {
298            topStartCmd += " -f 0x18000000";
299        }
300        if (topWallpaper) {
301            topStartCmd += " --ez " + EXTRA_TOP_WALLPAPER + " true";
302        }
303        if (!testOpen) {
304            topStartCmd += " --ei " + EXTRA_FINISH_DELAY + " 1000";
305        }
306        executeShellCommand(topStartCmd);
307
308        SystemClock.sleep(5000);
309        if (testOpen) {
310            mAmWmState.computeState(topActivity);
311        } else {
312            mAmWmState.computeState(BOTTOM_ACTIVITY);
313        }
314
315        assertEquals("Picked wrong transition", expectedTransit,
316                mAmWmState.getWmState().getLastTransition());
317    }
318}
319