Avoid ANR of TetherPreferenceController
Off load the following work from main thread, - Calculate title - Calculate summery This also helps improve the latency. Also migrate to registerTetheringEventCallback() since TetheringManager.ACTION_TETHER_STATE_CHANGED is deprecated. Fix: 311848767 Test: manual - on Network & internet page and turn on / off tethering Test: unit tests Change-Id: I6ee182b41ef51f691ea31938142be1a41faf5573
This commit is contained in:
@@ -1,212 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothPan;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.TetheringManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
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;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class TetherPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private TetheringManager mTetheringManager;
|
||||
@Mock
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
@Mock
|
||||
private Preference mPreference;
|
||||
|
||||
private TetherPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
doReturn(null).when(mContext)
|
||||
.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
mController = spy(new TetherPreferenceController(mContext, /* lifecycle= */ null));
|
||||
ReflectionHelpers.setField(mController, "mContext", mContext);
|
||||
ReflectionHelpers.setField(mController, "mTetheringManager", mTetheringManager);
|
||||
ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter);
|
||||
ReflectionHelpers.setField(mController, "mPreference", mPreference);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifeCycle_onCreate_shouldInitBluetoothPan() {
|
||||
when(mBluetoothAdapter.getState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||
mController.onCreate(null);
|
||||
|
||||
verify(mBluetoothAdapter).getState();
|
||||
verify(mBluetoothAdapter).getProfileProxy(mContext, mController.mBtProfileServiceListener,
|
||||
BluetoothProfile.PAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lifeCycle_onCreate_shouldNotInitBluetoothPanWhenBluetoothOff() {
|
||||
when(mBluetoothAdapter.getState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
||||
mController.onCreate(null);
|
||||
|
||||
verify(mBluetoothAdapter).getState();
|
||||
verifyNoMoreInteractions(mBluetoothAdapter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void goThroughLifecycle_shouldDestoryBluetoothProfile() {
|
||||
final BluetoothPan pan = mock(BluetoothPan.class);
|
||||
final AtomicReference<BluetoothPan> panRef =
|
||||
ReflectionHelpers.getField(mController, "mBluetoothPan");
|
||||
panRef.set(pan);
|
||||
|
||||
mController.onDestroy();
|
||||
|
||||
verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSummary_noPreference_noInteractionWithTetheringManager() {
|
||||
ReflectionHelpers.setField(mController, "mPreference", null);
|
||||
mController.updateSummary();
|
||||
verifyNoMoreInteractions(mTetheringManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSummary_wifiTethered_shouldShowHotspotMessage() {
|
||||
when(mTetheringManager.getTetheredIfaces()).thenReturn(new String[]{"123"});
|
||||
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"123"});
|
||||
|
||||
mController.updateSummary();
|
||||
verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_off);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSummary_btThetherOn_shouldShowTetherMessage() {
|
||||
when(mTetheringManager.getTetheredIfaces()).thenReturn(new String[]{"123"});
|
||||
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"});
|
||||
|
||||
mController.updateSummary();
|
||||
verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_off_tether_on);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void updateSummary_tetherOff_shouldShowTetherOffMessage() {
|
||||
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"123"});
|
||||
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"});
|
||||
|
||||
mController.updateSummary();
|
||||
verify(mPreference).setSummary(R.string.switch_off_text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSummary_wifiBtTetherOn_shouldShowHotspotAndTetherMessage() {
|
||||
when(mTetheringManager.getTetheredIfaces()).thenReturn(new String[]{"123", "456"});
|
||||
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"456"});
|
||||
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[]{"23"});
|
||||
|
||||
mController.updateSummary();
|
||||
verify(mPreference).setSummary(R.string.tether_settings_summary_hotspot_on_tether_on);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void airplaneModeOn_shouldUpdateSummaryToOff() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
ReflectionHelpers.setField(mController, "mContext", context);
|
||||
|
||||
Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
|
||||
|
||||
mController.onResume();
|
||||
|
||||
verifyNoInteractions(mPreference);
|
||||
|
||||
Settings.Global.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
|
||||
|
||||
final ContentObserver observer =
|
||||
ReflectionHelpers.getField(mController, "mAirplaneModeObserver");
|
||||
observer.onChange(true, Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON));
|
||||
|
||||
verify(mPreference).setSummary(R.string.switch_off_text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onResume_shouldRegisterTetherReceiver() {
|
||||
when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
|
||||
|
||||
mController.onResume();
|
||||
|
||||
verify(mContext).registerReceiver(
|
||||
any(TetherPreferenceController.TetherBroadcastReceiver.class),
|
||||
any(IntentFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPause_shouldUnregisterTetherReceiver() {
|
||||
when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
|
||||
mController.onResume();
|
||||
|
||||
mController.onPause();
|
||||
|
||||
verify(mContext)
|
||||
.unregisterReceiver(any(TetherPreferenceController.TetherBroadcastReceiver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tetherStatesChanged_shouldUpdateSummary() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
ReflectionHelpers.setField(mController, "mContext", context);
|
||||
mController.onResume();
|
||||
|
||||
context.sendBroadcast(new Intent(TetheringManager.ACTION_TETHER_STATE_CHANGED));
|
||||
|
||||
shadowMainLooper().idle();
|
||||
verify(mController).updateSummary();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 android.content.Context
|
||||
import android.net.TetheringManager
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.android.dx.mockito.inline.extended.ExtendedMockito
|
||||
import com.android.settings.R
|
||||
import com.android.settings.core.BasePreferenceController
|
||||
import com.android.settingslib.TetherUtil
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.MockitoSession
|
||||
import org.mockito.quality.Strictness
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class TetherPreferenceControllerTest {
|
||||
private lateinit var mockSession: MockitoSession
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
|
||||
private val controller = TetherPreferenceController(context, TEST_KEY)
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
mockSession = ExtendedMockito.mockitoSession()
|
||||
.initMocks(this)
|
||||
.mockStatic(TetherUtil::class.java)
|
||||
.strictness(Strictness.LENIENT)
|
||||
.startMocking()
|
||||
|
||||
ExtendedMockito.doReturn(true).`when` { TetherUtil.isTetherAvailable(context) }
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
mockSession.finishMocking()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getAvailabilityStatus_whenTetherAvailable() {
|
||||
ExtendedMockito.doReturn(true).`when` { TetherUtil.isTetherAvailable(context) }
|
||||
|
||||
val availabilityStatus = controller.availabilityStatus
|
||||
|
||||
assertThat(availabilityStatus).isEqualTo(BasePreferenceController.AVAILABLE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getAvailabilityStatus_whenTetherNotAvailable() {
|
||||
ExtendedMockito.doReturn(false).`when` { TetherUtil.isTetherAvailable(context) }
|
||||
|
||||
val availabilityStatus = controller.availabilityStatus
|
||||
|
||||
assertThat(availabilityStatus).isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getSummaryResId_bothWifiAndBluetoothOn() {
|
||||
val summaryResId = controller.getSummaryResId(
|
||||
setOf(TetheringManager.TETHERING_WIFI, TetheringManager.TETHERING_BLUETOOTH)
|
||||
)
|
||||
|
||||
assertThat(summaryResId).isEqualTo(R.string.tether_settings_summary_hotspot_on_tether_on)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getSummaryResId_onlyWifiHotspotOn() {
|
||||
val summaryResId = controller.getSummaryResId(setOf(TetheringManager.TETHERING_WIFI))
|
||||
|
||||
assertThat(summaryResId).isEqualTo(R.string.tether_settings_summary_hotspot_on_tether_off)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getSummaryResId_onlyBluetoothTetheringOn() {
|
||||
val summaryResId = controller.getSummaryResId(setOf(TetheringManager.TETHERING_BLUETOOTH))
|
||||
|
||||
assertThat(summaryResId).isEqualTo(R.string.tether_settings_summary_hotspot_off_tether_on)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getSummaryResId_allOff() {
|
||||
val summaryResId = controller.getSummaryResId(emptySet())
|
||||
|
||||
assertThat(summaryResId).isEqualTo(R.string.tether_preference_summary_off)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TEST_KEY = "test_key"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.bluetooth.BluetoothPan
|
||||
import android.bluetooth.BluetoothProfile
|
||||
import android.content.Context
|
||||
import android.net.TetheringInterface
|
||||
import android.net.TetheringManager
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doAnswer
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.eq
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.stub
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class TetheredRepositoryTest {
|
||||
|
||||
private var tetheringInterfaces: Set<TetheringInterface> = emptySet()
|
||||
|
||||
private var tetheringEventCallback: TetheringManager.TetheringEventCallback? = null
|
||||
|
||||
private val mockTetheringManager = mock<TetheringManager> {
|
||||
on { registerTetheringEventCallback(any(), any()) } doAnswer {
|
||||
tetheringEventCallback = it.arguments[1] as TetheringManager.TetheringEventCallback
|
||||
tetheringEventCallback?.onTetheredInterfacesChanged(tetheringInterfaces)
|
||||
}
|
||||
}
|
||||
|
||||
private val mockBluetoothPan = mock<BluetoothPan> {
|
||||
on { isTetheringOn } doReturn false
|
||||
}
|
||||
|
||||
private val mockBluetoothAdapter = mock<BluetoothAdapter> {
|
||||
on { getProfileProxy(any(), any(), eq(BluetoothProfile.PAN)) } doAnswer {
|
||||
val listener = it.arguments[1] as BluetoothProfile.ServiceListener
|
||||
listener.onServiceConnected(BluetoothProfile.PAN, mockBluetoothPan)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private val mockBluetoothManager = mock<BluetoothManager> {
|
||||
on { adapter } doReturn mockBluetoothAdapter
|
||||
}
|
||||
|
||||
private val context = mock<Context> {
|
||||
on { getSystemService(TetheringManager::class.java) } doReturn mockTetheringManager
|
||||
on { getSystemService(BluetoothManager::class.java) } doReturn mockBluetoothManager
|
||||
}
|
||||
|
||||
private val repository = TetheredRepository(context)
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_allOff() = runBlocking {
|
||||
val tetheredTypes = repository.tetheredTypesFlow().firstWithTimeoutOrNull()
|
||||
|
||||
assertThat(tetheredTypes).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_wifiHotspotOn(): Unit = runBlocking {
|
||||
tetheringInterfaces = setOf(TetheringInterface(TetheringManager.TETHERING_WIFI, ""))
|
||||
|
||||
val tetheredTypes = repository.tetheredTypesFlow().firstWithTimeoutOrNull()
|
||||
|
||||
assertThat(tetheredTypes).containsExactly(TetheringManager.TETHERING_WIFI)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_usbTetheringTurnOnLater(): Unit = runBlocking {
|
||||
val tetheredTypeDeferred = async {
|
||||
repository.tetheredTypesFlow().mapNotNull {
|
||||
it.singleOrNull()
|
||||
}.firstWithTimeoutOrNull()
|
||||
}
|
||||
delay(100)
|
||||
|
||||
tetheringEventCallback?.onTetheredInterfacesChanged(
|
||||
setOf(TetheringInterface(TetheringManager.TETHERING_USB, ""))
|
||||
)
|
||||
|
||||
assertThat(tetheredTypeDeferred.await()).isEqualTo(TetheringManager.TETHERING_USB)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_bluetoothOff(): Unit = runBlocking {
|
||||
mockBluetoothAdapter.stub {
|
||||
on { state } doReturn BluetoothAdapter.STATE_OFF
|
||||
}
|
||||
|
||||
val tetheredTypes = repository.tetheredTypesFlow().firstWithTimeoutOrNull()
|
||||
|
||||
assertThat(tetheredTypes).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_bluetoothOnTetheringOff(): Unit = runBlocking {
|
||||
mockBluetoothAdapter.stub {
|
||||
on { state } doReturn BluetoothAdapter.STATE_ON
|
||||
}
|
||||
|
||||
val tetheredTypes = repository.tetheredTypesFlow().firstWithTimeoutOrNull()
|
||||
|
||||
assertThat(tetheredTypes).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun tetheredTypesFlow_bluetoothTetheringOn(): Unit = runBlocking {
|
||||
mockBluetoothAdapter.stub {
|
||||
on { state } doReturn BluetoothAdapter.STATE_ON
|
||||
}
|
||||
mockBluetoothPan.stub {
|
||||
on { isTetheringOn } doReturn true
|
||||
}
|
||||
|
||||
val tetheredTypes = repository.tetheredTypesFlow().firstWithTimeoutOrNull()
|
||||
|
||||
assertThat(tetheredTypes).containsExactly(TetheringManager.TETHERING_BLUETOOTH)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user