diff options
author | Ted Wang <tedwang@google.com> | 2020-09-01 14:45:46 +0800 |
---|---|---|
committer | Ted Wang <tedwang@google.com> | 2020-09-10 06:15:41 +0000 |
commit | 47f2ebb1af7c9268bed98cb9381b51db36a56c2d (patch) | |
tree | cc2620f3b718b8f0b93fbbc611079764644fa700 /tests/robotests/src | |
parent | e8b877e3ee8863f5a58ba7635268bb4665492da8 (diff) | |
download | packages_apps_Settings-47f2ebb1af7c9268bed98cb9381b51db36a56c2d.tar.gz packages_apps_Settings-47f2ebb1af7c9268bed98cb9381b51db36a56c2d.tar.bz2 packages_apps_Settings-47f2ebb1af7c9268bed98cb9381b51db36a56c2d.zip |
Update state when there is Bluetooth tethering state changed
Monitor Bluetooth Tethering state and update preference when there is
Bluetooth tethering state change.
Add test case to verify when recieve
BluetoothPan.TETHERING_STATE_CHANGED will update bluetooth state
Bug: 138688805
Test: make RunSettingsRoboTests
Change-Id: I8f468d1d99ed6d87f6cd8305ef56b0d7a5dec95b
Diffstat (limited to 'tests/robotests/src')
-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); |