Snap for 11402394 from 2f1943b267 to 24Q2-release

Change-Id: I068902bd0a3c006dcc2297730b558654069d6846
This commit is contained in:
Android Build Coastguard Worker
2024-02-04 22:21:00 +00:00
15 changed files with 382 additions and 115 deletions

View File

@@ -33,4 +33,11 @@ flag {
namespace: "pixel_cross_device_control" namespace: "pixel_cross_device_control"
description: "Order the saved bluetooth devices by most recently connected." description: "Order the saved bluetooth devices by most recently connected."
bug: "306160434" bug: "306160434"
}
flag {
name: "enable_hide_exclusively_managed_bluetooth_device"
namespace: "dck_framework"
description: "Hide exclusively managed Bluetooth devices in BT settings menu."
bug: "322285078"
} }

View File

@@ -24,6 +24,8 @@ import android.util.Log;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.flags.Flags;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
/** /**
@@ -95,6 +97,15 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched); cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
} }
} }
if (Flags.enableHideExclusivelyManagedBluetoothDevice()) {
if (BluetoothUtils.isExclusivelyManagedBluetoothDevice(mContext,
cachedDevice.getDevice())) {
if (DBG) {
Log.d(TAG, "isFilterMatched() hide BluetoothDevice with exclusive manager");
}
return false;
}
}
return isFilterMatched; return isFilterMatched;
} }

View File

@@ -25,6 +25,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.flags.Flags;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
@@ -99,12 +101,22 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater
@Override @Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) { public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
final BluetoothDevice device = cachedDevice.getDevice(); final BluetoothDevice device = cachedDevice.getDevice();
Log.d(TAG, "isFilterMatched() device name : " + cachedDevice.getName() + boolean isExclusivelyManaged = BluetoothUtils.isExclusivelyManagedBluetoothDevice(mContext,
", is connected : " + device.isConnected() + ", is profile connected : " cachedDevice.getDevice());
+ cachedDevice.isConnected()); Log.d(TAG, "isFilterMatched() device name : " + cachedDevice.getName()
return device.getBondState() == BluetoothDevice.BOND_BONDED + ", is connected : " + device.isConnected() + ", is profile connected : "
&& (mShowConnectedDevice || (!device.isConnected() && isDeviceInCachedDevicesList( + cachedDevice.isConnected() + ", is exclusively managed : "
cachedDevice))); + isExclusivelyManaged);
if (Flags.enableHideExclusivelyManagedBluetoothDevice()) {
return device.getBondState() == BluetoothDevice.BOND_BONDED
&& (mShowConnectedDevice || (!device.isConnected()
&& isDeviceInCachedDevicesList(cachedDevice)))
&& !isExclusivelyManaged;
} else {
return device.getBondState() == BluetoothDevice.BOND_BONDED
&& (mShowConnectedDevice || (!device.isConnected()
&& isDeviceInCachedDevicesList(cachedDevice)));
}
} }
@Override @Override

View File

@@ -26,7 +26,7 @@ import android.content.pm.ApplicationInfo
import android.os.UserManager import android.os.UserManager
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import com.android.settings.R import com.android.settings.R
import com.android.settingslib.spa.livedata.observeAsCallback import com.android.settingslib.spa.lifecycle.collectAsCallbackWithLifecycle
import com.android.settingslib.spaprivileged.model.app.AppOpsController import com.android.settingslib.spaprivileged.model.app.AppOpsController
import com.android.settingslib.spaprivileged.model.app.AppRecord import com.android.settingslib.spaprivileged.model.app.AppRecord
import com.android.settingslib.spaprivileged.model.app.userId import com.android.settingslib.spaprivileged.model.app.userId
@@ -81,7 +81,7 @@ class InstallUnknownAppsListModel(private val context: Context) :
@Composable @Composable
override fun isAllowed(record: InstallUnknownAppsRecord) = override fun isAllowed(record: InstallUnknownAppsRecord) =
record.appOpsController.isAllowed.observeAsCallback() record.appOpsController.isAllowed.collectAsCallbackWithLifecycle()
override fun isChangeable(record: InstallUnknownAppsRecord) = override fun isChangeable(record: InstallUnknownAppsRecord) =
isChangeable(record, getPotentialPackageNames(record.app.userId)) isChangeable(record, getPotentialPackageNames(record.app.userId))

View File

@@ -27,7 +27,7 @@ import android.content.pm.PackageManager.PackageInfoFlags
import android.util.Log import android.util.Log
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import com.android.settings.R import com.android.settings.R
import com.android.settingslib.spa.livedata.observeAsCallback import com.android.settingslib.spa.lifecycle.collectAsCallbackWithLifecycle
import com.android.settingslib.spaprivileged.model.app.AppOpsController import com.android.settingslib.spaprivileged.model.app.AppOpsController
import com.android.settingslib.spaprivileged.model.app.AppRecord import com.android.settingslib.spaprivileged.model.app.AppRecord
import com.android.settingslib.spaprivileged.model.app.installed import com.android.settingslib.spaprivileged.model.app.installed
@@ -92,7 +92,7 @@ class PictureInPictureListModel(private val context: Context) :
@Composable @Composable
override fun isAllowed(record: PictureInPictureRecord) = override fun isAllowed(record: PictureInPictureRecord) =
record.appOpsController.isAllowed.observeAsCallback() record.appOpsController.isAllowed.collectAsCallbackWithLifecycle()
override fun isChangeable(record: PictureInPictureRecord) = record.isSupport override fun isChangeable(record: PictureInPictureRecord) = record.isSupport

View File

@@ -39,26 +39,24 @@ import com.android.settings.Utils;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment; import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.wifi.WifiPermissionChecker; import com.android.settingslib.wifi.WifiPermissionChecker;
/** /** This activity requests users permission to allow scanning even when Wi-Fi is turned off */
* This activity requests users permission to allow scanning even when Wi-Fi is turned off
*/
public class WifiScanModeActivity extends FragmentActivity { public class WifiScanModeActivity extends FragmentActivity {
private static final String TAG = "WifiScanModeActivity"; private static final String TAG = "WifiScanModeActivity";
private DialogFragment mDialog; private DialogFragment mDialog;
@VisibleForTesting @VisibleForTesting String mApp;
String mApp; @VisibleForTesting WifiPermissionChecker mWifiPermissionChecker;
@VisibleForTesting
WifiPermissionChecker mWifiPermissionChecker;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getWindow().addSystemFlags( getWindow()
WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); .addSystemFlags(
WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
Intent intent = getIntent(); Intent intent = getIntent();
if (savedInstanceState == null) { if (savedInstanceState == null) {
if (intent != null && WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE if (intent != null
.equals(intent.getAction())) { && WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE.equals(
intent.getAction())) {
refreshAppLabel(); refreshAppLabel();
} else { } else {
finish(); finish();
@@ -92,6 +90,12 @@ public class WifiScanModeActivity extends FragmentActivity {
return; return;
} }
if (!isWifiScanModeConfigAllowed(getApplicationContext())) {
Log.e(TAG, "This user is not allowed to configure Wi-Fi Scan Mode!");
finish();
return;
}
if (mDialog == null) { if (mDialog == null) {
mDialog = AlertDialogFragment.newInstance(mApp); mDialog = AlertDialogFragment.newInstance(mApp);
mDialog.show(getSupportFragmentManager(), "dialog"); mDialog.show(getSupportFragmentManager(), "dialog");
@@ -140,6 +144,7 @@ public class WifiScanModeActivity extends FragmentActivity {
} }
private final String mApp; private final String mApp;
public AlertDialogFragment(String app) { public AlertDialogFragment(String app) {
super(); super();
mApp = app; mApp = app;
@@ -158,25 +163,27 @@ public class WifiScanModeActivity extends FragmentActivity {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()) return new AlertDialog.Builder(getActivity())
.setMessage(TextUtils.isEmpty(mApp) ? .setMessage(
getString(R.string.wifi_scan_always_turn_on_message_unknown) : TextUtils.isEmpty(mApp)
getString(R.string.wifi_scan_always_turnon_message, mApp)) ? getString(R.string.wifi_scan_always_turn_on_message_unknown)
.setPositiveButton(R.string.wifi_scan_always_confirm_allow, : getString(R.string.wifi_scan_always_turnon_message, mApp))
.setPositiveButton(
R.string.wifi_scan_always_confirm_allow,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
((WifiScanModeActivity) getActivity()).doPositiveClick(); ((WifiScanModeActivity) getActivity()).doPositiveClick();
} }
} })
) .setNegativeButton(
.setNegativeButton(R.string.wifi_scan_always_confirm_deny, R.string.wifi_scan_always_confirm_deny,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
((WifiScanModeActivity) getActivity()).doNegativeClick(); ((WifiScanModeActivity) getActivity()).doNegativeClick();
} }
} })
)
.create(); .create();
} }
@Override @Override
public void onCancel(DialogInterface dialog) { public void onCancel(DialogInterface dialog) {
((WifiScanModeActivity) getActivity()).doNegativeClick(); ((WifiScanModeActivity) getActivity()).doNegativeClick();
@@ -184,9 +191,14 @@ public class WifiScanModeActivity extends FragmentActivity {
} }
private static boolean isGuestUser(Context context) { private static boolean isGuestUser(Context context) {
if (context == null) return false;
final UserManager userManager = context.getSystemService(UserManager.class); final UserManager userManager = context.getSystemService(UserManager.class);
if (userManager == null) return false; if (userManager == null) return false;
return userManager.isGuestUser(); return userManager.isGuestUser();
} }
private static boolean isWifiScanModeConfigAllowed(Context context) {
final UserManager userManager = context.getSystemService(UserManager.class);
if (userManager == null) return true;
return !userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_LOCATION);
}
} }

