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 
17 package android.server.wm.animations;
18 
19 import static android.server.wm.StateLogger.log;
20 import static android.server.wm.animations.DialogFrameTestActivity.EXTRA_TEST_CASE;
21 
22 import static org.junit.Assert.assertTrue;
23 
24 import android.app.Activity;
25 import android.content.ComponentName;
26 import android.content.Intent;
27 import android.server.wm.ActivityManagerTestBase;
28 import android.server.wm.WindowManagerState.WindowState;
29 
30 import androidx.test.rule.ActivityTestRule;
31 
32 abstract class ParentChildTestBase<T extends Activity> extends ActivityManagerTestBase {
33 
34     interface ParentChildTest {
doTest(WindowState parent, WindowState child)35         void doTest(WindowState parent, WindowState child);
36     }
37 
startTestCase(String testCase)38     private void startTestCase(String testCase) throws Exception {
39         final Intent intent = new Intent()
40                 .putExtra(EXTRA_TEST_CASE, testCase);
41         activityRule().launchActivity(intent);
42     }
43 
startTestCaseDocked(String testCase)44     private void startTestCaseDocked(String testCase) throws Exception {
45         startTestCase(testCase);
46         mWmState.computeState(activityName());
47         putActivityInPrimarySplit(activityName());
48     }
49 
activityName()50     abstract ComponentName activityName();
activityRule()51     abstract ActivityTestRule<T> activityRule();
52 
doSingleTest(ParentChildTest t)53     abstract void doSingleTest(ParentChildTest t) throws Exception;
54 
doFullscreenTest(String testCase, ParentChildTest t)55     void doFullscreenTest(String testCase, ParentChildTest t) throws Exception {
56         log("Running test fullscreen");
57         startTestCase(testCase);
58         doSingleTest(t);
59         activityRule().finishActivity();
60     }
61 
doDockedTest(String testCase, ParentChildTest t)62     private void doDockedTest(String testCase, ParentChildTest t) throws Exception {
63         log("Running test docked");
64         if (!supportsSplitScreenMultiWindow()) {
65             log("Skipping test: no split multi-window support");
66             return;
67         }
68         startTestCaseDocked(testCase);
69         doSingleTest(t);
70         activityRule().finishActivity();
71 
72         mWmState.waitFor(wmState -> !wmState.containsActivity(activityName()),
73                 "activity must be removed");
74         assertTrue(mTaskOrganizer.getPrimarySplitTaskCount() == 0);
75     }
76 
doParentChildTest(String testCase, ParentChildTest t)77     void doParentChildTest(String testCase, ParentChildTest t) throws Exception {
78         doFullscreenTest(testCase, t);
79         doDockedTest(testCase, t);
80     }
81 }
82