Merge changes from topics "low_battery_tip", "new_settings_card" into main
* changes: [BatteryTip] Implement new CardPreference to apply new style [BatteryTips] Separate the low battery tips
This commit is contained in:
@@ -22,11 +22,17 @@ import android.content.Context;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BatterySettingsFeatureProviderImplTest {
|
||||
private BatterySettingsFeatureProviderImpl mImpl;
|
||||
@@ -52,4 +58,15 @@ public class BatterySettingsFeatureProviderImplTest {
|
||||
public void isBatteryInfoEnabled_returnFalse() {
|
||||
assertThat(mImpl.isBatteryInfoEnabled(mContext)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBatteryTipDetector_containsLowBatteryTip() {
|
||||
var tips = new ArrayList<BatteryTip>();
|
||||
|
||||
mImpl.addBatteryTipDetector(
|
||||
mContext, tips, new BatteryInfo(), new BatteryTipPolicy(mContext));
|
||||
|
||||
var expectedResult = tips.stream().anyMatch(tip -> tip instanceof LowBatteryTip);
|
||||
assertThat(expectedResult).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,20 +19,25 @@ package com.android.settings.fuelgauge.batterytip.detectors;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.fuelgauge.BatteryInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -40,73 +45,79 @@ import java.util.concurrent.TimeUnit;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class LowBatteryDetectorTest {
|
||||
|
||||
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Mock private BatteryInfo mBatteryInfo;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
|
||||
private BatteryTipPolicy mBatteryTipPolicy;
|
||||
private LowBatteryDetector mLowBatteryDetector;
|
||||
private Context mContext;
|
||||
private PowerManager mPowerManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mBatteryTipPolicy = spy(new BatteryTipPolicy(mContext));
|
||||
|
||||
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
|
||||
mContext = RuntimeEnvironment.application;
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
|
||||
mPowerManager = mContext.getSystemService(PowerManager.class);
|
||||
shadowOf(mPowerManager).setIsPowerSaveMode(false);
|
||||
|
||||
ReflectionHelpers.setField(mBatteryTipPolicy, "lowBatteryEnabled", true);
|
||||
mBatteryInfo.discharging = true;
|
||||
|
||||
mLowBatteryDetector =
|
||||
new LowBatteryDetector(
|
||||
mContext, mPolicy, mBatteryInfo, false /* isPowerSaveMode */);
|
||||
mLowBatteryDetector = new LowBatteryDetector(mContext, mBatteryTipPolicy, mBatteryInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
|
||||
mLowBatteryDetector =
|
||||
new LowBatteryDetector(mContext, mPolicy, mBatteryInfo, true /* isPowerSaveMode */);
|
||||
public void detect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mBatteryTipPolicy, "lowBatteryEnabled", false);
|
||||
shadowOf(mPowerManager).setIsPowerSaveMode(true);
|
||||
mLowBatteryDetector = new LowBatteryDetector(mContext, mBatteryTipPolicy, mBatteryInfo);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_enabledByTest_tipNew() {
|
||||
ReflectionHelpers.setField(mPolicy, "testLowBatteryTip", true);
|
||||
public void detect_enabledByTest_tipNew() {
|
||||
ReflectionHelpers.setField(mBatteryTipPolicy, "testLowBatteryTip", true);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_lowBattery_tipNew() {
|
||||
public void detect_lowBattery_tipNew() {
|
||||
mBatteryInfo.batteryLevel = 20;
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.DAYS.toMillis(1);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_batterySaverOn_tipInvisible() {
|
||||
mLowBatteryDetector =
|
||||
new LowBatteryDetector(mContext, mPolicy, mBatteryInfo, true /* isPowerSaveMode */);
|
||||
public void detect_batterySaverOn_tipInvisible() {
|
||||
shadowOf(mPowerManager).setIsPowerSaveMode(true);
|
||||
mLowBatteryDetector = new LowBatteryDetector(mContext, mBatteryTipPolicy, mBatteryInfo);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().getState())
|
||||
.isEqualTo(BatteryTip.StateType.INVISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_charging_tipInvisible() {
|
||||
public void detect_charging_tipInvisible() {
|
||||
mBatteryInfo.discharging = false;
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_lowTimeEstimation_tipInvisible() {
|
||||
public void detect_lowTimeEstimation_tipInvisible() {
|
||||
mBatteryInfo.batteryLevel = 50;
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.MINUTES.toMillis(1);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_noEarlyWarning_tipInvisible() {
|
||||
public void detect_noEarlyWarning_tipInvisible() {
|
||||
mBatteryInfo.remainingTimeUs = TimeUnit.DAYS.toMicros(1);
|
||||
mBatteryInfo.batteryLevel = 100;
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ public class BatteryDefenderTipTest {
|
||||
public void updatePreference_shouldSetPrimaryButtonVisible() {
|
||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||
|
||||
verify(mCardPreference).setPrimaryButtonVisible(true);
|
||||
verify(mCardPreference).setPrimaryButtonVisibility(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,14 +134,14 @@ public class BatteryDefenderTipTest {
|
||||
|
||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||
|
||||
verify(mCardPreference).setPrimaryButtonVisible(true);
|
||||
verify(mCardPreference).setPrimaryButtonVisibility(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePreference_whenNotCharging_setSecondaryButtonVisibleToBeFalse() {
|
||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||
|
||||
verify(mCardPreference).setSecondaryButtonVisible(false);
|
||||
verify(mCardPreference).setSecondaryButtonVisibility(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -150,7 +150,7 @@ public class BatteryDefenderTipTest {
|
||||
|
||||
mBatteryDefenderTip.updatePreference(mCardPreference);
|
||||
|
||||
verify(mCardPreference).setSecondaryButtonVisible(false);
|
||||
verify(mCardPreference).setSecondaryButtonVisibility(false);
|
||||
}
|
||||
|
||||
private void fakeGetChargingStatusFailed() {
|
||||
|
||||
@@ -91,7 +91,7 @@ public class BatteryTipTest {
|
||||
mContext, R.layout.card_preference_layout, /* parent= */ null));
|
||||
CardPreference cardPreference = new CardPreference(mContext);
|
||||
cardPreference.onBindViewHolder(holder);
|
||||
cardPreference.setPrimaryButtonVisible(true);
|
||||
cardPreference.setPrimaryButtonVisibility(true);
|
||||
|
||||
mBatteryTip.updatePreference(cardPreference);
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ public final class IncompatibleChargerTipTest {
|
||||
@Test
|
||||
public void updatePreference_shouldSetSecondaryButtonVisible() {
|
||||
mIncompatibleChargerTip.updatePreference(mCardPreference);
|
||||
verify(mCardPreference).setPrimaryButtonVisible(true);
|
||||
verify(mCardPreference).setPrimaryButtonVisibility(true);
|
||||
}
|
||||
|
||||
private String getLastErrorLog() {
|
||||
|
||||
@@ -1,344 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*Visibility_setGoneForPrimaryButton_buttonGroupIsGone
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.widget;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class CardPreferenceTest {
|
||||
|
||||
private CardPreference mCardPreference;
|
||||
private PreferenceViewHolder mHolder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Context context = ApplicationProvider.getApplicationContext();
|
||||
context.setTheme(R.style.Theme_Settings);
|
||||
mCardPreference = new CardPreference(context);
|
||||
|
||||
mHolder = PreferenceViewHolder.createInstanceForTests(
|
||||
View.inflate(context, R.layout.card_preference_layout, /* parent= */ null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newACardPreference_layoutResourceShouldBeCardPreferenceLayout() {
|
||||
Context context = ApplicationProvider.getApplicationContext();
|
||||
context.setTheme(R.style.SettingsPreferenceTheme);
|
||||
|
||||
CardPreference cardPreference = new CardPreference(context);
|
||||
|
||||
assertThat(cardPreference.getLayoutResource()).isEqualTo(R.layout.card_preference_layout);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_noButtonVisible_buttonsLayoutIsGone() {
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setPrimaryButtonVisibility_buttonsLayoutIsVisible() {
|
||||
mCardPreference.setPrimaryButtonVisible(true);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setPrimaryButtonVisibilityToVisible() {
|
||||
mCardPreference.setPrimaryButtonVisible(true);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getPrimaryButton().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setSecondaryButtonVisibility_buttonsLayoutIsVisible() {
|
||||
mCardPreference.setSecondaryButtonVisible(true);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setSecondaryButtonVisibilityToVisible() {
|
||||
mCardPreference.setSecondaryButtonVisible(true);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getSecondaryButton().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setPrimaryButtonTextToExpectedText() {
|
||||
String expectedText = "primary-button";
|
||||
mCardPreference.setPrimaryButtonText(expectedText);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getPrimaryButton().getText().toString()).isEqualTo(expectedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setSecondaryButtonTextToExpectedText() {
|
||||
String expectedText = "secondary-button";
|
||||
mCardPreference.setSecondaryButtonText(expectedText);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getSecondaryButton().getText().toString()).isEqualTo(expectedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_initialTextForPrimaryButtonShouldBeEmpty() {
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getPrimaryButton().getText().toString()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_initialTextForSecondaryButtonShouldBeEmpty() {
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getSecondaryButton().getText().toString()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void performClickOnPrimaryButton_callClickListener() {
|
||||
final boolean[] hasCalled = {false};
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = true;
|
||||
mCardPreference.setPrimaryButtonClickListener(clickListener);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
getPrimaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void performClickOnSecondaryButton_callClickListener() {
|
||||
final boolean[] hasCalled = {false};
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = true;
|
||||
mCardPreference.setSecondaryButtonClickListener(clickListener);
|
||||
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
getSecondaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_primaryButtonDefaultIsGone() {
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getPrimaryButton().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_secondaryButtonDefaultIsGone() {
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(getSecondaryButton().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonVisibility_setTrueAfterBindViewHolder_isVisible() {
|
||||
mCardPreference.setPrimaryButtonVisible(false);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setPrimaryButtonVisible(true);
|
||||
|
||||
assertThat(getPrimaryButton().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonText_setAfterBindViewHolder_setOnUi() {
|
||||
String expectedText = "123456";
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setPrimaryButtonText(expectedText);
|
||||
|
||||
assertThat(getPrimaryButton().getText().toString()).isEqualTo(expectedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonText_setNull_isEmptyText() {
|
||||
final String emptyString = "";
|
||||
mCardPreference.setPrimaryButtonText("1234");
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setPrimaryButtonText(null);
|
||||
|
||||
assertThat(getPrimaryButton().getText().toString()).isEqualTo(emptyString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonClickListener_setAfterOnBindViewHolder() {
|
||||
final String[] hasCalled = {""};
|
||||
String expectedClickedResult = "was called";
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = expectedClickedResult;
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setPrimaryButtonClickListener(clickListener);
|
||||
getPrimaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isEqualTo(expectedClickedResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonClickListener_setNull_clearTheOnClickListener() {
|
||||
final String[] hasCalled = {"not called"};
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = "called once";
|
||||
mCardPreference.setPrimaryButtonClickListener(clickListener);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setPrimaryButtonClickListener(null);
|
||||
getPrimaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isEqualTo("not called");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonVisibility_setTrueAfterBindViewHolder_isVisible() {
|
||||
mCardPreference.setSecondaryButtonVisible(false);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setSecondaryButtonVisible(true);
|
||||
|
||||
assertThat(getSecondaryButton().getVisibility()).isEqualTo(VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonText_setAfterBindViewHolder_setOnUi() {
|
||||
String expectedText = "10101010";
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setSecondaryButtonText(expectedText);
|
||||
|
||||
assertThat(getSecondaryButton().getText().toString()).isEqualTo(expectedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonText_setNull_isEmptyText() {
|
||||
String emptyString = "";
|
||||
mCardPreference.setSecondaryButtonText("1234");
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setSecondaryButtonText(null);
|
||||
|
||||
assertThat(getSecondaryButton().getText().toString()).isEqualTo(emptyString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonClickListener_setAfterOnBindViewHolder() {
|
||||
final String[] hasCalled = {""};
|
||||
String expectedClickedResult = "2nd was called";
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = expectedClickedResult;
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setSecondaryButtonClickListener(clickListener);
|
||||
getSecondaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isEqualTo(expectedClickedResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonClickListener_setNull_clearTheOnClickListener() {
|
||||
final String[] hasCalled = {"not called"};
|
||||
View.OnClickListener clickListener = v -> hasCalled[0] = "called once";
|
||||
mCardPreference.setSecondaryButtonClickListener(clickListener);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.setSecondaryButtonClickListener(null);
|
||||
getSecondaryButton().performClick();
|
||||
|
||||
assertThat(hasCalled[0]).isEqualTo("not called");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPrimaryButtonVisibility_setGoneForSecondaryButton_buttonGroupIsGone() {
|
||||
mCardPreference.setPrimaryButtonVisible(true);
|
||||
mCardPreference.setSecondaryButtonVisible(false);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
assertWithMessage("PreCondition: buttonsView should be Visible")
|
||||
.that(getCardPreferenceButtonsView().getVisibility())
|
||||
.isEqualTo(VISIBLE);
|
||||
|
||||
mCardPreference.setPrimaryButtonVisible(false);
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSecondaryButtonVisibility_setGoneForPrimaryButton_buttonGroupIsGone() {
|
||||
mCardPreference.setPrimaryButtonVisible(false);
|
||||
mCardPreference.setSecondaryButtonVisible(true);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
assertWithMessage("PreCondition: buttonsView should be Visible")
|
||||
.that(getCardPreferenceButtonsView().getVisibility())
|
||||
.isEqualTo(VISIBLE);
|
||||
|
||||
mCardPreference.setSecondaryButtonVisible(false);
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resetLayoutState_buttonGroupIsGone() {
|
||||
mCardPreference.setPrimaryButtonVisible(true);
|
||||
mCardPreference.setSecondaryButtonVisible(true);
|
||||
mCardPreference.onBindViewHolder(mHolder);
|
||||
|
||||
mCardPreference.resetLayoutState();
|
||||
|
||||
assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE);
|
||||
}
|
||||
|
||||
private View getCardPreferenceButtonsView() {
|
||||
return mHolder.findViewById(R.id.card_preference_buttons);
|
||||
}
|
||||
|
||||
private Button getPrimaryButton() {
|
||||
return (Button) mHolder.findViewById(android.R.id.button1);
|
||||
}
|
||||
|
||||
private Button getSecondaryButton() {
|
||||
return (Button) mHolder.findViewById(android.R.id.button2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.widget
|
||||
|
||||
import android.content.Context
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.test.assertContentDescriptionEquals
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotDisplayed
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.android.settings.R
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class CardPreferenceTest {
|
||||
|
||||
@get:Rule val composeTestRule = createComposeRule()
|
||||
private lateinit var context: Context
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
context = ApplicationProvider.getApplicationContext()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun enableDismiss_whenEnable_shouldBeDisplayed() {
|
||||
composeTestRule.setContent { buildCardPreference(enableDismiss = true) }
|
||||
|
||||
composeTestRule.onNodeWithContentDescription("Dismiss").assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun enableDismiss_whenDisable_shouldBeDisplayed() {
|
||||
composeTestRule.setContent { buildCardPreference(enableDismiss = false) }
|
||||
|
||||
composeTestRule.onNodeWithContentDescription("Dismiss").assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun primaryButton_whenVisible_shouldBeDisplayed() {
|
||||
val expectedPrimaryButtonText = "You can see me"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
primaryButtonText = expectedPrimaryButtonText,
|
||||
primaryButtonVisibility = true,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(expectedPrimaryButtonText).assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun primaryButton_whenInvisible_shouldBeDisplayed() {
|
||||
val expectedButtonText = "You cannot see me"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
primaryButtonText = expectedButtonText,
|
||||
primaryButtonVisibility = false,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(expectedButtonText).assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun primaryButtonAction_whenClick_performAction() {
|
||||
val buttonText = "click me"
|
||||
var clicked = false
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
primaryButtonText = buttonText,
|
||||
primaryButtonVisibility = true,
|
||||
primaryButtonAction = { clicked = true }
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).performClick()
|
||||
|
||||
assertThat(clicked).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun primaryButtonContentDescription_whenSet_shouldBeExists() {
|
||||
val expectedText = "this is a content description"
|
||||
val buttonText = "primary-button"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
primaryButtonText = buttonText,
|
||||
primaryButtonContentDescription = expectedText,
|
||||
primaryButtonVisibility = true,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).assertContentDescriptionEquals(expectedText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun secondaryButton_whenVisible_shouldBeDisplayed() {
|
||||
val expectedSecondaryButtonText = "You can see me"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
secondaryButtonText = expectedSecondaryButtonText,
|
||||
secondaryButtonVisibility = true,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(expectedSecondaryButtonText).assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun secondaryButton_whenInvisible_shouldBeDisplayed() {
|
||||
val expectedButtonText = "You cannot see me"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
secondaryButtonText = expectedButtonText,
|
||||
secondaryButtonVisibility = false,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(expectedButtonText).assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun secondaryButtonAction_whenClick_performAction() {
|
||||
val buttonText = "click me2"
|
||||
var clicked = false
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
secondaryButtonText = buttonText,
|
||||
secondaryButtonVisibility = true,
|
||||
secondaryButtonAction = { clicked = true }
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).performClick()
|
||||
|
||||
assertThat(clicked).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun secondaryButtonContentDescription_whenSet_shouldBeExists() {
|
||||
val expectedText = "found bug yay"
|
||||
val buttonText = "secondary-button"
|
||||
composeTestRule.setContent {
|
||||
buildCardPreference(
|
||||
secondaryButtonText = buttonText,
|
||||
secondaryButtonContentDescription = expectedText,
|
||||
secondaryButtonVisibility = true,
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).assertContentDescriptionEquals(expectedText)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resetLayoutState_shouldRemoveThePrimaryButton() {
|
||||
val buttonText = "9527"
|
||||
val cardPreference =
|
||||
CardPreference(context)
|
||||
.apply {
|
||||
primaryButtonText = buttonText
|
||||
primaryButtonVisibility = true
|
||||
}
|
||||
.also { it.buildContent() }
|
||||
|
||||
cardPreference.resetLayoutState()
|
||||
composeTestRule.setContent { cardPreference.Content() }
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resetLayoutState_shouldRemoveTheSecondaryButton() {
|
||||
val buttonText = "4567"
|
||||
val cardPreference =
|
||||
CardPreference(context)
|
||||
.apply {
|
||||
secondaryButtonText = buttonText
|
||||
secondaryButtonVisibility = true
|
||||
}
|
||||
.also { it.buildContent() }
|
||||
|
||||
cardPreference.resetLayoutState()
|
||||
composeTestRule.setContent { cardPreference.Content() }
|
||||
|
||||
composeTestRule.onNodeWithText(buttonText).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun buildCardPreference(
|
||||
iconResId: Int? = R.drawable.ic_battery_status_protected_24dp,
|
||||
primaryButtonText: String = "primary text",
|
||||
primaryButtonContentDescription: String? = "primary description",
|
||||
primaryButtonAction: () -> Unit = {},
|
||||
primaryButtonVisibility: Boolean = false,
|
||||
secondaryButtonText: String = "secondary button",
|
||||
secondaryButtonContentDescription: String? = null,
|
||||
secondaryButtonAction: () -> Unit = {},
|
||||
secondaryButtonVisibility: Boolean = false,
|
||||
enableDismiss: Boolean = true,
|
||||
) =
|
||||
CardPreference(context)
|
||||
.apply {
|
||||
this.iconResId = iconResId
|
||||
this.primaryButtonText = primaryButtonText
|
||||
this.primaryButtonContentDescription = primaryButtonContentDescription
|
||||
this.primaryButtonAction = primaryButtonAction
|
||||
this.primaryButtonVisibility = primaryButtonVisibility
|
||||
this.secondaryButtonText = secondaryButtonText
|
||||
this.secondaryButtonContentDescription = secondaryButtonContentDescription
|
||||
this.secondaryButtonAction = secondaryButtonAction
|
||||
this.secondaryButtonVisibility = secondaryButtonVisibility
|
||||
this.enableDismiss(enableDismiss)
|
||||
}
|
||||
.also { it.buildContent() }
|
||||
.Content()
|
||||
}
|
||||
Reference in New Issue
Block a user