TestUrlRequestCallback: assert internal states using matchers
Currently, TestUrlRequestCallback asserts state transitions through
boolean expressions. When these fail, they simply yell "true != false",
without surfacing the wrong underlying state transition (e.g.,
`next_state == ON_REDIRECT`).
Using assertThat & matchers avoids this problem.
Test: m & test run
Change-Id: Ia26dc0e6f2fe9b53a1f92638ab0b74e39b936d3d
diff --git a/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java b/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
index fc4912c..efbcff6 100644
--- a/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
+++ b/Cronet/tests/cts/src/android/net/http/cts/util/TestUrlRequestCallback.java
@@ -17,11 +17,14 @@
package android.net.http.cts.util;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.core.AnyOf.anyOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeThat;
@@ -288,8 +291,9 @@
UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
checkExecutorThread();
assertFalse(request.isDone());
- assertTrue(mResponseStep == ResponseStep.NOTHING
- || mResponseStep == ResponseStep.ON_RECEIVED_REDIRECT);
+ assertThat(mResponseStep, anyOf(
+ equalTo(ResponseStep.NOTHING),
+ equalTo(ResponseStep.ON_RECEIVED_REDIRECT)));
assertNull(mError);
mResponseStep = ResponseStep.ON_RECEIVED_REDIRECT;
@@ -306,8 +310,9 @@
public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
checkExecutorThread();
assertFalse(request.isDone());
- assertTrue(mResponseStep == ResponseStep.NOTHING
- || mResponseStep == ResponseStep.ON_RECEIVED_REDIRECT);
+ assertThat(mResponseStep, anyOf(
+ equalTo(ResponseStep.NOTHING),
+ equalTo(ResponseStep.ON_RECEIVED_REDIRECT)));
assertNull(mError);
mResponseStep = ResponseStep.ON_RESPONSE_STARTED;
@@ -322,8 +327,9 @@
public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
checkExecutorThread();
assertFalse(request.isDone());
- assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED
- || mResponseStep == ResponseStep.ON_READ_COMPLETED);
+ assertThat(mResponseStep, anyOf(
+ equalTo(ResponseStep.ON_RESPONSE_STARTED),
+ equalTo(ResponseStep.ON_READ_COMPLETED)));
assertNull(mError);
mResponseStep = ResponseStep.ON_READ_COMPLETED;
@@ -349,8 +355,9 @@
public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
checkExecutorThread();
assertTrue(request.isDone());
- assertTrue(mResponseStep == ResponseStep.ON_RESPONSE_STARTED
- || mResponseStep == ResponseStep.ON_READ_COMPLETED);
+ assertThat(mResponseStep, anyOf(
+ equalTo(ResponseStep.ON_RESPONSE_STARTED),
+ equalTo(ResponseStep.ON_READ_COMPLETED)));
assertFalse(mOnErrorCalled);
assertFalse(mOnCanceledCalled);
assertNull(mError);
@@ -373,7 +380,7 @@
checkExecutorThread();
assertTrue(request.isDone());
// Shouldn't happen after success.
- assertTrue(mResponseStep != ResponseStep.ON_SUCCEEDED);
+ assertNotEquals(ResponseStep.ON_SUCCEEDED, mResponseStep);
// Should happen at most once for a single request.
assertFalse(mOnErrorCalled);
assertFalse(mOnCanceledCalled);