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 package com.android.settings.datausage; 17 18 import static android.net.NetworkPolicy.CYCLE_NONE; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.ArgumentMatchers.anyLong; 25 import static org.mockito.ArgumentMatchers.anyObject; 26 import static org.mockito.ArgumentMatchers.anyString; 27 import static org.mockito.ArgumentMatchers.nullable; 28 import static org.mockito.Mockito.doNothing; 29 import static org.mockito.Mockito.doReturn; 30 import static org.mockito.Mockito.mock; 31 import static org.mockito.Mockito.never; 32 import static org.mockito.Mockito.spy; 33 import static org.mockito.Mockito.verify; 34 import static org.mockito.Mockito.when; 35 36 import android.content.Context; 37 import android.content.DialogInterface; 38 import android.content.SharedPreferences; 39 import android.content.pm.PackageManager; 40 import android.content.res.Resources; 41 import android.net.NetworkPolicyManager; 42 import android.os.Bundle; 43 44 import androidx.fragment.app.FragmentActivity; 45 import androidx.preference.Preference; 46 import androidx.preference.PreferenceManager; 47 import androidx.preference.SwitchPreference; 48 49 import com.android.settings.testutils.shadow.ShadowFragment; 50 import com.android.settingslib.NetworkPolicyEditor; 51 52 import org.junit.Before; 53 import org.junit.Ignore; 54 import org.junit.Test; 55 import org.junit.runner.RunWith; 56 import org.mockito.Mock; 57 import org.mockito.MockitoAnnotations; 58 import org.robolectric.RobolectricTestRunner; 59 import org.robolectric.RuntimeEnvironment; 60 import org.robolectric.annotation.Config; 61 62 @RunWith(RobolectricTestRunner.class) 63 public class BillingCycleSettingsTest { 64 65 private static final int LIMIT_BYTES = 123; 66 67 @Mock 68 private BillingCycleSettings mMockBillingCycleSettings; 69 private BillingCycleSettings.ConfirmLimitFragment mConfirmLimitFragment; 70 @Mock 71 private PreferenceManager mMockPreferenceManager; 72 @Mock 73 private NetworkPolicyEditor mNetworkPolicyEditor; 74 @Mock 75 private NetworkPolicyManager mNetworkPolicyManager; 76 @Mock 77 private PackageManager mMockPackageManager; 78 79 private Context mContext; 80 @Mock 81 private Preference mBillingCycle; 82 @Mock 83 private Preference mDataWarning; 84 @Mock 85 private Preference mDataLimit; 86 @Mock 87 private SwitchPreference mEnableDataWarning; 88 @Mock 89 private SwitchPreference mEnableDataLimit; 90 91 private SharedPreferences mSharedPreferences; 92 93 @Before setUp()94 public void setUp() { 95 MockitoAnnotations.initMocks(this); 96 mContext = spy(RuntimeEnvironment.application); 97 mConfirmLimitFragment = new BillingCycleSettings.ConfirmLimitFragment(); 98 mConfirmLimitFragment.setTargetFragment(mMockBillingCycleSettings, 0); 99 mSharedPreferences = RuntimeEnvironment.application.getSharedPreferences( 100 "testSharedPreferences", Context.MODE_PRIVATE); 101 when(mMockBillingCycleSettings.getPreferenceManager()).thenReturn(mMockPreferenceManager); 102 when(mMockPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences); 103 final Bundle args = new Bundle(); 104 args.putLong(BillingCycleSettings.ConfirmLimitFragment.EXTRA_LIMIT_BYTES, LIMIT_BYTES); 105 mConfirmLimitFragment.setArguments(args); 106 mSharedPreferences.edit().putBoolean( 107 BillingCycleSettings.KEY_SET_DATA_LIMIT, false).apply(); 108 } 109 110 @Test testDataUsageLimit_shouldNotBeSetOnCancel()111 public void testDataUsageLimit_shouldNotBeSetOnCancel() { 112 mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE); 113 114 assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true)) 115 .isFalse(); 116 verify(mMockBillingCycleSettings, never()).setPolicyLimitBytes(anyLong()); 117 } 118 119 @Test testDataUsageLimit_shouldBeSetOnConfirmation()120 public void testDataUsageLimit_shouldBeSetOnConfirmation() { 121 mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_POSITIVE); 122 123 assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false)) 124 .isTrue(); 125 verify(mMockBillingCycleSettings).setPolicyLimitBytes(LIMIT_BYTES); 126 } 127 128 @Test testDataUsageSummary_shouldBeNull()129 public void testDataUsageSummary_shouldBeNull() { 130 final BillingCycleSettings billingCycleSettings = spy(new BillingCycleSettings()); 131 when(billingCycleSettings.getContext()).thenReturn(mContext); 132 billingCycleSettings.setUpForTest(mNetworkPolicyEditor, mBillingCycle, 133 mDataLimit, mDataWarning, mEnableDataLimit, mEnableDataWarning); 134 135 doReturn("some-string").when(billingCycleSettings).getString(anyInt(), anyInt()); 136 when(mNetworkPolicyEditor.getPolicyCycleDay(anyObject())).thenReturn(CYCLE_NONE + 1); 137 when(mNetworkPolicyEditor.getPolicyLimitBytes(anyObject())).thenReturn(2000L); 138 when(mNetworkPolicyEditor.getPolicyWarningBytes(anyObject())).thenReturn(1000L); 139 140 billingCycleSettings.updatePrefs(); 141 142 verify(mBillingCycle).setSummary(null); 143 } 144 145 @Test 146 @Config(shadows = ShadowFragment.class) 147 @Ignore onCreate_emptyArguments_shouldSetDefaultNetworkTemplate()148 public void onCreate_emptyArguments_shouldSetDefaultNetworkTemplate() { 149 final BillingCycleSettings billingCycleSettings = spy(new BillingCycleSettings()); 150 when(billingCycleSettings.getContext()).thenReturn(mContext); 151 when(billingCycleSettings.getArguments()).thenReturn(Bundle.EMPTY); 152 final FragmentActivity activity = mock(FragmentActivity.class); 153 when(billingCycleSettings.getActivity()).thenReturn(activity); 154 final Resources.Theme theme = mContext.getTheme(); 155 when(activity.getTheme()).thenReturn(theme); 156 doNothing().when(billingCycleSettings) 157 .onCreatePreferences(any(Bundle.class), nullable(String.class)); 158 when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE)) 159 .thenReturn(mNetworkPolicyManager); 160 when(mContext.getPackageManager()).thenReturn(mMockPackageManager); 161 when(mMockPackageManager.hasSystemFeature(any())).thenReturn(true); 162 final SwitchPreference preference = mock(SwitchPreference.class); 163 when(billingCycleSettings.findPreference(anyString())).thenReturn(preference); 164 165 billingCycleSettings.onCreate(Bundle.EMPTY); 166 167 assertThat(billingCycleSettings.mNetworkTemplate).isNotNull(); 168 } 169 } 170