View File

@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -28,18 +30,26 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.AudioManager; import android.media.AudioManager;
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 android.util.Pair;
import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowAudioManager; import com.android.settings.testutils.shadow.ShadowAudioManager;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager; import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@@ -58,6 +68,10 @@ import java.util.Collection;
public class ConnectedBluetoothDeviceUpdaterTest { public class ConnectedBluetoothDeviceUpdaterTest {
private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
private static final String FAKE_EXCLUSIVE_MANAGER_NAME = "com.fake.name";
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Mock @Mock
private DashboardFragment mDashboardFragment; private DashboardFragment mDashboardFragment;
@@ -69,6 +83,8 @@ public class ConnectedBluetoothDeviceUpdaterTest {
private BluetoothDevice mBluetoothDevice; private BluetoothDevice mBluetoothDevice;
@Mock @Mock
private Drawable mDrawable; private Drawable mDrawable;
@Mock
private PackageManager mPackageManager;
private Context mContext; private Context mContext;
private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater; private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -82,7 +98,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device");
mContext = RuntimeEnvironment.application; mContext = spy(RuntimeEnvironment.application);
mAudioManager = mContext.getSystemService(AudioManager.class); mAudioManager = mContext.getSystemService(AudioManager.class);
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
mShadowBluetoothAdapter.setEnabled(true); mShadowBluetoothAdapter.setEnabled(true);
@@ -92,6 +108,7 @@ public class ConnectedBluetoothDeviceUpdaterTest {
mCachedDevices = new ArrayList<>(); mCachedDevices = new ArrayList<>();
mCachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs);
@@ -320,4 +337,97 @@ public class ConnectedBluetoothDeviceUpdaterTest {
assertThat(btPreference.shouldHideSecondTarget()).isTrue(); assertThat(btPreference.shouldHideSecondTarget()).isTrue();
} }
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_notExclusiveManagedDevice_addDevice() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater
.isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
null);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_notAllowedExclusiveManagedDevice_addDevice() {
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater
.isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
FAKE_EXCLUSIVE_MANAGER_NAME.getBytes());
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_existingExclusivelyManagedDeviceWithPackageInstalled_removePreference()
throws Exception {
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater
.isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_newExclusivelyManagedDeviceWithPackageInstalled_doNotAddPreference()
throws Exception {
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater
.isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_exclusivelyManagedDeviceWithoutPackageInstalled_addDevice()
throws Exception {
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
when(mBluetoothDeviceUpdater
.isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true);
when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager).getPackageInfo(
exclusiveManagerName, 0);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice);
}
} }

