Fix two problems related to data connectivity in the multi-SIM header

When you have multiple active SIMs, the Network & internet page has a
header showing entries for each one, with summary text indicating which
one is used for data (and whether it is just set as the default, or
actively using data). We were not properly setting this text when either
data wasn't being used, eg when connected to wifi, or mobile data was
disabled for this SIM. This CL fixes both these problems by adding new
helper classes to listen for relevant events.

Test: make RunSettingsRoboTests
Fixes: 124394250
Fixes: 128857712
Change-Id: I34f2679752fa41a50247dd0b12581cbfd77a34f6
This commit is contained in:
Antony Sargent
2019-05-07 16:01:09 -07:00
parent 02a54a0150
commit 4bb253358b
6 changed files with 419 additions and 7 deletions

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2019 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.network;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Handler;
import com.android.settings.network.telephony.DataConnectivityListener;
import org.junit.Before;
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;
@RunWith(RobolectricTestRunner.class)
public class DataConnectivityListenerTest {
@Mock
private DataConnectivityListener.Client mClient;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private Network mActiveNetwork;
private Context mContext;
private DataConnectivityListener mListener;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager);
when(mConnectivityManager.getActiveNetwork()).thenReturn(mActiveNetwork);
mListener = new DataConnectivityListener(mContext, mClient);
}
@Test
public void noStart_doesNotRegister() {
verify(mConnectivityManager, never()).registerNetworkCallback(any(NetworkRequest.class),
any(ConnectivityManager.NetworkCallback.class), any(Handler.class));
}
@Test
public void start_doesRegister() {
mListener.start();
verify(mConnectivityManager).registerNetworkCallback(any(NetworkRequest.class),
eq(mListener), any(Handler.class));
}
@Test
public void onCapabilitiesChanged_notActiveNetwork_noCallback() {
Network changedNetwork = mock(Network.class);
mListener.onCapabilitiesChanged(changedNetwork, mock(NetworkCapabilities.class));
verify(mClient, never()).onDataConnectivityChange();
}
@Test
public void onCapabilitiesChanged_activeNetwork_onDataConnectivityChangeFires() {
mListener.onCapabilitiesChanged(mActiveNetwork, mock(NetworkCapabilities.class));
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLosing_notActiveNetwork_onDataConnectivityChangeFires() {
Network changedNetwork = mock(Network.class);
mListener.onLosing(changedNetwork, 500);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLosing_activeNetwork_onDataConnectivityChangeFires() {
mListener.onLosing(mActiveNetwork, 500);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLost_notActiveNetwork_onDataConnectivityChangeFires() {
Network changedNetwork = mock(Network.class);
mListener.onLost(changedNetwork);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLost_activeNetwork_onDataConnectivityChangeFires() {
mListener.onLost(mActiveNetwork);
verify(mClient).onDataConnectivityChange();
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2019 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.network;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import org.junit.Before;
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;
@RunWith(RobolectricTestRunner.class)
public class MobileDataEnabledListenerTest {
private static final int SUB_ID_ONE = 111;
private static final int SUB_ID_TWO = 222;
@Mock
private MobileDataEnabledListener.Client mClient;
private Context mContext;
private MobileDataEnabledListener mListener;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mListener = new MobileDataEnabledListener(mContext, mClient);
}
@Test
public void onMobileDataEnabledChange_firesCorrectly() {
mListener.start(SUB_ID_ONE);
final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_ONE);
mContext.getContentResolver().notifyChange(uri, null);
verify(mClient).onMobileDataEnabledChange();
}
@Test
public void onMobileDataEnabledChange_doesNotFireAfterStop() {
mListener.start(SUB_ID_ONE);
mListener.stop();
final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_ONE);
mContext.getContentResolver().notifyChange(uri, null);
verify(mClient, never()).onMobileDataEnabledChange();
}
@Test
public void onMobileDataEnabledChange_changedToDifferentId_firesCorrectly() {
mListener.start(SUB_ID_ONE);
mListener.stop();
mListener.start(SUB_ID_TWO);
final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_TWO);
mContext.getContentResolver().notifyChange(uri, null);
verify(mClient).onMobileDataEnabledChange();
}
}

View File

@@ -33,6 +33,9 @@ import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -74,7 +77,13 @@ public class SubscriptionsPreferenceControllerTest {
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private Network mActiveNetwork;
@Mock
private NetworkCapabilities mCapabilities;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
@@ -90,7 +99,10 @@ public class SubscriptionsPreferenceControllerTest {
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mConnectivityManager.getActiveNetwork()).thenReturn(mActiveNetwork);
when(mConnectivityManager.getNetworkCapabilities(mActiveNetwork)).thenReturn(mCapabilities);
when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
when(mScreen.findPreference(eq(KEY))).thenReturn(mPreferenceCategory);
when(mPreferenceCategory.getContext()).thenReturn(mContext);
@@ -308,7 +320,8 @@ public class SubscriptionsPreferenceControllerTest {
ShadowSubscriptionManager.setDefaultDataSubscriptionId(11);
ShadowSubscriptionManager.setDefaultSmsSubscriptionId(11);
ShadowSubscriptionManager.setDefaultVoiceSubscriptionId(11);
when(mTelephonyManager.getDataState()).thenReturn(TelephonyManager.DATA_CONNECTED);
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
when(mCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(true);
assertThat(mController.getSummary(11)).isEqualTo(
mContext.getString(R.string.default_for_calls_and_sms) + System.lineSeparator()
@@ -318,6 +331,27 @@ public class SubscriptionsPreferenceControllerTest {
mContext.getString(R.string.subscription_available));
}
@Test
public void getSummary_twoSubsOneDefaultForEverythingDataNotActive() {
final SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
final SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(11);
when(sub2.getSubscriptionId()).thenReturn(22);
SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(sub1, sub2));
ShadowSubscriptionManager.setDefaultDataSubscriptionId(11);
ShadowSubscriptionManager.setDefaultSmsSubscriptionId(11);
ShadowSubscriptionManager.setDefaultVoiceSubscriptionId(11);
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
assertThat(mController.getSummary(11)).isEqualTo(
mContext.getString(R.string.default_for_calls_and_sms) + System.lineSeparator()
+ mContext.getString(R.string.default_for_mobile_data));
assertThat(mController.getSummary(22)).isEqualTo(
mContext.getString(R.string.subscription_available));
}
@Test
public void getSummary_twoSubsOneDefaultForEverythingDataDisabled() {
final SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
@@ -329,7 +363,6 @@ public class SubscriptionsPreferenceControllerTest {
ShadowSubscriptionManager.setDefaultVoiceSubscriptionId(11);
ShadowSubscriptionManager.setDefaultSmsSubscriptionId(11);
ShadowSubscriptionManager.setDefaultDataSubscriptionId(11);
when(mTelephonyManager.getDataState()).thenReturn(TelephonyManager.DATA_DISCONNECTED);
when(mTelephonyManager.isDataEnabled()).thenReturn(false);
assertThat(mController.getSummary(11)).isEqualTo(
@@ -351,7 +384,6 @@ public class SubscriptionsPreferenceControllerTest {
ShadowSubscriptionManager.setDefaultDataSubscriptionId(11);
ShadowSubscriptionManager.setDefaultSmsSubscriptionId(22);
ShadowSubscriptionManager.setDefaultVoiceSubscriptionId(11);
when(mTelephonyManager.getDataState()).thenReturn(TelephonyManager.DATA_DISCONNECTED);
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
assertThat(mController.getSummary(11)).isEqualTo(