From c4b40b06697b5fcf71311c28d188f1ac0e5e8b0c Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Tue, 9 Oct 2018 17:10:34 -0700 Subject: [PATCH] Use default network template in billing cycle settings. - if the network template is not set in the bundle arguments, use the default network template for getting the billing data. - change to use ConnectivityManager.from(context) in DataUsageUtils to be consistent with the usage in other methods. Change-Id: I25eb27f8f26d9ecafc66aa2c69806e94c6b63499 Fixes: 117452991 Test: make RunSettingsRoboTests --- .../datausage/BillingCycleSettings.java | 10 +++- .../settings/datausage/DataUsageUtils.java | 3 +- .../datausage/BillingCycleSettingsTest.java | 54 +++++++++++++++++-- ...aUsageSummaryPreferenceControllerTest.java | 2 +- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/com/android/settings/datausage/BillingCycleSettings.java b/src/com/android/settings/datausage/BillingCycleSettings.java index 2d96fe92092..45997f8efc3 100644 --- a/src/com/android/settings/datausage/BillingCycleSettings.java +++ b/src/com/android/settings/datausage/BillingCycleSettings.java @@ -73,7 +73,8 @@ public class BillingCycleSettings extends DataUsageBaseFragment implements static final String KEY_SET_DATA_LIMIT = "set_data_limit"; private static final String KEY_DATA_LIMIT = "data_limit"; - private NetworkTemplate mNetworkTemplate; + @VisibleForTesting + NetworkTemplate mNetworkTemplate; private Preference mBillingCycle; private Preference mDataWarning; private SwitchPreference mEnableDataWarning; @@ -100,10 +101,15 @@ public class BillingCycleSettings extends DataUsageBaseFragment implements public void onCreate(Bundle icicle) { super.onCreate(icicle); - mDataUsageController = new DataUsageController(getContext()); + final Context context = getContext(); + mDataUsageController = new DataUsageController(context); Bundle args = getArguments(); mNetworkTemplate = args.getParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE); + if (mNetworkTemplate == null) { + mNetworkTemplate = DataUsageUtils.getDefaultTemplate(context, + DataUsageUtils.getDefaultSubscriptionId(context)); + } mBillingCycle = findPreference(KEY_BILLING_CYCLE); mEnableDataWarning = (SwitchPreference) findPreference(KEY_SET_DATA_WARNING); diff --git a/src/com/android/settings/datausage/DataUsageUtils.java b/src/com/android/settings/datausage/DataUsageUtils.java index 3001d2e3f1e..53565ac803e 100644 --- a/src/com/android/settings/datausage/DataUsageUtils.java +++ b/src/com/android/settings/datausage/DataUsageUtils.java @@ -172,8 +172,7 @@ public final class DataUsageUtils { return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi"); } - ConnectivityManager connectivityManager = - context.getSystemService(ConnectivityManager.class); + final ConnectivityManager connectivityManager = ConnectivityManager.from(context); return connectivityManager != null && connectivityManager.isNetworkSupported(TYPE_WIFI); } diff --git a/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java b/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java index eb5e4911664..1560af3261b 100644 --- a/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java +++ b/tests/robotests/src/com/android/settings/datausage/BillingCycleSettingsTest.java @@ -16,14 +16,17 @@ package com.android.settings.datausage; import static android.net.NetworkPolicy.CYCLE_NONE; +import static com.google.common.truth.Truth.assertThat; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; - +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.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; @@ -32,14 +35,20 @@ import static org.mockito.Mockito.when; import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; +import android.content.res.Resources; +import android.net.ConnectivityManager; +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.SettingsRobolectricTestRunner; import com.android.settingslib.NetworkPolicyEditor; +import com.android.settingslib.widget.FooterPreference; +import com.android.settingslib.widget.FooterPreferenceMixinCompat; import org.junit.Before; import org.junit.Test; @@ -47,6 +56,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) public class BillingCycleSettingsTest { @@ -60,6 +70,10 @@ public class BillingCycleSettingsTest { PreferenceManager mMockPreferenceManager; @Mock private NetworkPolicyEditor mNetworkPolicyEditor; + @Mock + private ConnectivityManager mConnectivityManager; + @Mock + private NetworkPolicyManager mNetworkPolicyManager; private Context mContext; @Mock @@ -96,7 +110,8 @@ public class BillingCycleSettingsTest { public void testDataUsageLimit_shouldNotBeSetOnCancel() { mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE); - assertFalse(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true)); + assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true)) + .isFalse(); verify(mMockBillingCycleSettings, never()).setPolicyLimitBytes(anyLong()); } @@ -104,7 +119,8 @@ public class BillingCycleSettingsTest { public void testDataUsageLimit_shouldBeSetOnConfirmation() { mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_POSITIVE); - assertTrue(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false)); + assertThat(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false)) + .isTrue(); verify(mMockBillingCycleSettings).setPolicyLimitBytes(LIMIT_BYTES); } @@ -124,4 +140,32 @@ public class BillingCycleSettingsTest { verify(mBillingCycle).setSummary(null); } + + @Test + 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.getSystemService(Context.CONNECTIVITY_SERVICE)) + .thenReturn(mConnectivityManager); + when(mConnectivityManager.isNetworkSupported(anyInt())).thenReturn(true); + final SwitchPreference preference = mock(SwitchPreference.class); + when(billingCycleSettings.findPreference(anyString())).thenReturn(preference); + final FooterPreferenceMixinCompat footer = mock(FooterPreferenceMixinCompat.class); + ReflectionHelpers.setField(billingCycleSettings, "mFooterPreferenceMixin", footer); + when(footer.createFooterPreference()).thenReturn(mock(FooterPreference.class)); + + billingCycleSettings.onCreate(Bundle.EMPTY); + + assertThat(billingCycleSettings.mNetworkTemplate).isNotNull(); + } + } \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java index 254e4f6f654..517154867c5 100644 --- a/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datausage/DataUsageSummaryPreferenceControllerTest.java @@ -121,7 +121,7 @@ public class DataUsageSummaryPreferenceControllerTest { mActivity = spy(Robolectric.buildActivity(FragmentActivity.class).get()); when(mActivity.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); - when(mActivity.getSystemService(ConnectivityManager.class)) + when(mActivity.getSystemService(Context.CONNECTIVITY_SERVICE)) .thenReturn(mConnectivityManager); when(mTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mConnectivityManager.isNetworkSupported(TYPE_WIFI)).thenReturn(false);