summaryrefslogtreecommitdiff
path: root/Settings/tests/robotests/src/com/android/tv/settings/users/RestrictedProfileTest.java
blob: 0169823d86ce111a0c5051c493f01346c2b00c49 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2018 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 com.android.tv.settings.users;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;

import com.android.tv.settings.library.users.RestrictedProfileModel;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class RestrictedProfileTest {

    private final UserInfo mOwnerInfo = new UserInfo(123, "owner", 0);
    private final UserInfo mProfileInfo = new UserInfo(345, "profile", UserInfo.FLAG_RESTRICTED);
    private final UserInfo mSystemInfo = new UserInfo(UserHandle.USER_SYSTEM, "system", 0);

    @Mock private Context mContext;
    @Mock private ActivityManager mActivityManager;
    @Mock private UserManager mUserManager;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(mActivityManager);
        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
        mProfileInfo.restrictedProfileParentId = mOwnerInfo.id;
    }

    @Test
    public void testGetUser_isSelf() {
        setCurrentUser(mProfileInfo);
        assertThat(newRpm().getUser()).isSameInstanceAs(mProfileInfo);
    }

    @Test
    public void testGetUser_isChild() {
        setCurrentUser(mOwnerInfo).addOtherUsers(mProfileInfo);
        assertThat(newRpm().getUser()).isSameInstanceAs(mProfileInfo);
    }

    @Test
    public void testGetUser_doesNotExist() {
        setCurrentUser(mOwnerInfo);
        assertThat(newRpm().getUser()).isNull();
    }

    @Test
    public void testEnterUser_isOwner_profileExists() {
        setCurrentUser(mOwnerInfo).addOtherUsers(mProfileInfo);
        assertThat(newRpm().enterUser()).isTrue();
        verify(mActivityManager, times(1)).switchUser(eq(mProfileInfo.id));
    }

    @Test
    public void testEnterUser_isOwner_profileDoesNotExist() {
        setCurrentUser(mOwnerInfo);
        assertThat(newRpm().enterUser()).isFalse();
        verify(mActivityManager, never()).switchUser(anyInt());
    }

    @Test
    public void testEnterUser_isProfile() {
        setCurrentUser(mProfileInfo).addOtherUsers(mOwnerInfo);
        assertThat(newRpm().enterUser()).isFalse();
        verify(mActivityManager, never()).switchUser(anyInt());
    }

    @Test
    public void testExitUser_isOwner() {
        setCurrentUser(mOwnerInfo).addOtherUsers(mProfileInfo);
        newRpm().exitUser();
        verify(mActivityManager, never()).switchUser(anyInt());
    }

    @Test
    public void testExitUser_isProfile() {
        setCurrentUser(mProfileInfo).addOtherUsers(mOwnerInfo, mSystemInfo);
        newRpm().exitUser();
        verify(mActivityManager, times(1)).switchUser(eq(mOwnerInfo.id));
    }

    @Test
    public void testExitUser_isProfile_legacyParent() {
        setCurrentUser(mProfileInfo).addOtherUsers(mProfileInfo, mSystemInfo);
        mProfileInfo.restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID;
        newRpm().exitUser();
        verify(mActivityManager, times(1)).switchUser(eq(mSystemInfo.id));
    }

    private RestrictedProfileModel newRpm() {
        return new RestrictedProfileModel(mContext, /* applyRestrictions= */ false);
    }

    private UserSetter setCurrentUser(UserInfo user) {
        when(mContext.getUserId()).thenReturn(user.id);
        return new UserSetter().addOtherUsers(user);
    }

    private class UserSetter {
        private final List<UserInfo> mUsers = new ArrayList<>();

        private UserSetter addOtherUsers(UserInfo... users) {
            for (UserInfo user : users) {
                when(mUserManager.getUserInfo(eq(user.id))).thenReturn(user);
            }
            mUsers.addAll(Arrays.asList(users));
            when(mUserManager.getUsers()).thenReturn(mUsers);
            return this;
        }
    }
}