Home | History | Annotate | Download | only in settings
      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 
     17 package com.android.settings;
     18 
     19 import static junit.framework.Assert.assertEquals;
     20 import static junit.framework.Assert.assertNull;
     21 import static org.mockito.Mockito.eq;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.content.pm.UserInfo;
     25 import android.os.UserHandle;
     26 import android.os.UserManager;
     27 import android.test.AndroidTestCase;
     28 import android.test.suitebuilder.annotation.SmallTest;
     29 
     30 import java.util.ArrayList;
     31 import java.util.Arrays;
     32 
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 
     36 public class UtilsTest extends AndroidTestCase {
     37     private static final int TEST_PRIMARY_USER_ID = 10;
     38     private static final int TEST_MANAGED_PROFILE_ID = 11;
     39 
     40     @Mock private UserManager mUserManager;
     41 
     42     @Override
     43     public void setUp() throws Exception {
     44         super.setUp();
     45         // // this is necessary for mockito to work
     46         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
     47 
     48         MockitoAnnotations.initMocks(this);
     49         when(mUserManager.getUserHandle()).thenReturn(TEST_PRIMARY_USER_ID);
     50         UserInfo primaryUser = new UserInfo(TEST_PRIMARY_USER_ID, null,
     51                 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_PRIMARY);
     52         when(mUserManager.getUserInfo(TEST_PRIMARY_USER_ID)).thenReturn(primaryUser);
     53         UserInfo managedProfile = new UserInfo(TEST_MANAGED_PROFILE_ID, null,
     54                 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
     55         when(mUserManager.getUserInfo(eq(TEST_MANAGED_PROFILE_ID))).thenReturn(managedProfile);
     56     }
     57 
     58     @SmallTest
     59     public void testGetManagedProfile() {
     60         UserHandle[] userHandles = new UserHandle[] {
     61             new UserHandle(TEST_PRIMARY_USER_ID),
     62             new UserHandle(TEST_MANAGED_PROFILE_ID)
     63         };
     64         when(mUserManager.getUserProfiles())
     65                 .thenReturn(new ArrayList<UserHandle>(Arrays.asList(userHandles)));
     66         assertEquals(TEST_MANAGED_PROFILE_ID,
     67                 Utils.getManagedProfile(mUserManager).getIdentifier());
     68     }
     69 
     70     @SmallTest
     71     public void testGetManagedProfile_notPresent() {
     72         UserHandle[] userHandles = new UserHandle[] {
     73             new UserHandle(TEST_PRIMARY_USER_ID)
     74         };
     75         when(mUserManager.getUserProfiles())
     76                 .thenReturn(new ArrayList<UserHandle>(Arrays.asList(userHandles)));
     77         assertNull(Utils.getManagedProfile(mUserManager));
     78     }
     79 }
     80