Skip to content

Commit

Permalink
Merge pull request robolectric#5729 from robolectric/piper_318266155
Browse files Browse the repository at this point in the history
Added query with Bundle
  • Loading branch information
hoisie authored Jul 1, 2020
2 parents ae9d3e8 + fea27df commit e503843
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.robolectric.shadows;

import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION;
import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS;
import static android.content.ContentResolver.QUERY_ARG_SQL_SORT_ORDER;
import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.O;
import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -283,6 +287,29 @@ public void query_shouldKnowWhatItsParamsWere() {
assertThat(testCursor.sortOrder).isEqualTo(sortOrder);
}

@Test
@Config(minSdk = O)
public void query_shouldKnowWhatIsInBundle() {
String[] projection = {};
String selection = "select";
String[] selectionArgs = {};
String sortOrder = "order";
Bundle queryArgs = new Bundle();
queryArgs.putString(QUERY_ARG_SQL_SELECTION, selection);
queryArgs.putStringArray(QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
queryArgs.putString(QUERY_ARG_SQL_SORT_ORDER, sortOrder);

QueryParamTrackingCursor testCursor = new QueryParamTrackingCursor();
shadowContentResolver.setCursor(testCursor);
Cursor cursor = shadowContentResolver.query(uri21, projection, queryArgs, null);
assertThat((QueryParamTrackingCursor) cursor).isEqualTo(testCursor);
assertThat(testCursor.uri).isEqualTo(uri21);
assertThat(testCursor.projection).isEqualTo(projection);
assertThat(testCursor.selection).isEqualTo(selection);
assertThat(testCursor.selectionArgs).isEqualTo(selectionArgs);
assertThat(testCursor.sortOrder).isEqualTo(sortOrder);
}

@Test
public void acquireUnstableProvider_shouldDefaultToNull() {
assertThat(contentResolver.acquireUnstableProvider(uri21)).isNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.robolectric.shadows;

import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION;
import static android.content.ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS;
import static android.content.ContentResolver.QUERY_ARG_SQL_SORT_ORDER;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.O;

import android.accounts.Account;
import android.annotation.NonNull;
Expand Down Expand Up @@ -236,6 +240,26 @@ protected int update(Uri uri, ContentValues values, String where, String[] selec
}
}

@Implementation(minSdk = O)
protected final Cursor query(
Uri uri, String[] projection, Bundle queryArgs, CancellationSignal cancellationSignal) {
ContentProvider provider = getProvider(uri);
if (provider != null) {
return provider.query(uri, projection, queryArgs, cancellationSignal);
} else {
BaseCursor returnCursor = getCursor(uri);
if (returnCursor == null) {
return null;
}
String selection = queryArgs.getString(QUERY_ARG_SQL_SELECTION);
String[] selectionArgs = queryArgs.getStringArray(QUERY_ARG_SQL_SELECTION_ARGS);
String sortOrder = queryArgs.getString(QUERY_ARG_SQL_SORT_ORDER);

returnCursor.setQuery(uri, projection, selection, selectionArgs, sortOrder);
return returnCursor;
}
}

@Implementation
protected final Cursor query(
Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Expand Down

0 comments on commit e503843

Please sign in to comment.