From efad242f357151b252c45ef434782fca8da60264 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 12 Jun 2019 11:01:18 -0600 Subject: Wait until any background events are processed. Some MediaProvider events (such as thumbnail invalidation) are handled on a background thread. To reduce flaky tests, wait until all background events have been processed, which is more robust than sleep. Bug: 134879702 Test: atest --test-mapping packages/providers/MediaProvider Change-Id: Ia032b19c1b0e5c0096c55bf3d46a5153525e89fa --- api/test-current.txt | 1 + core/java/android/provider/MediaStore.java | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/api/test-current.txt b/api/test-current.txt index c787bfa53b33..c326a99546d4 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -2319,6 +2319,7 @@ package android.provider { method public static android.net.Uri scanFile(android.content.Context, java.io.File); method public static android.net.Uri scanFileFromShell(android.content.Context, java.io.File); method public static void scanVolume(android.content.Context, java.io.File); + method public static void waitForIdle(android.content.Context); } public final class Settings { diff --git a/core/java/android/provider/MediaStore.java b/core/java/android/provider/MediaStore.java index 2a590ce5a65e..2299aad6f79a 100644 --- a/core/java/android/provider/MediaStore.java +++ b/core/java/android/provider/MediaStore.java @@ -136,6 +136,8 @@ public final class MediaStore { */ public static final String VOLUME_EXTERNAL_PRIMARY = "external_primary"; + /** {@hide} */ + public static final String WAIT_FOR_IDLE_CALL = "wait_for_idle"; /** {@hide} */ public static final String SCAN_FILE_CALL = "scan_file"; /** {@hide} */ @@ -3560,6 +3562,17 @@ public final class MediaStore { } } + /** @hide */ + @TestApi + public static void waitForIdle(Context context) { + final ContentResolver resolver = context.getContentResolver(); + try (ContentProviderClient client = resolver.acquireContentProviderClient(AUTHORITY)) { + client.call(WAIT_FOR_IDLE_CALL, null, null); + } catch (RemoteException e) { + throw e.rethrowAsRuntimeException(); + } + } + /** @hide */ @TestApi public static Uri scanFile(Context context, File file) { -- cgit v1.2.3-59-g8ed1b