Added argument checking on ContentCaptureContext.

Bug: 111276913
Test: atest CtsContentCaptureServiceTestCases:android.contentcaptureservice.cts.ContentCaptureContextTest

Change-Id: I819ebec57611a9ced29c1a1a6f40893be9d35b22
This commit is contained in:
Felipe Leme
2019-01-22 12:21:31 -08:00
parent 302d36b6a8
commit b67e949bcf
3 changed files with 22 additions and 8 deletions

View File

@@ -86,7 +86,6 @@ public final class ContentCaptureContext implements Parcelable {
private final @Nullable Uri mUri;
// Fields below are set by server when the session starts
// TODO(b/111276913): create new object for taskId + componentName / reuse on other places
private final @Nullable ComponentName mComponentName;
private final int mTaskId;
private final int mDisplayId;
@@ -213,6 +212,7 @@ public final class ContentCaptureContext implements Parcelable {
public static final class Builder {
private Bundle mExtras;
private Uri mUri;
private boolean mDestroyed;
/**
* Sets extra options associated with this context.
@@ -221,11 +221,13 @@ public final class ContentCaptureContext implements Parcelable {
*
* @param extras extra options.
* @return this builder.
*
* @throws IllegalStateException if {@link #build()} was already called.
*/
@NonNull
public Builder setExtras(@NonNull Bundle extras) {
// TODO(b/111276913): check build just once / throw exception / test / document
mExtras = Preconditions.checkNotNull(extras);
throwIfDestroyed();
return this;
}
@@ -236,23 +238,35 @@ public final class ContentCaptureContext implements Parcelable {
*
* @param uri URI associated with this context.
* @return this builder.
*
* @throws IllegalStateException if {@link #build()} was already called.
*/
@NonNull
public Builder setUri(@NonNull Uri uri) {
// TODO(b/111276913): check build just once / throw exception / test / document
mUri = Preconditions.checkNotNull(uri);
throwIfDestroyed();
return this;
}
/**
* Builds the {@link ContentCaptureContext}.
*
* @throws IllegalStateException if {@link #build()} was already called or no call to either
* {@link #setExtras(Bundle)} or {@link #setUri(Uri)} was made.
*
* @return the built {@code ContentCaptureContext}
*/
public ContentCaptureContext build() {
// TODO(b/111276913): check build just once / throw exception / test / document
// TODO(b/111276913): make sure it at least one property (uri / extras) / test /
// throw exception / documment
throwIfDestroyed();
Preconditions.checkState(mExtras != null || mUri != null, "Must call setUri() "
+ "or setExtras() before calling build()");
mDestroyed = true;
return new ContentCaptureContext(this);
}
private void throwIfDestroyed() {
Preconditions.checkState(!mDestroyed, "Already called #build()");
}
}
/**

View File

@@ -31,7 +31,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit test for {@link ContentCaptureSessionTest}.
* Unit tests for {@link ContentCaptureSession}.
*
* <p>To run it:
* {@code atest FrameworksCoreTests:android.view.contentcapture.ContentCaptureSessionTest}

View File

@@ -42,7 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Locale;
/**
* Unit test for {@link ViewNode}.
* Unit tests for {@link ViewNode}.
*
* <p>To run it: {@code atest FrameworksCoreTests:android.view.contentcapture.ViewNodeTest}
*/