diff options
author | Michael Hoisie <hoisie@google.com> | 2024-04-19 10:46:34 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-19 10:47:09 -0700 |
commit | 59bc604161f3a72de949ebbeb6f73e76500f4d2d (patch) | |
tree | 6f525d30f4441ab8b0dc2798cd3514dd8410c910 | |
parent | 43a7f0b102a9350cdb379cf88329858a150a2e12 (diff) | |
download | robolectric-59bc604161f3a72de949ebbeb6f73e76500f4d2d.tar.gz |
Use the correct value of ViewConfiguration.isFadingMarqueeEnabled
Due to the problematic ShadowViewConfiguration, the return value of
ViewConfiguration.isFadingMarqueeEnabled was previously true in Robolectric.
This value is not correct -- in all Android versions supported by Robolectric,
it is false.
PiperOrigin-RevId: 626410135
3 files changed, 40 insertions, 1 deletions
diff --git a/integration_tests/ctesque/src/sharedTest/java/android/view/ViewConfigurationTest.java b/integration_tests/ctesque/src/sharedTest/java/android/view/ViewConfigurationTest.java index afc2edbbb..30b81907c 100644 --- a/integration_tests/ctesque/src/sharedTest/java/android/view/ViewConfigurationTest.java +++ b/integration_tests/ctesque/src/sharedTest/java/android/view/ViewConfigurationTest.java @@ -6,6 +6,7 @@ import static com.google.common.truth.Truth.assertThat; import android.os.Build; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import java.lang.reflect.Method; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,7 +40,22 @@ public final class ViewConfigurationTest { } } + @Test + public void isFadingMarqueeEnabled_returnsFalse() throws Exception { + ViewConfiguration viewConfiguration = + ViewConfiguration.get(ApplicationProvider.getApplicationContext()); + // isFadingMarqueeEnabled is a '@hide' method. + boolean isFadingMarqueeEnabled = + callMethod(viewConfiguration, "isFadingMarqueeEnabled", Boolean.class); + assertThat(isFadingMarqueeEnabled).isFalse(); + } + public int pxToDp(int px) { return Math.round(px / density); } + + public <T> T callMethod(Object obj, String methodName, Class<T> returnType) throws Exception { + Method method = obj.getClass().getMethod(methodName); + return returnType.cast(method.invoke(obj)); + } } diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java index fd98c1a5e..811ee18af 100644 --- a/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java +++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java @@ -63,6 +63,7 @@ public class ShadowViewConfigurationTest { assertEquals(4000, viewConfiguration.getScaledMaximumFlingVelocity()); // The min value of getScaledMaximumDrawingCacheSize is 480 * 800 * 4. assertEquals(480 * 800 * 4, viewConfiguration.getScaledMaximumDrawingCacheSize()); + assertThat(viewConfiguration.isFadingMarqueeEnabled()).isFalse(); } @Test diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java index 8cd68ee34..a4c4a6f20 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java @@ -67,7 +67,8 @@ public class ShadowViewConfiguration { private static boolean hasPermanentMenuKey = true; private void setup(Context context) { - final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + final Resources resources = context.getResources(); + final DisplayMetrics metrics = resources.getDisplayMetrics(); float density = metrics.density; edgeSlop = (int) (density * ViewConfiguration.getEdgeSlop() + 0.5f); @@ -95,6 +96,11 @@ public class ShadowViewConfiguration { // TODO(hoisie): Investigate removing this Math.max logic. maximumDrawingCacheSize = Math.max(MIN_MAXIMUM_DRAWING_CACHE_SIZE, 4 * metrics.widthPixels * metrics.heightPixels); + boolean enableFadingMarquee = + resources.getBoolean( + reflector(AndroidInternalBoolReflector.class).getEnableFadingMarquee()); + reflector(ViewConfigurationReflector.class, realViewConfiguration) + .setFadingMarqueeEnabled(enableFadingMarquee); } @Implementation @@ -207,6 +213,9 @@ public class ShadowViewConfiguration { @Accessor("mScrollbarSize") void setScrollbarSize(int value); + + @Accessor("mFadingMarqueeEnabled") + void setFadingMarqueeEnabled(boolean enableFadingMarquee); } /** @@ -221,4 +230,17 @@ public class ShadowViewConfiguration { @Accessor("config_scrollbarSize") int getConfigScrollbarSize(); } + + /** + * Reflection is needed to access internal Android dimen constants, which are static final ints, + * so referencing them directly causes inlining. Note {@link AndroidInternalDimenReflector} is + * designed to be temporary until the real {@link + * android.view.ViewConfiguration#get(android.content.Context)} is called. + */ + @ForType(com.android.internal.R.bool.class) + interface AndroidInternalBoolReflector { + @Static + @Accessor("config_ui_enableFadingMarquee") + int getEnableFadingMarquee(); + } } |