From 1d76159de4dc626bc35fb4dc1a969d07508b6dc9 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Fri, 28 Oct 2022 12:25:04 -0700 Subject: [PATCH] Fixing flakyness in widget tests For some predicted app widgets, the layout happens before the recyclerView can layout completely (because of preview layout invalidation), causing the scroll cache to get in a wrong state. Checking for scroll cache validity everytime instead of only during data changes Bug: 255797215 Test: Verified by hardcoing the chrome widget as predicted widget Change-Id: Id2c68560baa45de89e08c53bdb9ee6820eab68c4 --- .../launcher3/util/ScrollableLayoutManager.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/com/android/launcher3/util/ScrollableLayoutManager.java b/src/com/android/launcher3/util/ScrollableLayoutManager.java index 17eaefda18..9bc4ddce38 100644 --- a/src/com/android/launcher3/util/ScrollableLayoutManager.java +++ b/src/com/android/launcher3/util/ScrollableLayoutManager.java @@ -44,8 +44,6 @@ public class ScrollableLayoutManager extends GridLayoutManager { * whereas widgets will have strictly increasing values * sample values: 0, 10, 50, 60, 110 */ - - // private int[] mTotalHeightCache = new int[1]; private int mLastValidHeightIndex = 0; @@ -62,16 +60,23 @@ public class ScrollableLayoutManager extends GridLayoutManager { @Override public void layoutDecorated(@NonNull View child, int left, int top, int right, int bottom) { super.layoutDecorated(child, left, top, right, bottom); - mCachedSizes.put( - mRv.getChildViewHolder(child).getItemViewType(), child.getMeasuredHeight()); + updateCachedSize(child); } @Override public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, int right, int bottom) { super.layoutDecoratedWithMargins(child, left, top, right, bottom); - mCachedSizes.put( - mRv.getChildViewHolder(child).getItemViewType(), child.getMeasuredHeight()); + updateCachedSize(child); + } + + private void updateCachedSize(@NonNull View child) { + int viewType = mRv.getChildViewHolder(child).getItemViewType(); + int size = child.getMeasuredHeight(); + if (mCachedSizes.get(viewType, -1) != size) { + invalidateScrollCache(); + } + mCachedSizes.put(viewType, size); } @Override