Merge "Use MainSwitchPreference on Bubbles, Screen Saver and One-Handed mode pages." into sc-dev

This commit is contained in:
Stanley Wang
2021-05-05 03:19:26 +00:00
committed by Android (Google) Code Review
12 changed files with 169 additions and 186 deletions

View File

@@ -16,43 +16,37 @@
package com.android.settings.dream;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.dream.DreamBackend;
import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.MainSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class StartNowPreferenceControllerTest {
private StartNowPreferenceController mController;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private PreferenceScreen mScreen;
@Mock
private LayoutPreference mLayoutPref;
@Mock
private Button mButton;
private MainSwitchPreference mPref;
@Mock
private DreamBackend mBackend;
@@ -60,36 +54,38 @@ public class StartNowPreferenceControllerTest {
public void setup() {
MockitoAnnotations.initMocks(this);
mController = new StartNowPreferenceController(mContext);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPref);
when(mLayoutPref.findViewById(R.id.dream_start_now_button)).thenReturn(mButton);
mContext = spy(RuntimeEnvironment.application);
mController = new StartNowPreferenceController(mContext, "key");
mPref = mock(MainSwitchPreference.class);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPref);
ReflectionHelpers.setField(mController, "mBackend", mBackend);
}
@Test
public void setsOnClickListenerForStartNow() {
ArgumentCaptor<OnClickListener> captor =
ArgumentCaptor.forClass(Button.OnClickListener.class);
public void displayPreference_shouldAddOnSwitchChangeListener() {
mController.displayPreference(mScreen);
verify(mButton).setOnClickListener(captor.capture());
assertThat(captor.getValue()).isNotNull();
verify(mPref).addOnSwitchChangeListener(mController);
}
@Test
public void buttonIsDisabledWhenNeverDreaming() {
public void updateState_neverDreaming_preferenceShouldDidabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
mController.displayPreference(mScreen);
mController.updateState(mLayoutPref);
verify(mButton).setEnabled(false);
mController.updateState(mPref);
verify(mPref).setEnabled(false);
}
@Test
public void buttonIsEnabledWhenDreamIsAvailable() {
public void updateState_dreamIsAvailable_preferenceShouldEnabled() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
mController.displayPreference(mScreen);
mController.updateState(mLayoutPref);
verify(mButton).setEnabled(true);
mController.updateState(mPref);
verify(mPref).setEnabled(true);
}
}

View File

@@ -26,17 +26,15 @@ import static com.android.settings.notification.BadgingNotificationPreferenceCon
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.ActivityManager;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.TwoStatePreference;
import com.android.settingslib.widget.MainSwitchPreference;
import org.junit.Before;
import org.junit.Test;
@@ -57,7 +55,7 @@ public class BubbleNotificationPreferenceControllerTest {
private PreferenceScreen mScreen;
private BubbleNotificationPreferenceController mController;
private Preference mPreference;
private MainSwitchPreference mPreference;
private static final String KEY_NOTIFICATION_BUBBLES = "notification_bubbles";
@@ -67,15 +65,16 @@ public class BubbleNotificationPreferenceControllerTest {
mContext = RuntimeEnvironment.application;
mController = new BubbleNotificationPreferenceController(mContext,
KEY_NOTIFICATION_BUBBLES);
mPreference = new Preference(RuntimeEnvironment.application);
mPreference = new MainSwitchPreference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable_lowRam_returnsUnsupported() {
final ShadowActivityManager activityManager =
Shadow.extract(mContext.getSystemService(ActivityManager.class));
Shadow.extract(mContext.getSystemService(ActivityManager.class));
activityManager.setIsLowRamDevice(true);
assertEquals(UNSUPPORTED_ON_DEVICE, mController.getAvailabilityStatus());
}
@@ -83,45 +82,40 @@ public class BubbleNotificationPreferenceControllerTest {
@Test
public void isAvailable_notLowRam_returnsAvailable() {
final ShadowActivityManager activityManager =
Shadow.extract(mContext.getSystemService(ActivityManager.class));
Shadow.extract(mContext.getSystemService(ActivityManager.class));
activityManager.setIsLowRamDevice(false);
assertEquals(AVAILABLE, mController.getAvailabilityStatus());
}
@Test
public void updateState_settingIsOn_preferenceSetChecked() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
mController.updateState(preference);
verify(preference).setChecked(true);
}
@Test
public void updateState_settingIsOff_preferenceSetUnchecked() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
mController.updateState(preference);
mPreference.updateStatus(false);
verify(preference).setChecked(false);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void isChecked_settingIsOff_shouldReturnFalse() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
public void onSwitchChanged_true_settingIsOff_flagShouldOn() {
Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
assertThat(mController.isChecked()).isFalse();
mController.onSwitchChanged(null, true);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, OFF)).isEqualTo(ON);
}
@Test
public void isChecked_settingIsOn_shouldReturnTrue() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
public void onSwitchChanged_false_settingIsOn_flagShouldOff() {
Settings.Global.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
assertThat(mController.isChecked()).isTrue();
mController.onSwitchChanged(null, false);
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, ON)).isEqualTo(OFF);
}
@Test
@@ -129,8 +123,8 @@ public class BubbleNotificationPreferenceControllerTest {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, ON);
mController.setChecked(false);
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, -1);
int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, ON);
assertThat(updatedValue).isEqualTo(OFF);
}
@@ -140,8 +134,8 @@ public class BubbleNotificationPreferenceControllerTest {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES, OFF);
mController.setChecked(true);
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, -1);
int updatedValue = Settings.Global.getInt(mContext.getContentResolver(),
NOTIFICATION_BUBBLES, OFF);
assertThat(updatedValue).isEqualTo(ON);
}