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