[Connected devices page] Reorder devices by most recently used.
Order the saved bluetooth devices by most recently connected in connected devices settings page. Test: atest: com.android.settings.connecteddevice.SavedDeviceGroupControllerTest, com.android.settings.connecteddevice.PreviouslyConnectedDevicePreferenceControllerTest Bug: 306160434 Change-Id: Id5ad8555a026d775d96ada37f989b4346336af93
This commit is contained in:
@@ -27,9 +27,14 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.platform.test.annotations.RequiresFlagsDisabled;
|
||||
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
@@ -42,11 +47,13 @@ import com.android.settings.bluetooth.BluetoothDevicePreference;
|
||||
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
|
||||
import com.android.settings.connecteddevice.dock.DockUpdater;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.widget.SingleTargetGearPreference;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -70,6 +77,9 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
private static final String FAKE_ADDRESS_4 = "AA:AA:AA:AA:AA:04";
|
||||
private static final String FAKE_ADDRESS_5 = "AA:AA:AA:AA:AA:05";
|
||||
|
||||
@Rule
|
||||
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||
|
||||
@Mock
|
||||
private DashboardFragment mDashboardFragment;
|
||||
@Mock
|
||||
@@ -105,6 +115,9 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
@Mock
|
||||
private Drawable mDrawable;
|
||||
|
||||
@Mock private BluetoothManager mBluetoothManager;
|
||||
@Mock private BluetoothAdapter mBluetoothAdapter;
|
||||
|
||||
private Context mContext;
|
||||
private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
@@ -117,10 +130,8 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
doReturn(mContext).when(mDashboardFragment).getContext();
|
||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||
mPreConnectedDeviceController =
|
||||
new PreviouslyConnectedDevicePreferenceController(mContext, KEY);
|
||||
mPreConnectedDeviceController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
|
||||
mPreConnectedDeviceController.setSavedDockUpdater(mDockUpdater);
|
||||
when(mContext.getSystemService(BluetoothManager.class)).thenReturn(mBluetoothManager);
|
||||
when(mBluetoothManager.getAdapter()).thenReturn(mBluetoothAdapter);
|
||||
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
|
||||
|
||||
when(mCachedDevice1.getDevice()).thenReturn(mBluetoothDevice1);
|
||||
@@ -145,7 +156,13 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
mMostRecentlyConnectedDevices.add(mBluetoothDevice4);
|
||||
mMostRecentlyConnectedDevices.add(mBluetoothDevice3);
|
||||
mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(mMostRecentlyConnectedDevices);
|
||||
when(mBluetoothAdapter.getMostRecentlyConnectedDevices())
|
||||
.thenReturn(mMostRecentlyConnectedDevices);
|
||||
|
||||
mPreConnectedDeviceController =
|
||||
new PreviouslyConnectedDevicePreferenceController(mContext, KEY);
|
||||
mPreConnectedDeviceController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
|
||||
mPreConnectedDeviceController.setSavedDockUpdater(mDockUpdater);
|
||||
mPreferenceGroup = spy(new PreferenceCategory(mContext));
|
||||
doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager();
|
||||
mPreferenceGroup.setVisible(false);
|
||||
@@ -249,6 +266,7 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void onDeviceAdded_addPreferenceNotExistInRecentlyDevices_noCrash() {
|
||||
final BluetoothDevicePreference preference = new BluetoothDevicePreference(
|
||||
mContext, mCachedDevice5, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
@@ -259,6 +277,18 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void onDeviceAdded_addPreferenceNotExistInRecentlyDevices_doNothing() {
|
||||
final BluetoothDevicePreference preference = new BluetoothDevicePreference(
|
||||
mContext, mCachedDevice5, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference);
|
||||
|
||||
// 1 see all preference
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceRemoved_removeLastDevice_showSeeAllPreference() {
|
||||
final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(
|
||||
@@ -277,6 +307,7 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
@Test
|
||||
public void updatePreferenceVisibility_bluetoothIsEnable_shouldShowCorrectText() {
|
||||
mShadowBluetoothAdapter.setEnabled(true);
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
|
||||
mPreConnectedDeviceController.updatePreferenceVisibility();
|
||||
|
||||
verify(mSeeAllPreference).setSummary("");
|
||||
@@ -285,9 +316,78 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
|
||||
@Test
|
||||
public void updatePreferenceVisibility_bluetoothIsDisable_shouldShowCorrectText() {
|
||||
mShadowBluetoothAdapter.setEnabled(false);
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(false);
|
||||
mPreConnectedDeviceController.updatePreferenceVisibility();
|
||||
|
||||
verify(mSeeAllPreference).setSummary(
|
||||
mContext.getString(R.string.connected_device_see_all_summary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void updatePreferenceGroup_bluetoothIsEnable_shouldOrderByMostRecentlyConnected() {
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
|
||||
final BluetoothDevicePreference preference4 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice4,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference3 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice3,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference2 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice2,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference4);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference3);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference2);
|
||||
|
||||
mPreConnectedDeviceController.updatePreferenceGroup();
|
||||
|
||||
// Refer to the order of {@link #mMostRecentlyConnectedDevices}, the first one is see all
|
||||
// preference
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(4);
|
||||
assertThat(preference2.getOrder()).isEqualTo(0);
|
||||
assertThat(preference4.getOrder()).isEqualTo(1);
|
||||
assertThat(preference3.getOrder()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void updatePreferenceGroup_bluetoothIsDisable_shouldShowOnlySeeAllPreference() {
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(false);
|
||||
final BluetoothDevicePreference preference4 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice4,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference3 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice3,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference2 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice2,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference4);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference3);
|
||||
mPreConnectedDeviceController.onDeviceAdded(preference2);
|
||||
|
||||
mPreConnectedDeviceController.updatePreferenceGroup();
|
||||
|
||||
// 1 see all preference
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,29 +25,52 @@ 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.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.android.settings.bluetooth.BluetoothDevicePreference;
|
||||
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
|
||||
import com.android.settings.connecteddevice.dock.DockUpdater;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class SavedDeviceGroupControllerTest {
|
||||
|
||||
private static final String FAKE_ADDRESS_1 = "AA:AA:AA:AA:AA:01";
|
||||
private static final String FAKE_ADDRESS_2 = "AA:AA:AA:AA:AA:02";
|
||||
private static final String FAKE_ADDRESS_3 = "AA:AA:AA:AA:AA:03";
|
||||
|
||||
@Rule
|
||||
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||
|
||||
@Mock
|
||||
private DashboardFragment mDashboardFragment;
|
||||
@Mock
|
||||
@@ -56,23 +79,57 @@ public class SavedDeviceGroupControllerTest {
|
||||
private DockUpdater mSavedDockUpdater;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock private BluetoothManager mBluetoothManager;
|
||||
@Mock private BluetoothAdapter mBluetoothAdapter;
|
||||
@Mock private CachedBluetoothDevice mCachedDevice1;
|
||||
@Mock private CachedBluetoothDevice mCachedDevice2;
|
||||
@Mock private CachedBluetoothDevice mCachedDevice3;
|
||||
@Mock private BluetoothDevice mBluetoothDevice1;
|
||||
@Mock private BluetoothDevice mBluetoothDevice2;
|
||||
@Mock private BluetoothDevice mBluetoothDevice3;
|
||||
@Mock private Drawable mDrawable;
|
||||
@Mock private PreferenceManager mPreferenceManager;
|
||||
|
||||
private Context mContext;
|
||||
private SavedDeviceGroupController mSavedDeviceGroupController;
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private Lifecycle mLifecycle;
|
||||
private PreferenceGroup mPreferenceGroup;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device");
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mLifecycleOwner = () -> mLifecycle;
|
||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||
doReturn(mContext).when(mDashboardFragment).getContext();
|
||||
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||
|
||||
when(mCachedDevice1.getDevice()).thenReturn(mBluetoothDevice1);
|
||||
when(mCachedDevice1.getAddress()).thenReturn(FAKE_ADDRESS_1);
|
||||
when(mCachedDevice1.getDrawableWithDescription()).thenReturn(pairs);
|
||||
when(mCachedDevice2.getDevice()).thenReturn(mBluetoothDevice2);
|
||||
when(mCachedDevice2.getAddress()).thenReturn(FAKE_ADDRESS_2);
|
||||
when(mCachedDevice2.getDrawableWithDescription()).thenReturn(pairs);
|
||||
when(mCachedDevice3.getDevice()).thenReturn(mBluetoothDevice3);
|
||||
when(mCachedDevice3.getAddress()).thenReturn(FAKE_ADDRESS_3);
|
||||
when(mCachedDevice3.getDrawableWithDescription()).thenReturn(pairs);
|
||||
final List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>();
|
||||
mMostRecentlyConnectedDevices.add(mBluetoothDevice1);
|
||||
mMostRecentlyConnectedDevices.add(mBluetoothDevice2);
|
||||
mMostRecentlyConnectedDevices.add(mBluetoothDevice3);
|
||||
when(mContext.getSystemService(BluetoothManager.class)).thenReturn(mBluetoothManager);
|
||||
when(mBluetoothManager.getAdapter()).thenReturn(mBluetoothAdapter);
|
||||
when(mBluetoothAdapter.getMostRecentlyConnectedDevices())
|
||||
.thenReturn(mMostRecentlyConnectedDevices);
|
||||
|
||||
mPreferenceGroup = spy(new PreferenceCategory(mContext));
|
||||
when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
mSavedDeviceGroupController = new SavedDeviceGroupController(mContext);
|
||||
mSavedDeviceGroupController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater);
|
||||
mSavedDeviceGroupController.setSavedDockUpdater(mSavedDockUpdater);
|
||||
mSavedDeviceGroupController.setPreferenceGroup(mPreferenceGroup);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,4 +175,71 @@ public class SavedDeviceGroupControllerTest {
|
||||
assertThat(mSavedDeviceGroupController.getAvailabilityStatus()).isEqualTo(
|
||||
AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void updatePreferenceGroup_bluetoothIsEnable_shouldOrderByMostRecentlyConnected() {
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
|
||||
final BluetoothDevicePreference preference3 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice3,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference2 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice2,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference1 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice1,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference3);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference2);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference1);
|
||||
|
||||
mSavedDeviceGroupController.updatePreferenceGroup();
|
||||
|
||||
// Refer to the order of {@link #mMostRecentlyConnectedDevices}
|
||||
assertThat(mPreferenceGroup.isVisible()).isTrue();
|
||||
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(3);
|
||||
assertThat(preference1.getOrder()).isEqualTo(0);
|
||||
assertThat(preference2.getOrder()).isEqualTo(1);
|
||||
assertThat(preference3.getOrder()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_SAVED_DEVICES_ORDER_BY_RECENCY)
|
||||
public void updatePreferenceGroup_bluetoothIsDisable_shouldShowNoPreference() {
|
||||
when(mBluetoothAdapter.isEnabled()).thenReturn(false);
|
||||
final BluetoothDevicePreference preference3 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice3,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference2 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice2,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
final BluetoothDevicePreference preference1 =
|
||||
new BluetoothDevicePreference(
|
||||
mContext,
|
||||
mCachedDevice2,
|
||||
true,
|
||||
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference3);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference2);
|
||||
mSavedDeviceGroupController.onDeviceAdded(preference1);
|
||||
|
||||
mSavedDeviceGroupController.updatePreferenceGroup();
|
||||
|
||||
assertThat(mPreferenceGroup.isVisible()).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user