• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.os.UserManager;
26 import androidx.test.core.app.ApplicationProvider;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 
35 @RunWith(AndroidJUnit4.class)
36 public class TestingSettingsTest {
37 
38     @Mock
39     private UserManager mUserManager;
40     @Mock
41     private TestingSettings mTestingSettings;
42 
43     private Context mContext;
44 
45     @Before
setUp()46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         mContext = spy(ApplicationProvider.getApplicationContext());
49     }
50 
51     @Test
isRadioInfoVisible_returnFalse_whenUserRestricted()52     public void isRadioInfoVisible_returnFalse_whenUserRestricted() {
53         mockService(mContext, Context.USER_SERVICE, UserManager.class, mUserManager);
54 
55         doReturn(true).when(mUserManager).isAdminUser();
56         doReturn(false).when(mUserManager).isGuestUser();
57         doReturn(true).when(mUserManager)
58                 .hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
59 
60         assertThat(mTestingSettings.isRadioInfoVisible(mContext)).isFalse();
61     }
62 
mockService(Context context, String serviceName, Class<T> serviceClass, T service)63     private <T> void mockService(Context context, String serviceName,
64             Class<T> serviceClass, T service) {
65          when(context.getSystemServiceName(serviceClass)).thenReturn(serviceName);
66          when(context.getSystemService(serviceName)).thenReturn(service);
67      }
68 }
69