diff options
Diffstat (limited to 'tests/robotests/src/com/android/settings/TetherSettingsTest.java')
-rw-r--r-- | tests/robotests/src/com/android/settings/TetherSettingsTest.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/robotests/src/com/android/settings/TetherSettingsTest.java b/tests/robotests/src/com/android/settings/TetherSettingsTest.java index f46ca7a1cd..be14eda9b9 100644 --- a/tests/robotests/src/com/android/settings/TetherSettingsTest.java +++ b/tests/robotests/src/com/android/settings/TetherSettingsTest.java @@ -18,6 +18,8 @@ package com.android.settings; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -25,20 +27,28 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothPan; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.os.UserHandle; import android.os.UserManager; import android.util.FeatureFlagUtils; +import androidx.fragment.app.FragmentActivity; import androidx.preference.Preference; +import androidx.preference.SwitchPreference; import com.android.settings.core.FeatureFlags; 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.RobolectricTestRunner; @@ -160,6 +170,82 @@ public class TetherSettingsTest { verify(mockPreference).setTitle(R.string.tethering_footer_info_sta_ap_concurrency); } + @Test + public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOn() { + final TetherSettings spyTetherSettings = spy(new TetherSettings()); + when(spyTetherSettings.getContext()).thenReturn(mContext); + final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class); + when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING)) + .thenReturn(mockSwitchPreference); + final FragmentActivity mockActivity = mock(FragmentActivity.class); + when(spyTetherSettings.getActivity()).thenReturn(mockActivity); + final ArgumentCaptor<BroadcastReceiver> captor = + ArgumentCaptor.forClass(BroadcastReceiver.class); + when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class))) + .thenReturn(null); + // Bluetooth tethering state is on + when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); + when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(true); + + spyTetherSettings.setupTetherPreference(); + spyTetherSettings.registerReceiver(); + updateOnlyBluetoothState(spyTetherSettings); + + // Simulate Bluetooth tethering state changed + final BroadcastReceiver receiver = captor.getValue(); + final Intent bluetoothTetheringOn = new Intent(BluetoothPan.ACTION_TETHERING_STATE_CHANGED); + bluetoothTetheringOn.putExtra(BluetoothPan.EXTRA_TETHERING_STATE, + BluetoothPan.TETHERING_STATE_ON); + receiver.onReceive(mockActivity, bluetoothTetheringOn); + + verify(mockSwitchPreference).setEnabled(true); + verify(mockSwitchPreference).setChecked(true); + } + + @Test + public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOff() { + final TetherSettings spyTetherSettings = spy(new TetherSettings()); + when(spyTetherSettings.getContext()).thenReturn(mContext); + final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class); + when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING)) + .thenReturn(mockSwitchPreference); + final FragmentActivity mockActivity = mock(FragmentActivity.class); + when(spyTetherSettings.getActivity()).thenReturn(mockActivity); + final ArgumentCaptor<BroadcastReceiver> captor = + ArgumentCaptor.forClass(BroadcastReceiver.class); + when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class))) + .thenReturn(null); + // Bluetooth tethering state is off + when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON); + when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(false); + + spyTetherSettings.setupTetherPreference(); + spyTetherSettings.registerReceiver(); + updateOnlyBluetoothState(spyTetherSettings); + + // Simulate Bluetooth tethering state changed + final BroadcastReceiver receiver = captor.getValue(); + final Intent bluetoothTetheringOn = new Intent(BluetoothPan.ACTION_TETHERING_STATE_CHANGED); + bluetoothTetheringOn.putExtra(BluetoothPan.EXTRA_TETHERING_STATE, + BluetoothPan.TETHERING_STATE_ON); + receiver.onReceive(mockActivity, bluetoothTetheringOn); + + verify(mockSwitchPreference).setEnabled(true); + verify(mockSwitchPreference).setChecked(false); + } + + private void updateOnlyBluetoothState(TetherSettings tetherSettings) { + doReturn(mConnectivityManager).when(tetherSettings) + .getSystemService(Context.CONNECTIVITY_SERVICE); + when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]); + when(mConnectivityManager.getTetheredIfaces()).thenReturn(new String[0]); + when(mConnectivityManager.getTetheringErroredIfaces()).thenReturn(new String[0]); + doNothing().when(tetherSettings).updateUsbState(any(String[].class), any(String[].class), + any(String[].class)); + doNothing().when(tetherSettings).updateEthernetState(any(String[].class), + any(String[].class)); + } + private void setupIsTetherAvailable(boolean returnValue) { when(mConnectivityManager.isTetheringSupported()).thenReturn(true); |