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