View File

@@ -18,6 +18,7 @@ package com.android.settings.bluetooth;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@@ -28,17 +29,26 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; 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 android.util.Pair;
import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@@ -56,6 +66,10 @@ import java.util.List;
public class SavedBluetoothDeviceUpdaterTest { public class SavedBluetoothDeviceUpdaterTest {
private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
private static final String FAKE_EXCLUSIVE_MANAGER_NAME = "com.fake.name";
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Mock @Mock
private DashboardFragment mDashboardFragment; private DashboardFragment mDashboardFragment;
@@ -73,6 +87,8 @@ public class SavedBluetoothDeviceUpdaterTest {
private LocalBluetoothManager mBluetoothManager; private LocalBluetoothManager mBluetoothManager;
@Mock @Mock
private Drawable mDrawable; private Drawable mDrawable;
@Mock
private PackageManager mPackageManager;
private Context mContext; private Context mContext;
private SavedBluetoothDeviceUpdater mBluetoothDeviceUpdater; private SavedBluetoothDeviceUpdater mBluetoothDeviceUpdater;
@@ -84,12 +100,13 @@ public class SavedBluetoothDeviceUpdaterTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device");
mContext = RuntimeEnvironment.application; mContext = spy(RuntimeEnvironment.application);
doReturn(mContext).when(mDashboardFragment).getContext(); doReturn(mContext).when(mDashboardFragment).getContext();
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mContext, mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mContext,
mDevicePreferenceCallback, false, /* metricsCategory= */ 0)); mDevicePreferenceCallback, false, /* metricsCategory= */ 0));
@@ -103,10 +120,10 @@ public class SavedBluetoothDeviceUpdaterTest {
mCachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices); when(mDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices);
} }
@Test @Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_filterMatch_addPreference() { public void update_filterMatch_addPreference() {
doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState(); doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState();
doReturn(false).when(mBluetoothDevice).isConnected(); doReturn(false).when(mBluetoothDevice).isConnected();
@@ -118,6 +135,7 @@ public class SavedBluetoothDeviceUpdaterTest {
} }
@Test @Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_filterNotMatch_removePreference() { public void update_filterNotMatch_removePreference() {
doReturn(BluetoothDevice.BOND_NONE).when(mBluetoothDevice).getBondState(); doReturn(BluetoothDevice.BOND_NONE).when(mBluetoothDevice).getBondState();
doReturn(true).when(mBluetoothDevice).isConnected(); doReturn(true).when(mBluetoothDevice).isConnected();
@@ -298,4 +316,125 @@ public class SavedBluetoothDeviceUpdaterTest {
verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice, verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT); BluetoothDevicePreference.SortType.TYPE_NO_SORT);
} }
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_notExclusivelyManagedDevice_addDevice() {
final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
cachedDevices.add(mCachedBluetoothDevice);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.isConnected()).thenReturn(false);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
null);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_notAllowedExclusivelyManagedDevice_addDevice() {
final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
cachedDevices.add(mCachedBluetoothDevice);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.isConnected()).thenReturn(false);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
FAKE_EXCLUSIVE_MANAGER_NAME.getBytes());
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_existingExclusivelyManagedDeviceWithPackageInstalled_removePreference()
throws Exception {
final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.isConnected()).thenReturn(false);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0);
mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_newExclusivelyManagedDeviceWithPackageInstalled_doNotAddPreference()
throws Exception {
final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
cachedDevices.add(mCachedBluetoothDevice);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.isConnected()).thenReturn(false);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE)
public void update_exclusivelyManagedDeviceWithoutPackageInstalled_addDevice()
throws Exception {
final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
final String exclusiveManagerName =
BluetoothUtils.getExclusiveManagers().stream().findAny().orElse(
FAKE_EXCLUSIVE_MANAGER_NAME);
cachedDevices.add(mCachedBluetoothDevice);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mBluetoothDevice.isConnected()).thenReturn(false);
when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn(
exclusiveManagerName.getBytes());
doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager).getPackageInfo(
exclusiveManagerName, 0);
mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);
verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice,
BluetoothDevicePreference.SortType.TYPE_NO_SORT);
}
} }

