Commit 70d6bad2 authored by Finnur Thorarinsson's avatar Finnur Thorarinsson Committed by Commit Bot

[Android] Photo Picker: Add test coverage for FileEnumWorker.

This changelist also refactors the FileEnumWorkerTask to
facilitate easier testing. Functions createImageCursor and
shouldShowCameraTile were added so that that behavior could be
overridden during testing. Also, onCancelled was added so
that the test would get notified when the thread has to be
shut down due to an Exception being thrown (otherwise the
Roboelectric test just hangs).

Bug: 656015
Change-Id: Ie601a84c6e8028a57720ddaf9802eecd194c4f4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2127708
Commit-Queue: Finnur Thorarinsson <finnur@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Auto-Submit: Finnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756316}
parent aa6b82a5
...@@ -184,6 +184,7 @@ chrome_junit_test_java_sources = [ ...@@ -184,6 +184,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/password_manager/settings/TimedCallbackDelayerTest.java", "junit/src/org/chromium/chrome/browser/password_manager/settings/TimedCallbackDelayerTest.java",
"junit/src/org/chromium/chrome/browser/payments/AutofillContactTest.java", "junit/src/org/chromium/chrome/browser/payments/AutofillContactTest.java",
"junit/src/org/chromium/chrome/browser/payments/AutofillContactUnitTest.java", "junit/src/org/chromium/chrome/browser/payments/AutofillContactUnitTest.java",
"junit/src/org/chromium/chrome/browser/photo_picker/FileEnumWorkerTaskTest.java",
"junit/src/org/chromium/chrome/browser/photo_picker/PickerBitmapViewTest.java", "junit/src/org/chromium/chrome/browser/photo_picker/PickerBitmapViewTest.java",
"junit/src/org/chromium/chrome/browser/preferences/PrefServiceBridgeTest.java", "junit/src/org/chromium/chrome/browser/preferences/PrefServiceBridgeTest.java",
"junit/src/org/chromium/chrome/browser/privacy/settings/PrivacyPreferencesManagerTest.java", "junit/src/org/chromium/chrome/browser/privacy/settings/PrivacyPreferencesManagerTest.java",
......
...@@ -101,7 +101,7 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> { ...@@ -101,7 +101,7 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> {
*/ */
@Override @Override
protected List<PickerBitmap> doInBackground() { protected List<PickerBitmap> doInBackground() {
assert !ThreadUtils.runningOnUiThread(); ThreadUtils.assertOnBackgroundThread();
if (isCancelled()) return null; if (isCancelled()) return null;
...@@ -158,8 +158,7 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> { ...@@ -158,8 +158,7 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> {
Uri contentUri = MediaStore.Files.getContentUri("external"); Uri contentUri = MediaStore.Files.getContentUri("external");
Cursor imageCursor = Cursor imageCursor =
mContentResolver.query(contentUri, selectColumns, whereClause, whereArgs, orderBy); createImageCursor(contentUri, selectColumns, whereClause, whereArgs, orderBy);
if (imageCursor == null) { if (imageCursor == null) {
Log.e(TAG, "Content Resolver query() returned null"); Log.e(TAG, "Content Resolver query() returned null");
return null; return null;
...@@ -190,18 +189,19 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> { ...@@ -190,18 +189,19 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> {
imageCursor.close(); imageCursor.close();
pickerBitmaps.add(0, new PickerBitmap(null, 0, PickerBitmap.TileTypes.GALLERY)); pickerBitmaps.add(0, new PickerBitmap(null, 0, PickerBitmap.TileTypes.GALLERY));
boolean hasCameraAppAvailable = if (shouldShowCameraTile()) {
mWindowAndroid.canResolveActivity(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
boolean hasOrCanRequestCameraPermission =
mWindowAndroid.hasPermission(Manifest.permission.CAMERA)
|| mWindowAndroid.canRequestPermission(Manifest.permission.CAMERA);
if (hasCameraAppAvailable && hasOrCanRequestCameraPermission) {
pickerBitmaps.add(0, new PickerBitmap(null, 0, PickerBitmap.TileTypes.CAMERA)); pickerBitmaps.add(0, new PickerBitmap(null, 0, PickerBitmap.TileTypes.CAMERA));
} }
return pickerBitmaps; return pickerBitmaps;
} }
@Override
protected void onCancelled() {
super.onCancelled();
mCallback.filesEnumeratedCallback(null);
}
/** /**
* Communicates the results back to the client. Called on the UI thread. * Communicates the results back to the client. Called on the UI thread.
* @param files The resulting list of files on disk. * @param files The resulting list of files on disk.
...@@ -214,4 +214,25 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> { ...@@ -214,4 +214,25 @@ class FileEnumWorkerTask extends AsyncTask<List<PickerBitmap>> {
mCallback.filesEnumeratedCallback(files); mCallback.filesEnumeratedCallback(files);
} }
/**
* Creates a cursor containing the image files to show. Can be overridden in tests to provide
* fake data.
*/
protected Cursor createImageCursor(Uri contentUri, String[] selectColumns, String whereClause,
String[] whereArgs, String orderBy) {
return mContentResolver.query(contentUri, selectColumns, whereClause, whereArgs, orderBy);
}
/**
* Returns whether to include the Camera tile also.
*/
protected boolean shouldShowCameraTile() {
boolean hasCameraAppAvailable =
mWindowAndroid.canResolveActivity(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
boolean hasOrCanRequestCameraPermission =
(mWindowAndroid.hasPermission(Manifest.permission.CAMERA)
|| mWindowAndroid.canRequestPermission(Manifest.permission.CAMERA));
return hasCameraAppAvailable && hasOrCanRequestCameraPermission;
}
} }
...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.photo_picker; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.photo_picker;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
...@@ -102,4 +103,13 @@ public class PickerBitmap implements Comparable<PickerBitmap> { ...@@ -102,4 +103,13 @@ public class PickerBitmap implements Comparable<PickerBitmap> {
public int compareTo(PickerBitmap other) { public int compareTo(PickerBitmap other) {
return ApiCompatibilityUtils.compareLong(other.mLastModified, mLastModified); return ApiCompatibilityUtils.compareLong(other.mLastModified, mLastModified);
} }
/**
* Accessor for the last modified date (for testing use only).
* @return The last modified date.
*/
@VisibleForTesting
public long getLastModifiedForTesting() {
return mLastModified;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment