1/*
2 * Copyright (C) 2016 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
17package com.android.settingslib.drawer;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.UserInfo;
22import android.os.UserHandle;
23import android.os.UserManager;
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.mockito.Mock;
31import org.mockito.MockitoAnnotations;
32
33import static junit.framework.Assert.assertEquals;
34import static org.mockito.Mockito.never;
35import static org.mockito.Mockito.times;
36import static org.mockito.Mockito.verify;
37import static org.mockito.Mockito.when;
38
39@RunWith(AndroidJUnit4.class)
40@SmallTest
41public class ProfileSelectDialogTest {
42
43    @Mock
44    private Context mContext;
45    @Mock
46    private UserManager mUserManager;
47    private static final UserHandle NORMAL_USER = UserHandle.of(1111);
48    private static final UserHandle REMOVED_USER = UserHandle.of(2222);
49
50    @Before
51    public void setUp() {
52        MockitoAnnotations.initMocks(this);
53        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
54        final UserInfo userInfo = new UserInfo(
55                NORMAL_USER.getIdentifier(), "test_user", UserInfo.FLAG_RESTRICTED);
56        when(mUserManager.getUserInfo(NORMAL_USER.getIdentifier())).thenReturn(userInfo);
57    }
58
59    @Test
60    public void testUpdateUserHandlesIfNeeded_Normal() {
61        final Tile tile = new Tile();
62        tile.intent = new Intent();
63        tile.userHandle.add(NORMAL_USER);
64
65        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
66
67        assertEquals(tile.userHandle.size(), 1);
68        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
69        verify(mUserManager, never()).getUserInfo(NORMAL_USER.getIdentifier());
70    }
71
72    @Test
73    public void testUpdateUserHandlesIfNeeded_Remove() {
74        final Tile tile = new Tile();
75        tile.intent = new Intent();
76        tile.userHandle.add(REMOVED_USER);
77        tile.userHandle.add(NORMAL_USER);
78        tile.userHandle.add(REMOVED_USER);
79
80        ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
81
82        assertEquals(tile.userHandle.size(), 1);
83        assertEquals(tile.userHandle.get(0).getIdentifier(), NORMAL_USER.getIdentifier());
84        verify(mUserManager, times(1)).getUserInfo(NORMAL_USER.getIdentifier());
85        verify(mUserManager, times(2)).getUserInfo(REMOVED_USER.getIdentifier());
86    }
87}
88