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