0

Fix ContentUriTestUtils.java

This CL fixes a test that will break as a result of:
https://chromium-review.googlesource.com/c/catapult/+/6373645

I think the cause of the failure is that the Android OS makes a scan
of media files (video, image, audio files) when they are added to the
Android device, and add the new files to a database.
The previous unzipping mechanism did not trigger this scan for
whatever reason, but the new archive extraction mechanism did trigger
this scan, which caused the test to fail.

Bug: 397459590
Change-Id: I5c00f15ff2a3bd5ca373577b134a84c93a033855
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6409751
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Martin Kong <martinkong@google.com>
Cr-Commit-Position: refs/heads/main@{#1439657}
This commit is contained in:
Martin Kong
2025-03-28 13:17:08 -07:00
committed by Chromium LUCI CQ
parent ac121e7ecc
commit ed65673630

@ -6,12 +6,16 @@ package org.chromium.base;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;
import org.jni_zero.CalledByNative;
import org.jni_zero.JniType;
import java.util.concurrent.CompletableFuture;
/** Utilities for testing operations on content URI. */
public class ContentUriTestUtils {
/**
@ -41,13 +45,39 @@ public class ContentUriTestUtils {
.toString();
}
// Insert the content URI into MediaStore.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, path);
Uri uri =
ContextUtils.getApplicationContext()
.getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
return uri.toString();
try {
// Insert the content URI into MediaStore.
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, path);
Uri uri =
ContextUtils.getApplicationContext()
.getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
return uri.toString();
} catch (SQLException e) {
// Although we have already checked via a call to query() that the database does not
// contain the path to the image file, inserting the path to the database can still fail
// and the database will claim that the path being inserted already exists.
// This is a known issue and is documented in the following StackOverflow post:
// https://stackoverflow.com/questions/22184729/sqliteconstraintexception-thrown-when-trying-to-insert
CompletableFuture<Uri> future = new CompletableFuture();
MediaScannerConnection.scanFile(
ContextUtils.getApplicationContext(),
new String[] {path},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String scannedPath, Uri scannedUri) {
if (scannedPath.equals(path)) {
future.complete(scannedUri);
}
}
});
try {
return future.get().toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}