diff options
author | 2024-03-05 14:36:05 -0800 | |
---|---|---|
committer | 2024-03-05 15:43:32 -0800 | |
commit | 8d679c317202c01bf0bebf1ae96305c7603a3861 (patch) | |
tree | 66f807229c713662ac9b79c0fd48d0c1a3f020f1 /tests/activity | |
parent | 2bf5c23334dd691c3677cf6a3d9a5869dd9f301e (diff) |
Address test flakiness
RecyclerView might run an item transition animation while a test is
reading its chid count; reading an unexpected number as a result.
Force-end recycler view item animations in such cases.
Bug: N/A
Test: atest IntentResolver-tests-activity
Change-Id: Ibfeb724ff746834440266891c7f3bed6c75d794b
Diffstat (limited to 'tests/activity')
3 files changed, 34 insertions, 0 deletions
diff --git a/tests/activity/src/com/android/intentresolver/UnbundledChooserActivityTest.java b/tests/activity/src/com/android/intentresolver/UnbundledChooserActivityTest.java index c7b41ce0..4077295c 100644 --- a/tests/activity/src/com/android/intentresolver/UnbundledChooserActivityTest.java +++ b/tests/activity/src/com/android/intentresolver/UnbundledChooserActivityTest.java @@ -119,6 +119,7 @@ import androidx.test.rule.ActivityTestRule; import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.contentpreview.ImageLoader; +import com.android.intentresolver.ext.RecyclerViewExt; import com.android.intentresolver.logging.EventLog; import com.android.intentresolver.logging.FakeEventLog; import com.android.intentresolver.shortcuts.ShortcutLoader; @@ -935,6 +936,7 @@ public class UnbundledChooserActivityTest { throw exception; } RecyclerView recyclerView = (RecyclerView) view; + RecyclerViewExt.endAnimations(recyclerView); assertThat(recyclerView.getAdapter().getItemCount(), is(1)); assertThat(recyclerView.getChildCount(), is(1)); View imageView = recyclerView.getChildAt(0); @@ -1094,6 +1096,7 @@ public class UnbundledChooserActivityTest { throw exception; } RecyclerView recyclerView = (RecyclerView) view; + RecyclerViewExt.endAnimations(recyclerView); assertThat(recyclerView.getChildCount()).isAtLeast(1); // the first view is a preview View imageView = recyclerView.getChildAt(0).findViewById(R.id.image); diff --git a/tests/activity/src/com/android/intentresolver/ext/RecyclerViewExt.kt b/tests/activity/src/com/android/intentresolver/ext/RecyclerViewExt.kt new file mode 100644 index 00000000..90acaa60 --- /dev/null +++ b/tests/activity/src/com/android/intentresolver/ext/RecyclerViewExt.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2024 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. + */ + +@file:JvmName("RecyclerViewExt") + +package com.android.intentresolver.ext + +import androidx.recyclerview.widget.RecyclerView + +/** Ends active RecyclerView animations, if any */ +fun RecyclerView.endAnimations() { + if (isAnimating) { + itemAnimator?.endAnimations() + } +} diff --git a/tests/activity/src/com/android/intentresolver/v2/UnbundledChooserActivityTest.java b/tests/activity/src/com/android/intentresolver/v2/UnbundledChooserActivityTest.java index a7221c10..b910e4f6 100644 --- a/tests/activity/src/com/android/intentresolver/v2/UnbundledChooserActivityTest.java +++ b/tests/activity/src/com/android/intentresolver/v2/UnbundledChooserActivityTest.java @@ -129,6 +129,7 @@ import com.android.intentresolver.TestContentProvider; import com.android.intentresolver.chooser.DisplayResolveInfo; import com.android.intentresolver.contentpreview.ImageLoader; import com.android.intentresolver.contentpreview.ImageLoaderModule; +import com.android.intentresolver.ext.RecyclerViewExt; import com.android.intentresolver.inject.PackageManagerModule; import com.android.intentresolver.logging.EventLog; import com.android.intentresolver.logging.FakeEventLog; @@ -977,6 +978,7 @@ public class UnbundledChooserActivityTest { throw exception; } RecyclerView recyclerView = (RecyclerView) view; + RecyclerViewExt.endAnimations(recyclerView); assertThat("recyclerView adapter item count", recyclerView.getAdapter().getItemCount(), is(1)); assertThat("recyclerView child view count", @@ -1130,6 +1132,7 @@ public class UnbundledChooserActivityTest { throw exception; } RecyclerView recyclerView = (RecyclerView) view; + RecyclerViewExt.endAnimations(recyclerView); assertThat(recyclerView.getChildCount()).isAtLeast(1); // the first view is a preview View imageView = recyclerView.getChildAt(0).findViewById(R.id.image); |