Merge "Cronet: Fix linting errors for API tests"
This commit is contained in:
@@ -24,17 +24,11 @@ import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.Random;
|
||||
|
||||
import org.chromium.net.CronetEngine;
|
||||
import org.chromium.net.CronetException;
|
||||
import org.chromium.net.UrlRequest;
|
||||
@@ -43,19 +37,22 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class CronetApiTest {
|
||||
private static final String TAG = CronetApiTest.class.getSimpleName();
|
||||
static final String HTTPS_PREFIX = "https://";
|
||||
static final int TIMEOUT_MS = 12_000;
|
||||
private static final String HTTPS_PREFIX = "https://";
|
||||
private static final int TIMEOUT_MS = 12_000;
|
||||
|
||||
private final String[] mTestDomains = {"www.google.com", "www.android.com"};
|
||||
@NonNull
|
||||
private CronetEngine mCronetEngine;
|
||||
@NonNull
|
||||
private ConnectivityManager mCm;
|
||||
@NonNull
|
||||
private Executor mExecutor;
|
||||
@NonNull private CronetEngine mCronetEngine;
|
||||
@NonNull private ConnectivityManager mCm;
|
||||
@NonNull private Executor mExecutor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
@@ -64,19 +61,18 @@ public class CronetApiTest {
|
||||
CronetEngine.Builder builder = new CronetEngine.Builder(context);
|
||||
builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
|
||||
.enableHttp2(true)
|
||||
//.enableBrotli(true)
|
||||
// .enableBrotli(true)
|
||||
.enableQuic(true);
|
||||
mCronetEngine = builder.build();
|
||||
mExecutor = new Handler(Looper.getMainLooper())::post;
|
||||
}
|
||||
|
||||
static private void assertGreaterThan(String msg, int first, int second) {
|
||||
private static void assertGreaterThan(String msg, int first, int second) {
|
||||
assertTrue(msg + " Excepted " + first + " to be greater than " + second, first > second);
|
||||
}
|
||||
|
||||
private void assertHasTestableNetworks() {
|
||||
assertNotNull("This test requires a working Internet connection",
|
||||
mCm.getActiveNetwork());
|
||||
assertNotNull("This test requires a working Internet connection", mCm.getActiveNetwork());
|
||||
}
|
||||
|
||||
private String getRandomDomain() {
|
||||
@@ -84,11 +80,11 @@ public class CronetApiTest {
|
||||
return mTestDomains[index];
|
||||
}
|
||||
|
||||
class VerifyUrlRequestCallback extends UrlRequest.Callback {
|
||||
private static class TestUrlRequestCallback extends UrlRequest.Callback {
|
||||
private final CountDownLatch mLatch = new CountDownLatch(1);
|
||||
private final String mUrl;
|
||||
|
||||
VerifyUrlRequestCallback(@NonNull String url) {
|
||||
TestUrlRequestCallback(@NonNull String url) {
|
||||
this.mUrl = url;
|
||||
}
|
||||
|
||||
@@ -109,18 +105,19 @@ public class CronetApiTest {
|
||||
|
||||
@Override
|
||||
public void onReadCompleted(
|
||||
UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
|
||||
UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
|
||||
byteBuffer.clear();
|
||||
request.read(byteBuffer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
|
||||
assertEquals("Unexpected http status code from " + mUrl + ".",
|
||||
200, info.getHttpStatusCode());
|
||||
assertGreaterThan("Received byte from " + mUrl + " is 0.",
|
||||
(int)info.getReceivedByteCount(), 0);
|
||||
assertEquals(
|
||||
"Unexpected http status code from " + mUrl + ".",
|
||||
200,
|
||||
info.getHttpStatusCode());
|
||||
assertGreaterThan(
|
||||
"Received byte from " + mUrl + " is 0.", (int) info.getReceivedByteCount(), 0);
|
||||
mLatch.countDown();
|
||||
}
|
||||
|
||||
@@ -131,14 +128,12 @@ public class CronetApiTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlGet() throws Exception {
|
||||
public void testUrlRequestGet_CompletesSuccessfully() throws Exception {
|
||||
assertHasTestableNetworks();
|
||||
String url = HTTPS_PREFIX + getRandomDomain();
|
||||
VerifyUrlRequestCallback callback = new VerifyUrlRequestCallback(url);
|
||||
TestUrlRequestCallback callback = new TestUrlRequestCallback(url);
|
||||
UrlRequest.Builder builder = mCronetEngine.newUrlRequestBuilder(url, callback, mExecutor);
|
||||
builder.build().start();
|
||||
assertTrue(url + " but not complete after " + TIMEOUT_MS + "ms.",
|
||||
callback.waitForAnswer());
|
||||
assertTrue(url + " but not complete after " + TIMEOUT_MS + "ms.", callback.waitForAnswer());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user