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
This commit is contained in:
Doris Ling
2018-10-09 17:10:34 -07:00
parent b3eaf62562
commit c4b40b0669
4 changed files with 59 additions and 10 deletions

View File

@@ -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();
}
}