• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.datausage;
18 
19 import static org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 
24 import android.content.Context;
25 import android.os.INetworkManagementService;
26 import android.os.RemoteException;
27 import android.os.UserManager;
28 import android.telephony.TelephonyManager;
29 import androidx.test.core.app.ApplicationProvider;
30 import androidx.test.ext.junit.runners.AndroidJUnit4;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class BillingCyclePreferenceTest {
39 
40     private Context mContext;
41     private BillingCyclePreference mPreference;
42     private TemplatePreference.NetworkServices mServices;
43     @Mock
44     private INetworkManagementService mNetManageSerice;
45     @Mock
46     private TelephonyManager mTelephonyManager;
47     @Mock
48     private UserManager mUserManager;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mContext = spy(ApplicationProvider.getApplicationContext());
54 
55         mServices = new TemplatePreference.NetworkServices();
56         mServices.mNetworkService = mNetManageSerice;
57         mServices.mTelephonyManager = mTelephonyManager;
58         mServices.mUserManager = mUserManager;
59 
60         doReturn(mTelephonyManager).when(mTelephonyManager)
61                 .createForSubscriptionId(anyInt());
62 
63         mPreference = spy(new BillingCyclePreference(mContext, null /* attrs */));
64         mPreference.setTemplate(null, 0, mServices);
65     }
66 
67     @Test
testPreferenceUpdate_onMobileDataEnabledChange_accessDataEnabledApi()68     public void testPreferenceUpdate_onMobileDataEnabledChange_accessDataEnabledApi() {
69         try {
70             doReturn(true).when(mNetManageSerice).isBandwidthControlEnabled();
71         } catch (RemoteException exception) {}
72         doReturn(true).when(mUserManager).isAdminUser();
73         mPreference.onMobileDataEnabledChange();
74 
75         verify(mTelephonyManager)
76                 .isDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER);
77     }
78 }