From be36ba3432eada2aa63a9c69a58d548da8151c48 Mon Sep 17 00:00:00 2001 From: jeffreyhuang Date: Mon, 20 Nov 2017 16:18:06 -0800 Subject: [PATCH] Update SetupChooseLockPatternTest to SDK 26 - create a new ShadowResourcesImpl because sdk 26 calls loadDrawable from ResourcesImpl instead of Resources Test: make RunSettingsRoboTests -j40 Change-Id: I8eed76ea779972c68893d4e7fdfc4493465b9b22 --- .../settings/SetupChooseLockPatternTest.java | 6 +- .../shadow/SettingsShadowResources.java | 7 +++ .../shadow/SettingsShadowResourcesImpl.java | 57 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java diff --git a/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java index 206ba953a57..17d10218b8c 100644 --- a/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java +++ b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java @@ -30,6 +30,7 @@ import com.android.settings.password.ChooseLockPattern.IntentBuilder; import com.android.settings.password.SetupChooseLockPattern; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.shadow.SettingsShadowResources; +import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; import com.android.settings.testutils.shadow.ShadowEventLogWriter; import com.android.settings.testutils.shadow.ShadowUtils; @@ -44,9 +45,10 @@ import org.robolectric.shadows.ShadowPackageManager.ComponentState; @RunWith(SettingsRobolectricTestRunner.class) @Config( manifest = TestConfig.MANIFEST_PATH, - sdk = TestConfig.SDK_VERSION, + sdk = TestConfig.SDK_VERSION_O, shadows = { SettingsShadowResources.class, + SettingsShadowResourcesImpl.class, SettingsShadowResources.SettingsShadowTheme.class, ShadowEventLogWriter.class, ShadowUtils.class @@ -62,7 +64,7 @@ public class SetupChooseLockPatternTest { PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); - mActivity = Robolectric.buildActivity( + mActivity = Robolectric.buildActivity( SetupChooseLockPattern.class, SetupChooseLockPattern.modifyIntentForSetup( application, diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java index 183f5ad9c70..b0cd484dd2e 100644 --- a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java +++ b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java @@ -101,6 +101,13 @@ public class SettingsShadowResources extends ShadowResources { return directlyOn(realResources, Resources.class).getColorStateList(id, theme); } + /** + * Deprecated because SDK 24+ uses + * {@link SettingsShadowResourcesImpl#loadDrawable(Resources, TypedValue, int, int, Theme)} + * + * TODO: Delete when all tests have been migrated to sdk 26 + */ + @Deprecated @Implementation public Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException { diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java new file mode 100644 index 00000000000..42b02f3c23d --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResourcesImpl.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 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.testutils.shadow; + +import android.content.res.Resources; +import android.content.res.ResourcesImpl; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.util.TypedValue; + +import com.android.settings.R; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.shadows.ShadowResourcesImpl; + +@Implements( + value = ResourcesImpl.class, + isInAndroidSdk = false, + minSdk = 26 +) +public class SettingsShadowResourcesImpl extends ShadowResourcesImpl { + + @Implementation + public Drawable loadDrawable(Resources wrapper, TypedValue value, int id, int density, + Resources.Theme theme) { + // The drawable item in switchbar_background.xml refers to a very recent color attribute + // that Robolectric isn't yet aware of. + // TODO: Remove this once Robolectric is updated. + if (id == R.drawable.switchbar_background) { + return new ColorDrawable(); + } else if (id == R.drawable.ic_launcher_settings) { + // ic_launcher_settings uses adaptive-icon, which is not supported by robolectric, + // change it to a normal drawable. + id = R.drawable.ic_settings_wireless; + } else if (id == R.drawable.app_filter_spinner_background) { + id = R.drawable.ic_expand_more_inverse; + } else if (id == R.drawable.selectable_card_grey) { + id = R.drawable.ic_expand_more_inverse; + } + return super.loadDrawable(wrapper, value, id, density, theme); + } +}