Merge "Use BluetoothAdapter instead of LocalBluetoothAdapter"
This commit is contained in:
committed by
Android (Google) Code Review
commit
1b47c056b2
@@ -17,41 +17,41 @@ package com.android.settings.bluetooth;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class AlwaysDiscoverableTest {
|
||||
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
|
||||
@Mock
|
||||
private Context mContext;
|
||||
|
||||
private AlwaysDiscoverable mAlwaysDiscoverable;
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
|
||||
mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,14 +76,15 @@ public class AlwaysDiscoverableTest {
|
||||
public void stopWithoutStart() {
|
||||
mAlwaysDiscoverable.stop();
|
||||
// expect no crash
|
||||
verify(mLocalAdapter, never()).setScanMode(anyInt());
|
||||
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startSetsModeAndRegistersReceiver() {
|
||||
when(mLocalAdapter.getScanMode()).thenReturn(BluetoothAdapter.SCAN_MODE_NONE);
|
||||
mShadowBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE);
|
||||
mAlwaysDiscoverable.start();
|
||||
verify(mLocalAdapter).setScanMode(eq(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
|
||||
assertThat(mBluetoothAdapter.getScanMode())
|
||||
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
verify(mContext).registerReceiver(eq(mAlwaysDiscoverable), any());
|
||||
}
|
||||
|
||||
@@ -97,18 +98,18 @@ public class AlwaysDiscoverableTest {
|
||||
@Test
|
||||
public void resetsToDiscoverableModeWhenScanModeChanges() {
|
||||
mAlwaysDiscoverable.start();
|
||||
verify(mLocalAdapter, times(1))
|
||||
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
assertThat(mBluetoothAdapter.getScanMode())
|
||||
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
|
||||
sendScanModeChangedIntent(BluetoothAdapter.SCAN_MODE_CONNECTABLE,
|
||||
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
|
||||
verify(mLocalAdapter, times(2))
|
||||
.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
assertThat(mBluetoothAdapter.getScanMode())
|
||||
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
}
|
||||
|
||||
private void sendScanModeChangedIntent(int newMode, int previousMode) {
|
||||
when(mLocalAdapter.getScanMode()).thenReturn(newMode);
|
||||
mShadowBluetoothAdapter.setScanMode(newMode);
|
||||
Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
||||
intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
|
||||
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);
|
||||
|
@@ -24,22 +24,25 @@ 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.Context;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
|
||||
private static final String DEVICE_NAME = "Nightshade";
|
||||
@@ -48,8 +51,6 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
private Preference mPreference;
|
||||
|
||||
@@ -64,8 +65,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
when(mPreferenceScreen.getContext()).thenReturn(mContext);
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(KEY_DEVICE_NAME);
|
||||
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter,
|
||||
KEY_DEVICE_NAME));
|
||||
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, KEY_DEVICE_NAME));
|
||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||
}
|
||||
|
||||
|
@@ -24,11 +24,11 @@ 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.Context;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -41,15 +41,16 @@ import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
|
||||
private static final String DEVICE_NAME = "Nightshade";
|
||||
private static final String PREF_KEY = "bt_rename_devices";
|
||||
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Fragment mFragment;
|
||||
@Mock
|
||||
@@ -59,6 +60,8 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
private Context mContext;
|
||||
private Preference mPreference;
|
||||
private BluetoothDeviceRenamePreferenceController mController;
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -67,9 +70,10 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(PREF_KEY);
|
||||
mBluetoothAdapter = ShadowBluetoothAdapter.getDefaultAdapter();
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
|
||||
mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, mLocalAdapter,
|
||||
PREF_KEY));
|
||||
mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, PREF_KEY));
|
||||
mController.setFragment(mFragment);
|
||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||
@@ -102,7 +106,7 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updatePreferenceState_whenBTisOnPreferenceShouldBeVisible() {
|
||||
when(mLocalAdapter.isEnabled()).thenReturn(true);
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
|
||||
mController.updatePreferenceState(mPreference);
|
||||
|
||||
@@ -111,7 +115,7 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updatePreferenceState_whenBTisOffPreferenceShouldBeHide() {
|
||||
when(mLocalAdapter.isEnabled()).thenReturn(false);
|
||||
mShadowBluetoothAdapter.setEnabled(false);
|
||||
|
||||
mController.updatePreferenceState(mPreference);
|
||||
|
||||
|
@@ -35,13 +35,12 @@ import android.view.View;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
import com.android.settings.widget.SwitchBarController;
|
||||
import com.android.settings.widget.SwitchWidgetController;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -53,11 +52,12 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
|
||||
@Config(shadows = {SettingsShadowResources.SettingsShadowTheme.class, ShadowBluetoothAdapter.class})
|
||||
public class BluetoothEnablerTest {
|
||||
|
||||
private static EnforcedAdmin sFakeEnforcedAdmin;
|
||||
@@ -74,21 +74,17 @@ public class BluetoothEnablerTest {
|
||||
@Mock
|
||||
private RestrictionUtils mRestrictionUtils;
|
||||
@Mock
|
||||
private LocalBluetoothManager mBluetoothManager;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mBluetoothAdapter;
|
||||
@Mock
|
||||
private SwitchWidgetController.OnSwitchChangeListener mCallback;
|
||||
|
||||
private Context mContext;
|
||||
private SwitchWidgetController mSwitchController;
|
||||
private BluetoothEnabler mBluetoothEnabler;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
||||
|
||||
mRestrictedSwitchPreference = new RestrictedSwitchPreference(mContext);
|
||||
mSwitchController = spy(new SwitchBarController(new SwitchBar(mContext)));
|
||||
@@ -96,12 +92,12 @@ public class BluetoothEnablerTest {
|
||||
mContext,
|
||||
mSwitchController,
|
||||
mMetricsFeatureProvider,
|
||||
mBluetoothManager,
|
||||
123,
|
||||
mRestrictionUtils);
|
||||
mHolder = PreferenceViewHolder.createInstanceForTests(mock(View.class));
|
||||
mRestrictedSwitchPreference.onBindViewHolder(mHolder);
|
||||
mBluetoothEnabler.setToggleCallback(mCallback);
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -191,7 +187,7 @@ public class BluetoothEnablerTest {
|
||||
|
||||
@Test
|
||||
public void startWithBluetoothOff_switchIsOff() {
|
||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
||||
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_OFF);
|
||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||
mBluetoothEnabler.resume(mContext);
|
||||
verify(mSwitchController, never()).setChecked(true);
|
||||
@@ -199,7 +195,7 @@ public class BluetoothEnablerTest {
|
||||
|
||||
@Test
|
||||
public void startWithBluetoothOn_switchIsOn() {
|
||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_ON);
|
||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||
mBluetoothEnabler.resume(mContext);
|
||||
verify(mSwitchController, never()).setChecked(false);
|
||||
@@ -211,7 +207,7 @@ public class BluetoothEnablerTest {
|
||||
// Start up with bluetooth turned on. The switch should get turned on.
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
|
||||
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_ON);
|
||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||
mBluetoothEnabler.resume(mContext);
|
||||
verify(mSwitchController, never()).setChecked(false);
|
||||
@@ -235,7 +231,7 @@ public class BluetoothEnablerTest {
|
||||
// Start up with bluetooth turned on. The switch should be left off.
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
when(mContext.registerReceiver(captor.capture(), any(IntentFilter.class))).thenReturn(null);
|
||||
when(mBluetoothAdapter.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_OFF);
|
||||
mShadowBluetoothAdapter.setState(BluetoothAdapter.STATE_OFF);
|
||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||
mBluetoothEnabler.resume(mContext);
|
||||
verify(mSwitchController, never()).setChecked(anyBoolean());
|
||||
|
@@ -33,8 +33,8 @@ import android.content.res.Resources;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.widget.FooterPreference;
|
||||
|
||||
@@ -45,16 +45,17 @@ import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
import androidx.preference.PreferenceGroup;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class BluetoothPairingDetailTest {
|
||||
|
||||
@Mock
|
||||
private Resources mResource;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
@Mock
|
||||
@@ -63,6 +64,8 @@ public class BluetoothPairingDetailTest {
|
||||
private Context mContext;
|
||||
private BluetoothProgressCategory mAvailableDevicesCategory;
|
||||
private FooterPreference mFooterPreference;
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -75,11 +78,13 @@ public class BluetoothPairingDetailTest {
|
||||
|
||||
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
||||
mFooterPreference = new FooterPreference(mContext);
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
|
||||
mFragment.mLocalAdapter = mLocalAdapter;
|
||||
mFragment.mBluetoothAdapter = mBluetoothAdapter;
|
||||
mFragment.mLocalManager = mLocalManager;
|
||||
mFragment.mDeviceListGroup = mPreferenceGroup;
|
||||
mFragment.mAlwaysDiscoverable = new AlwaysDiscoverable(mContext, mLocalAdapter);
|
||||
mFragment.mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,7 +107,7 @@ public class BluetoothPairingDetailTest {
|
||||
|
||||
mFragment.enableScanning();
|
||||
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
verify(mAvailableDevicesCategory).removeAll();
|
||||
}
|
||||
|
||||
@@ -117,7 +122,8 @@ public class BluetoothPairingDetailTest {
|
||||
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
|
||||
R.string.bluetooth_preference_found_media_devices,
|
||||
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
|
||||
verify(mLocalAdapter).setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
assertThat(mBluetoothAdapter.getScanMode())
|
||||
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,16 +135,16 @@ public class BluetoothPairingDetailTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateBluetooth_bluetoothOff_turnOnBluetooth() {
|
||||
doReturn(false).when(mLocalAdapter).isEnabled();
|
||||
mShadowBluetoothAdapter.setEnabled(false);
|
||||
|
||||
mFragment.updateBluetooth();
|
||||
|
||||
verify(mLocalAdapter).enable();
|
||||
assertThat(mBluetoothAdapter.isEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateBluetooth_bluetoothOn_updateState() {
|
||||
doReturn(true).when(mLocalAdapter).isEnabled();
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
doNothing().when(mFragment).updateContent(anyInt());
|
||||
|
||||
mFragment.updateBluetooth();
|
||||
@@ -157,27 +163,27 @@ public class BluetoothPairingDetailTest {
|
||||
mFragment.updateContent(BluetoothAdapter.STATE_ON);
|
||||
verify(mFragment).enableScanning();
|
||||
assertThat(mAvailableDevicesCategory.getPreferenceCount()).isEqualTo(0);
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
|
||||
// Subsequent scan started event will not trigger start/stop nor list clear
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(1)).startScanning();
|
||||
verify(mAvailableDevicesCategory, times(1)).setProgress(true);
|
||||
|
||||
// Subsequent scan finished event will trigger scan start without list clean
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
verify(mAvailableDevicesCategory, times(2)).setProgress(true);
|
||||
|
||||
// Subsequent scan started event will not trigger any change
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
verify(mAvailableDevicesCategory, times(3)).setProgress(true);
|
||||
verify(mLocalAdapter, never()).stopScanning();
|
||||
verify(mFragment, never()).stopScanning();
|
||||
|
||||
// Disable scanning will trigger scan stop
|
||||
mFragment.disableScanning();
|
||||
verify(mLocalAdapter, times(1)).stopScanning();
|
||||
verify(mFragment, times(1)).stopScanning();
|
||||
|
||||
// Subsequent scan start event will not trigger any change besides progress circle
|
||||
mFragment.onScanningStateChanged(true);
|
||||
@@ -187,8 +193,8 @@ public class BluetoothPairingDetailTest {
|
||||
// progress circle from spinning
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mAvailableDevicesCategory, times(1)).setProgress(false);
|
||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
||||
verify(mLocalAdapter, times(1)).stopScanning();
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
verify(mFragment, times(1)).stopScanning();
|
||||
|
||||
// Verify that clean up only happen once at initialization
|
||||
verify(mAvailableDevicesCategory, times(1)).removeAll();
|
||||
|
@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
@@ -29,10 +30,7 @@ import android.content.res.Resources;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.SliceTester;
|
||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter;
|
||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothProfileManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -52,7 +50,7 @@ import androidx.slice.core.SliceAction;
|
||||
import androidx.slice.widget.SliceLiveData;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowLocalBluetoothAdapter.class, ShadowLocalBluetoothProfileManager.class})
|
||||
@Config(shadows = {ShadowLocalBluetoothProfileManager.class})
|
||||
public class BluetoothSliceBuilderTest {
|
||||
|
||||
private Context mContext;
|
||||
@@ -90,11 +88,10 @@ public class BluetoothSliceBuilderTest {
|
||||
|
||||
@Test
|
||||
public void handleUriChange_updatesBluetooth() {
|
||||
final LocalBluetoothAdapter adapter = LocalBluetoothManager.getInstance(mContext,
|
||||
null /* callback */).getBluetoothAdapter();
|
||||
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
final Intent intent = new Intent();
|
||||
intent.putExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, true);
|
||||
adapter.setBluetoothEnabled(false /* enabled */);
|
||||
adapter.disable()/* enabled */;
|
||||
|
||||
BluetoothSliceBuilder.handleUriChange(mContext, intent);
|
||||
|
||||
|
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.doCallRealMethod;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
@@ -31,8 +30,8 @@ import android.content.Context;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -42,11 +41,14 @@ import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
private static final String DEVICE_NAME = "Nightshade";
|
||||
@@ -56,8 +58,6 @@ public class BluetoothSummaryUpdaterTest {
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private LocalBluetoothManager mBluetoothManager;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mBtAdapter;
|
||||
@Mock
|
||||
private BluetoothDevice mConnectedDevice;
|
||||
@Mock
|
||||
private BluetoothDevice mConnectedKeyBoardDevice;
|
||||
@@ -72,16 +72,13 @@ public class BluetoothSummaryUpdaterTest {
|
||||
private final boolean[] mDeviceConnected = {false, false};
|
||||
private final Set<BluetoothDevice> mBondedDevices = new HashSet<>();
|
||||
private BluetoothSummaryUpdater mSummaryUpdater;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application.getApplicationContext();
|
||||
doCallRealMethod().when(mListener).onSummaryChanged(anyString());
|
||||
// Setup mock adapter
|
||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBtAdapter);
|
||||
doAnswer(invocation -> mAdapterEnabled[0]).when(mBtAdapter).isEnabled();
|
||||
doAnswer(invocation -> mAdapterConnectionState[0]).when(mBtAdapter).getConnectionState();
|
||||
mSummaryUpdater = new BluetoothSummaryUpdater(mContext, mListener, mBluetoothManager);
|
||||
// Setup first device
|
||||
doReturn(DEVICE_NAME).when(mConnectedDevice).getName();
|
||||
@@ -89,7 +86,11 @@ public class BluetoothSummaryUpdaterTest {
|
||||
// Setup second device
|
||||
doReturn(DEVICE_KEYBOARD_NAME).when(mConnectedKeyBoardDevice).getName();
|
||||
doAnswer(invocation -> mDeviceConnected[1]).when(mConnectedKeyBoardDevice).isConnected();
|
||||
doReturn(mBondedDevices).when(mBtAdapter).getBondedDevices();
|
||||
// Setup BluetoothAdapter
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
mShadowBluetoothAdapter.setEnabled(mAdapterEnabled[0]);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mShadowBluetoothAdapter.setConnectionState(mAdapterConnectionState[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,9 +109,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void register_true_shouldSendSummaryChange() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = true;
|
||||
|
||||
mSummaryUpdater.register(true);
|
||||
@@ -122,8 +124,9 @@ public class BluetoothSummaryUpdaterTest {
|
||||
@Test
|
||||
public void onBluetoothStateChanged_btDisabled_shouldSendDisabledSummary() {
|
||||
// These states should be ignored
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = true;
|
||||
|
||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||
@@ -133,9 +136,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_btEnabled_connected_shouldSendConnectedSummary() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = true;
|
||||
|
||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
||||
@@ -146,9 +150,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_btEnabled_connectedMisMatch_shouldSendNotConnected() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
// State mismatch
|
||||
mDeviceConnected[0] = false;
|
||||
|
||||
@@ -159,9 +164,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_btEnabled_notConnected_shouldSendDisconnectedMessage() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
// This should be ignored
|
||||
mDeviceConnected[0] = true;
|
||||
|
||||
@@ -172,22 +178,23 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_ConnectedDisabledEnabled_shouldSendDisconnectedSummary() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = false;
|
||||
|
||||
mSummaryUpdater.register(true);
|
||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mDeviceConnected[0] = true;
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
BluetoothAdapter.STATE_CONNECTED);
|
||||
verify(mListener).onSummaryChanged(
|
||||
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME));
|
||||
|
||||
mAdapterEnabled[0] = false;
|
||||
mShadowBluetoothAdapter.setEnabled(false);
|
||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
|
||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
||||
|
||||
@@ -196,7 +203,7 @@ public class BluetoothSummaryUpdaterTest {
|
||||
// There should still be only one invocation of disabled message
|
||||
verify(mListener).onSummaryChanged(mContext.getString(R.string.bluetooth_disabled));
|
||||
|
||||
mAdapterEnabled[0] = true;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mDeviceConnected[0] = false;
|
||||
mSummaryUpdater.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
|
||||
verify(mListener, times(2)).onSummaryChanged(mContext.getString(R.string.disconnected));
|
||||
@@ -205,9 +212,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onConnectionStateChanged_connected_shouldSendConnectedMessage() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = true;
|
||||
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
@@ -219,9 +227,10 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onConnectionStateChanged_inconsistentState_shouldSendDisconnectedMessage() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTED);
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = false;
|
||||
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
@@ -232,8 +241,8 @@ public class BluetoothSummaryUpdaterTest {
|
||||
|
||||
@Test
|
||||
public void onConnectionStateChanged_noBondedDevice_shouldSendDisconnectedMessage() {
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTED;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTED);
|
||||
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
BluetoothAdapter.STATE_CONNECTED);
|
||||
@@ -244,8 +253,8 @@ public class BluetoothSummaryUpdaterTest {
|
||||
@Test
|
||||
public void onConnectionStateChanged_connecting_shouldSendConnectingMessage() {
|
||||
// No need for bonded devices
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_CONNECTING;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_CONNECTING);
|
||||
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
BluetoothAdapter.STATE_CONNECTING);
|
||||
@@ -256,8 +265,8 @@ public class BluetoothSummaryUpdaterTest {
|
||||
@Test
|
||||
public void onConnectionStateChanged_disconnecting_shouldSendDisconnectingMessage() {
|
||||
// No need for bonded devices
|
||||
mAdapterEnabled[0] = true;
|
||||
mAdapterConnectionState[0] = BluetoothAdapter.STATE_DISCONNECTING;
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
mShadowBluetoothAdapter.setConnectionState(BluetoothAdapter.STATE_DISCONNECTING);
|
||||
|
||||
mSummaryUpdater.onConnectionStateChanged(null /* device */,
|
||||
BluetoothAdapter.STATE_DISCONNECTING);
|
||||
@@ -268,6 +277,7 @@ public class BluetoothSummaryUpdaterTest {
|
||||
@Test
|
||||
public void getConnectedDeviceSummary_hasConnectedDevice_returnOneDeviceSummary() {
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
mDeviceConnected[0] = true;
|
||||
final String expectedSummary =
|
||||
mContext.getString(R.string.bluetooth_connected_summary, DEVICE_NAME);
|
||||
@@ -279,6 +289,8 @@ public class BluetoothSummaryUpdaterTest {
|
||||
public void getConnectedDeviceSummary_multipleDevices_returnMultipleDevicesSummary() {
|
||||
mBondedDevices.add(mConnectedDevice);
|
||||
mBondedDevices.add(mConnectedKeyBoardDevice);
|
||||
mShadowBluetoothAdapter.setBondedDevices(mBondedDevices);
|
||||
|
||||
mDeviceConnected[0] = true;
|
||||
mDeviceConnected[1] = true;
|
||||
final String expectedSummary =
|
||||
|
@@ -43,8 +43,6 @@ public class BluetoothSwitchPreferenceControllerTest {
|
||||
|
||||
public static final String BLUETOOTH_INFO_STRING = "When Bluetooth is turned on, your device"
|
||||
+ " can communicate with other nearby Bluetooth devices.";
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private LocalBluetoothManager mBluetoothManager;
|
||||
@Mock
|
||||
private RestrictionUtils mRestrictionUtils;
|
||||
@Mock
|
||||
@@ -62,7 +60,7 @@ public class BluetoothSwitchPreferenceControllerTest {
|
||||
FakeFeatureFactory.setupForTest();
|
||||
|
||||
mController =
|
||||
new BluetoothSwitchPreferenceController(mContext, mBluetoothManager, mRestrictionUtils,
|
||||
new BluetoothSwitchPreferenceController(mContext, mRestrictionUtils,
|
||||
mSwitchController, mFooterPreference);
|
||||
}
|
||||
|
||||
|
@@ -18,20 +18,20 @@ package com.android.settings.bluetooth;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -40,12 +40,14 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class DeviceListPreferenceFragmentTest {
|
||||
|
||||
private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
|
||||
@@ -54,8 +56,7 @@ public class DeviceListPreferenceFragmentTest {
|
||||
private Resources mResource;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mLocalAdapter;
|
||||
|
||||
private TestFragment mFragment;
|
||||
private Preference mMyDevicePreference;
|
||||
|
||||
@@ -66,7 +67,7 @@ public class DeviceListPreferenceFragmentTest {
|
||||
mFragment = spy(new TestFragment());
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
doReturn(mResource).when(mFragment).getResources();
|
||||
mFragment.mLocalAdapter = mLocalAdapter;
|
||||
mFragment.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
|
||||
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
|
||||
}
|
||||
@@ -84,11 +85,11 @@ public class DeviceListPreferenceFragmentTest {
|
||||
@Test
|
||||
public void testEnableDisableScanning_testStateAfterEanbleDisable() {
|
||||
mFragment.enableScanning();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
|
||||
mFragment.disableScanning();
|
||||
verify(mLocalAdapter).stopScanning();
|
||||
verify(mFragment).stopScanning();
|
||||
assertThat(mFragment.mScanEnabled).isFalse();
|
||||
}
|
||||
|
||||
@@ -96,21 +97,21 @@ public class DeviceListPreferenceFragmentTest {
|
||||
public void testScanningStateChanged_testScanStarted() {
|
||||
mFragment.enableScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(1)).startScanning();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanningStateChanged_testScanFinished() {
|
||||
// Could happen when last scanning not done while current scan gets enabled
|
||||
mFragment.enableScanning();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,53 +119,53 @@ public class DeviceListPreferenceFragmentTest {
|
||||
// Could happen when last scanning not done while current scan gets enabled
|
||||
mFragment.enableScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(1)).startScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mLocalAdapter, times(2)).startScanning(true);
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
|
||||
mFragment.disableScanning();
|
||||
verify(mLocalAdapter).stopScanning();
|
||||
verify(mFragment).stopScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(2)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(2)).startScanning();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanningStateChanged_testScanFinishedAfterDisable() {
|
||||
mFragment.enableScanning();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
|
||||
mFragment.disableScanning();
|
||||
verify(mLocalAdapter).stopScanning();
|
||||
verify(mFragment).stopScanning();
|
||||
assertThat(mFragment.mScanEnabled).isFalse();
|
||||
|
||||
mFragment.onScanningStateChanged(false);
|
||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(1)).startScanning();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanningStateChanged_testScanStartedAfterDisable() {
|
||||
mFragment.enableScanning();
|
||||
verify(mLocalAdapter).startScanning(true);
|
||||
verify(mFragment).startScanning();
|
||||
assertThat(mFragment.mScanEnabled).isTrue();
|
||||
|
||||
mFragment.disableScanning();
|
||||
verify(mLocalAdapter).stopScanning();
|
||||
verify(mFragment).stopScanning();
|
||||
assertThat(mFragment.mScanEnabled).isFalse();
|
||||
|
||||
mFragment.onScanningStateChanged(true);
|
||||
verify(mLocalAdapter, times(1)).startScanning(anyBoolean());
|
||||
verify(mFragment, times(1)).startScanning();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -30,7 +30,6 @@ import com.android.settings.R;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.testutils.FragmentTestUtils;
|
||||
|
||||
@@ -50,10 +49,6 @@ import androidx.appcompat.app.AlertDialog;
|
||||
@Config(shadows = {ShadowAlertDialogCompat.class, SettingsShadowResourcesImpl.class})
|
||||
public class LocalDeviceNameDialogFragmentTest {
|
||||
|
||||
@Mock
|
||||
private LocalBluetoothManager mManager;
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mAdapter;
|
||||
@Mock
|
||||
private InputMethodManager mInputMethodManager;
|
||||
|
||||
@@ -65,8 +60,6 @@ public class LocalDeviceNameDialogFragmentTest {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
doReturn(mInputMethodManager).when(mContext).getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
ReflectionHelpers.setStaticField(LocalBluetoothManager.class, "sInstance", mManager);
|
||||
when(mManager.getBluetoothAdapter()).thenReturn(mAdapter);
|
||||
|
||||
mFragment = spy(LocalDeviceNameDialogFragment.newInstance());
|
||||
when(mFragment.getContext()).thenReturn(mContext);
|
||||
|
@@ -35,7 +35,6 @@ import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothPan;
|
||||
import com.android.settings.testutils.shadow.ShadowLocalBluetoothAdapter;
|
||||
import com.android.settingslib.widget.FooterPreference;
|
||||
import com.android.settingslib.widget.FooterPreferenceMixinCompat;
|
||||
|
||||
@@ -47,6 +46,7 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadow.api.Shadow;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -55,8 +55,7 @@ import java.util.List;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class,
|
||||
ShadowLocalBluetoothAdapter.class})
|
||||
@Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class})
|
||||
public class DiscoverableFooterPreferenceControllerTest {
|
||||
private static final String DEVICE_NAME = "device name";
|
||||
private static final String KEY = "discoverable_footer_preference";
|
||||
@@ -75,6 +74,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
||||
private DiscoverableFooterPreferenceController mDiscoverableFooterPreferenceController;
|
||||
private BroadcastReceiver mBluetoothChangedReceiver;
|
||||
private ShadowApplication mShadowApplication;
|
||||
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -90,6 +90,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
||||
mAlwaysDiscoverable);
|
||||
mBluetoothChangedReceiver = mDiscoverableFooterPreferenceController
|
||||
.mBluetoothChangedReceiver;
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -135,7 +136,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_bluetoothOn_updateTitle() {
|
||||
ShadowLocalBluetoothAdapter.setName(DEVICE_NAME);
|
||||
mShadowBluetoothAdapter.setName(DEVICE_NAME);
|
||||
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_ON);
|
||||
|
||||
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(DEVICE_NAME));
|
||||
@@ -143,7 +144,7 @@ public class DiscoverableFooterPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onBluetoothStateChanged_bluetoothOff_updateTitle(){
|
||||
ShadowLocalBluetoothAdapter.setName(DEVICE_NAME);
|
||||
mShadowBluetoothAdapter.setName(DEVICE_NAME);
|
||||
sendBluetoothStateChangedIntent(BluetoothAdapter.STATE_OFF);
|
||||
|
||||
assertThat(mPreference.getTitle()).isEqualTo(generateTitle(null));
|
||||
|
@@ -20,11 +20,10 @@ import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
@@ -32,14 +31,12 @@ import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.widget.ValidatedEditTextPreference;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
@@ -49,13 +46,10 @@ import org.robolectric.shadows.ShadowApplication;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowBluetoothAdapter.class})
|
||||
public class DeviceNamePreferenceControllerTest {
|
||||
private static final String TESTING_STRING = "Testing";
|
||||
|
||||
@Mock
|
||||
private LocalBluetoothAdapter mBluetoothAdapter;
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private LocalBluetoothManager mBluetoothManager;
|
||||
@Mock
|
||||
private WifiManager mWifiManager;
|
||||
@Mock
|
||||
@@ -63,6 +57,7 @@ public class DeviceNamePreferenceControllerTest {
|
||||
private ValidatedEditTextPreference mPreference;
|
||||
private DeviceNamePreferenceController mController;
|
||||
private Context mContext;
|
||||
private BluetoothAdapter mBluetoothAdapter;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -72,14 +67,13 @@ public class DeviceNamePreferenceControllerTest {
|
||||
shadowApplication.setSystemService(Context.WIFI_SERVICE, mWifiManager);
|
||||
mContext = shadowApplication.getApplicationContext();
|
||||
mPreference = new ValidatedEditTextPreference(mContext);
|
||||
when(mBluetoothManager.getBluetoothAdapter()).thenReturn(mBluetoothAdapter);
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||
final WifiConfiguration configuration = new WifiConfiguration();
|
||||
configuration.SSID = "test-ap";
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration);
|
||||
|
||||
mController = new DeviceNamePreferenceController(mContext);
|
||||
mController.setLocalBluetoothManager(mBluetoothManager);
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,7 +97,6 @@ public class DeviceNamePreferenceControllerTest {
|
||||
Settings.Global.putString(
|
||||
mContext.getContentResolver(), Settings.Global.DEVICE_NAME, "Test");
|
||||
mController = new DeviceNamePreferenceController(mContext);
|
||||
mController.setLocalBluetoothManager(mBluetoothManager);
|
||||
assertThat(mController.getSummary()).isEqualTo("Test");
|
||||
}
|
||||
|
||||
@@ -132,7 +125,7 @@ public class DeviceNamePreferenceControllerTest {
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||
|
||||
verify(mBluetoothAdapter).setName(eq(TESTING_STRING));
|
||||
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,10 +148,11 @@ public class DeviceNamePreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void setDeviceName_ignoresIfCancelPressed() {
|
||||
forceAcceptDeviceName();
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, TESTING_STRING);
|
||||
|
||||
verify(mBluetoothAdapter, never()).setName(eq(TESTING_STRING));
|
||||
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
|
||||
}
|
||||
|
||||
private void forceAcceptDeviceName() {
|
||||
|
@@ -26,6 +26,11 @@ import java.util.List;
|
||||
|
||||
@Implements(value = BluetoothAdapter.class, inheritImplementationMethods = true)
|
||||
public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBluetoothAdapter {
|
||||
|
||||
private String mName;
|
||||
private int mScanMode;
|
||||
private int mState;
|
||||
|
||||
/**
|
||||
* Do nothing, implement it to avoid null pointer error inside BluetoothAdapter
|
||||
*/
|
||||
@@ -33,4 +38,32 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
|
||||
public List<Integer> getSupportedProfiles() {
|
||||
return new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
mName = name;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public String getName() {
|
||||
return mName;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public void setScanMode(int scanMode) {
|
||||
mScanMode = scanMode;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public int getScanMode() {
|
||||
return mScanMode;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public int getConnectionState() {
|
||||
return mState;
|
||||
}
|
||||
|
||||
public void setConnectionState(int state) {
|
||||
mState = state;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user