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.cts;
18 
19 import java.lang.Exception;
20 import java.lang.String;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import junit.framework.Assert;
26 
27 import static com.android.ddmlib.Log.LogLevel.INFO;
28 
29 import com.android.tradefed.device.DeviceNotAvailableException;
30 import com.android.tradefed.log.LogUtil.CLog;
31 
32 /**
33  * Build: mmma -j32 cts/hostsidetests/services
34  * Run: cts/hostsidetests/services/activityandwindowmanager/util/run-test CtsServicesHostTestCases android.server.cts.ActivityManagerReplaceWindowTests
35  */
36 public class ActivityManagerReplaceWindowTests extends ActivityManagerTestBase {
37 
38     private static final String SLOW_CREATE_ACTIVITY_NAME = "SlowCreateActivity";
39     private static final String NO_RELAUNCH_ACTIVITY_NAME = "NoRelaunchActivity";
40 
41     private List<String> mTempWindowTokens = new ArrayList();
42 
testReplaceWindow_Dock_Relaunch()43     public void testReplaceWindow_Dock_Relaunch() throws Exception {
44         testReplaceWindow_Dock(true);
45     }
46 
testReplaceWindow_Dock_NoRelaunch()47     public void testReplaceWindow_Dock_NoRelaunch() throws Exception {
48         testReplaceWindow_Dock(false);
49     }
50 
testReplaceWindow_Dock(boolean relaunch)51     private void testReplaceWindow_Dock(boolean relaunch) throws Exception {
52         if (!supportsSplitScreenMultiWindow()) {
53             CLog.logAndDisplay(INFO, "Skipping test: no multi-window support");
54             return;
55         }
56 
57         final String activityName =
58                 relaunch ? SLOW_CREATE_ACTIVITY_NAME : NO_RELAUNCH_ACTIVITY_NAME;
59         final String windowName = getWindowName(activityName);
60         final String amStartCmd = getAmStartCmd(activityName);
61 
62         executeShellCommand(amStartCmd);
63 
64         // Sleep 2 seconds, then check if the window is started properly.
65         // SlowCreateActivity will do a sleep inside its onCreate() to simulate a
66         // slow-starting app. So instead of relying on WindowManagerState's
67         // retrying mechanism, we do an explicit sleep to avoid excess spews
68         // from WindowManagerState.
69         if (SLOW_CREATE_ACTIVITY_NAME.equals(activityName)) {
70             Thread.sleep(2000);
71         }
72 
73         CLog.logAndDisplay(INFO, "==========Before Docking========");
74         final String oldToken = getWindowToken(windowName, activityName);
75 
76         // Move to docked stack
77         final int taskId = getActivityTaskId(activityName);
78         final String cmd = AM_MOVE_TASK + taskId + " " + DOCKED_STACK_ID + " true";
79         executeShellCommand(cmd);
80 
81         // Sleep 5 seconds, then check if the window is replaced properly.
82         Thread.sleep(5000);
83 
84         CLog.logAndDisplay(INFO, "==========After Docking========");
85         final String newToken = getWindowToken(windowName, activityName);
86 
87         // For both relaunch and not relaunch case, we'd like the window to be kept.
88         Assert.assertEquals("Window replaced while docking.", oldToken, newToken);
89     }
90 
getWindowToken(String windowName, String activityName)91     private String getWindowToken(String windowName, String activityName)
92             throws Exception {
93         mAmWmState.computeState(mDevice, new String[] {activityName});
94 
95         mAmWmState.assertVisibility(activityName, true);
96 
97         mAmWmState.getWmState().getMatchingWindowTokens(windowName, mTempWindowTokens);
98 
99         Assert.assertEquals("Should have exactly one window for the activity.",
100                 1, mTempWindowTokens.size());
101 
102         return mTempWindowTokens.get(0);
103     }
104 }
105