summaryrefslogtreecommitdiff
path: root/tests/automotive/health/multiuser/src/android/platform/scenario/multiuser/nonui/SwitchToNewGuest.java
blob: facc1ac6f0d6f926ef26582a20afdc2a2cfe078d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.platform.scenario.multiuser;

import android.app.UiAutomation;
import android.content.pm.UserInfo;
import android.os.SystemClock;
import android.platform.helpers.MultiUserHelper;
import android.platform.test.scenario.annotation.Scenario;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * This test will always switch to a newly created guest from default initial user.
 *
 * <p>It should be running under user 0, otherwise instrumentation may be killed after user
 * switched.
 */
@Scenario
@RunWith(JUnit4.class)
public class SwitchToNewGuest {

    private final MultiUserHelper mMultiUserHelper = MultiUserHelper.getInstance();
    private int mGuestId;
    private UiAutomation mUiAutomation = null;
    private static final String CREATE_USERS_PERMISSION = "android.permission.CREATE_USERS";

    @Before
    public void setup() throws Exception {
        /*
        TODO: Create setup util API
         */
        // Execute user manager APIs with elevated permissions
        mUiAutomation = getUiAutomation();
        // TODO: b/302175460 - update minimum SDK version
        mUiAutomation.adoptShellPermissionIdentity(CREATE_USERS_PERMISSION);
        UserInfo currentUser = mMultiUserHelper.getCurrentForegroundUserInfo();

        // Drop elevated permissions
        mUiAutomation.dropShellPermissionIdentity();

        if (currentUser.id != MultiUserConstants.DEFAULT_INITIAL_USER) {
            SystemClock.sleep(MultiUserConstants.WAIT_FOR_IDLE_TIME_MS);

            // Execute user manager APIs with elevated permissions
            mUiAutomation = getUiAutomation();
            mUiAutomation.adoptShellPermissionIdentity(CREATE_USERS_PERMISSION);
            mMultiUserHelper.switchAndWaitForStable(
                MultiUserConstants.DEFAULT_INITIAL_USER, MultiUserConstants.WAIT_FOR_IDLE_TIME_MS);

            // Drop elevated permissions
            mUiAutomation.dropShellPermissionIdentity();
        }

        if (!MultiUserConstants.INCLUDE_CREATION_TIME) {
            // Execute user manager APIs with elevated permissions
            mUiAutomation = getUiAutomation();
            mUiAutomation.adoptShellPermissionIdentity(CREATE_USERS_PERMISSION);
            mGuestId = mMultiUserHelper.createUser(MultiUserConstants.GUEST_NAME, true);

            // Drop elevated permissions
            mUiAutomation.dropShellPermissionIdentity();
        }
    }

    @Test
    public void testSwitch() throws Exception {

        // Execute user manager APIs with elevated permissions
        mUiAutomation = getUiAutomation();
        mUiAutomation.adoptShellPermissionIdentity(CREATE_USERS_PERMISSION);

        if (MultiUserConstants.INCLUDE_CREATION_TIME) {
            mGuestId = mMultiUserHelper.createUser(MultiUserConstants.GUEST_NAME, true);
        }
        mMultiUserHelper.switchToUserId(mGuestId);

        // Drop elevated permissions
        mUiAutomation.dropShellPermissionIdentity();
    }

    private UiAutomation getUiAutomation() {
        return InstrumentationRegistry.getInstrumentation().getUiAutomation();
    }
}