[Settings] Create a proxy for SubscriptionManager
There're many access from Settings app to SubscriptionManager. Lots of them are checking for similar information. Create a proxy can reduce the number of inter-process access from Settings app to SubscriptionManager, and improve Settings app's UI performance. Bug: 141833767 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=GlobalSettingsChangeListenerTest Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=ActiveSubsciptionsListenerTest Change-Id: I3591c478045500a97245955767a7ddd5d361da77
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* 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.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Looper;
|
||||
import android.telephony.CarrierConfigManager;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.telephony.TelephonyIntents;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
import org.robolectric.shadows.ShadowBroadcastReceiver;
|
||||
import org.robolectric.shadows.ShadowContextImpl;
|
||||
import org.robolectric.shadows.ShadowSubscriptionManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ActiveSubsciptionsListenerTest {
|
||||
private static final int SUB_ID1 = 3;
|
||||
private static final int SUB_ID2 = 7;
|
||||
|
||||
private static final Intent INTENT_RADIO_TECHNOLOGY_CHANGED =
|
||||
new Intent(TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
|
||||
|
||||
private static final Intent INTENT_MULTI_SIM_CONFIG_CHANGED =
|
||||
new Intent(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
|
||||
|
||||
private static final Intent INTENT_CARRIER_CONFIG_CHANGED =
|
||||
new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
|
||||
|
||||
private Context mContext;
|
||||
private ShadowContextImpl mShadowContextImpl;
|
||||
private SubscriptionManager mSubscriptionManager;
|
||||
private ShadowSubscriptionManager mShadowSubscriptionManager;
|
||||
private List<SubscriptionInfo> mActiveSubscriptions;
|
||||
|
||||
private ActiveSubsciptionsListenerImpl mListener;
|
||||
private BroadcastReceiver mReceiver;
|
||||
private ShadowBroadcastReceiver mShadowReceiver;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application.getBaseContext();
|
||||
mShadowContextImpl = Shadow.extract(mContext);
|
||||
|
||||
mSubscriptionManager = spy(mContext.getSystemService(SubscriptionManager.class));
|
||||
mShadowSubscriptionManager = shadowOf(mSubscriptionManager);
|
||||
|
||||
mActiveSubscriptions = new ArrayList<SubscriptionInfo>();
|
||||
mActiveSubscriptions.add(ShadowSubscriptionManager.SubscriptionInfoBuilder
|
||||
.newBuilder().setId(SUB_ID1).buildSubscriptionInfo());
|
||||
mActiveSubscriptions.add(ShadowSubscriptionManager.SubscriptionInfoBuilder
|
||||
.newBuilder().setId(SUB_ID2).buildSubscriptionInfo());
|
||||
mShadowSubscriptionManager.setActiveSubscriptionInfoList(mActiveSubscriptions);
|
||||
|
||||
mListener = spy(new ActiveSubsciptionsListenerImpl(Looper.getMainLooper(), mContext));
|
||||
doReturn(mSubscriptionManager).when(mListener).getSubscriptionManager();
|
||||
mReceiver = mListener.getSubscriptionChangeReceiver();
|
||||
mShadowReceiver = shadowOf(mReceiver);
|
||||
doReturn(mReceiver).when(mListener).getSubscriptionChangeReceiver();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
mListener.close();
|
||||
}
|
||||
|
||||
private class ActiveSubsciptionsListenerImpl extends ActiveSubsciptionsListener {
|
||||
private ActiveSubsciptionsListenerImpl(Looper looper, Context context) {
|
||||
super(looper, context);
|
||||
}
|
||||
public void onChanged() {}
|
||||
}
|
||||
|
||||
private void sendIntentToReceiver(Intent intent) {
|
||||
mShadowReceiver.onReceive(mContext, intent, new AtomicBoolean(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructor_noListeningWasSetup() {
|
||||
verify(mListener, never()).onChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void start_configChangedIntent_onChangedShouldBeCalled() {
|
||||
sendIntentToReceiver(INTENT_RADIO_TECHNOLOGY_CHANGED);
|
||||
sendIntentToReceiver(INTENT_MULTI_SIM_CONFIG_CHANGED);
|
||||
verify(mListener, never()).onChanged();
|
||||
|
||||
mListener.start();
|
||||
|
||||
sendIntentToReceiver(INTENT_RADIO_TECHNOLOGY_CHANGED);
|
||||
verify(mListener, times(1)).onChanged();
|
||||
|
||||
sendIntentToReceiver(INTENT_MULTI_SIM_CONFIG_CHANGED);
|
||||
verify(mListener, times(2)).onChanged();
|
||||
|
||||
mListener.stop();
|
||||
|
||||
sendIntentToReceiver(INTENT_RADIO_TECHNOLOGY_CHANGED);
|
||||
sendIntentToReceiver(INTENT_MULTI_SIM_CONFIG_CHANGED);
|
||||
verify(mListener, times(2)).onChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void start_carrierConfigChangedIntent_onChangedWhenSubIdBeenCached() {
|
||||
sendIntentToReceiver(INTENT_CARRIER_CONFIG_CHANGED);
|
||||
verify(mListener, never()).onChanged();
|
||||
|
||||
mListener.start();
|
||||
|
||||
mListener.getActiveSubscriptionsInfo();
|
||||
|
||||
sendIntentToReceiver(INTENT_CARRIER_CONFIG_CHANGED);
|
||||
verify(mListener, never()).onChanged();
|
||||
|
||||
INTENT_CARRIER_CONFIG_CHANGED.putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX,
|
||||
SUB_ID2);
|
||||
sendIntentToReceiver(INTENT_CARRIER_CONFIG_CHANGED);
|
||||
verify(mListener, times(1)).onChanged();
|
||||
|
||||
mListener.stop();
|
||||
|
||||
sendIntentToReceiver(INTENT_CARRIER_CONFIG_CHANGED);
|
||||
verify(mListener, times(1)).onChanged();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void start_alwaysFetchAndCacheResult() {
|
||||
mListener.start();
|
||||
|
||||
List<SubscriptionInfo> subInfoList = null;
|
||||
int numberOfAccess = 0;
|
||||
for (int numberOfSubInfo = mActiveSubscriptions.size(); numberOfSubInfo >= 0;
|
||||
numberOfSubInfo--) {
|
||||
if (mActiveSubscriptions.size() > numberOfSubInfo) {
|
||||
mActiveSubscriptions.remove(numberOfSubInfo);
|
||||
}
|
||||
mShadowSubscriptionManager.setActiveSubscriptionInfoList(mActiveSubscriptions);
|
||||
|
||||
// fetch twice and test if they generated access to SubscriptionManager only once
|
||||
subInfoList = mListener.getActiveSubscriptionsInfo();
|
||||
subInfoList = mListener.getActiveSubscriptionsInfo();
|
||||
|
||||
numberOfAccess++;
|
||||
verify(mSubscriptionManager, times(numberOfAccess)).getActiveSubscriptionInfoList();
|
||||
|
||||
mListener.clearCache();
|
||||
}
|
||||
|
||||
mShadowSubscriptionManager.setActiveSubscriptionInfoList(null);
|
||||
|
||||
// fetch twice and test if they generated access to SubscriptionManager only once
|
||||
subInfoList = mListener.getActiveSubscriptionsInfo();
|
||||
subInfoList = mListener.getActiveSubscriptionsInfo();
|
||||
|
||||
numberOfAccess++;
|
||||
verify(mSubscriptionManager, times(numberOfAccess)).getActiveSubscriptionInfoList();
|
||||
}
|
||||
}
|
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.doNothing;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
|
||||
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 GlobalSettingsChangeListenerTest {
|
||||
|
||||
@Mock
|
||||
private Lifecycle mLifecycle;
|
||||
|
||||
private Context mContext;
|
||||
private GlobalSettingsChangeListener mListener;
|
||||
|
||||
private static final String SETTINGS_FIELD = Settings.Global.AIRPLANE_MODE_ON;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mListener = spy(new GlobalSettingsChangeListener(Looper.getMainLooper(),
|
||||
mContext, SETTINGS_FIELD) {
|
||||
public void onChanged(String field) {}
|
||||
});
|
||||
|
||||
doNothing().when(mLifecycle).addObserver(mListener);
|
||||
doNothing().when(mLifecycle).removeObserver(mListener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChanged_onChangedBeenCalled() {
|
||||
mListener.onChange(false);
|
||||
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotifyChangeBasedOnLifecycle_onStopEvent_onChangedNotCalled() {
|
||||
mListener.notifyChangeBasedOn(mLifecycle);
|
||||
mListener.onStart();
|
||||
|
||||
mListener.onChange(false);
|
||||
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
|
||||
|
||||
mListener.onStop();
|
||||
|
||||
mListener.onChange(false);
|
||||
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
|
||||
|
||||
mListener.onStart();
|
||||
|
||||
mListener.onChange(false);
|
||||
verify(mListener, times(2)).onChanged(SETTINGS_FIELD);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user