aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Hoisie <hoisie@google.com>2024-04-16 10:41:39 -0700
committerCopybara-Service <copybara-worker@google.com>2024-04-16 10:42:37 -0700
commit27333928cda4b045448195c27c734992f9781b01 (patch)
tree2c6adec04e541649ad5b30c55cd279738db23353
parent6970abf2416fd09ac8fb0d4d993b20aaa2834bfe (diff)
downloadrobolectric-27333928cda4b045448195c27c734992f9781b01.tar.gz
Add a shadow method for ViewConfiguration.getScaledMaximumDrawingCacheSize
Previously, ViewConfiguration.getScaledMaximumDrawingCacheSize just returned a constant value, the default max value (1536000 == 480 * 800 * 4). The result of ViewConfiguration.getScaledMaximumDrawingCacheSize is used in View.buildDrawingCacheImpl(boolean autoScale), which is used when rendering View objects. For tests that configure large screen sizes, the 1536000 may not sufficient for the cache size. Views that exceed the cache size will not be drawn. Add a shadow for ViewConfiguration.getScaledMaximumDrawingCacheSize that scales with the display size. Also, remove the shadow for ViewConfiguration.getMaximumDrawingCacheSize, the real implementation can be used instead. Some tests depend on a minimum cache size of 1536000, such as when they configure a smaller display and render a large view into it. Add some special logic that keeps the min cache size at 1536000. PiperOrigin-RevId: 625381937
-rw-r--r--robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java21
-rw-r--r--shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java17
2 files changed, 33 insertions, 5 deletions
diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java
index 41661af4a..d0dfeadfb 100644
--- a/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java
+++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowViewConfigurationTest.java
@@ -11,6 +11,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
@RunWith(AndroidJUnit4.class)
public class ShadowViewConfigurationTest {
@@ -40,7 +41,7 @@ public class ShadowViewConfigurationTest {
assertEquals(16, ViewConfiguration.getWindowTouchSlop());
assertEquals(50, ViewConfiguration.getMinimumFlingVelocity());
assertEquals(4000, ViewConfiguration.getMaximumFlingVelocity());
- assertEquals(320 * 480 * 4, ViewConfiguration.getMaximumDrawingCacheSize());
+ assertEquals(480 * 800 * 4, ViewConfiguration.getMaximumDrawingCacheSize());
assertEquals(3000, ViewConfiguration.getZoomControlsTimeout());
assertEquals(500, ViewConfiguration.getGlobalActionKeyTimeout());
assertThat(ViewConfiguration.getScrollFriction()).isEqualTo(0.015f);
@@ -56,6 +57,8 @@ public class ShadowViewConfigurationTest {
assertEquals(16, viewConfiguration.getScaledWindowTouchSlop());
assertEquals(50, viewConfiguration.getScaledMinimumFlingVelocity());
assertEquals(4000, viewConfiguration.getScaledMaximumFlingVelocity());
+ // The min value of getScaledMaximumDrawingCacheSize is 480 * 800 * 4.
+ assertEquals(480 * 800 * 4, viewConfiguration.getScaledMaximumDrawingCacheSize());
}
@Test
@@ -83,4 +86,20 @@ public class ShadowViewConfigurationTest {
shadowViewConfiguration.setHasPermanentMenuKey(false);
assertThat(viewConfiguration.hasPermanentMenuKey()).isFalse();
}
+
+ @Config(qualifiers = "w420dp-h800dp-xxxhdpi")
+ @Test
+ public void getScaledMaximumFlingVelocity_scalesWithDisplaySize() {
+ ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
+ int expected = 4 * (4 * 420) * (4 * 800);
+ assertThat(viewConfiguration.getScaledMaximumDrawingCacheSize()).isEqualTo(expected);
+ }
+
+ @Config(qualifiers = "w100dp-h500dp")
+ @Test
+ public void getScaledMaximumFlingVelocity_minValue() {
+ ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
+ int expected = 480 * 800 * 4; // The min value
+ assertThat(viewConfiguration.getScaledMaximumDrawingCacheSize()).isEqualTo(expected);
+ }
}
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 33f800f7d..682f53b6a 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowViewConfiguration.java
@@ -46,7 +46,9 @@ public class ShadowViewConfiguration {
private static final int DOUBLE_TAP_SLOP = 100;
private static final int WINDOW_TOUCH_SLOP = 16;
private static final int MAXIMUM_FLING_VELOCITY = 4000;
- private static final int MAXIMUM_DRAWING_CACHE_SIZE = 320 * 480 * 4;
+
+ // The previous hardcoded value for draw cache size. Some screenshot tests depend on this value.
+ private static final int MIN_MAXIMUM_DRAWING_CACHE_SIZE = 480 * 800 * 4;
private int edgeSlop;
private int fadingEdgeLength;
@@ -57,10 +59,11 @@ public class ShadowViewConfiguration {
private int pagingTouchSlop;
private int doubleTapSlop;
private int windowTouchSlop;
+ private int maximumDrawingCacheSize;
private static boolean hasPermanentMenuKey = true;
private void setup(Context context) {
- DisplayMetrics metrics = context.getResources().getDisplayMetrics();
+ final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float density = metrics.density;
edgeSlop = (int) (density * ViewConfiguration.getEdgeSlop() + 0.5f);
@@ -72,6 +75,12 @@ public class ShadowViewConfiguration {
pagingTouchSlop = (int) (density * PAGING_TOUCH_SLOP + 0.5f);
doubleTapSlop = (int) (density * DOUBLE_TAP_SLOP + 0.5f);
windowTouchSlop = (int) (density * WINDOW_TOUCH_SLOP + 0.5f);
+ // Some screenshot tests were misconfigured and try to draw very large views onto small
+ // screens using SW rendering. To avoid breaking these tests, we keep the drawing cache a bit
+ // larger when screens are configured to be arbitrarily small.
+ // TODO(hoisie): Investigate removing this Math.max logic.
+ maximumDrawingCacheSize =
+ Math.max(MIN_MAXIMUM_DRAWING_CACHE_SIZE, 4 * metrics.widthPixels * metrics.heightPixels);
}
@Implementation
@@ -174,8 +183,8 @@ public class ShadowViewConfiguration {
}
@Implementation
- protected static int getMaximumDrawingCacheSize() {
- return MAXIMUM_DRAWING_CACHE_SIZE;
+ protected int getScaledMaximumDrawingCacheSize() {
+ return maximumDrawingCacheSize;
}
@Implementation