Merge "Add switch bar to enable/disable dev settings in new page."

This commit is contained in:
TreeHugger Robot
2017-09-12 19:50:15 +00:00
committed by Android (Google) Code Review
5 changed files with 283 additions and 4 deletions

View File

@@ -17,22 +17,32 @@
package com.android.settings.development;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch;
import com.android.settingslib.development.DevelopmentSettingsEnabler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
import java.util.List;
@@ -44,11 +54,24 @@ import java.util.List;
})
public class DevelopmentSettingsDashboardFragmentTest {
private SwitchBar mSwitchBar;
private ToggleSwitch mSwitch;
private Context mContext;
private DevelopmentSettingsDashboardFragment mDashboard;
@Before
public void setUp() {
mDashboard = new DevelopmentSettingsDashboardFragment();
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mSwitchBar = new SwitchBar(mContext);
mSwitch = mSwitchBar.getSwitch();
mDashboard = spy(new DevelopmentSettingsDashboardFragment());
ReflectionHelpers.setField(mDashboard, "mSwitchBar", mSwitchBar);
}
@After
public void tearDown() {
ShadowEnableDevelopmentSettingWarningDialog.reset();
}
@Test
@@ -95,4 +118,62 @@ public class DevelopmentSettingsDashboardFragmentTest {
assertThat(nonIndexableKeys).doesNotContain("development_prefs_screen");
}
@Test
@Config(shadows = {
ShadowEnableDevelopmentSettingWarningDialog.class
})
public void onSwitchChanged_sameState_shouldDoNothing() {
when(mDashboard.getContext()).thenReturn(mContext);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
mDashboard.onSwitchChanged(mSwitch, false /* isChecked */);
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
}
@Test
@Config(shadows = {
ShadowEnableDevelopmentSettingWarningDialog.class
})
public void onSwitchChanged_turnOn_shouldShowWarningDialog() {
when(mDashboard.getContext()).thenReturn(mContext);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
mDashboard.onSwitchChanged(mSwitch, true /* isChecked */);
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isTrue();
}
@Test
@Config(shadows = {
ShadowEnableDevelopmentSettingWarningDialog.class
})
public void onSwitchChanged_turnOff_shouldTurnOff() {
when(mDashboard.getContext()).thenReturn(mContext);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
mDashboard.onSwitchChanged(mSwitch, false /* isChecked */);
assertThat(ShadowEnableDevelopmentSettingWarningDialog.mShown).isFalse();
assertThat(DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext))
.isFalse();
}
@Implements(EnableDevelopmentSettingWarningDialog.class)
public static class ShadowEnableDevelopmentSettingWarningDialog {
static boolean mShown;
public static void reset() {
mShown = false;
}
@Implementation
public static void show(
DevelopmentSettingsDashboardFragment host) {
mShown = true;
}
}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.development;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -45,6 +46,8 @@ public class DevelopmentSwitchBarControllerTest {
@Mock
private DevelopmentSettings mSettings;
@Mock
private DevelopmentSettingsDashboardFragment mNewSettings;
private Lifecycle mLifecycle;
private SwitchBar mSwitchBar;
private DevelopmentSwitchBarController mController;
@@ -76,6 +79,21 @@ public class DevelopmentSwitchBarControllerTest {
assertThat(listeners).doesNotContain(mSettings);
}
@Test
public void runThroughLifecycle_v2_isMonkeyRun_shouldNotRegisterListener() {
ShadowUtils.setIsUserAMonkey(true);
mController = new DevelopmentSwitchBarController(mNewSettings, mSwitchBar,
true /* isAvailable */, mLifecycle);
final ArrayList<SwitchBar.OnSwitchChangeListener> listeners =
ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
mLifecycle.onStart();
assertThat(listeners).doesNotContain(mNewSettings);
mLifecycle.onStop();
assertThat(listeners).doesNotContain(mNewSettings);
}
@Test
public void runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener() {
ShadowUtils.setIsUserAMonkey(false);
@@ -91,6 +109,22 @@ public class DevelopmentSwitchBarControllerTest {
assertThat(listeners).doesNotContain(mSettings);
}
@Test
public void runThroughLifecycle_v2_isNotMonkeyRun_shouldRegisterAndRemoveListener() {
when(mNewSettings.getContext()).thenReturn(RuntimeEnvironment.application);
ShadowUtils.setIsUserAMonkey(false);
mController = new DevelopmentSwitchBarController(mNewSettings, mSwitchBar,
true /* isAvailable */, mLifecycle);
final ArrayList<SwitchBar.OnSwitchChangeListener> listeners =
ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
mLifecycle.onStart();
assertThat(listeners).contains(mNewSettings);
mLifecycle.onStop();
assertThat(listeners).doesNotContain(mNewSettings);
}
@Test
public void buildController_unavailable_shouldDisableSwitchBar() {
ShadowUtils.setIsUserAMonkey(false);