Files
app_Settings/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java
Chiachang Wang d507dc19b1 Replace hidden CM#isNetworkSupported() usage
It's a refactor work for connectivity mainline module. The
hidden methods access is no longer allowed, so the usage for
isNetworkSupported() should be replaced. Settings use it to
check if device support telephony, wifi or ethernet service.
There are alternative methods to check if device supports such
features. Replace as it is.

Bug: 172183305
Test: m ; make RunSettingsRoboTests
Change-Id: I8f1d11558b1be575a0777ed195abe027e838cb74
Merged-In: I8f1d11558b1be575a0777ed195abe027e838cb74
2021-03-17 15:22:40 +00:00

170 lines
6.8 KiB
Java

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.datausage;
import static android.net.NetworkPolicy.CYCLE_NONE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.NetworkPolicyManager;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreference;
import com.android.settings.testutils.shadow.ShadowFragment;
import com.android.settingslib.NetworkPolicyEditor;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class BillingCycleSettingsTest {
private static final int LIMIT_BYTES = 123;
@Mock
private BillingCycleSettings mMockBillingCycleSettings;
private BillingCycleSettings.ConfirmLimitFragment mConfirmLimitFragment;
@Mock
private PreferenceManager mMockPreferenceManager;
@Mock
private NetworkPolicyEditor mNetworkPolicyEditor;
@Mock
private NetworkPolicyManager mNetworkPolicyManager;
@Mock
private PackageManager mMockPackageManager;
private Context mContext;
@Mock
private Preference mBillingCycle;
@Mock
private Preference mDataWarning;
@Mock
private Preference mDataLimit;
@Mock
private SwitchPreference mEnableDataWarning;
@Mock
private SwitchPreference mEnableDataLimit;
private SharedPreferences mSharedPreferences;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mConfirmLimitFragment = new BillingCycleSettings.ConfirmLimitFragment();
mConfirmLimitFragment.setTargetFragment(mMockBillingCycleSettings, 0);
mSharedPreferences = RuntimeEnvironment.application.getSharedPreferences(
"testSharedPreferences", Context.MODE_PRIVATE);
when(mMockBillingCycleSettings.getPreferenceManager()).thenReturn(mMockPreferenceManager);
when(mMockPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences);
final Bundle args = new Bundle();
args.putLong(BillingCycleSettings.ConfirmLimitFragment.EXTRA_LIMIT_BYTES, LIMIT_BYTES);
mConfirmLimitFragment.setArguments(args);
mSharedPreferences.edit().putBoolean(
BillingCycleSettings.KEY_SET_DATA_LIMIT, false).apply();
}
@Test
public void testDataUsageLimit_shouldNotBeSetOnCancel() {
mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true))
.isFalse();
verify(mMockBillingCycleSettings, never()).setPolicyLimitBytes(anyLong());
}
@Test
public void testDataUsageLimit_shouldBeSetOnConfirmation() {
mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false))
.isTrue();
verify(mMockBillingCycleSettings).setPolicyLimitBytes(LIMIT_BYTES);
}
@Test
public void testDataUsageSummary_shouldBeNull() {
final BillingCycleSettings billingCycleSettings = spy(new BillingCycleSettings());
when(billingCycleSettings.getContext()).thenReturn(mContext);
billingCycleSettings.setUpForTest(mNetworkPolicyEditor, mBillingCycle,
mDataLimit, mDataWarning, mEnableDataLimit, mEnableDataWarning);
doReturn("some-string").when(billingCycleSettings).getString(anyInt(), anyInt());
when(mNetworkPolicyEditor.getPolicyCycleDay(anyObject())).thenReturn(CYCLE_NONE + 1);
when(mNetworkPolicyEditor.getPolicyLimitBytes(anyObject())).thenReturn(2000L);
when(mNetworkPolicyEditor.getPolicyWarningBytes(anyObject())).thenReturn(1000L);
billingCycleSettings.updatePrefs();
verify(mBillingCycle).setSummary(null);
}
@Test
@Config(shadows = ShadowFragment.class)
@Ignore
public void onCreate_emptyArguments_shouldSetDefaultNetworkTemplate() {
final BillingCycleSettings billingCycleSettings = spy(new BillingCycleSettings());
when(billingCycleSettings.getContext()).thenReturn(mContext);
when(billingCycleSettings.getArguments()).thenReturn(Bundle.EMPTY);
final FragmentActivity activity = mock(FragmentActivity.class);
when(billingCycleSettings.getActivity()).thenReturn(activity);
final Resources.Theme theme = mContext.getTheme();
when(activity.getTheme()).thenReturn(theme);
doNothing().when(billingCycleSettings)
.onCreatePreferences(any(Bundle.class), nullable(String.class));
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE))
.thenReturn(mNetworkPolicyManager);
when(mContext.getPackageManager()).thenReturn(mMockPackageManager);
when(mMockPackageManager.hasSystemFeature(any())).thenReturn(true);
final SwitchPreference preference = mock(SwitchPreference.class);
when(billingCycleSettings.findPreference(anyString())).thenReturn(preference);
billingCycleSettings.onCreate(Bundle.EMPTY);
assertThat(billingCycleSettings.mNetworkTemplate).isNotNull();
}
}