View File

@@ -171,6 +171,7 @@ public class CallsAndAlarmsPreferenceControllerTest {
mShadowBluetoothAdapter.setEnabled(false); mShadowBluetoothAdapter.setEnabled(false);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateVisibility(); mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.isVisible()).isFalse();
} }
@@ -180,6 +181,7 @@ public class CallsAndAlarmsPreferenceControllerTest {
mShadowBluetoothAdapter.setEnabled(false); mShadowBluetoothAdapter.setEnabled(false);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateVisibility(); mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.isVisible()).isFalse();
} }
@@ -188,6 +190,7 @@ public class CallsAndAlarmsPreferenceControllerTest {
when(mBroadcast.isEnabled(any())).thenReturn(false); when(mBroadcast.isEnabled(any())).thenReturn(false);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateVisibility(); mController.updateVisibility();
shadowOf(Looper.getMainLooper()).idle();
assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.isVisible()).isFalse();
} }

View File

@@ -20,30 +20,29 @@ import static com.android.settings.fuelgauge.batterytip.tips.BatteryTip.TipType.
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateUtils;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.InstrumentedPreferenceFragment; import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.CardPreference; import com.android.settings.widget.CardPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -53,8 +52,8 @@ public class BatteryTipPreferenceControllerTest {
private static final String KEY_PREF = "battery_tip"; private static final String KEY_PREF = "battery_tip";
private static final String KEY_TIP = "key_battery_tip"; private static final String KEY_TIP = "key_battery_tip";
private static final long AVERAGE_TIME_MS = DateUtils.HOUR_IN_MILLIS;
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTipPreferenceController.BatteryTipListener mBatteryTipListener; @Mock private BatteryTipPreferenceController.BatteryTipListener mBatteryTipListener;
@Mock private PreferenceScreen mPreferenceScreen; @Mock private PreferenceScreen mPreferenceScreen;
@Mock private BatteryTip mBatteryTip; @Mock private BatteryTip mBatteryTip;
@@ -64,21 +63,16 @@ public class BatteryTipPreferenceControllerTest {
private Context mContext; private Context mContext;
private CardPreference mCardPreference; private CardPreference mCardPreference;
private BatteryTipPreferenceController mBatteryTipPreferenceController; private BatteryTipPreferenceController mBatteryTipPreferenceController;
private List<BatteryTip> mOldBatteryTips;
private List<BatteryTip> mNewBatteryTips; private List<BatteryTip> mNewBatteryTips;
private FakeFeatureFactory mFeatureFactory;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mContext = ApplicationProvider.getApplicationContext();
mContext = RuntimeEnvironment.application;
mCardPreference = spy(new CardPreference(mContext)); mCardPreference = new CardPreference(mContext);
when(mPreferenceScreen.getContext()).thenReturn(mContext); when(mPreferenceScreen.getContext()).thenReturn(mContext);
doReturn(mCardPreference).when(mPreferenceScreen).findPreference(KEY_PREF); doReturn(mCardPreference).when(mPreferenceScreen).findPreference(KEY_PREF);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mOldBatteryTips = new ArrayList<>();
mNewBatteryTips = new ArrayList<>(); mNewBatteryTips = new ArrayList<>();
mBatteryTipPreferenceController = buildBatteryTipPreferenceController(); mBatteryTipPreferenceController = buildBatteryTipPreferenceController();
@@ -87,32 +81,32 @@ public class BatteryTipPreferenceControllerTest {
} }
@Test @Test
public void testDisplayPreference_isInvisible() { public void displayPreference_isInvisible() {
mBatteryTipPreferenceController.displayPreference(mPreferenceScreen); mBatteryTipPreferenceController.displayPreference(mPreferenceScreen);
assertThat(mCardPreference.isVisible()).isFalse(); assertThat(mCardPreference.isVisible()).isFalse();
} }
@Test @Test
public void testUpdateBatteryTips_tipsStateInvisible_isInvisible() { public void updateBatteryTips_tipsStateInvisible_isInvisible() {
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips); mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
assertThat(mCardPreference.isVisible()).isFalse(); assertThat(mCardPreference.isVisible()).isFalse();
} }
@Test @Test
public void testGetCurrentBatteryTip_noTips_isNull() { public void getCurrentBatteryTip_noTips_isNull() {
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull(); assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
} }
@Test @Test
public void testGetCurrentBatteryTip_tipsInvisible_isNull() { public void getCurrentBatteryTip_tipsInvisible_isNull() {
mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips); mBatteryTipPreferenceController.updateBatteryTips(mNewBatteryTips);
assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull(); assertThat(mBatteryTipPreferenceController.getCurrentBatteryTip()).isNull();
} }
@Test @Test
public void testRestoreFromNull_shouldNotCrash() { public void restoreFromNull_shouldNotCrash() {
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
// Battery tip list is null at this time // Battery tip list is null at this time
mBatteryTipPreferenceController.saveInstanceState(bundle); mBatteryTipPreferenceController.saveInstanceState(bundle);
@@ -124,7 +118,7 @@ public class BatteryTipPreferenceControllerTest {
} }
@Test @Test
public void testHandlePreferenceTreeClick_noDialog_invokeCallback() { public void handlePreferenceTreeClick_noDialog_invokeCallback() {
when(mBatteryTip.getType()).thenReturn(SMART_BATTERY_MANAGER); when(mBatteryTip.getType()).thenReturn(SMART_BATTERY_MANAGER);
List<BatteryTip> batteryTips = new ArrayList<>(); List<BatteryTip> batteryTips = new ArrayList<>();
batteryTips.add(mBatteryTip); batteryTips.add(mBatteryTip);

View File

@@ -17,8 +17,6 @@ package com.android.settings.fuelgauge.batterytip.tips;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -27,6 +25,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
@@ -34,12 +33,13 @@ import com.android.settings.widget.CardPreference;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLog; import org.robolectric.shadows.ShadowLog;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@@ -49,23 +49,22 @@ public class BatteryDefenderTipTest {
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private BatteryDefenderTip mBatteryDefenderTip; private BatteryDefenderTip mBatteryDefenderTip;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
private CardPreference mCardPreference;
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTip mBatteryTip; @Mock private BatteryTip mBatteryTip;
@Mock private Preference mPreference; @Mock private Preference mPreference;
@Mock private CardPreference mCardPreference;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mContext = ApplicationProvider.getApplicationContext();
mFeatureFactory = FakeFeatureFactory.setupForTest(); mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider; mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
mContext = RuntimeEnvironment.application;
mBatteryDefenderTip = mBatteryDefenderTip =
new BatteryDefenderTip(BatteryTip.StateType.NEW, false /* isPluggedIn */); new BatteryDefenderTip(BatteryTip.StateType.NEW, /* isPluggedIn= */ false);
mCardPreference = new CardPreference(mContext);
when(mPreference.getContext()).thenReturn(mContext); when(mPreference.getContext()).thenReturn(mContext);
when(mCardPreference.getContext()).thenReturn(mContext);
} }
@Test @Test
@@ -87,7 +86,7 @@ public class BatteryDefenderTipTest {
} }
@Test @Test
public void testLog_logMetric() { public void log_logMetric() {
mBatteryDefenderTip.updateState(mBatteryTip); mBatteryDefenderTip.updateState(mBatteryTip);
mBatteryDefenderTip.log(mContext, mMetricsFeatureProvider); mBatteryDefenderTip.log(mContext, mMetricsFeatureProvider);
@@ -108,7 +107,7 @@ public class BatteryDefenderTipTest {
mBatteryDefenderTip.updatePreference(mCardPreference); mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonText(expectedText); assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo(expectedText);
} }
@Test @Test
@@ -117,46 +116,31 @@ public class BatteryDefenderTipTest {
mBatteryDefenderTip.updatePreference(mCardPreference); mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setSecondaryButtonText(expected); assertThat(mCardPreference.getSecondaryButtonText()).isEqualTo(expected);
} }
@Test @Test
public void updatePreference_shouldSetPrimaryButtonVisible() { public void updatePreference_shouldSetPrimaryButtonVisible() {
mBatteryDefenderTip.updatePreference(mCardPreference); mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonVisibility(true); assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
} }
@Test @Test
public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() { public void updatePreference_whenCharging_setPrimaryButtonVisibleToBeTrue() {
mBatteryDefenderTip = mBatteryDefenderTip =
new BatteryDefenderTip(BatteryTip.StateType.NEW, true /* isPluggedIn */); new BatteryDefenderTip(BatteryTip.StateType.NEW, /* isPluggedIn= */ true);
mBatteryDefenderTip.updatePreference(mCardPreference); mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonVisibility(true); assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
} }
@Test @Test
public void updatePreference_whenNotCharging_setSecondaryButtonVisibleToBeFalse() { public void updatePreference_whenNotCharging_setSecondaryButtonVisibleToBeFalse() {
mBatteryDefenderTip.updatePreference(mCardPreference); mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setSecondaryButtonVisibility(false); assertThat(mCardPreference.getSecondaryButtonVisibility()).isFalse();
}
@Test
public void updatePreference_whenGetChargingStatusFailed_setSecondaryButtonVisibleToBeFalse() {
fakeGetChargingStatusFailed();
mBatteryDefenderTip.updatePreference(mCardPreference);
verify(mCardPreference).setSecondaryButtonVisibility(false);
}
private void fakeGetChargingStatusFailed() {
Context mockContext = mock(Context.class);
when(mockContext.getString(anyInt())).thenReturn("fake_string");
when(mCardPreference.getContext()).thenReturn(mockContext);
} }
private String getLastErrorLog() { private String getLastErrorLog() {

View File

@@ -20,11 +20,10 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.view.View;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder; import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.widget.CardPreference; import com.android.settings.widget.CardPreference;
@@ -32,10 +31,12 @@ import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.testutils.DrawableTestHelper; import com.android.settingslib.testutils.DrawableTestHelper;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -47,13 +48,15 @@ public class BatteryTipTest {
private static final String SUMMARY = "summary"; private static final String SUMMARY = "summary";
@DrawableRes private static final int ICON_ID = R.drawable.ic_fingerprint; @DrawableRes private static final int ICON_ID = R.drawable.ic_fingerprint;
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
private Context mContext; private Context mContext;
private TestBatteryTip mBatteryTip; private TestBatteryTip mBatteryTip;
@Before @Before
public void setUp() { public void setUp() {
mContext = RuntimeEnvironment.application;
mBatteryTip = new TestBatteryTip(); mBatteryTip = new TestBatteryTip();
mContext = ApplicationProvider.getApplicationContext();
} }
@Test @Test
@@ -84,19 +87,14 @@ public class BatteryTipTest {
@Test @Test
public void updatePreference_resetLayoutState() { public void updatePreference_resetLayoutState() {
mContext.setTheme(R.style.Theme_Settings);
PreferenceViewHolder holder =
PreferenceViewHolder.createInstanceForTests(
View.inflate(
mContext, R.layout.card_preference_layout, /* parent= */ null));
CardPreference cardPreference = new CardPreference(mContext); CardPreference cardPreference = new CardPreference(mContext);
cardPreference.onBindViewHolder(holder);
cardPreference.setPrimaryButtonVisibility(true); cardPreference.setPrimaryButtonVisibility(true);
cardPreference.setSecondaryButtonVisibility(true);
mBatteryTip.updatePreference(cardPreference); mBatteryTip.updatePreference(cardPreference);
View view = holder.findViewById(R.id.card_preference_buttons); assertThat(cardPreference.getPrimaryButtonVisibility()).isFalse();
assertThat(view.getVisibility()).isEqualTo(View.GONE); assertThat(cardPreference.getSecondaryButtonVisibility()).isFalse();
} }
@Test @Test

View File

@@ -25,6 +25,7 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
@@ -32,12 +33,13 @@ import com.android.settings.widget.CardPreference;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLog; import org.robolectric.shadows.ShadowLog;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@@ -47,22 +49,21 @@ public final class IncompatibleChargerTipTest {
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
private IncompatibleChargerTip mIncompatibleChargerTip; private IncompatibleChargerTip mIncompatibleChargerTip;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
private CardPreference mCardPreference;
@Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock private BatteryTip mBatteryTip; @Mock private BatteryTip mBatteryTip;
@Mock private Preference mPreference; @Mock private Preference mPreference;
@Mock private CardPreference mCardPreference;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
mFeatureFactory = FakeFeatureFactory.setupForTest(); mFeatureFactory = FakeFeatureFactory.setupForTest();
mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider; mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider;
mContext = RuntimeEnvironment.application; mContext = ApplicationProvider.getApplicationContext();
mIncompatibleChargerTip = new IncompatibleChargerTip(BatteryTip.StateType.NEW); mIncompatibleChargerTip = new IncompatibleChargerTip(BatteryTip.StateType.NEW);
mCardPreference = new CardPreference(mContext);
when(mPreference.getContext()).thenReturn(mContext); when(mPreference.getContext()).thenReturn(mContext);
when(mCardPreference.getContext()).thenReturn(mContext);
} }
@Test @Test
@@ -107,13 +108,13 @@ public final class IncompatibleChargerTipTest {
mIncompatibleChargerTip.updatePreference(mCardPreference); mIncompatibleChargerTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonText(expected); assertThat(mCardPreference.getPrimaryButtonText()).isEqualTo(expected);
} }
@Test @Test
public void updatePreference_shouldSetSecondaryButtonVisible() { public void updatePreference_shouldSetSecondaryButtonVisible() {
mIncompatibleChargerTip.updatePreference(mCardPreference); mIncompatibleChargerTip.updatePreference(mCardPreference);
verify(mCardPreference).setPrimaryButtonVisibility(true); assertThat(mCardPreference.getPrimaryButtonVisibility()).isTrue();
} }
private String getLastErrorLog() { private String getLastErrorLog() {

View File

@@ -21,7 +21,6 @@ import android.app.AppOpsManager
import android.content.Context import android.content.Context
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.junit4.createComposeRule
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
@@ -270,7 +269,7 @@ class WifiControlAppListModelTest {
private class FakeAppOpsController(private val fakeMode: Int) : IAppOpsController { private class FakeAppOpsController(private val fakeMode: Int) : IAppOpsController {
var setAllowedCalledWith: Boolean? = null var setAllowedCalledWith: Boolean? = null
override val mode = MutableLiveData(fakeMode) override val mode = flowOf(fakeMode)
override fun setAllowed(allowed: Boolean) { override fun setAllowed(allowed: Boolean) {
setAllowedCalledWith = allowed setAllowedCalledWith = allowed

View File

@@ -24,7 +24,6 @@ import android.companion.AssociationRequest
import android.content.Context import android.content.Context
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.platform.test.flag.junit.SetFlagsRule import android.platform.test.flag.junit.SetFlagsRule
import androidx.lifecycle.MutableLiveData
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.media.flags.Flags import com.android.media.flags.Flags
@@ -33,6 +32,7 @@ import com.android.settings.testutils.FakeFeatureFactory
import com.android.settingslib.spaprivileged.model.app.IAppOpsController import com.android.settingslib.spaprivileged.model.app.IAppOpsController
import com.android.settingslib.spaprivileged.template.app.AppOpPermissionRecord import com.android.settingslib.spaprivileged.template.app.AppOpPermissionRecord
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@@ -223,16 +223,13 @@ class MediaRoutingControlTest {
private class FakeAppOpsController(fakeMode: Int) : IAppOpsController { private class FakeAppOpsController(fakeMode: Int) : IAppOpsController {
override val mode = MutableLiveData(fakeMode) override val mode = MutableStateFlow(fakeMode)
override fun setAllowed(allowed: Boolean) { override fun setAllowed(allowed: Boolean) {
if (allowed) mode.value = if (allowed) AppOpsManager.MODE_ALLOWED else AppOpsManager.MODE_ERRORED
mode.postValue(AppOpsManager.MODE_ALLOWED)
else
mode.postValue(AppOpsManager.MODE_ERRORED)
} }
override fun getMode(): Int = mode.value!! override fun getMode(): Int = mode.value
} }
companion object { companion object {