Refactor internet connection check into utility function
The internet connectivity check will be required by multiple test
classes. Refactor it into a standalone utility function and port the
code to Kotlin at the same time.
Also, instead of asserting, make use of junit.Assume. This way when ther
is no internet connection the test won't fail but it will just be
reported as skipped
Test: atest CtsCronetTestCases
Change-Id: I170aa19e23a9532d84da72cbcec567b65519bb0b
diff --git a/Cronet/tests/cts/Android.bp b/Cronet/tests/cts/Android.bp
index 68e3cf1..2c28b8d 100644
--- a/Cronet/tests/cts/Android.bp
+++ b/Cronet/tests/cts/Android.bp
@@ -59,6 +59,7 @@
"ctstestrunner-axt",
"ctstestserver",
"junit",
+ "hamcrest-library",
],
libs: [
"android.test.runner",
diff --git a/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
index 598be0e..fc0b3ee 100644
--- a/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
+++ b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java
@@ -16,12 +16,13 @@
package android.net.http.cts;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static android.net.http.cts.util.TestUtilsKt.assertOKStatusCode;
+import static android.net.http.cts.util.TestUtilsKt.skipIfNoInternetConnection;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
import android.content.Context;
-import android.net.ConnectivityManager;
import android.net.http.HttpEngine;
import android.net.http.UrlRequest;
import android.net.http.UrlRequest.Status;
@@ -31,7 +32,6 @@
import android.net.http.cts.util.TestUrlRequestCallback;
import android.net.http.cts.util.TestUrlRequestCallback.ResponseStep;
-import androidx.annotation.NonNull;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
@@ -42,22 +42,15 @@
@RunWith(AndroidJUnit4.class)
public class CronetUrlRequestTest {
- private static final String TAG = CronetUrlRequestTest.class.getSimpleName();
-
- @NonNull private HttpEngine mHttpEngine;
- @NonNull private TestUrlRequestCallback mCallback;
- @NonNull private ConnectivityManager mCm;
- @NonNull private CronetCtsTestServer mTestServer;
+ private TestUrlRequestCallback mCallback;
+ private CronetCtsTestServer mTestServer;
+ private HttpEngine mHttpEngine;
@Before
public void setUp() throws Exception {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
- mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ skipIfNoInternetConnection(context);
HttpEngine.Builder builder = new HttpEngine.Builder(context);
- builder.setEnableHttpCache(HttpEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
- .setEnableHttp2(true)
- // .setEnableBrotli(true)
- .setEnableQuic(true);
mHttpEngine = builder.build();
mCallback = new TestUrlRequestCallback();
mTestServer = new CronetCtsTestServer(context);
@@ -65,16 +58,12 @@
@After
public void tearDown() throws Exception {
- mHttpEngine.shutdown();
- mTestServer.shutdown();
- }
-
- 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());
+ if (mHttpEngine != null) {
+ mHttpEngine.shutdown();
+ }
+ if (mTestServer != null) {
+ mTestServer.shutdown();
+ }
}
private UrlRequest buildUrlRequest(String url) {
@@ -83,18 +72,14 @@
@Test
public void testUrlRequestGet_CompletesSuccessfully() throws Exception {
- assertHasTestableNetworks();
String url = mTestServer.getSuccessUrl();
UrlRequest request = buildUrlRequest(url);
request.start();
mCallback.expectCallback(ResponseStep.ON_SUCCEEDED);
-
UrlResponseInfo info = mCallback.mResponseInfo;
- assertEquals(
- "Unexpected http status code from " + url + ".", 200, info.getHttpStatusCode());
- assertGreaterThan(
- "Received byte from " + url + " is 0.", (int) info.getReceivedByteCount(), 0);
+ assertOKStatusCode(info);
+ assertThat("Received byte count must be > 0", info.getReceivedByteCount(), greaterThan(0L));
}
@Test
diff --git a/Cronet/tests/cts/src/android/net/http/cts/util/TestUtils.kt b/Cronet/tests/cts/src/android/net/http/cts/util/TestUtils.kt
new file mode 100644
index 0000000..d30c059
--- /dev/null
+++ b/Cronet/tests/cts/src/android/net/http/cts/util/TestUtils.kt
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.http.cts.util
+
+import android.content.Context
+import android.net.ConnectivityManager
+import android.net.http.UrlResponseInfo
+import org.junit.Assert.assertEquals
+import org.junit.Assume.assumeNotNull
+
+fun skipIfNoInternetConnection(context: Context) {
+ val connectivityManager = context.getSystemService(ConnectivityManager::class.java)
+ assumeNotNull(
+ "This test requires a working Internet connection", connectivityManager.getActiveNetwork())
+}
+
+fun assertOKStatusCode(info: UrlResponseInfo) {
+ assertEquals("Status code must be 200 OK", 200, info.getHttpStatusCode())
+}