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.am;
18 
19 import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
20 import static android.server.am.ComponentNameUtils.getWindowName;
21 import static android.server.am.Components.NO_RELAUNCH_ACTIVITY;
22 import static android.server.am.Components.SLOW_CREATE_ACTIVITY;
23 import static android.server.am.StateLogger.log;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assume.assumeTrue;
27 
28 import android.content.ComponentName;
29 import android.os.SystemClock;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 
34 import java.util.List;
35 import java.util.concurrent.TimeUnit;
36 
37 /**
38  * Build/Install/Run:
39  *     atest CtsActivityManagerDeviceTestCases:ActivityManagerReplaceWindowTests
40  */
41 public class ActivityManagerReplaceWindowTests extends ActivityManagerTestBase {
42 
43     @Before
44     @Override
setUp()45     public void setUp() throws Exception {
46         super.setUp();
47 
48         assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());
49     }
50 
51     @Test
testReplaceWindow_Dock_Relaunch()52     public void testReplaceWindow_Dock_Relaunch() throws Exception {
53         testReplaceWindow_Dock(true);
54     }
55 
56     @Test
testReplaceWindow_Dock_NoRelaunch()57     public void testReplaceWindow_Dock_NoRelaunch() throws Exception {
58         testReplaceWindow_Dock(false);
59     }
60 
testReplaceWindow_Dock(boolean relaunch)61     private void testReplaceWindow_Dock(boolean relaunch) throws Exception {
62         final ComponentName activityName = relaunch ? SLOW_CREATE_ACTIVITY : NO_RELAUNCH_ACTIVITY;
63         final String windowName = getWindowName(activityName);
64         final String amStartCmd = getAmStartCmd(activityName);
65 
66         executeShellCommand(amStartCmd);
67 
68         // Sleep 2 seconds, then check if the window is started properly. SlowCreateActivity
69         // will do a sleep inside its onCreate() to simulate a slow-starting app. So instead of
70         // relying on WindowManagerState's retrying mechanism, we do an explicit sleep to avoid
71         // excess spews from WindowManagerState.
72         if (relaunch) {
73             SystemClock.sleep(TimeUnit.SECONDS.toMillis(2));
74         }
75 
76         log("==========Before Docking========");
77         final String oldToken = getWindowToken(windowName, activityName);
78 
79         // Move to docked stack
80         setActivityTaskWindowingMode(activityName, WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
81 
82         // Sleep 5 seconds, then check if the window is replaced properly.
83         SystemClock.sleep(TimeUnit.SECONDS.toMillis(5));
84 
85         log("==========After Docking========");
86         final String newToken = getWindowToken(windowName, activityName);
87 
88         // For both relaunch and not relaunch case, we'd like the window to be kept.
89         assertEquals("Window replaced while docking.", oldToken, newToken);
90     }
91 
getWindowToken(String windowName, ComponentName activityName)92     private String getWindowToken(String windowName, ComponentName activityName) throws Exception {
93         mAmWmState.computeState(activityName);
94 
95         mAmWmState.assertVisibility(activityName, true);
96 
97         final List<String> windowTokens =
98                 mAmWmState.getWmState().getMatchingWindowTokens(windowName);
99 
100         assertEquals("Should have exactly one window for the activity.",
101                 1, windowTokens.size());
102 
103         return windowTokens.get(0);
104     }
105 }
106