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