1 /*
2  * Copyright (C) 2021 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 com.android.tradefed.device;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 import com.android.tradefed.testtype.IDeviceTest;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.List;
31 import java.util.Map;
32 
33 /** Functional tests for the {@link ITestDevice} user management APIs */
34 @RunWith(DeviceJUnit4ClassRunner.class)
35 public class TestDeviceUserFuncTest implements IDeviceTest {
36     private TestDevice mTestDevice;
37 
38     @Override
setDevice(ITestDevice device)39     public void setDevice(ITestDevice device) {
40         mTestDevice = (TestDevice) device;
41     }
42 
43     @Override
getDevice()44     public ITestDevice getDevice() {
45         return mTestDevice;
46     }
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         // Ensure at set-up that the device is available.
51         mTestDevice.waitForDeviceAvailable();
52     }
53 
54     /**
55      * Tests several of the user creation, listing, and deletion methods
56      *
57      * <p>Specifically this tests {@link ITestDevice#createUser(String, boolean, boolean)}, {@link
58      * ITestDevice#listUsers()}, and {@link ITestDevice#getUserInfos()}.
59      */
60     @Test
testUserLifecycle()61     public void testUserLifecycle() throws Exception {
62         int userId = -1;
63         try {
64             final String userName = "Google";
65             userId = mTestDevice.createUser(userName, false, false);
66             assertTrue(userId > 0);
67 
68             List<Integer> listedIDs = mTestDevice.listUsers();
69             boolean containsUserId = listedIDs.contains(userId);
70             assertEquals(true, containsUserId);
71 
72             Map<Integer, UserInfo> userInfos = mTestDevice.getUserInfos();
73             boolean userInfoContainsUserId = userInfos.containsKey(userId);
74             assertEquals(true, userInfoContainsUserId);
75             UserInfo info = userInfos.get(userId);
76             assertNotNull(info);
77             assertEquals(userName, info.userName());
78         } finally {
79             if (userId != -1) {
80                 mTestDevice.removeUser(userId);
81             }
82         }
83     }
84 
85     /** Tries creating, starting, and stopping a user. */
86     @Test
testStartUser_IsRunningUser_StopUser()87     public void testStartUser_IsRunningUser_StopUser() throws Exception {
88         int userId = -1;
89         try {
90             final String username = "Google";
91             userId = mTestDevice.createUser(username, false, false);
92             assertTrue(userId > 0);
93 
94             mTestDevice.startUser(userId);
95 
96             assertTrue(mTestDevice.isUserRunning(userId));
97 
98             mTestDevice.stopUser(userId, true, false);
99 
100             assertFalse(mTestDevice.isUserRunning(userId));
101         } finally {
102             if (userId != -1) {
103                 mTestDevice.removeUser(userId);
104             }
105         }
106     }
107 
108     /**
109      * Tests the isUserRunning method semi-independently
110      *
111      * <p>Assuming the device is left in a default state after other tests, the default user should
112      * be running, which means we can test our method. If this test fails, it means either a)
113      * another test isn't cleaning up properly or b) our method is broken. Either result is worth
114      * knowing.
115      */
116     @Test
testIsUserRunning()117     public void testIsUserRunning() throws Exception {
118         final int DEFAULT_USER = 0;
119         assertTrue(mTestDevice.isUserRunning(DEFAULT_USER));
120     }
121 }
122