Merge "Update state when there is Bluetooth tethering state changed" into rvc-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
e0e283cb4c
@@ -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,27 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
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 +169,80 @@ 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 bluetoothTetheringChanged =
|
||||
new Intent(TetherSettings.BLUETOOTH_TETHERING_STATE_CHANGED);
|
||||
receiver.onReceive(mockActivity, bluetoothTetheringChanged);
|
||||
|
||||
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 bluetoothTetheringChanged =
|
||||
new Intent(TetherSettings.BLUETOOTH_TETHERING_STATE_CHANGED);
|
||||
receiver.onReceive(mockActivity, bluetoothTetheringChanged);
|
||||
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user