1 /*
2  * Copyright (C) 2023 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 package android.car.ui;
17 
18 import android.car.cts.CarHostJUnit4TestCase;
19 
20 import com.android.compatibility.common.util.CommonTestUtils;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.util.HashMap;
30 
31 /** Check portrait UI resumes after reboot. */
32 @RunWith(DeviceJUnit4ClassRunner.class)
33 public final class RebootHostTest extends CarHostJUnit4TestCase {
34     private static final int SYSTEM_RESTART_TIMEOUT_SEC = 100;
35     private static final String APP_GRID =
36             "com.android.car.portraitlauncher/com.android.car.carlauncher.AppGridActivity";
37     private static final String BACKGROUND_BASE =
38             "com.android.car.portraitlauncher/com.android.car.portraitlauncher.homeactivities"
39                     + ".BackgroundPanelBaseActivity";
40 
41     @Test
reboot_requiredTaskShows()42     public void reboot_requiredTaskShows() throws Exception {
43         restartOrReboot();
44         CommonTestUtils.waitUntil("timed out waiting for carlauncher", SYSTEM_RESTART_TIMEOUT_SEC,
45                 this::isCarPortraitLauncherReady);
46     }
47 
isCarPortraitLauncherReady()48     private boolean isCarPortraitLauncherReady() {
49         String cmd = "am stack list";
50         String stackList = null;
51         try {
52             stackList = getDevice().executeShellCommand(cmd);
53         } catch (DeviceNotAvailableException e) {
54             CLog.i("%s failed: %s", cmd, e.getMessage());
55             return false;
56         }
57 
58         CLog.i("Stack list: %s" + stackList);
59         return isCarPortraitLauncherActivitiesLaunched(stackList);
60     }
61 
isCarPortraitLauncherActivitiesLaunched(String stackList)62     private boolean isCarPortraitLauncherActivitiesLaunched(String stackList) {
63         HashMap<String, TaskInfo> activeComponents = TaskInfo.getActiveComponents(stackList);
64         return isTaskVisible(BACKGROUND_BASE, activeComponents) && isTaskVisible(APP_GRID,
65                 activeComponents);
66     }
67 
isTaskVisible(String componentString, HashMap<String, TaskInfo> activeComponents)68     private boolean isTaskVisible(String componentString,
69             HashMap<String, TaskInfo> activeComponents) {
70         return activeComponents.containsKey(componentString) && activeComponents.get(
71                 componentString).isVisible();
72     }
73 
restartOrReboot()74     private void restartOrReboot() throws DeviceNotAvailableException {
75         ITestDevice device = getDevice();
76 
77         if (device.isAdbRoot()) {
78             CLog.d("Restarting system server");
79             device.executeShellCommand("stop");
80             device.executeShellCommand("start");
81             return;
82         }
83 
84         CLog.d("Only root user can restart system server; rebooting instead");
85         getDevice().reboot();
86     }
87 }
88