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