diff --git a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java index 2fb538e53c2..9132daa691c 100644 --- a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java +++ b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java @@ -21,6 +21,7 @@ import android.os.UserHandle; import android.provider.Settings; import android.support.v14.preference.SwitchPreference; import android.support.v7.preference.Preference; +import android.support.annotation.VisibleForTesting; import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.settings.R; @@ -81,7 +82,7 @@ public class AmbientDisplayAlwaysOnPreferenceController extends @Override public boolean isAvailable() { - return isAvailable(mConfig); + return alwaysOnAvailableForUser(mConfig); } public static boolean isAvailable(AmbientDisplayConfiguration config) { @@ -102,4 +103,9 @@ public class AmbientDisplayAlwaysOnPreferenceController extends ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(), ON /* defaultValue */); } + + @VisibleForTesting + boolean alwaysOnAvailableForUser(AmbientDisplayConfiguration config) { + return isAvailable(config); + } } diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java index dfe81dbc4c7..d1d479a3ba7 100644 --- a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java @@ -17,7 +17,11 @@ package com.android.settings.display; import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Matchers.anyInt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -41,22 +45,27 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowSecureSettings.class}) public class AmbientDisplayAlwaysOnPreferenceControllerTest { - @Mock Context mContext; - @Mock AmbientDisplayConfiguration mConfig; - @Mock SwitchPreference mSwitchPreference; + @Mock + private Context mContext; + @Mock + private AmbientDisplayConfiguration mConfig; + @Mock + private SwitchPreference mSwitchPreference; - AmbientDisplayAlwaysOnPreferenceController mController; - boolean mCallbackInvoked; + private AmbientDisplayAlwaysOnPreferenceController mController; + private boolean mCallbackInvoked; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mController = new AmbientDisplayAlwaysOnPreferenceController(mContext, mConfig, - () -> { mCallbackInvoked = true; }); + () -> { + mCallbackInvoked = true; + }); } @Test @@ -91,7 +100,7 @@ public class AmbientDisplayAlwaysOnPreferenceControllerTest { mController.onPreferenceChange(mSwitchPreference, true); assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ALWAYS_ON, -1)) - .isEqualTo(1); + .isEqualTo(1); } @Test @@ -99,39 +108,45 @@ public class AmbientDisplayAlwaysOnPreferenceControllerTest { mController.onPreferenceChange(mSwitchPreference, false); assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ALWAYS_ON, -1)) - .isEqualTo(0); + .isEqualTo(0); } @Test public void isAvailable_available() throws Exception { - when(mConfig.alwaysOnAvailableForUser(anyInt())) - .thenReturn(true); + mController = spy(mController); + doReturn(true).when(mController).alwaysOnAvailableForUser(any()); assertThat(mController.isAvailable()).isTrue(); } @Test public void isAvailable_unavailable() throws Exception { - when(mConfig.alwaysOnAvailableForUser(anyInt())) - .thenReturn(false); + mController = spy(mController); + doReturn(false).when(mController).alwaysOnAvailableForUser(any()); + assertThat(mController.isAvailable()).isFalse(); } @Test public void testPreferenceController_ProperResultPayloadType() { + mController = spy(mController); + doReturn(false).when(mController).alwaysOnAvailableForUser(any()); assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class); } @Test @Config(shadows = ShadowSecureSettings.class) public void testSetValue_updatesCorrectly() { - int newValue = 1; - ContentResolver resolver = mContext.getContentResolver(); - Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 0); + mController = spy(mController); + doReturn(false).when(mController).alwaysOnAvailableForUser(any()); + final int newValue = 1; + final ContentResolver resolver = mContext.getContentResolver(); + Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 0 /* value */); ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); - int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 1); + final int updatedValue = Settings.Secure.getInt(resolver, + Settings.Secure.DOZE_ALWAYS_ON, 1 /* default */); assertThat(updatedValue).isEqualTo(newValue); } @@ -139,11 +154,13 @@ public class AmbientDisplayAlwaysOnPreferenceControllerTest { @Test @Config(shadows = ShadowSecureSettings.class) public void testGetValue_correctValueReturned() { - int currentValue = 1; - ContentResolver resolver = mContext.getContentResolver(); + mController = spy(mController); + doReturn(false).when(mController).alwaysOnAvailableForUser(any()); + final int currentValue = 1; + final ContentResolver resolver = mContext.getContentResolver(); Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, currentValue); - int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); + final int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); assertThat(newValue).isEqualTo(currentValue); } diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java index e1ce6945c13..d442c821a8e 100644 --- a/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java @@ -46,7 +46,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowSecureSettings.class}) public class AmbientDisplayNotificationsPreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayPreferenceControllerTest.java index 2933517cc11..123dd859549 100644 --- a/tests/robotests/src/com/android/settings/display/AmbientDisplayPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayPreferenceControllerTest.java @@ -39,7 +39,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O, shadows = {ShadowSecureSettings.class}) public class AmbientDisplayPreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/display/AppGridViewTest.java b/tests/robotests/src/com/android/settings/display/AppGridViewTest.java index d0a92c9783a..a2cc76de96f 100644 --- a/tests/robotests/src/com/android/settings/display/AppGridViewTest.java +++ b/tests/robotests/src/com/android/settings/display/AppGridViewTest.java @@ -18,9 +18,16 @@ package com.android.settings.display; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + import android.content.Context; import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; import android.util.IconDrawableFactory; import com.android.settings.TestConfig; @@ -35,13 +42,19 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class AppGridViewTest { @Mock private ResolveInfo mInfo; @Mock private ActivityInfo mActivityInfo; + @Mock + private ApplicationInfo mApplicationInfo; + @Mock + private Drawable mIcon; + @Mock + private PackageManager mPackageManager; private Context mContext; private IconDrawableFactory mIconFactory; @@ -49,13 +62,16 @@ public class AppGridViewTest { public void setUp() { MockitoAnnotations.initMocks(this); mInfo.activityInfo = mActivityInfo; - mContext = RuntimeEnvironment.application; + mInfo.activityInfo.applicationInfo = mApplicationInfo; + mContext = spy(RuntimeEnvironment.application); + doReturn(mPackageManager).when(mContext).getPackageManager(); mIconFactory = IconDrawableFactory.newInstance(mContext); } @Test public void appEntry_shouldLoadIcon() { - + when(mPackageManager.loadUnbadgedItemIcon(mActivityInfo, mApplicationInfo)).thenReturn( + mIcon); final AppGridView.ActivityEntry activityEntry = new AppGridView.ActivityEntry( mInfo, "label", mIconFactory); diff --git a/tests/robotests/src/com/android/settings/display/AutoRotatePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AutoRotatePreferenceControllerTest.java index 091ae1aaab6..febbaf1a9ae 100644 --- a/tests/robotests/src/com/android/settings/display/AutoRotatePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/AutoRotatePreferenceControllerTest.java @@ -48,7 +48,7 @@ import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) @Config( manifest = TestConfig.MANIFEST_PATH, - sdk = TestConfig.SDK_VERSION, + sdk = TestConfig.SDK_VERSION_O, shadows = ShadowSystemSettings.class ) public class AutoRotatePreferenceControllerTest { diff --git a/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java index 73c5374555f..655cf51a359 100644 --- a/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/BatteryPercentagePreferenceControllerTest.java @@ -31,7 +31,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class BatteryPercentagePreferenceControllerTest { @Mock private Context mContext; private BatteryPercentagePreferenceController mController; diff --git a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java index 95144bd7aa5..f8999b5299d 100644 --- a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java @@ -46,7 +46,7 @@ import org.robolectric.shadow.api.Shadow; import org.robolectric.shadows.ShadowContentResolver; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class BrightnessLevelPreferenceControllerTest { @Mock private Context mContext; diff --git a/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java index dc3d27ac802..dc9dc5b6237 100644 --- a/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java +++ b/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java @@ -46,7 +46,7 @@ import org.robolectric.util.ReflectionHelpers; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ColorModePreferenceFragmentTest { private ColorModePreferenceFragment mFragment; diff --git a/tests/robotests/src/com/android/settings/display/ShowOperatorNamePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/ShowOperatorNamePreferenceControllerTest.java index 23b43d6011c..4d701ca831a 100644 --- a/tests/robotests/src/com/android/settings/display/ShowOperatorNamePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/ShowOperatorNamePreferenceControllerTest.java @@ -36,7 +36,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ShowOperatorNamePreferenceControllerTest { private static final String KEY_SHOW_OPERATOR_NAME = "show_operator_name"; diff --git a/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java index 3d99f658391..289b8c19580 100644 --- a/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/ThemePreferenceControllerTest.java @@ -47,7 +47,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class ThemePreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/display/TimeoutPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/TimeoutPreferenceControllerTest.java index 480e41fe84f..48997c3cda2 100644 --- a/tests/robotests/src/com/android/settings/display/TimeoutPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/TimeoutPreferenceControllerTest.java @@ -33,7 +33,7 @@ import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; import static com.google.common.truth.Truth.assertThat; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class TimeoutPreferenceControllerTest { private static final int TIMEOUT = 30; @Mock(answer = Answers.RETURNS_DEEP_STUBS) diff --git a/tests/robotests/src/com/android/settings/display/VrDisplayPreferencePickerTest.java b/tests/robotests/src/com/android/settings/display/VrDisplayPreferencePickerTest.java index 191a8fb39cd..f874007732f 100644 --- a/tests/robotests/src/com/android/settings/display/VrDisplayPreferencePickerTest.java +++ b/tests/robotests/src/com/android/settings/display/VrDisplayPreferencePickerTest.java @@ -39,7 +39,7 @@ import org.robolectric.annotation.Config; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class VrDisplayPreferencePickerTest { private VrDisplayPreferencePicker mPicker; diff --git a/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java index 1419ad551da..17d12c0daed 100644 --- a/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java @@ -43,7 +43,7 @@ import java.util.ArrayList; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O) public class WallpaperPreferenceControllerTest { private static final String WALLPAPER_PACKAGE = "TestPkg";