Merge "Refactor Battery Chart View State Controll"

This commit is contained in:
TreeHugger Robot
2022-07-20 13:34:35 +00:00
committed by Android (Google) Code Review
3 changed files with 42 additions and 45 deletions

View File

@@ -347,6 +347,7 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
trapezoidIndex, mBatteryIndexedMap.size(), isForce)); trapezoidIndex, mBatteryIndexedMap.size(), isForce));
mTrapezoidIndex = trapezoidIndex; mTrapezoidIndex = trapezoidIndex;
mBatteryChartView.setSelectedIndex(mTrapezoidIndex);
mHandler.post(() -> { mHandler.post(() -> {
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
removeAndCacheAllPrefs(); removeAndCacheAllPrefs();

View File

@@ -110,9 +110,11 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
@VisibleForTesting @VisibleForTesting
Paint mTrapezoidCurvePaint = null; Paint mTrapezoidCurvePaint = null;
private TrapezoidSlot[] mTrapezoidSlots; @VisibleForTesting
TrapezoidSlot[] mTrapezoidSlots;
// Records the location to calculate selected index. // Records the location to calculate selected index.
private float mTouchUpEventX = Float.MIN_VALUE; @VisibleForTesting
float mTouchUpEventX = Float.MIN_VALUE;
private BatteryChartViewV2.OnSelectListener mOnSelectListener; private BatteryChartViewV2.OnSelectListener mOnSelectListener;
public BatteryChartViewV2(Context context) { public BatteryChartViewV2(Context context) {
@@ -161,10 +163,6 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
if (mSelectedIndex != index) { if (mSelectedIndex != index) {
mSelectedIndex = index; mSelectedIndex = index;
invalidate(); invalidate();
// Callbacks to the listener if we have.
if (mOnSelectListener != null) {
mOnSelectListener.onSelect(mSelectedIndex);
}
} }
} }
@@ -301,11 +299,9 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
|| !isValidToDraw(trapezoidIndex)) { || !isValidToDraw(trapezoidIndex)) {
return; return;
} }
// Selects all if users click the same trapezoid item two times. if (mOnSelectListener != null) {
if (trapezoidIndex == mSelectedIndex) { mOnSelectListener.onSelect(
setSelectedIndex(SELECTED_INDEX_ALL); trapezoidIndex == mSelectedIndex ? SELECTED_INDEX_ALL : trapezoidIndex);
} else {
setSelectedIndex(trapezoidIndex);
} }
view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK); view.performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK);
} }
@@ -614,7 +610,8 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
} }
// A container class for each trapezoid left and right location. // A container class for each trapezoid left and right location.
private static final class TrapezoidSlot { @VisibleForTesting
static final class TrapezoidSlot {
public float mLeft; public float mLeft;
public float mRight; public float mRight;

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
import android.accessibilityservice.AccessibilityServiceInfo; import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context; import android.content.Context;
import android.os.LocaleList; import android.os.LocaleList;
import android.view.View;
import android.view.accessibility.AccessibilityManager; import android.view.accessibility.AccessibilityManager;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider; import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
@@ -55,6 +56,8 @@ public final class BatteryChartViewV2Test {
private AccessibilityServiceInfo mMockAccessibilityServiceInfo; private AccessibilityServiceInfo mMockAccessibilityServiceInfo;
@Mock @Mock
private AccessibilityManager mMockAccessibilityManager; private AccessibilityManager mMockAccessibilityManager;
@Mock
private View mMockView;
@Before @Before
public void setUp() { public void setUp() {
@@ -74,13 +77,13 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testIsAccessibilityEnabled_disable_returnFalse() { public void isAccessibilityEnabled_disable_returnFalse() {
doReturn(false).when(mMockAccessibilityManager).isEnabled(); doReturn(false).when(mMockAccessibilityManager).isEnabled();
assertThat(BatteryChartViewV2.isAccessibilityEnabled(mContext)).isFalse(); assertThat(BatteryChartViewV2.isAccessibilityEnabled(mContext)).isFalse();
} }
@Test @Test
public void testIsAccessibilityEnabled_emptyInfo_returnFalse() { public void isAccessibilityEnabled_emptyInfo_returnFalse() {
doReturn(true).when(mMockAccessibilityManager).isEnabled(); doReturn(true).when(mMockAccessibilityManager).isEnabled();
doReturn(new ArrayList<AccessibilityServiceInfo>()) doReturn(new ArrayList<AccessibilityServiceInfo>())
.when(mMockAccessibilityManager) .when(mMockAccessibilityManager)
@@ -90,45 +93,41 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testIsAccessibilityEnabled_validServiceId_returnTrue() { public void isAccessibilityEnabled_validServiceId_returnTrue() {
doReturn(true).when(mMockAccessibilityManager).isEnabled(); doReturn(true).when(mMockAccessibilityManager).isEnabled();
assertThat(BatteryChartViewV2.isAccessibilityEnabled(mContext)).isTrue(); assertThat(BatteryChartViewV2.isAccessibilityEnabled(mContext)).isTrue();
} }
@Test @Test
public void testSetSelectedIndex_invokesCallback() { public void onClick_invokesCallback() {
mBatteryChartView.setLevels(new int[] {90, 80, 70, 60});
for (int i = 0; i < mBatteryChartView.mTrapezoidSlots.length; i++) {
mBatteryChartView.mTrapezoidSlots[i] = new BatteryChartViewV2.TrapezoidSlot();
mBatteryChartView.mTrapezoidSlots[i].mLeft = i;
mBatteryChartView.mTrapezoidSlots[i].mRight = i + 0.5f;
}
mBatteryChartView.mSelectedIndex = 2;
final int[] selectedIndex = new int[1]; final int[] selectedIndex = new int[1];
final int expectedIndex = 2;
mBatteryChartView.mSelectedIndex = 1;
mBatteryChartView.setOnSelectListener( mBatteryChartView.setOnSelectListener(
trapezoidIndex -> { trapezoidIndex -> {
selectedIndex[0] = trapezoidIndex; selectedIndex[0] = trapezoidIndex;
}); });
mBatteryChartView.setSelectedIndex(expectedIndex); // Verify onClick() a different index 1.
mBatteryChartView.mTouchUpEventX = 1;
selectedIndex[0] = Integer.MIN_VALUE;
mBatteryChartView.onClick(mMockView);
assertThat(selectedIndex[0]).isEqualTo(1);
assertThat(mBatteryChartView.mSelectedIndex) // Verify onClick() the same index 2.
.isEqualTo(expectedIndex); mBatteryChartView.mTouchUpEventX = 2;
assertThat(selectedIndex[0]).isEqualTo(expectedIndex); selectedIndex[0] = Integer.MIN_VALUE;
mBatteryChartView.onClick(mMockView);
assertThat(selectedIndex[0]).isEqualTo(BatteryChartViewV2.SELECTED_INDEX_ALL);
} }
@Test @Test
public void testSetSelectedIndex_sameIndex_notInvokesCallback() { public void clickable_isChartGraphSlotsEnabledIsFalse_notClickable() {
final int[] selectedIndex = new int[1];
final int expectedIndex = 1;
mBatteryChartView.mSelectedIndex = expectedIndex;
mBatteryChartView.setOnSelectListener(
trapezoidIndex -> {
selectedIndex[0] = trapezoidIndex;
});
mBatteryChartView.setSelectedIndex(expectedIndex);
assertThat(selectedIndex[0]).isNotEqualTo(expectedIndex);
}
@Test
public void testClickable_isChartGraphSlotsEnabledIsFalse_notClickable() {
mBatteryChartView.setClickableForce(true); mBatteryChartView.setClickableForce(true);
when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext)) when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext))
.thenReturn(false); .thenReturn(false);
@@ -139,7 +138,7 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testClickable_accessibilityIsDisabled_clickable() { public void clickable_accessibilityIsDisabled_clickable() {
mBatteryChartView.setClickableForce(true); mBatteryChartView.setClickableForce(true);
when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext)) when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext))
.thenReturn(true); .thenReturn(true);
@@ -151,7 +150,7 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testClickable_accessibilityIsEnabledWithoutValidId_clickable() { public void clickable_accessibilityIsEnabledWithoutValidId_clickable() {
mBatteryChartView.setClickableForce(true); mBatteryChartView.setClickableForce(true);
when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext)) when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext))
.thenReturn(true); .thenReturn(true);
@@ -166,7 +165,7 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testClickable_accessibilityIsEnabledWithValidId_notClickable() { public void clickable_accessibilityIsEnabledWithValidId_notClickable() {
mBatteryChartView.setClickableForce(true); mBatteryChartView.setClickableForce(true);
when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext)) when(mPowerUsageFeatureProvider.isChartGraphSlotsEnabled(mContext))
.thenReturn(true); .thenReturn(true);
@@ -178,7 +177,7 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testClickable_restoreFromNonClickableState() { public void clickable_restoreFromNonClickableState() {
final int[] levels = new int[13]; final int[] levels = new int[13];
for (int index = 0; index < levels.length; index++) { for (int index = 0; index < levels.length; index++) {
levels[index] = index + 1; levels[index] = index + 1;
@@ -200,14 +199,14 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testOnAttachedToWindow_addAccessibilityStateChangeListener() { public void onAttachedToWindow_addAccessibilityStateChangeListener() {
mBatteryChartView.onAttachedToWindow(); mBatteryChartView.onAttachedToWindow();
verify(mMockAccessibilityManager) verify(mMockAccessibilityManager)
.addAccessibilityStateChangeListener(mBatteryChartView); .addAccessibilityStateChangeListener(mBatteryChartView);
} }
@Test @Test
public void testOnDetachedFromWindow_removeAccessibilityStateChangeListener() { public void onDetachedFromWindow_removeAccessibilityStateChangeListener() {
mBatteryChartView.onAttachedToWindow(); mBatteryChartView.onAttachedToWindow();
mBatteryChartView.mHandler.postDelayed( mBatteryChartView.mHandler.postDelayed(
mBatteryChartView.mUpdateClickableStateRun, 1000); mBatteryChartView.mUpdateClickableStateRun, 1000);
@@ -222,7 +221,7 @@ public final class BatteryChartViewV2Test {
} }
@Test @Test
public void testOnAccessibilityStateChanged_postUpdateStateRunnable() { public void onAccessibilityStateChanged_postUpdateStateRunnable() {
mBatteryChartView.mHandler = spy(mBatteryChartView.mHandler); mBatteryChartView.mHandler = spy(mBatteryChartView.mHandler);
mBatteryChartView.onAccessibilityStateChanged(/*enabled=*/ true); mBatteryChartView.onAccessibilityStateChanged(/*enabled=*/ true);