• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.network.telephony;
18 
19 import static com.android.settings.network.telephony.MobileNetworkSettings.REQUEST_CODE_DELETE_SUBSCRIPTION;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.usage.NetworkStatsManager;
30 import android.content.Context;
31 import android.net.NetworkPolicyManager;
32 import android.os.Bundle;
33 import android.os.UserManager;
34 import android.provider.Settings;
35 import android.telephony.TelephonyManager;
36 
37 import androidx.fragment.app.FragmentActivity;
38 
39 import com.android.settings.core.FeatureFlags;
40 import com.android.settings.datausage.DataUsageSummaryPreferenceController;
41 import com.android.settings.development.featureflags.FeatureFlagPersistent;
42 import com.android.settings.search.BaseSearchIndexProvider;
43 import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
44 import com.android.settings.widget.EntityHeaderController;
45 import com.android.settingslib.core.AbstractPreferenceController;
46 
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.robolectric.RobolectricTestRunner;
53 import org.robolectric.RuntimeEnvironment;
54 import org.robolectric.annotation.Config;
55 import org.robolectric.util.ReflectionHelpers;
56 
57 import java.util.List;
58 
59 @RunWith(RobolectricTestRunner.class)
60 @Config(shadows = ShadowEntityHeaderController.class)
61 public class MobileNetworkSettingsTest {
62     @Mock
63     private TelephonyManager mTelephonyManager;
64     @Mock
65     private NetworkStatsManager mNetworkStatsManager;
66     @Mock
67     private NetworkPolicyManager mNetworkPolicyManager;
68     @Mock
69     private FragmentActivity mActivity;
70 
71     private Context mContext;
72     private MobileNetworkSettings mFragment;
73 
74     @Before
setUp()75     public void setUp() {
76         MockitoAnnotations.initMocks(this);
77         mContext = spy(RuntimeEnvironment.application);
78         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
79         when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
80         when(mContext.getSystemService(NetworkStatsManager.class)).thenReturn(mNetworkStatsManager);
81         ShadowEntityHeaderController.setUseMock(mock(EntityHeaderController.class));
82 
83         mFragment = spy(new MobileNetworkSettings());
84         final Bundle args = new Bundle();
85         final int subscriptionId = 1234;
86         args.putInt(Settings.EXTRA_SUB_ID, subscriptionId);
87         mFragment.setArguments(args);
88         when(mFragment.getActivity()).thenReturn(mActivity);
89         when(mActivity.isFinishing()).thenReturn(false);
90         when(mActivity.getSystemService(NetworkPolicyManager.class)).thenReturn(
91                 mNetworkPolicyManager);
92     }
93 
94     @Test
onAttach_noV2Flag_noCrash()95     public void onAttach_noV2Flag_noCrash() {
96         FeatureFlagPersistent.setEnabled(mContext, FeatureFlags.NETWORK_INTERNET_V2, false);
97         mFragment.onAttach(mContext);
98     }
99 
100     @Test
onAttach_v2Flag_noCrash()101     public void onAttach_v2Flag_noCrash() {
102         FeatureFlagPersistent.setEnabled(mContext, FeatureFlags.NETWORK_INTERNET_V2, true);
103         mFragment.onAttach(mContext);
104     }
105 
106     @Test
createPreferenceControllers_noV2Flag_noDataUsageSummaryController()107     public void createPreferenceControllers_noV2Flag_noDataUsageSummaryController() {
108         FeatureFlagPersistent.setEnabled(mContext, FeatureFlags.NETWORK_INTERNET_V2, false);
109         final List<AbstractPreferenceController> controllers =
110                 mFragment.createPreferenceControllers(mContext);
111         assertThat(controllers.stream().filter(
112                 c -> c.getClass().equals(DataUsageSummaryPreferenceController.class))
113                 .count())
114                 .isEqualTo(0);
115     }
116 
117     @Test
createPreferenceControllers_v2Flag_createsDataUsageSummaryController()118     public void createPreferenceControllers_v2Flag_createsDataUsageSummaryController() {
119         FeatureFlagPersistent.setEnabled(mContext, FeatureFlags.NETWORK_INTERNET_V2, true);
120 
121         final List<AbstractPreferenceController> controllers =
122                 mFragment.createPreferenceControllers(mContext);
123         assertThat(controllers.stream().filter(
124                 c -> c.getClass().equals(DataUsageSummaryPreferenceController.class))
125                 .count())
126                 .isEqualTo(1);
127     }
128 
129     @Test
onActivityResult_noActivity_noCrash()130     public void onActivityResult_noActivity_noCrash() {
131         when(mFragment.getActivity()).thenReturn(null);
132         // this should not crash
133         mFragment.onActivityResult(REQUEST_CODE_DELETE_SUBSCRIPTION, 0, null);
134     }
135 
136     @Test
onActivityResult_deleteSubscription_activityFinishes()137     public void onActivityResult_deleteSubscription_activityFinishes() {
138         mFragment.onActivityResult(REQUEST_CODE_DELETE_SUBSCRIPTION, 0, null);
139         verify(mActivity).finish();
140     }
141 
142     @Test
isPageSearchEnabled_adminUser_shouldReturnTrue()143     public void isPageSearchEnabled_adminUser_shouldReturnTrue() {
144         final UserManager userManager = mock(UserManager.class);
145         when(mContext.getSystemService(UserManager.class)).thenReturn(userManager);
146         when(userManager.isAdminUser()).thenReturn(true);
147         final BaseSearchIndexProvider provider =
148                 (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
149 
150         final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
151                 ReflectionHelpers.ClassParameter.from(Context.class, mContext));
152         final boolean isEnabled = (Boolean) obj;
153 
154         assertThat(isEnabled).isTrue();
155     }
156 
157     @Test
isPageSearchEnabled_nonAdminUser_shouldReturnFalse()158     public void isPageSearchEnabled_nonAdminUser_shouldReturnFalse() {
159         final UserManager userManager = mock(UserManager.class);
160         when(mContext.getSystemService(UserManager.class)).thenReturn(userManager);
161         when(userManager.isAdminUser()).thenReturn(false);
162         final BaseSearchIndexProvider provider =
163                 (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
164 
165         final Object obj = ReflectionHelpers.callInstanceMethod(provider, "isPageSearchEnabled",
166                 ReflectionHelpers.ClassParameter.from(Context.class, mContext));
167         final boolean isEnabled = (Boolean) obj;
168 
169         assertThat(isEnabled).isFalse();
170     }
171 }
172