Add page listing multiple mobile networks

When a device supports simultaneous connection to multiple mobile
networks, we want the "Mobile network" entry on the Network & internet
page to list the number of active SIMs and when clicked, go to a page
that lists them all.

Bug: 116349402
Test: make RunSettingsRoboTests
Change-Id: Ie642d7801cda07dcbbe74d42c234db6605566be4
This commit is contained in:
Antony Sargent
2019-01-11 13:11:57 -08:00
parent de324b9b70
commit f4ee4ef3bf
9 changed files with 763 additions and 7 deletions

View File

@@ -0,0 +1,152 @@
/*
* 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 android.provider.Settings.EXTRA_SUB_ID;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.SubscriptionInfo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class MobileNetworkListControllerTest {
@Mock
private Lifecycle mLifecycle;
@Mock
private PreferenceScreen mPreferenceScreen;
private Context mContext;
private MobileNetworkListController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(Robolectric.setupActivity(Activity.class));
when(mPreferenceScreen.getContext()).thenReturn(mContext);
mController = new MobileNetworkListController(mContext, mLifecycle);
}
@After
public void tearDown() {
SubscriptionUtil.setAvailableSubscriptionsForTesting(null);
}
@Test
public void displayPreference_noSubscriptions_noCrash() {
mController.displayPreference(mPreferenceScreen);
mController.onResume();
}
@Test
public void displayPreference_twoSubscriptions_correctlySetup() {
final SubscriptionInfo sub1 = createMockSubscription(1, "sub1");
final SubscriptionInfo sub2 = createMockSubscription(2, "sub2");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
// Check that the preferences get created with the correct titles.
final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
Preference.class);
verify(mPreferenceScreen, times(2)).addPreference(preferenceCaptor.capture());
final Preference pref1 = preferenceCaptor.getAllValues().get(0);
final Preference pref2 = preferenceCaptor.getAllValues().get(1);
assertThat(pref1.getTitle()).isEqualTo("sub1");
assertThat(pref2.getTitle()).isEqualTo("sub2");
// Check that the onclick listeners are setup to fire with the right subscription id.
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
pref1.getOnPreferenceClickListener().onPreferenceClick(pref1);
pref2.getOnPreferenceClickListener().onPreferenceClick(pref2);
verify(mContext, times(2)).startActivity(intentCaptor.capture());
final Intent intent1 = intentCaptor.getAllValues().get(0);
final Intent intent2 = intentCaptor.getAllValues().get(1);
assertThat(intent1.getIntExtra(EXTRA_SUB_ID, INVALID_SUBSCRIPTION_ID)).isEqualTo(1);
assertThat(intent2.getIntExtra(EXTRA_SUB_ID, INVALID_SUBSCRIPTION_ID)).isEqualTo(2);
}
@Test
public void onSubscriptionsChanged_twoSubscriptionsOneChangesName_preferenceUpdated() {
final SubscriptionInfo sub1 = createMockSubscription(1, "sub1");
final SubscriptionInfo sub2 = createMockSubscription(2, "sub2");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
Preference.class);
verify(mPreferenceScreen, times(2)).addPreference(preferenceCaptor.capture());
when(sub2.getDisplayName()).thenReturn("new name");
mController.onSubscriptionsChanged();
assertThat(preferenceCaptor.getAllValues().get(1).getTitle()).isEqualTo("new name");
}
@Test
public void onSubscriptionsChanged_startWithThreeSubsAndRemoveOne_correctPreferenceRemoved() {
final SubscriptionInfo sub1 = createMockSubscription(1, "sub1");
final SubscriptionInfo sub2 = createMockSubscription(2, "sub2");
final SubscriptionInfo sub3 = createMockSubscription(3, "sub3");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2, sub3));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
Preference.class);
verify(mPreferenceScreen, times(3)).addPreference(preferenceCaptor.capture());
// remove sub2, and check that the second pref was removed from the screen
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub3));
mController.onSubscriptionsChanged();
final ArgumentCaptor<Preference> removedPrefCaptor = ArgumentCaptor.forClass(
Preference.class);
verify(mPreferenceScreen).removePreference(removedPrefCaptor.capture());
assertThat(removedPrefCaptor.getValue().getTitle()).isEqualTo("sub2");
}
private SubscriptionInfo createMockSubscription(int id, String displayName) {
final SubscriptionInfo sub = mock(SubscriptionInfo.class);
when(sub.getSubscriptionId()).thenReturn(id);
when(sub.getDisplayName()).thenReturn(displayName);
return sub;
}
}

View File

@@ -0,0 +1,171 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.SubscriptionInfo;
import com.android.settings.network.telephony.MobileNetworkActivity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class MobileNetworkSummaryControllerTest {
@Mock
private Lifecycle mLifecycle;
@Mock
PreferenceScreen mPreferenceScreen;
Preference mPreference;
private Context mContext;
private MobileNetworkSummaryController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(Robolectric.setupActivity(Activity.class));
mController = new MobileNetworkSummaryController(mContext, mLifecycle);
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey());
when(mPreferenceScreen.findPreference(eq(mController.getPreferenceKey()))).thenReturn(
mPreference);
}
@After
public void tearDown() {
SubscriptionUtil.setAvailableSubscriptionsForTesting(null);
}
@Test
public void getSummary_noSubscriptions_correctSummary() {
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("Add a network");
}
@Test
public void getSummary_oneSubscription_correctSummaryAndClickHandler() {
SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(1);
when(sub1.getDisplayName()).thenReturn("sub1");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("sub1");
assertThat(mPreference.getFragment()).isNull();
mPreference.getOnPreferenceClickListener().onPreferenceClick(mPreference);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(intentCaptor.capture());
assertThat(intentCaptor.getValue().getComponent().getClassName()).isEqualTo(
MobileNetworkActivity.class.getName());
}
@Test
public void getSummary_twoSubscriptions_correctSummaryAndFragment() {
SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(1);
when(sub2.getSubscriptionId()).thenReturn(2);
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("2 SIMs");
assertThat(mPreference.getFragment()).isEqualTo(MobileNetworkListFragment.class.getName());
}
@Test
public void getSummaryAfterUpdate_twoSubscriptionsBecomesOne_correctSummaryAndFragment() {
SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(1);
when(sub2.getSubscriptionId()).thenReturn(2);
when(sub1.getDisplayName()).thenReturn("sub1");
when(sub2.getDisplayName()).thenReturn("sub2");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("2 SIMs");
assertThat(mPreference.getFragment()).isEqualTo(MobileNetworkListFragment.class.getName());
// Simulate sub2 having disappeared - the end result should change to be the same as
// if there were just one subscription.
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1));
mController.onSubscriptionsChanged();
assertThat(mController.getSummary()).isEqualTo("sub1");
assertThat(mPreference.getFragment()).isNull();
mPreference.getOnPreferenceClickListener().onPreferenceClick(mPreference);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(intentCaptor.capture());
assertThat(intentCaptor.getValue().getComponent().getClassName()).isEqualTo(
MobileNetworkActivity.class.getName());
}
@Test
public void getSummaryAfterUpdate_oneSubscriptionBecomesTwo_correctSummaryAndFragment() {
SubscriptionInfo sub1 = mock(SubscriptionInfo.class);
SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
when(sub1.getSubscriptionId()).thenReturn(1);
when(sub2.getSubscriptionId()).thenReturn(2);
when(sub1.getDisplayName()).thenReturn("sub1");
when(sub2.getDisplayName()).thenReturn("sub2");
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("sub1");
assertThat(mPreference.getFragment()).isNull();
mPreference.getOnPreferenceClickListener().onPreferenceClick(mPreference);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(intentCaptor.capture());
assertThat(intentCaptor.getValue().getComponent().getClassName()).isEqualTo(
MobileNetworkActivity.class.getName());
// Simulate sub2 appearing in the list of subscriptions and check the results.
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2));
mController.displayPreference(mPreferenceScreen);
mController.onResume();
assertThat(mController.getSummary()).isEqualTo("2 SIMs");
assertThat(mPreference.getFragment()).isEqualTo(MobileNetworkListFragment.class.getName());
}
}