1 /* 2 * Copyright (C) 2019 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.host.multiuser; 17 18 import com.android.tradefed.util.RunUtil; 19 import static com.google.common.truth.Truth.assertWithMessage; 20 21 import android.host.multiuser.BaseMultiUserTest.SupportsMultiUserRule; 22 23 import com.android.compatibility.common.util.CddTest; 24 import com.android.tradefed.log.LogUtil.CLog; 25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 26 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * Run: atest SecondaryUsersTest 35 */ 36 @RunWith(DeviceJUnit4ClassRunner.class) 37 public final class SecondaryUsersTest extends BaseMultiUserTest { 38 39 // Extra time to give the system to switch into secondary user after boot complete. 40 private static final long SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS = 100_000; 41 42 private static final long POLL_INTERVAL_MS = 1_000; 43 private static final long WAIT_FOR_DEVICE_READY_INTERVAL_MS = 10_000; 44 private static final long WAIT_FOR_BOOT_COMPLETE_INTERVAL_MINUTES = 2; 45 46 @Rule 47 public final SupportsMultiUserRule mSupportsMultiUserRule = new SupportsMultiUserRule(this); 48 49 @CddTest(requirement="9.5/A-1-2") 50 @Test testSwitchToSecondaryUserBeforeBootComplete()51 public void testSwitchToSecondaryUserBeforeBootComplete() throws Exception { 52 assumeIsAutomotive(); 53 54 CLog.d("Rebooting"); 55 getDevice().nonBlockingReboot(); 56 CLog.d("Waiting " + WAIT_FOR_BOOT_COMPLETE_INTERVAL_MINUTES + " minutes for boot complete"); 57 getDevice().waitForBootComplete(TimeUnit.MINUTES.toMillis(2)); 58 CLog.d("Boot completed; waiting until current user is a secondary user"); 59 60 int currentUser = -10000; // UserHandle.USER_NULL; 61 boolean isUserSecondary = false; 62 long ti = System.currentTimeMillis(); 63 64 // TODO(b/208518721): Verify if current user is secondary when the UI is ready for user 65 // interaction. A possibility is to check if the CarLauncher is started in the 66 // Activity Stack, but this becomes tricky in OEM implementation, where CarLauncher is 67 // replaced with another launcher. Launcher can usually identify by 68 // android.intent.category.HOME (type=home) and priority = -1000. But there is no clear way 69 // to determine this via adb. 70 while (!isUserSecondary 71 && System.currentTimeMillis() - ti < SECONDARY_USER_BOOT_COMPLETE_TIMEOUT_MS) { 72 try { 73 currentUser = getDevice().getCurrentUser(); 74 isUserSecondary = getDevice().isUserSecondary(currentUser); 75 CLog.d("Current user: %d isSecondary: %b", currentUser, isUserSecondary); 76 if (isUserSecondary) { 77 CLog.d("Saul Goodman!"); 78 break; 79 } 80 CLog.v("Sleeping for %d ms as user %d is not a secondary user yet", 81 POLL_INTERVAL_MS, currentUser); 82 RunUtil.getDefault().sleep(POLL_INTERVAL_MS); 83 } catch (Exception e) { 84 CLog.d("Device not available yet (%s); sleeping for %d ms", e, 85 WAIT_FOR_DEVICE_READY_INTERVAL_MS); 86 RunUtil.getDefault().sleep(WAIT_FOR_DEVICE_READY_INTERVAL_MS); 87 } 88 } 89 assertWithMessage("Current user (%s) is a secondary user after boot", currentUser) 90 .that(isUserSecondary).isTrue(); 91 } 92 } 93